Skip to content

Commit fee693d

Browse files
committed
Auto merge of rust-lang#80119 - GuillaumeGomez:str-to-symbol, r=jyn514
Continue String to Symbol conversion in rustdoc Follow-up of rust-lang#80091. This PR is already big enough so I'll stop here before the next one. r? `@jyn514`
2 parents 8d006c0 + 44e226c commit fee693d

File tree

9 files changed

+74
-69
lines changed

9 files changed

+74
-69
lines changed

src/librustdoc/clean/auto_trait.rs

+7-7
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

+4-4
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

+12-13
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

+17-15
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

+1-1
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

+8-9
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

+7-7
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
})

0 commit comments

Comments
 (0)