Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace String with Symbol where possible #80091

Merged
merged 1 commit into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/librustdoc/clean/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,9 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
match br {
// We only care about named late bound regions, as we need to add them
// to the 'for<>' section
ty::BrNamed(_, name) => Some(GenericParamDef {
name: name.to_string(),
kind: GenericParamDefKind::Lifetime,
}),
ty::BrNamed(_, name) => {
Some(GenericParamDef { name, kind: GenericParamDefKind::Lifetime })
}
_ => None,
}
})
Expand Down Expand Up @@ -569,7 +568,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
}
WherePredicate::EqPredicate { lhs, rhs } => {
match lhs {
Type::QPath { name: ref left_name, ref self_type, ref trait_ } => {
Type::QPath { name: left_name, ref self_type, ref trait_ } => {
let ty = &*self_type;
match **trait_ {
Type::ResolvedPath {
Expand All @@ -580,7 +579,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
} => {
let mut new_trait_path = trait_path.clone();

if self.is_fn_ty(tcx, trait_) && left_name == FN_OUTPUT_NAME {
if self.is_fn_ty(tcx, trait_) && left_name == sym::Output {
ty_to_fn
.entry(*ty.clone())
.and_modify(|e| *e = (e.0.clone(), Some(rhs.clone())))
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_metadata::creader::LoadedMacro;
use rustc_middle::ty;
use rustc_mir::const_eval::is_min_const_fn;
use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{sym, Symbol};
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span;

use crate::clean::{self, Attributes, GetDefId, ToSource, TypeKind};
Expand Down Expand Up @@ -583,7 +583,7 @@ fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean:
for pred in &mut g.where_predicates {
match *pred {
clean::WherePredicate::BoundPredicate { ty: clean::Generic(ref s), ref mut bounds }
if *s == "Self" =>
if *s == kw::SelfUpper =>
{
bounds.retain(|bound| match *bound {
clean::GenericBound::TraitBound(
Expand All @@ -606,7 +606,7 @@ fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean:
name: ref _name,
},
ref bounds,
} => !(bounds.is_empty() || *s == "Self" && did == trait_did),
} => !(bounds.is_empty() || *s == kw::SelfUpper && did == trait_did),
_ => true,
});
g
Expand All @@ -621,7 +621,7 @@ fn separate_supertrait_bounds(
let mut ty_bounds = Vec::new();
g.where_predicates.retain(|pred| match *pred {
clean::WherePredicate::BoundPredicate { ty: clean::Generic(ref s), ref bounds }
if *s == "Self" =>
if *s == kw::SelfUpper =>
{
ty_bounds.extend(bounds.iter().cloned());
false
Expand Down
67 changes: 30 additions & 37 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ crate use self::types::Type::*;
crate use self::types::Visibility::{Inherited, Public};
crate use self::types::*;

const FN_OUTPUT_NAME: &str = "Output";

crate trait Clean<T> {
fn clean(&self, cx: &DocContext<'_>) -> T;
}
Expand Down Expand Up @@ -329,10 +327,9 @@ impl Clean<GenericBound> for (ty::PolyTraitRef<'_>, &[TypeBinding]) {
.collect_referenced_late_bound_regions(&poly_trait_ref)
.into_iter()
.filter_map(|br| match br {
ty::BrNamed(_, name) => Some(GenericParamDef {
name: name.to_string(),
kind: GenericParamDefKind::Lifetime,
}),
ty::BrNamed(_, name) => {
Some(GenericParamDef { name, kind: GenericParamDefKind::Lifetime })
}
_ => None,
})
.collect();
Expand Down Expand Up @@ -546,7 +543,7 @@ impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
GenericBound::Outlives(_) => panic!("cleaning a trait got a lifetime"),
};
Type::QPath {
name: cx.tcx.associated_item(self.item_def_id).ident.name.clean(cx),
name: cx.tcx.associated_item(self.item_def_id).ident.name,
self_type: box self.self_ty().clean(cx),
trait_: box trait_,
}
Expand All @@ -556,14 +553,12 @@ impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
impl Clean<GenericParamDef> for ty::GenericParamDef {
fn clean(&self, cx: &DocContext<'_>) -> GenericParamDef {
let (name, kind) = match self.kind {
ty::GenericParamDefKind::Lifetime => {
(self.name.to_string(), GenericParamDefKind::Lifetime)
}
ty::GenericParamDefKind::Lifetime => (self.name, GenericParamDefKind::Lifetime),
ty::GenericParamDefKind::Type { has_default, synthetic, .. } => {
let default =
if has_default { Some(cx.tcx.type_of(self.def_id).clean(cx)) } else { None };
(
self.name.clean(cx),
self.name,
GenericParamDefKind::Type {
did: self.def_id,
bounds: vec![], // These are filled in from the where-clauses.
Expand All @@ -573,7 +568,7 @@ impl Clean<GenericParamDef> for ty::GenericParamDef {
)
}
ty::GenericParamDefKind::Const { .. } => (
self.name.clean(cx),
self.name,
GenericParamDefKind::Const {
did: self.def_id,
ty: cx.tcx.type_of(self.def_id).clean(cx),
Expand All @@ -599,14 +594,14 @@ impl Clean<GenericParamDef> for hir::GenericParam<'_> {
for bound in bounds {
s.push_str(&format!(" + {}", bound.name.ident()));
}
s
Symbol::intern(&s)
} else {
self.name.ident().to_string()
self.name.ident().name
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
};
(name, GenericParamDefKind::Lifetime)
}
hir::GenericParamKind::Type { ref default, synthetic } => (
self.name.ident().name.clean(cx),
self.name.ident().name,
GenericParamDefKind::Type {
did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(),
bounds: self.bounds.clean(cx),
Expand All @@ -615,7 +610,7 @@ impl Clean<GenericParamDef> for hir::GenericParam<'_> {
},
),
hir::GenericParamKind::Const { ref ty } => (
self.name.ident().name.clean(cx),
self.name.ident().name,
GenericParamDefKind::Const {
did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(),
ty: ty.clean(cx),
Expand Down Expand Up @@ -730,7 +725,7 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
.collect::<Vec<GenericParamDef>>();

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

let where_predicates = preds
.predicates
Expand Down Expand Up @@ -778,11 +773,10 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
if let Some(((_, trait_did, name), rhs)) =
proj.as_ref().and_then(|(lhs, rhs)| Some((lhs.projection()?, rhs)))
{
impl_trait_proj.entry(param_idx).or_default().push((
trait_did,
name.to_string(),
rhs,
));
impl_trait_proj
.entry(param_idx)
.or_default()
.push((trait_did, name, rhs));
}

return None;
Expand All @@ -800,7 +794,7 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
if let crate::core::ImplTraitParam::ParamIndex(idx) = param {
if let Some(proj) = impl_trait_proj.remove(&idx) {
for (trait_did, name, rhs) in proj {
simplify::merge_bounds(cx, &mut bounds, trait_did, &name, &rhs.clean(cx));
simplify::merge_bounds(cx, &mut bounds, trait_did, name, &rhs.clean(cx));
}
}
} else {
Expand Down Expand Up @@ -936,9 +930,9 @@ impl<'a> Clean<Arguments> for (&'a [hir::Ty<'a>], &'a [Ident]) {
.iter()
.enumerate()
.map(|(i, ty)| {
let mut name = self.1.get(i).map(|ident| ident.to_string()).unwrap_or_default();
let mut name = self.1.get(i).map(|ident| ident.name).unwrap_or(kw::Invalid);
if name.is_empty() {
name = "_".to_string();
name = kw::Underscore;
}
Argument { name, type_: ty.clean(cx) }
})
Expand Down Expand Up @@ -995,7 +989,7 @@ impl<'tcx> Clean<FnDecl> for (DefId, ty::PolyFnSig<'tcx>) {
.iter()
.map(|t| Argument {
type_: t.clean(cx),
name: names.next().map_or_else(|| String::new(), |name| name.to_string()),
name: names.next().map(|i| i.name).unwrap_or(kw::Invalid),
})
.collect(),
},
Expand Down Expand Up @@ -1150,12 +1144,12 @@ impl Clean<Item> for ty::AssocItem {
};
let self_arg_ty = sig.input(0).skip_binder();
if self_arg_ty == self_ty {
decl.inputs.values[0].type_ = Generic(String::from("Self"));
decl.inputs.values[0].type_ = Generic(kw::SelfUpper);
} else if let ty::Ref(_, ty, _) = *self_arg_ty.kind() {
if ty == self_ty {
match decl.inputs.values[0].type_ {
BorrowedRef { ref mut type_, .. } => {
**type_ = Generic(String::from("Self"))
**type_ = Generic(kw::SelfUpper)
}
_ => unreachable!(),
}
Expand Down Expand Up @@ -1210,7 +1204,7 @@ impl Clean<Item> for ty::AssocItem {
}
}
ty::AssocKind::Type => {
let my_name = self.ident.name.clean(cx);
let my_name = self.ident.name;

if let ty::TraitContainer(_) = self.container {
let bounds = cx.tcx.explicit_item_bounds(self.def_id);
Expand All @@ -1235,7 +1229,7 @@ impl Clean<Item> for ty::AssocItem {
_ => return None,
}
match **self_type {
Generic(ref s) if *s == "Self" => {}
Generic(ref s) if *s == kw::SelfUpper => {}
_ => return None,
}
Some(bounds)
Expand Down Expand Up @@ -1408,7 +1402,7 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &DocContext<'_>) -> Type {
segments: trait_segments.clean(cx),
};
Type::QPath {
name: p.segments.last().expect("segments were empty").ident.name.clean(cx),
name: p.segments.last().expect("segments were empty").ident.name,
self_type: box qself.clean(cx),
trait_: box resolve_type(cx, trait_path, hir_id),
}
Expand All @@ -1422,7 +1416,7 @@ fn clean_qpath(hir_ty: &hir::Ty<'_>, cx: &DocContext<'_>) -> Type {
};
let trait_path = hir::Path { span, res, segments: &[] };
Type::QPath {
name: segment.ident.name.clean(cx),
name: segment.ident.name,
self_type: box qself.clean(cx),
trait_: box resolve_type(cx, trait_path.clean(cx), hir_id),
}
Expand Down Expand Up @@ -1625,7 +1619,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
let mut bindings = vec![];
for pb in obj.projection_bounds() {
bindings.push(TypeBinding {
name: cx.tcx.associated_item(pb.item_def_id()).ident.name.clean(cx),
name: cx.tcx.associated_item(pb.item_def_id()).ident.name,
kind: TypeBindingKind::Equality { ty: pb.skip_binder().ty.clean(cx) },
});
}
Expand All @@ -1644,7 +1638,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
if let Some(bounds) = cx.impl_trait_bounds.borrow_mut().remove(&p.index.into()) {
ImplTrait(bounds)
} else {
Generic(p.name.to_string())
Generic(p.name)
}
}

Expand Down Expand Up @@ -1702,8 +1696,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
.tcx
.associated_item(proj.projection_ty.item_def_id)
.ident
.name
.clean(cx),
.name,
kind: TypeBindingKind::Equality {
ty: proj.ty.clean(cx),
},
Expand Down Expand Up @@ -2339,7 +2332,7 @@ impl Clean<Item> for (&hir::MacroDef<'_>, Option<Symbol>) {

impl Clean<TypeBinding> for hir::TypeBinding<'_> {
fn clean(&self, cx: &DocContext<'_>) -> TypeBinding {
TypeBinding { name: self.ident.name.clean(cx), kind: self.kind.clean(cx) }
TypeBinding { name: self.ident.name, kind: self.kind.clean(cx) }
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/librustdoc/clean/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::collections::BTreeMap;

use rustc_hir::def_id::DefId;
use rustc_middle::ty;
use rustc_span::Symbol;

use crate::clean;
use crate::clean::GenericArgs as PP;
Expand Down Expand Up @@ -78,7 +79,7 @@ crate fn merge_bounds(
cx: &clean::DocContext<'_>,
bounds: &mut Vec<clean::GenericBound>,
trait_did: DefId,
name: &str,
name: Symbol,
rhs: &clean::Type,
) -> bool {
!bounds.iter_mut().any(|b| {
Expand All @@ -100,7 +101,7 @@ crate fn merge_bounds(
match last.args {
PP::AngleBracketed { ref mut bindings, .. } => {
bindings.push(clean::TypeBinding {
name: name.to_string(),
name,
kind: clean::TypeBindingKind::Equality { ty: rhs.clone() },
});
}
Expand Down
Loading