From 44e226ceb753075b44bcaadf349b1d1b6a23ad8d Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 17 Dec 2020 14:02:09 +0100 Subject: [PATCH] Continue String to Symbol conversion in rustdoc --- src/librustdoc/clean/auto_trait.rs | 14 ++++++------- src/librustdoc/clean/inline.rs | 8 ++++---- src/librustdoc/clean/mod.rs | 25 +++++++++++----------- src/librustdoc/clean/types.rs | 32 +++++++++++++++-------------- src/librustdoc/clean/utils.rs | 2 +- src/librustdoc/html/format.rs | 17 ++++++++------- src/librustdoc/html/render/cache.rs | 14 ++++++------- src/librustdoc/html/render/mod.rs | 12 +++++------ src/librustdoc/json/conversions.rs | 19 ++++++++++------- 9 files changed, 74 insertions(+), 69 deletions(-) diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index 72603f00697..40c59ed1e0b 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -61,10 +61,10 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> { .params .iter() .filter_map(|param| match param.kind { - ty::GenericParamDefKind::Lifetime => Some(param.name.to_string()), + ty::GenericParamDefKind::Lifetime => Some(param.name), _ => None, }) - .map(|name| (name.clone(), Lifetime(name))) + .map(|name| (name, Lifetime(name))) .collect(); let lifetime_predicates = self.handle_lifetimes(®ion_data, &names_map); let new_generics = self.param_env_to_generics( @@ -145,21 +145,21 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> { fn get_lifetime( &self, region: Region<'_>, - names_map: &FxHashMap, + names_map: &FxHashMap, ) -> Lifetime { self.region_name(region) .map(|name| { names_map.get(&name).unwrap_or_else(|| { - panic!("Missing lifetime with name {:?} for {:?}", name, region) + panic!("Missing lifetime with name {:?} for {:?}", name.as_str(), region) }) }) .unwrap_or(&Lifetime::statik()) .clone() } - fn region_name(&self, region: Region<'_>) -> Option { + fn region_name(&self, region: Region<'_>) -> Option { match region { - &ty::ReEarlyBound(r) => Some(r.name.to_string()), + &ty::ReEarlyBound(r) => Some(r.name), _ => None, } } @@ -177,7 +177,7 @@ fn region_name(&self, region: Region<'_>) -> Option { fn handle_lifetimes<'cx>( &self, regions: &RegionConstraintData<'cx>, - names_map: &FxHashMap, + names_map: &FxHashMap, ) -> Vec { // Our goal is to 'flatten' the list of constraints by eliminating // all intermediate RegionVids. At the end, all constraints should diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 7bb8e5e8cfc..3cff5fa07b1 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -486,13 +486,13 @@ fn fill_in( const_stability: None, deprecation: None, kind: clean::ImportItem(clean::Import::new_simple( - item.ident.to_string(), + item.ident.name, clean::ImportSource { path: clean::Path { global: false, res: item.res, segments: vec![clean::PathSegment { - name: clean::PrimitiveType::from(p).as_str().to_string(), + name: clean::PrimitiveType::from(p).as_sym(), args: clean::GenericArgs::AngleBracketed { args: Vec::new(), bindings: Vec::new(), @@ -562,11 +562,11 @@ fn build_macro(cx: &DocContext<'_>, did: DefId, name: Symbol) -> clean::ItemKind .collect::() ); - clean::MacroItem(clean::Macro { source, imported_from: Some(imported_from).clean(cx) }) + clean::MacroItem(clean::Macro { source, imported_from: Some(imported_from) }) } LoadedMacro::ProcMacro(ext) => clean::ProcMacroItem(clean::ProcMacro { kind: ext.macro_kind(), - helpers: ext.helper_attrs.clean(cx), + helpers: ext.helper_attrs, }), } } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 2105ec0b0ba..43272aeda61 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -379,7 +379,7 @@ fn clean(&self, cx: &DocContext<'_>) -> Lifetime { } _ => {} } - Lifetime(self.name.ident().to_string()) + Lifetime(self.name.ident().name) } } @@ -397,9 +397,9 @@ fn clean(&self, _: &DocContext<'_>) -> Lifetime { for bound in bounds { s.push_str(&format!(" + {}", bound.name.ident())); } - Lifetime(s) + Lifetime(Symbol::intern(&s)) } else { - Lifetime(self.name.ident().to_string()) + Lifetime(self.name.ident().name) } } _ => panic!(), @@ -423,16 +423,16 @@ fn clean(&self, cx: &DocContext<'_>) -> Constant { impl Clean for ty::GenericParamDef { fn clean(&self, _cx: &DocContext<'_>) -> Lifetime { - Lifetime(self.name.to_string()) + Lifetime(self.name) } } impl Clean> for ty::RegionKind { - fn clean(&self, cx: &DocContext<'_>) -> Option { + fn clean(&self, _cx: &DocContext<'_>) -> Option { match *self { ty::ReStatic => Some(Lifetime::statik()), - ty::ReLateBound(_, ty::BrNamed(_, name)) => Some(Lifetime(name.to_string())), - ty::ReEarlyBound(ref data) => Some(Lifetime(data.name.clean(cx))), + ty::ReLateBound(_, ty::BrNamed(_, name)) => Some(Lifetime(name)), + ty::ReEarlyBound(ref data) => Some(Lifetime(data.name)), ty::ReLateBound(..) | ty::ReFree(..) @@ -897,7 +897,7 @@ fn clean_fn_or_proc_macro( } } } - ProcMacroItem(ProcMacro { kind, helpers: helpers.clean(cx) }) + ProcMacroItem(ProcMacro { kind, helpers }) } None => { let mut func = (sig, generics, body_id).clean(cx); @@ -1914,7 +1914,7 @@ fn clean(&self, cx: &DocContext<'_>) -> GenericArgs { impl Clean for hir::PathSegment<'_> { fn clean(&self, cx: &DocContext<'_>) -> PathSegment { - PathSegment { name: self.ident.name.clean(cx), args: self.generic_args().clean(cx) } + PathSegment { name: self.ident.name, args: self.generic_args().clean(cx) } } } @@ -2132,7 +2132,6 @@ fn clean_extern_crate( return items; } } - let path = orig_name.map(|x| x.to_string()); // FIXME: using `from_def_id_and_kind` breaks `rustdoc/masked` for some reason vec![Item { name: None, @@ -2143,7 +2142,7 @@ fn clean_extern_crate( stability: None, const_stability: None, deprecation: None, - kind: ExternCrateItem(name.clean(cx), path), + kind: ExternCrateItem(name, orig_name), }] } @@ -2215,7 +2214,7 @@ fn clean(&self, cx: &DocContext<'_>) -> Vec { const_stability: None, deprecation: None, kind: ImportItem(Import::new_simple( - self.name.clean(cx), + self.name, resolve_use_source(cx, path), false, )), @@ -2223,7 +2222,7 @@ fn clean(&self, cx: &DocContext<'_>) -> Vec { return items; } } - Import::new_simple(name.clean(cx), resolve_use_source(cx, path), true) + Import::new_simple(name, resolve_use_source(cx, path), true) }; vec![Item { diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 9bade5ad2ec..4920e6fd6f1 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -295,7 +295,7 @@ pub fn from_def_id_and_parts( #[derive(Clone, Debug)] crate enum ItemKind { - ExternCrateItem(String, Option), + ExternCrateItem(Symbol, Option), ImportItem(Import), StructItem(Struct), UnionItem(Union), @@ -877,21 +877,19 @@ impl GenericBound { } #[derive(Clone, PartialEq, Eq, Debug, Hash)] -crate struct Lifetime(pub String); +crate struct Lifetime(pub Symbol); impl Lifetime { - crate fn get_ref<'a>(&'a self) -> &'a str { - let Lifetime(ref s) = *self; - let s: &'a str = s; - s + crate fn get_ref(&self) -> SymbolStr { + self.0.as_str() } crate fn statik() -> Lifetime { - Lifetime("'static".to_string()) + Lifetime(kw::StaticLifetime) } crate fn elided() -> Lifetime { - Lifetime("'_".to_string()) + Lifetime(kw::UnderscoreLifetime) } } @@ -1675,13 +1673,17 @@ impl Span { } impl Path { - crate fn last_name(&self) -> &str { + crate fn last(&self) -> Symbol { + self.segments.last().expect("segments were empty").name + } + + crate fn last_name(&self) -> SymbolStr { self.segments.last().expect("segments were empty").name.as_str() } crate fn whole_name(&self) -> String { String::from(if self.global { "::" } else { "" }) - + &self.segments.iter().map(|s| s.name.clone()).collect::>().join("::") + + &self.segments.iter().map(|s| s.name.to_string()).collect::>().join("::") } } @@ -1700,7 +1702,7 @@ impl Path { #[derive(Clone, PartialEq, Eq, Debug, Hash)] crate struct PathSegment { - crate name: String, + crate name: Symbol, crate args: GenericArgs, } @@ -1777,7 +1779,7 @@ fn def_id(&self) -> Option { } impl Import { - crate fn new_simple(name: String, source: ImportSource, should_be_displayed: bool) -> Self { + crate fn new_simple(name: Symbol, source: ImportSource, should_be_displayed: bool) -> Self { Self { kind: ImportKind::Simple(name), source, should_be_displayed } } @@ -1789,7 +1791,7 @@ impl Import { #[derive(Clone, Debug)] crate enum ImportKind { // use source as str; - Simple(String), + Simple(Symbol), // use source::*; Glob, } @@ -1803,13 +1805,13 @@ impl Import { #[derive(Clone, Debug)] crate struct Macro { crate source: String, - crate imported_from: Option, + crate imported_from: Option, } #[derive(Clone, Debug)] crate struct ProcMacro { crate kind: MacroKind, - crate helpers: Vec, + crate helpers: Vec, } /// An type binding on an associated type (e.g., `A = Bar` in `Foo` or diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index ec922b182e2..1ae2e5de82c 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -153,7 +153,7 @@ pub(super) fn external_path( global: false, res: Res::Err, segments: vec![PathSegment { - name: name.to_string(), + name, args: external_generic_args(cx, trait_did, has_self, bindings, substs), }], } diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index c49c4892237..f80346aa50b 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -308,7 +308,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { } impl clean::Lifetime { - crate fn print(&self) -> &str { + crate fn print(&self) -> impl fmt::Display + '_ { self.get_ref() } } @@ -445,11 +445,10 @@ fn print(&self) -> impl fmt::Display + '_ { impl clean::PathSegment { crate fn print(&self) -> impl fmt::Display + '_ { display_fn(move |f| { - f.write_str(&self.name)?; if f.alternate() { - write!(f, "{:#}", self.args.print()) + write!(f, "{}{:#}", self.name, self.args.print()) } else { - write!(f, "{}", self.args.print()) + write!(f, "{}{}", self.name, self.args.print()) } }) } @@ -544,7 +543,7 @@ fn resolved_path( last.name.to_string() } } else { - anchor(did, &last.name).to_string() + anchor(did, &*last.name.as_str()).to_string() }; write!(w, "{}{}", path, last.args.print())?; } @@ -1159,11 +1158,11 @@ fn print_with_space(&self) -> &str { impl clean::Import { crate fn print(&self) -> impl fmt::Display + '_ { display_fn(move |f| match self.kind { - clean::ImportKind::Simple(ref name) => { - if *name == self.source.path.last_name() { + clean::ImportKind::Simple(name) => { + if name == self.source.path.last() { write!(f, "use {};", self.source.print()) } else { - write!(f, "use {} as {};", self.source.print(), *name) + write!(f, "use {} as {};", self.source.print(), name) } } clean::ImportKind::Glob => { @@ -1187,7 +1186,7 @@ impl clean::ImportSource { } let name = self.path.last_name(); if let hir::def::Res::PrimTy(p) = self.path.res { - primitive_link(f, PrimitiveType::from(p), name)?; + primitive_link(f, PrimitiveType::from(p), &*name)?; } else { write!(f, "{}", name)?; } diff --git a/src/librustdoc/html/render/cache.rs b/src/librustdoc/html/render/cache.rs index 80f54d8e161..94a8b3fef47 100644 --- a/src/librustdoc/html/render/cache.rs +++ b/src/librustdoc/html/render/cache.rs @@ -2,7 +2,7 @@ use std::path::Path; use rustc_data_structures::fx::FxHashMap; -use rustc_span::symbol::sym; +use rustc_span::symbol::{sym, Symbol}; use serde::Serialize; use crate::clean::types::GetDefId; @@ -191,12 +191,12 @@ fn get_index_type(clean_type: &clean::Type) -> RenderType { RenderType { ty: clean_type.def_id(), idx: None, - name: get_index_type_name(clean_type, true).map(|s| s.to_ascii_lowercase()), + name: get_index_type_name(clean_type, true).map(|s| s.as_str().to_ascii_lowercase()), generics: get_generics(clean_type), } } -fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option { +fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option { match *clean_type { clean::ResolvedPath { ref path, .. } => { let segments = &path.segments; @@ -206,10 +206,10 @@ fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option clean_type, accept_generic ) }); - Some(path_segment.name.clone()) + Some(path_segment.name) } - clean::Generic(s) if accept_generic => Some(s.to_string()), - clean::Primitive(ref p) => Some(format!("{:?}", p)), + clean::Generic(s) if accept_generic => Some(s), + clean::Primitive(ref p) => Some(p.as_sym()), clean::BorrowedRef { ref type_, .. } => get_index_type_name(type_, accept_generic), // FIXME: add all from clean::Type. _ => None, @@ -222,7 +222,7 @@ fn get_generics(clean_type: &clean::Type) -> Option> { .iter() .filter_map(|t| { get_index_type_name(t, false).map(|name| Generic { - name: name.to_ascii_lowercase(), + name: name.as_str().to_ascii_lowercase(), defid: t.def_id(), idx: None, }) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 00294878fe5..1e888e83e19 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -2137,14 +2137,14 @@ fn cmp(i1: &clean::Item, i2: &clean::Item, idx1: usize, idx2: usize) -> Ordering w, "{}extern crate {} as {};", myitem.visibility.print_with_space(), - anchor(myitem.def_id, src), + anchor(myitem.def_id, &*src.as_str()), name ), None => write!( w, "{}extern crate {};", myitem.visibility.print_with_space(), - anchor(myitem.def_id, name) + anchor(myitem.def_id, &*name.as_str()) ), } write!(w, ""); @@ -2444,7 +2444,7 @@ fn render_implementor( implementor: &Impl, parent: &clean::Item, w: &mut Buffer, - implementor_dups: &FxHashMap<&str, (DefId, bool)>, + implementor_dups: &FxHashMap, aliases: &[String], cache: &Cache, ) { @@ -2455,7 +2455,7 @@ fn render_implementor( | clean::BorrowedRef { type_: box clean::ResolvedPath { ref path, is_generic: false, .. }, .. - } => implementor_dups[path.last_name()].1, + } => implementor_dups[&path.last()].1, _ => false, }; render_impl( @@ -2704,7 +2704,7 @@ fn trait_item(w: &mut Buffer, cx: &Context, m: &clean::Item, t: &clean::Item, ca if let Some(implementors) = cache.implementors.get(&it.def_id) { // The DefId is for the first Type found with that name. The bool is // if any Types with the same name but different DefId have been found. - let mut implementor_dups: FxHashMap<&str, (DefId, bool)> = FxHashMap::default(); + let mut implementor_dups: FxHashMap = FxHashMap::default(); for implementor in implementors { match implementor.inner_impl().for_ { clean::ResolvedPath { ref path, did, is_generic: false, .. } @@ -2713,7 +2713,7 @@ fn trait_item(w: &mut Buffer, cx: &Context, m: &clean::Item, t: &clean::Item, ca .. } => { let &mut (prev_did, ref mut has_duplicates) = - implementor_dups.entry(path.last_name()).or_insert((did, false)); + implementor_dups.entry(path.last()).or_insert((did, false)); if prev_did != did { *has_duplicates = true; } diff --git a/src/librustdoc/json/conversions.rs b/src/librustdoc/json/conversions.rs index 809cfb9d743..9dc27e3411d 100644 --- a/src/librustdoc/json/conversions.rs +++ b/src/librustdoc/json/conversions.rs @@ -120,7 +120,7 @@ impl From for GenericArg { fn from(arg: clean::GenericArg) -> Self { use clean::GenericArg::*; match arg { - Lifetime(l) => GenericArg::Lifetime(l.0), + Lifetime(l) => GenericArg::Lifetime(l.0.to_string()), Type(t) => GenericArg::Type(t.into()), Const(c) => GenericArg::Const(c.into()), } @@ -163,7 +163,9 @@ fn from(item: clean::ItemKind) -> Self { use clean::ItemKind::*; match item { ModuleItem(m) => ItemEnum::ModuleItem(m.into()), - ExternCrateItem(c, a) => ItemEnum::ExternCrateItem { name: c, rename: a }, + ExternCrateItem(c, a) => { + ItemEnum::ExternCrateItem { name: c.to_string(), rename: a.map(|x| x.to_string()) } + } ImportItem(i) => ItemEnum::ImportItem(i.into()), StructItem(s) => ItemEnum::StructItem(s.into()), UnionItem(u) => ItemEnum::StructItem(u.into()), @@ -302,7 +304,7 @@ fn from(predicate: clean::WherePredicate) -> Self { bounds: bounds.into_iter().map(Into::into).collect(), }, RegionPredicate { lifetime, bounds } => WherePredicate::RegionPredicate { - lifetime: lifetime.0, + lifetime: lifetime.0.to_string(), bounds: bounds.into_iter().map(Into::into).collect(), }, EqPredicate { lhs, rhs } => { @@ -323,7 +325,7 @@ fn from(bound: clean::GenericBound) -> Self { modifier: modifier.into(), } } - Outlives(lifetime) => GenericBound::Outlives(lifetime.0), + Outlives(lifetime) => GenericBound::Outlives(lifetime.0.to_string()), } } } @@ -365,7 +367,7 @@ fn from(ty: clean::Type) -> Self { type_: Box::new((*type_).into()), }, BorrowedRef { lifetime, mutability, type_ } => Type::BorrowedRef { - lifetime: lifetime.map(|l| l.0), + lifetime: lifetime.map(|l| l.0.to_string()), mutable: mutability == ast::Mutability::Mut, type_: Box::new((*type_).into()), }, @@ -503,7 +505,7 @@ fn from(import: clean::Import) -> Self { match import.kind { Simple(s) => Import { span: import.source.path.whole_name(), - name: s, + name: s.to_string(), id: import.source.did.map(Into::into), glob: false, }, @@ -519,7 +521,10 @@ fn from(import: clean::Import) -> Self { impl From for ProcMacro { fn from(mac: clean::ProcMacro) -> Self { - ProcMacro { kind: mac.kind.into(), helpers: mac.helpers } + ProcMacro { + kind: mac.kind.into(), + helpers: mac.helpers.iter().map(|x| x.to_string()).collect(), + } } } -- 2.44.0