Skip to content

Commit bdd0a78

Browse files
committed
Auto merge of #80091 - GuillaumeGomez:str-to-symbol, r=jyn514
Replace String with Symbol where possible The same as #80047 but on different types. Might be interesting to run some perf comparison. r? `@jyn514`
2 parents 9b84d36 + 50d221c commit bdd0a78

File tree

11 files changed

+109
-106
lines changed

11 files changed

+109
-106
lines changed

src/librustdoc/clean/auto_trait.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,9 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
333333
match br {
334334
// We only care about named late bound regions, as we need to add them
335335
// to the 'for<>' section
336-
ty::BrNamed(_, name) => Some(GenericParamDef {
337-
name: name.to_string(),
338-
kind: GenericParamDefKind::Lifetime,
339-
}),
336+
ty::BrNamed(_, name) => {
337+
Some(GenericParamDef { name, kind: GenericParamDefKind::Lifetime })
338+
}
340339
_ => None,
341340
}
342341
})
@@ -569,7 +568,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
569568
}
570569
WherePredicate::EqPredicate { lhs, rhs } => {
571570
match lhs {
572-
Type::QPath { name: ref left_name, ref self_type, ref trait_ } => {
571+
Type::QPath { name: left_name, ref self_type, ref trait_ } => {
573572
let ty = &*self_type;
574573
match **trait_ {
575574
Type::ResolvedPath {
@@ -580,7 +579,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
580579
} => {
581580
let mut new_trait_path = trait_path.clone();
582581

583-
if self.is_fn_ty(tcx, trait_) && left_name == FN_OUTPUT_NAME {
582+
if self.is_fn_ty(tcx, trait_) && left_name == sym::Output {
584583
ty_to_fn
585584
.entry(*ty.clone())
586585
.and_modify(|e| *e = (e.0.clone(), Some(rhs.clone())))

src/librustdoc/clean/inline.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_metadata::creader::LoadedMacro;
1212
use rustc_middle::ty;
1313
use rustc_mir::const_eval::is_min_const_fn;
1414
use rustc_span::hygiene::MacroKind;
15-
use rustc_span::symbol::{sym, Symbol};
15+
use rustc_span::symbol::{kw, sym, Symbol};
1616
use rustc_span::Span;
1717

1818
use crate::clean::{self, Attributes, GetDefId, ToSource, TypeKind};
@@ -583,7 +583,7 @@ fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean:
583583
for pred in &mut g.where_predicates {
584584
match *pred {
585585
clean::WherePredicate::BoundPredicate { ty: clean::Generic(ref s), ref mut bounds }
586-
if *s == "Self" =>
586+
if *s == kw::SelfUpper =>
587587
{
588588
bounds.retain(|bound| match *bound {
589589
clean::GenericBound::TraitBound(
@@ -606,7 +606,7 @@ fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean:
606606
name: ref _name,
607607
},
608608
ref bounds,
609-
} => !(bounds.is_empty() || *s == "Self" && did == trait_did),
609+
} => !(bounds.is_empty() || *s == kw::SelfUpper && did == trait_did),
610610
_ => true,
611611
});
612612
g
@@ -621,7 +621,7 @@ fn separate_supertrait_bounds(
621621
let mut ty_bounds = Vec::new();
622622
g.where_predicates.retain(|pred| match *pred {
623623
clean::WherePredicate::BoundPredicate { ty: clean::Generic(ref s), ref bounds }
624-
if *s == "Self" =>
624+
if *s == kw::SelfUpper =>
625625
{
626626
ty_bounds.extend(bounds.iter().cloned());
627627
false

src/librustdoc/clean/mod.rs

+30-37
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ crate use self::types::Type::*;
4848
crate use self::types::Visibility::{Inherited, Public};
4949
crate use self::types::*;
5050

51-
const FN_OUTPUT_NAME: &str = "Output";
52-
5351
crate trait Clean<T> {
5452
fn clean(&self, cx: &DocContext<'_>) -> T;
5553
}
@@ -329,10 +327,9 @@ impl Clean<GenericBound> for (ty::PolyTraitRef<'_>, &[TypeBinding]) {
329327
.collect_referenced_late_bound_regions(&poly_trait_ref)
330328
.into_iter()
331329
.filter_map(|br| match br {
332-
ty::BrNamed(_, name) => Some(GenericParamDef {
333-
name: name.to_string(),
334-
kind: GenericParamDefKind::Lifetime,
335-
}),
330+
ty::BrNamed(_, name) => {
331+
Some(GenericParamDef { name, kind: GenericParamDefKind::Lifetime })
332+
}
336333
_ => None,
337334
})
338335
.collect();
@@ -546,7 +543,7 @@ impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
546543
GenericBound::Outlives(_) => panic!("cleaning a trait got a lifetime"),
547544
};
548545
Type::QPath {
549-
name: cx.tcx.associated_item(self.item_def_id).ident.name.clean(cx),
546+
name: cx.tcx.associated_item(self.item_def_id).ident.name,
550547
self_type: box self.self_ty().clean(cx),
551548
trait_: box trait_,
552549
}
@@ -556,14 +553,12 @@ impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
556553
impl Clean<GenericParamDef> for ty::GenericParamDef {
557554
fn clean(&self, cx: &DocContext<'_>) -> GenericParamDef {
558555
let (name, kind) = match self.kind {
559-
ty::GenericParamDefKind::Lifetime => {
560-
(self.name.to_string(), GenericParamDefKind::Lifetime)
561-
}
556+
ty::GenericParamDefKind::Lifetime => (self.name, GenericParamDefKind::Lifetime),
562557
ty::GenericParamDefKind::Type { has_default, synthetic, .. } => {
563558
let default =
564559
if has_default { Some(cx.tcx.type_of(self.def_id).clean(cx)) } else { None };
565560
(
566-
self.name.clean(cx),
561+
self.name,
567562
GenericParamDefKind::Type {
568563
did: self.def_id,
569564
bounds: vec![], // These are filled in from the where-clauses.
@@ -573,7 +568,7 @@ impl Clean<GenericParamDef> for ty::GenericParamDef {
573568
)
574569
}
575570
ty::GenericParamDefKind::Const { .. } => (
576-
self.name.clean(cx),
571+
self.name,
577572
GenericParamDefKind::Const {
578573
did: self.def_id,
579574
ty: cx.tcx.type_of(self.def_id).clean(cx),
@@ -599,14 +594,14 @@ impl Clean<GenericParamDef> for hir::GenericParam<'_> {
599594
for bound in bounds {
600595
s.push_str(&format!(" + {}", bound.name.ident()));
601596
}
602-
s
597+
Symbol::intern(&s)
603598
} else {
604-
self.name.ident().to_string()
599+
self.name.ident().name
605600
};
606601
(name, GenericParamDefKind::Lifetime)
607602
}
608603
hir::GenericParamKind::Type { ref default, synthetic } => (
609-
self.name.ident().name.clean(cx),
604+
self.name.ident().name,
610605
GenericParamDefKind::Type {
611606
did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(),
612607
bounds: self.bounds.clean(cx),
@@ -615,7 +610,7 @@ impl Clean<GenericParamDef> for hir::GenericParam<'_> {
615610
},
616611
),
617612
hir::GenericParamKind::Const { ref ty } => (
618-
self.name.ident().name.clean(cx),
613+
self.name.ident().name,
619614
GenericParamDefKind::Const {
620615
did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(),
621616
ty: ty.clean(cx),
@@ -730,7 +725,7 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
730725
.collect::<Vec<GenericParamDef>>();
731726

732727
// param index -> [(DefId of trait, associated type name, type)]
733-
let mut impl_trait_proj = FxHashMap::<u32, Vec<(DefId, String, Ty<'tcx>)>>::default();
728+
let mut impl_trait_proj = FxHashMap::<u32, Vec<(DefId, Symbol, Ty<'tcx>)>>::default();
734729

735730
let where_predicates = preds
736731
.predicates
@@ -778,11 +773,10 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
778773
if let Some(((_, trait_did, name), rhs)) =
779774
proj.as_ref().and_then(|(lhs, rhs)| Some((lhs.projection()?, rhs)))
780775
{
781-
impl_trait_proj.entry(param_idx).or_default().push((
782-
trait_did,
783-
name.to_string(),
784-
rhs,
785-
));
776+
impl_trait_proj
777+
.entry(param_idx)
778+
.or_default()
779+
.push((trait_did, name, rhs));
786780
}
787781

788782
return None;
@@ -800,7 +794,7 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
800794
if let crate::core::ImplTraitParam::ParamIndex(idx) = param {
801795
if let Some(proj) = impl_trait_proj.remove(&idx) {
802796
for (trait_did, name, rhs) in proj {
803-
simplify::merge_bounds(cx, &mut bounds, trait_did, &name, &rhs.clean(cx));
797+
simplify::merge_bounds(cx, &mut bounds, trait_did, name, &rhs.clean(cx));
804798
}
805799
}
806800
} else {
@@ -936,9 +930,9 @@ impl<'a> Clean<Arguments> for (&'a [hir::Ty<'a>], &'a [Ident]) {
936930
.iter()
937931
.enumerate()
938932
.map(|(i, ty)| {
939-
let mut name = self.1.get(i).map(|ident| ident.to_string()).unwrap_or_default();
933+
let mut name = self.1.get(i).map(|ident| ident.name).unwrap_or(kw::Invalid);
940934
if name.is_empty() {
941-
name = "_".to_string();
935+
name = kw::Underscore;
942936
}
943937
Argument { name, type_: ty.clean(cx) }
944938
})
@@ -995,7 +989,7 @@ impl<'tcx> Clean<FnDecl> for (DefId, ty::PolyFnSig<'tcx>) {
995989
.iter()
996990
.map(|t| Argument {
997991
type_: t.clean(cx),
998-
name: names.next().map_or_else(|| String::new(), |name| name.to_string()),
992+
name: names.next().map(|i| i.name).unwrap_or(kw::Invalid),
999993
})
1000994
.collect(),
1001995
},
@@ -1150,12 +1144,12 @@ impl Clean<Item> for ty::AssocItem {
11501144
};
11511145
let self_arg_ty = sig.input(0).skip_binder();
11521146
if self_arg_ty == self_ty {
1153-
decl.inputs.values[0].type_ = Generic(String::from("Self"));
1147+
decl.inputs.values[0].type_ = Generic(kw::SelfUpper);
11541148
} else if let ty::Ref(_, ty, _) = *self_arg_ty.kind() {
11551149
if ty == self_ty {
11561150
match decl.inputs.values[0].type_ {
11571151
BorrowedRef { ref mut type_, .. } => {
1158-
**type_ = Generic(String::from("Self"))
1152+
**type_ = Generic(kw::SelfUpper)
11591153
}
11601154
_ => unreachable!(),
11611155
}
@@ -1210,7 +1204,7 @@ impl Clean<Item> for ty::AssocItem {
12101204
}
12111205
}
12121206
ty::AssocKind::Type => {
1213-
let my_name = self.ident.name.clean(cx);
1207+
let my_name = self.ident.name;
12141208

12151209
if let ty::TraitContainer(_) = self.container {
12161210
let bounds = cx.tcx.explicit_item_bounds(self.def_id);
@@ -1235,7 +1229,7 @@ impl Clean<Item> for ty::AssocItem {
12351229
_ => return None,
12361230
}
12371231
match **self_type {
1238-
Generic(ref s) if *s == "Self" => {}
1232+
Generic(ref s) if *s == kw::SelfUpper => {}
12391233
_ => return None,
12401234
}
12411235
Some(bounds)
@@ -1408,7 +1402,7 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &DocContext<'_>) -> Type {
14081402
segments: trait_segments.clean(cx),
14091403
};
14101404
Type::QPath {
1411-
name: p.segments.last().expect("segments were empty").ident.name.clean(cx),
1405+
name: p.segments.last().expect("segments were empty").ident.name,
14121406
self_type: box qself.clean(cx),
14131407
trait_: box resolve_type(cx, trait_path, hir_id),
14141408
}
@@ -1422,7 +1416,7 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &DocContext<'_>) -> Type {
14221416
};
14231417
let trait_path = hir::Path { span, res, segments: &[] };
14241418
Type::QPath {
1425-
name: segment.ident.name.clean(cx),
1419+
name: segment.ident.name,
14261420
self_type: box qself.clean(cx),
14271421
trait_: box resolve_type(cx, trait_path.clean(cx), hir_id),
14281422
}
@@ -1625,7 +1619,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
16251619
let mut bindings = vec![];
16261620
for pb in obj.projection_bounds() {
16271621
bindings.push(TypeBinding {
1628-
name: cx.tcx.associated_item(pb.item_def_id()).ident.name.clean(cx),
1622+
name: cx.tcx.associated_item(pb.item_def_id()).ident.name,
16291623
kind: TypeBindingKind::Equality { ty: pb.skip_binder().ty.clean(cx) },
16301624
});
16311625
}
@@ -1644,7 +1638,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
16441638
if let Some(bounds) = cx.impl_trait_bounds.borrow_mut().remove(&p.index.into()) {
16451639
ImplTrait(bounds)
16461640
} else {
1647-
Generic(p.name.to_string())
1641+
Generic(p.name)
16481642
}
16491643
}
16501644

@@ -1702,8 +1696,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
17021696
.tcx
17031697
.associated_item(proj.projection_ty.item_def_id)
17041698
.ident
1705-
.name
1706-
.clean(cx),
1699+
.name,
17071700
kind: TypeBindingKind::Equality {
17081701
ty: proj.ty.clean(cx),
17091702
},
@@ -2339,7 +2332,7 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {
23392332

23402333
impl Clean<TypeBinding> for hir::TypeBinding<'_> {
23412334
fn clean(&self, cx: &DocContext<'_>) -> TypeBinding {
2342-
TypeBinding { name: self.ident.name.clean(cx), kind: self.kind.clean(cx) }
2335+
TypeBinding { name: self.ident.name, kind: self.kind.clean(cx) }
23432336
}
23442337
}
23452338

src/librustdoc/clean/simplify.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::collections::BTreeMap;
1515

1616
use rustc_hir::def_id::DefId;
1717
use rustc_middle::ty;
18+
use rustc_span::Symbol;
1819

1920
use crate::clean;
2021
use crate::clean::GenericArgs as PP;
@@ -78,7 +79,7 @@ crate fn merge_bounds(
7879
cx: &clean::DocContext<'_>,
7980
bounds: &mut Vec<clean::GenericBound>,
8081
trait_did: DefId,
81-
name: &str,
82+
name: Symbol,
8283
rhs: &clean::Type,
8384
) -> bool {
8485
!bounds.iter_mut().any(|b| {
@@ -100,7 +101,7 @@ crate fn merge_bounds(
100101
match last.args {
101102
PP::AngleBracketed { ref mut bindings, .. } => {
102103
bindings.push(clean::TypeBinding {
103-
name: name.to_string(),
104+
name,
104105
kind: clean::TypeBindingKind::Equality { ty: rhs.clone() },
105106
});
106107
}

0 commit comments

Comments
 (0)