Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 44e226c

Browse files
committedDec 17, 2020
Continue String to Symbol conversion in rustdoc
1 parent bdd0a78 commit 44e226c

File tree

9 files changed

+74
-69
lines changed

9 files changed

+74
-69
lines changed
 

‎src/librustdoc/clean/auto_trait.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
6161
.params
6262
.iter()
6363
.filter_map(|param| match param.kind {
64-
ty::GenericParamDefKind::Lifetime => Some(param.name.to_string()),
64+
ty::GenericParamDefKind::Lifetime => Some(param.name),
6565
_ => None,
6666
})
67-
.map(|name| (name.clone(), Lifetime(name)))
67+
.map(|name| (name, Lifetime(name)))
6868
.collect();
6969
let lifetime_predicates = self.handle_lifetimes(&region_data, &names_map);
7070
let new_generics = self.param_env_to_generics(
@@ -145,21 +145,21 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
145145
fn get_lifetime(
146146
&self,
147147
region: Region<'_>,
148-
names_map: &FxHashMap<String, Lifetime>,
148+
names_map: &FxHashMap<Symbol, Lifetime>,
149149
) -> Lifetime {
150150
self.region_name(region)
151151
.map(|name| {
152152
names_map.get(&name).unwrap_or_else(|| {
153-
panic!("Missing lifetime with name {:?} for {:?}", name, region)
153+
panic!("Missing lifetime with name {:?} for {:?}", name.as_str(), region)
154154
})
155155
})
156156
.unwrap_or(&Lifetime::statik())
157157
.clone()
158158
}
159159

160-
fn region_name(&self, region: Region<'_>) -> Option<String> {
160+
fn region_name(&self, region: Region<'_>) -> Option<Symbol> {
161161
match region {
162-
&ty::ReEarlyBound(r) => Some(r.name.to_string()),
162+
&ty::ReEarlyBound(r) => Some(r.name),
163163
_ => None,
164164
}
165165
}
@@ -177,7 +177,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
177177
fn handle_lifetimes<'cx>(
178178
&self,
179179
regions: &RegionConstraintData<'cx>,
180-
names_map: &FxHashMap<String, Lifetime>,
180+
names_map: &FxHashMap<Symbol, Lifetime>,
181181
) -> Vec<WherePredicate> {
182182
// Our goal is to 'flatten' the list of constraints by eliminating
183183
// all intermediate RegionVids. At the end, all constraints should

‎src/librustdoc/clean/inline.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,13 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>)
486486
const_stability: None,
487487
deprecation: None,
488488
kind: clean::ImportItem(clean::Import::new_simple(
489-
item.ident.to_string(),
489+
item.ident.name,
490490
clean::ImportSource {
491491
path: clean::Path {
492492
global: false,
493493
res: item.res,
494494
segments: vec![clean::PathSegment {
495-
name: clean::PrimitiveType::from(p).as_str().to_string(),
495+
name: clean::PrimitiveType::from(p).as_sym(),
496496
args: clean::GenericArgs::AngleBracketed {
497497
args: Vec::new(),
498498
bindings: Vec::new(),
@@ -562,11 +562,11 @@ fn build_macro(cx: &DocContext<'_>, did: DefId, name: Symbol) -> clean::ItemKind
562562
.collect::<String>()
563563
);
564564

565-
clean::MacroItem(clean::Macro { source, imported_from: Some(imported_from).clean(cx) })
565+
clean::MacroItem(clean::Macro { source, imported_from: Some(imported_from) })
566566
}
567567
LoadedMacro::ProcMacro(ext) => clean::ProcMacroItem(clean::ProcMacro {
568568
kind: ext.macro_kind(),
569-
helpers: ext.helper_attrs.clean(cx),
569+
helpers: ext.helper_attrs,
570570
}),
571571
}
572572
}

‎src/librustdoc/clean/mod.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ impl Clean<Lifetime> for hir::Lifetime {
379379
}
380380
_ => {}
381381
}
382-
Lifetime(self.name.ident().to_string())
382+
Lifetime(self.name.ident().name)
383383
}
384384
}
385385

@@ -397,9 +397,9 @@ impl Clean<Lifetime> for hir::GenericParam<'_> {
397397
for bound in bounds {
398398
s.push_str(&format!(" + {}", bound.name.ident()));
399399
}
400-
Lifetime(s)
400+
Lifetime(Symbol::intern(&s))
401401
} else {
402-
Lifetime(self.name.ident().to_string())
402+
Lifetime(self.name.ident().name)
403403
}
404404
}
405405
_ => panic!(),
@@ -423,16 +423,16 @@ impl Clean<Constant> for hir::ConstArg {
423423

424424
impl Clean<Lifetime> for ty::GenericParamDef {
425425
fn clean(&self, _cx: &DocContext<'_>) -> Lifetime {
426-
Lifetime(self.name.to_string())
426+
Lifetime(self.name)
427427
}
428428
}
429429

430430
impl Clean<Option<Lifetime>> for ty::RegionKind {
431-
fn clean(&self, cx: &DocContext<'_>) -> Option<Lifetime> {
431+
fn clean(&self, _cx: &DocContext<'_>) -> Option<Lifetime> {
432432
match *self {
433433
ty::ReStatic => Some(Lifetime::statik()),
434-
ty::ReLateBound(_, ty::BrNamed(_, name)) => Some(Lifetime(name.to_string())),
435-
ty::ReEarlyBound(ref data) => Some(Lifetime(data.name.clean(cx))),
434+
ty::ReLateBound(_, ty::BrNamed(_, name)) => Some(Lifetime(name)),
435+
ty::ReEarlyBound(ref data) => Some(Lifetime(data.name)),
436436

437437
ty::ReLateBound(..)
438438
| ty::ReFree(..)
@@ -897,7 +897,7 @@ fn clean_fn_or_proc_macro(
897897
}
898898
}
899899
}
900-
ProcMacroItem(ProcMacro { kind, helpers: helpers.clean(cx) })
900+
ProcMacroItem(ProcMacro { kind, helpers })
901901
}
902902
None => {
903903
let mut func = (sig, generics, body_id).clean(cx);
@@ -1914,7 +1914,7 @@ impl Clean<GenericArgs> for hir::GenericArgs<'_> {
19141914

19151915
impl Clean<PathSegment> for hir::PathSegment<'_> {
19161916
fn clean(&self, cx: &DocContext<'_>) -> PathSegment {
1917-
PathSegment { name: self.ident.name.clean(cx), args: self.generic_args().clean(cx) }
1917+
PathSegment { name: self.ident.name, args: self.generic_args().clean(cx) }
19181918
}
19191919
}
19201920

@@ -2132,7 +2132,6 @@ fn clean_extern_crate(
21322132
return items;
21332133
}
21342134
}
2135-
let path = orig_name.map(|x| x.to_string());
21362135
// FIXME: using `from_def_id_and_kind` breaks `rustdoc/masked` for some reason
21372136
vec![Item {
21382137
name: None,
@@ -2143,7 +2142,7 @@ fn clean_extern_crate(
21432142
stability: None,
21442143
const_stability: None,
21452144
deprecation: None,
2146-
kind: ExternCrateItem(name.clean(cx), path),
2145+
kind: ExternCrateItem(name, orig_name),
21472146
}]
21482147
}
21492148

@@ -2215,15 +2214,15 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
22152214
const_stability: None,
22162215
deprecation: None,
22172216
kind: ImportItem(Import::new_simple(
2218-
self.name.clean(cx),
2217+
self.name,
22192218
resolve_use_source(cx, path),
22202219
false,
22212220
)),
22222221
});
22232222
return items;
22242223
}
22252224
}
2226-
Import::new_simple(name.clean(cx), resolve_use_source(cx, path), true)
2225+
Import::new_simple(name, resolve_use_source(cx, path), true)
22272226
};
22282227

22292228
vec![Item {

‎src/librustdoc/clean/types.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ impl Item {
295295

296296
#[derive(Clone, Debug)]
297297
crate enum ItemKind {
298-
ExternCrateItem(String, Option<String>),
298+
ExternCrateItem(Symbol, Option<Symbol>),
299299
ImportItem(Import),
300300
StructItem(Struct),
301301
UnionItem(Union),
@@ -877,21 +877,19 @@ impl GenericBound {
877877
}
878878

879879
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
880-
crate struct Lifetime(pub String);
880+
crate struct Lifetime(pub Symbol);
881881

882882
impl Lifetime {
883-
crate fn get_ref<'a>(&'a self) -> &'a str {
884-
let Lifetime(ref s) = *self;
885-
let s: &'a str = s;
886-
s
883+
crate fn get_ref(&self) -> SymbolStr {
884+
self.0.as_str()
887885
}
888886

889887
crate fn statik() -> Lifetime {
890-
Lifetime("'static".to_string())
888+
Lifetime(kw::StaticLifetime)
891889
}
892890

893891
crate fn elided() -> Lifetime {
894-
Lifetime("'_".to_string())
892+
Lifetime(kw::UnderscoreLifetime)
895893
}
896894
}
897895

@@ -1675,13 +1673,17 @@ crate struct Path {
16751673
}
16761674

16771675
impl Path {
1678-
crate fn last_name(&self) -> &str {
1676+
crate fn last(&self) -> Symbol {
1677+
self.segments.last().expect("segments were empty").name
1678+
}
1679+
1680+
crate fn last_name(&self) -> SymbolStr {
16791681
self.segments.last().expect("segments were empty").name.as_str()
16801682
}
16811683

16821684
crate fn whole_name(&self) -> String {
16831685
String::from(if self.global { "::" } else { "" })
1684-
+ &self.segments.iter().map(|s| s.name.clone()).collect::<Vec<_>>().join("::")
1686+
+ &self.segments.iter().map(|s| s.name.to_string()).collect::<Vec<_>>().join("::")
16851687
}
16861688
}
16871689

@@ -1700,7 +1702,7 @@ crate enum GenericArgs {
17001702

17011703
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
17021704
crate struct PathSegment {
1703-
crate name: String,
1705+
crate name: Symbol,
17041706
crate args: GenericArgs,
17051707
}
17061708

@@ -1777,7 +1779,7 @@ crate struct Import {
17771779
}
17781780

17791781
impl Import {
1780-
crate fn new_simple(name: String, source: ImportSource, should_be_displayed: bool) -> Self {
1782+
crate fn new_simple(name: Symbol, source: ImportSource, should_be_displayed: bool) -> Self {
17811783
Self { kind: ImportKind::Simple(name), source, should_be_displayed }
17821784
}
17831785

@@ -1789,7 +1791,7 @@ impl Import {
17891791
#[derive(Clone, Debug)]
17901792
crate enum ImportKind {
17911793
// use source as str;
1792-
Simple(String),
1794+
Simple(Symbol),
17931795
// use source::*;
17941796
Glob,
17951797
}
@@ -1803,13 +1805,13 @@ crate struct ImportSource {
18031805
#[derive(Clone, Debug)]
18041806
crate struct Macro {
18051807
crate source: String,
1806-
crate imported_from: Option<String>,
1808+
crate imported_from: Option<Symbol>,
18071809
}
18081810

18091811
#[derive(Clone, Debug)]
18101812
crate struct ProcMacro {
18111813
crate kind: MacroKind,
1812-
crate helpers: Vec<String>,
1814+
crate helpers: Vec<Symbol>,
18131815
}
18141816

18151817
/// An type binding on an associated type (e.g., `A = Bar` in `Foo<A = Bar>` or

‎src/librustdoc/clean/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub(super) fn external_path(
153153
global: false,
154154
res: Res::Err,
155155
segments: vec![PathSegment {
156-
name: name.to_string(),
156+
name,
157157
args: external_generic_args(cx, trait_did, has_self, bindings, substs),
158158
}],
159159
}

‎src/librustdoc/html/format.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ impl<'a> fmt::Display for WhereClause<'a> {
308308
}
309309

310310
impl clean::Lifetime {
311-
crate fn print(&self) -> &str {
311+
crate fn print(&self) -> impl fmt::Display + '_ {
312312
self.get_ref()
313313
}
314314
}
@@ -445,11 +445,10 @@ impl clean::GenericArgs {
445445
impl clean::PathSegment {
446446
crate fn print(&self) -> impl fmt::Display + '_ {
447447
display_fn(move |f| {
448-
f.write_str(&self.name)?;
449448
if f.alternate() {
450-
write!(f, "{:#}", self.args.print())
449+
write!(f, "{}{:#}", self.name, self.args.print())
451450
} else {
452-
write!(f, "{}", self.args.print())
451+
write!(f, "{}{}", self.name, self.args.print())
453452
}
454453
})
455454
}
@@ -544,7 +543,7 @@ fn resolved_path(
544543
last.name.to_string()
545544
}
546545
} else {
547-
anchor(did, &last.name).to_string()
546+
anchor(did, &*last.name.as_str()).to_string()
548547
};
549548
write!(w, "{}{}", path, last.args.print())?;
550549
}
@@ -1159,11 +1158,11 @@ impl PrintWithSpace for hir::Mutability {
11591158
impl clean::Import {
11601159
crate fn print(&self) -> impl fmt::Display + '_ {
11611160
display_fn(move |f| match self.kind {
1162-
clean::ImportKind::Simple(ref name) => {
1163-
if *name == self.source.path.last_name() {
1161+
clean::ImportKind::Simple(name) => {
1162+
if name == self.source.path.last() {
11641163
write!(f, "use {};", self.source.print())
11651164
} else {
1166-
write!(f, "use {} as {};", self.source.print(), *name)
1165+
write!(f, "use {} as {};", self.source.print(), name)
11671166
}
11681167
}
11691168
clean::ImportKind::Glob => {
@@ -1187,7 +1186,7 @@ impl clean::ImportSource {
11871186
}
11881187
let name = self.path.last_name();
11891188
if let hir::def::Res::PrimTy(p) = self.path.res {
1190-
primitive_link(f, PrimitiveType::from(p), name)?;
1189+
primitive_link(f, PrimitiveType::from(p), &*name)?;
11911190
} else {
11921191
write!(f, "{}", name)?;
11931192
}

‎src/librustdoc/html/render/cache.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::BTreeMap;
22
use std::path::Path;
33

44
use rustc_data_structures::fx::FxHashMap;
5-
use rustc_span::symbol::sym;
5+
use rustc_span::symbol::{sym, Symbol};
66
use serde::Serialize;
77

88
use crate::clean::types::GetDefId;
@@ -191,12 +191,12 @@ fn get_index_type(clean_type: &clean::Type) -> RenderType {
191191
RenderType {
192192
ty: clean_type.def_id(),
193193
idx: None,
194-
name: get_index_type_name(clean_type, true).map(|s| s.to_ascii_lowercase()),
194+
name: get_index_type_name(clean_type, true).map(|s| s.as_str().to_ascii_lowercase()),
195195
generics: get_generics(clean_type),
196196
}
197197
}
198198

199-
fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option<String> {
199+
fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option<Symbol> {
200200
match *clean_type {
201201
clean::ResolvedPath { ref path, .. } => {
202202
let segments = &path.segments;
@@ -206,10 +206,10 @@ fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option
206206
clean_type, accept_generic
207207
)
208208
});
209-
Some(path_segment.name.clone())
209+
Some(path_segment.name)
210210
}
211-
clean::Generic(s) if accept_generic => Some(s.to_string()),
212-
clean::Primitive(ref p) => Some(format!("{:?}", p)),
211+
clean::Generic(s) if accept_generic => Some(s),
212+
clean::Primitive(ref p) => Some(p.as_sym()),
213213
clean::BorrowedRef { ref type_, .. } => get_index_type_name(type_, accept_generic),
214214
// FIXME: add all from clean::Type.
215215
_ => None,
@@ -222,7 +222,7 @@ fn get_generics(clean_type: &clean::Type) -> Option<Vec<Generic>> {
222222
.iter()
223223
.filter_map(|t| {
224224
get_index_type_name(t, false).map(|name| Generic {
225-
name: name.to_ascii_lowercase(),
225+
name: name.as_str().to_ascii_lowercase(),
226226
defid: t.def_id(),
227227
idx: None,
228228
})

‎src/librustdoc/html/render/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,14 +2137,14 @@ fn item_module(w: &mut Buffer, cx: &Context, item: &clean::Item, items: &[clean:
21372137
w,
21382138
"<tr><td><code>{}extern crate {} as {};",
21392139
myitem.visibility.print_with_space(),
2140-
anchor(myitem.def_id, src),
2140+
anchor(myitem.def_id, &*src.as_str()),
21412141
name
21422142
),
21432143
None => write!(
21442144
w,
21452145
"<tr><td><code>{}extern crate {};",
21462146
myitem.visibility.print_with_space(),
2147-
anchor(myitem.def_id, name)
2147+
anchor(myitem.def_id, &*name.as_str())
21482148
),
21492149
}
21502150
write!(w, "</code></td></tr>");
@@ -2444,7 +2444,7 @@ fn render_implementor(
24442444
implementor: &Impl,
24452445
parent: &clean::Item,
24462446
w: &mut Buffer,
2447-
implementor_dups: &FxHashMap<&str, (DefId, bool)>,
2447+
implementor_dups: &FxHashMap<Symbol, (DefId, bool)>,
24482448
aliases: &[String],
24492449
cache: &Cache,
24502450
) {
@@ -2455,7 +2455,7 @@ fn render_implementor(
24552455
| clean::BorrowedRef {
24562456
type_: box clean::ResolvedPath { ref path, is_generic: false, .. },
24572457
..
2458-
} => implementor_dups[path.last_name()].1,
2458+
} => implementor_dups[&path.last()].1,
24592459
_ => false,
24602460
};
24612461
render_impl(
@@ -2704,7 +2704,7 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait,
27042704
if let Some(implementors) = cache.implementors.get(&it.def_id) {
27052705
// The DefId is for the first Type found with that name. The bool is
27062706
// if any Types with the same name but different DefId have been found.
2707-
let mut implementor_dups: FxHashMap<&str, (DefId, bool)> = FxHashMap::default();
2707+
let mut implementor_dups: FxHashMap<Symbol, (DefId, bool)> = FxHashMap::default();
27082708
for implementor in implementors {
27092709
match implementor.inner_impl().for_ {
27102710
clean::ResolvedPath { ref path, did, is_generic: false, .. }
@@ -2713,7 +2713,7 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait,
27132713
..
27142714
} => {
27152715
let &mut (prev_did, ref mut has_duplicates) =
2716-
implementor_dups.entry(path.last_name()).or_insert((did, false));
2716+
implementor_dups.entry(path.last()).or_insert((did, false));
27172717
if prev_did != did {
27182718
*has_duplicates = true;
27192719
}

‎src/librustdoc/json/conversions.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl From<clean::GenericArg> for GenericArg {
120120
fn from(arg: clean::GenericArg) -> Self {
121121
use clean::GenericArg::*;
122122
match arg {
123-
Lifetime(l) => GenericArg::Lifetime(l.0),
123+
Lifetime(l) => GenericArg::Lifetime(l.0.to_string()),
124124
Type(t) => GenericArg::Type(t.into()),
125125
Const(c) => GenericArg::Const(c.into()),
126126
}
@@ -163,7 +163,9 @@ impl From<clean::ItemKind> for ItemEnum {
163163
use clean::ItemKind::*;
164164
match item {
165165
ModuleItem(m) => ItemEnum::ModuleItem(m.into()),
166-
ExternCrateItem(c, a) => ItemEnum::ExternCrateItem { name: c, rename: a },
166+
ExternCrateItem(c, a) => {
167+
ItemEnum::ExternCrateItem { name: c.to_string(), rename: a.map(|x| x.to_string()) }
168+
}
167169
ImportItem(i) => ItemEnum::ImportItem(i.into()),
168170
StructItem(s) => ItemEnum::StructItem(s.into()),
169171
UnionItem(u) => ItemEnum::StructItem(u.into()),
@@ -302,7 +304,7 @@ impl From<clean::WherePredicate> for WherePredicate {
302304
bounds: bounds.into_iter().map(Into::into).collect(),
303305
},
304306
RegionPredicate { lifetime, bounds } => WherePredicate::RegionPredicate {
305-
lifetime: lifetime.0,
307+
lifetime: lifetime.0.to_string(),
306308
bounds: bounds.into_iter().map(Into::into).collect(),
307309
},
308310
EqPredicate { lhs, rhs } => {
@@ -323,7 +325,7 @@ impl From<clean::GenericBound> for GenericBound {
323325
modifier: modifier.into(),
324326
}
325327
}
326-
Outlives(lifetime) => GenericBound::Outlives(lifetime.0),
328+
Outlives(lifetime) => GenericBound::Outlives(lifetime.0.to_string()),
327329
}
328330
}
329331
}
@@ -365,7 +367,7 @@ impl From<clean::Type> for Type {
365367
type_: Box::new((*type_).into()),
366368
},
367369
BorrowedRef { lifetime, mutability, type_ } => Type::BorrowedRef {
368-
lifetime: lifetime.map(|l| l.0),
370+
lifetime: lifetime.map(|l| l.0.to_string()),
369371
mutable: mutability == ast::Mutability::Mut,
370372
type_: Box::new((*type_).into()),
371373
},
@@ -503,7 +505,7 @@ impl From<clean::Import> for Import {
503505
match import.kind {
504506
Simple(s) => Import {
505507
span: import.source.path.whole_name(),
506-
name: s,
508+
name: s.to_string(),
507509
id: import.source.did.map(Into::into),
508510
glob: false,
509511
},
@@ -519,7 +521,10 @@ impl From<clean::Import> for Import {
519521

520522
impl From<clean::ProcMacro> for ProcMacro {
521523
fn from(mac: clean::ProcMacro) -> Self {
522-
ProcMacro { kind: mac.kind.into(), helpers: mac.helpers }
524+
ProcMacro {
525+
kind: mac.kind.into(),
526+
helpers: mac.helpers.iter().map(|x| x.to_string()).collect(),
527+
}
523528
}
524529
}
525530

0 commit comments

Comments
 (0)
This repository has been archived.