Skip to content

Commit 8e849d8

Browse files
committed
Auto merge of #60815 - nnethercote:use-Symbol-more-2, r=<try>
Use `Symbol` even more These patches simplify the code a bit (fewer conversions) and also speed things up a bit (fewer `with_interner` calls). r? @petrochenkov
2 parents 80e7cde + 67b969e commit 8e849d8

File tree

31 files changed

+127
-110
lines changed

31 files changed

+127
-110
lines changed

src/librustc/hir/map/definitions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ impl DefPathData {
593593
ImplTrait => "{{opaque}}",
594594
};
595595

596-
Symbol::intern(s).as_interned_str()
596+
InternedString::intern(s)
597597
}
598598

599599
pub fn to_string(&self) -> String {

src/librustc/hir/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::util::nodemap::{NodeMap, FxHashSet};
1616
use crate::mir::mono::Linkage;
1717

1818
use errors::FatalError;
19-
use syntax_pos::{Span, DUMMY_SP, symbol::InternedString};
19+
use syntax_pos::{Span, DUMMY_SP};
2020
use syntax::source_map::Spanned;
2121
use rustc_target::spec::abi::Abi;
2222
use syntax::ast::{self, CrateSugar, Ident, Name, NodeId, AsmDialect};
@@ -609,9 +609,9 @@ impl Generics {
609609
own_counts
610610
}
611611

612-
pub fn get_named(&self, name: &InternedString) -> Option<&GenericParam> {
612+
pub fn get_named(&self, name: Symbol) -> Option<&GenericParam> {
613613
for param in &self.params {
614-
if *name == param.name.ident().as_interned_str() {
614+
if name == param.name.ident().name {
615615
return Some(param);
616616
}
617617
}

src/librustc/infer/error_reporting/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -194,20 +194,20 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
194194
let mut sp = cm.def_span(self.hir().span_by_hir_id(node));
195195
if let Some(param) = self.hir()
196196
.get_generics(scope)
197-
.and_then(|generics| generics.get_named(&br.name))
197+
.and_then(|generics| generics.get_named(br.name))
198198
{
199199
sp = param.span;
200200
}
201201
(format!("the lifetime {} as defined on", br.name), sp)
202202
}
203203
ty::ReFree(ty::FreeRegion {
204-
bound_region: ty::BoundRegion::BrNamed(_, ref name),
204+
bound_region: ty::BoundRegion::BrNamed(_, name),
205205
..
206206
}) => {
207207
let mut sp = cm.def_span(self.hir().span_by_hir_id(node));
208208
if let Some(param) = self.hir()
209209
.get_generics(scope)
210-
.and_then(|generics| generics.get_named(&name))
210+
.and_then(|generics| generics.get_named(name))
211211
{
212212
sp = param.span;
213213
}

src/librustc/infer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use std::cell::{Cell, Ref, RefCell, RefMut};
3232
use std::collections::BTreeMap;
3333
use std::fmt;
3434
use syntax::ast;
35-
use syntax_pos::symbol::InternedString;
35+
use syntax_pos::symbol::Symbol;
3636
use syntax_pos::Span;
3737

3838
use self::combine::CombineFields;
@@ -383,7 +383,7 @@ pub enum RegionVariableOrigin {
383383
Coercion(Span),
384384

385385
/// Region variables created as the values for early-bound regions
386-
EarlyBoundRegion(Span, InternedString),
386+
EarlyBoundRegion(Span, Symbol),
387387

388388
/// Region variables created for bound regions
389389
/// in a function or method that is called

src/librustc/infer/type_variable.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use syntax::symbol::InternedString;
1+
use syntax::symbol::Symbol;
22
use syntax_pos::Span;
33
use crate::ty::{self, Ty, TyVid};
44

@@ -43,7 +43,7 @@ pub enum TypeVariableOrigin {
4343
MiscVariable(Span),
4444
NormalizeProjectionType(Span),
4545
TypeInference(Span),
46-
TypeParameterDefinition(Span, InternedString),
46+
TypeParameterDefinition(Span, Symbol),
4747

4848
/// one of the upvars or closure kind parameters in a `ClosureSubsts`
4949
/// (before it has been determined)

src/librustc/infer/unify_key.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::mir::interpret::ConstValue;
33
use rustc_data_structures::unify::{NoError, EqUnifyValue, UnifyKey, UnifyValue, UnificationTable};
44
use rustc_data_structures::unify::InPlace;
55
use syntax_pos::{Span, DUMMY_SP};
6-
use syntax::symbol::InternedString;
6+
use syntax::symbol::Symbol;
77

88
use std::cmp;
99
use std::marker::PhantomData;
@@ -84,7 +84,7 @@ impl ToType for FloatVarValue {
8484
pub enum ConstVariableOrigin {
8585
MiscVariable(Span),
8686
ConstInference(Span),
87-
ConstParameterDefinition(Span, InternedString),
87+
ConstParameterDefinition(Span, Symbol),
8888
SubstitutionPlaceholder(Span),
8989
}
9090

src/librustc/lint/context.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -828,8 +828,8 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
828828

829829
// This shouldn't ever be needed, but just in case:
830830
Ok(vec![match trait_ref {
831-
Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)).as_str(),
832-
None => Symbol::intern(&format!("<{}>", self_ty)).as_str(),
831+
Some(trait_ref) => LocalInternedString::intern(&format!("{:?}", trait_ref)),
832+
None => LocalInternedString::intern(&format!("<{}>", self_ty)),
833833
}])
834834
}
835835

@@ -845,9 +845,10 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
845845
// This shouldn't ever be needed, but just in case:
846846
path.push(match trait_ref {
847847
Some(trait_ref) => {
848-
Symbol::intern(&format!("<impl {} for {}>", trait_ref, self_ty)).as_str()
848+
LocalInternedString::intern(&format!("<impl {} for {}>", trait_ref,
849+
self_ty))
849850
},
850-
None => Symbol::intern(&format!("<impl {}>", self_ty)).as_str(),
851+
None => LocalInternedString::intern(&format!("<impl {}>", self_ty)),
851852
});
852853

853854
Ok(path)

src/librustc/mir/mono.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::hir::def_id::{DefId, CrateNum, LOCAL_CRATE};
22
use crate::hir::HirId;
3-
use syntax::symbol::{Symbol, InternedString};
3+
use syntax::symbol::InternedString;
44
use crate::ty::{Instance, TyCtxt};
55
use crate::util::nodemap::FxHashMap;
66
use rustc_data_structures::base_n;
@@ -280,7 +280,7 @@ impl<'a, 'gcx: 'tcx, 'tcx: 'a> CodegenUnitNameBuilder<'a, 'gcx, 'tcx> {
280280
cgu_name
281281
} else {
282282
let cgu_name = &cgu_name.as_str()[..];
283-
Symbol::intern(&CodegenUnit::mangle_name(cgu_name)).as_interned_str()
283+
InternedString::intern(&CodegenUnit::mangle_name(cgu_name))
284284
}
285285
}
286286

@@ -336,6 +336,6 @@ impl<'a, 'gcx: 'tcx, 'tcx: 'a> CodegenUnitNameBuilder<'a, 'gcx, 'tcx> {
336336
write!(cgu_name, ".{}", special_suffix).unwrap();
337337
}
338338

339-
Symbol::intern(&cgu_name[..]).as_interned_str()
339+
InternedString::intern(&cgu_name[..])
340340
}
341341
}

src/librustc/traits/object_safety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
539539
// are implemented
540540
let unsized_self_ty: Ty<'tcx> = self.mk_ty_param(
541541
::std::u32::MAX,
542-
Name::intern("RustaceansAreAwesome").as_interned_str(),
542+
Name::intern("RustaceansAreAwesome"),
543543
);
544544

545545
// `Receiver[Self => U]`

src/librustc/traits/on_unimplemented.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedFormatString {
250250
Position::ArgumentNamed(s) if s == "from_desugaring" => (),
251251
// So is `{A}` if A is a type parameter
252252
Position::ArgumentNamed(s) => match generics.params.iter().find(|param|
253-
param.name == s
253+
param.name.as_str() == s
254254
) {
255255
Some(_) => (),
256256
None => {

src/librustc/traits/structural_impls.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::traits;
44
use crate::traits::project::Normalized;
55
use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
66
use crate::ty::{self, Lift, Ty, TyCtxt};
7-
use syntax::symbol::InternedString;
7+
use syntax::symbol::Symbol;
88

99
use std::fmt;
1010
use std::rc::Rc;
@@ -261,11 +261,11 @@ impl fmt::Display for traits::QuantifierKind {
261261
/// for debug output in tests anyway.
262262
struct BoundNamesCollector {
263263
// Just sort by name because `BoundRegion::BrNamed` does not have a `BoundVar` index anyway.
264-
regions: BTreeSet<InternedString>,
264+
regions: BTreeSet<Symbol>,
265265

266266
// Sort by `BoundVar` index, so usually this should be equivalent to the order given
267267
// by the list of type parameters.
268-
types: BTreeMap<u32, InternedString>,
268+
types: BTreeMap<u32, Symbol>,
269269

270270
binder_index: ty::DebruijnIndex,
271271
}
@@ -312,8 +312,6 @@ impl<'tcx> TypeVisitor<'tcx> for BoundNamesCollector {
312312
}
313313

314314
fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
315-
use syntax::symbol::Symbol;
316-
317315
match t.sty {
318316
ty::Bound(debruijn, bound_ty) if debruijn == self.binder_index => {
319317
self.types.insert(
@@ -322,7 +320,7 @@ impl<'tcx> TypeVisitor<'tcx> for BoundNamesCollector {
322320
ty::BoundTyKind::Param(name) => name,
323321
ty::BoundTyKind::Anon => Symbol::intern(
324322
&format!("^{}", bound_ty.var.as_u32())
325-
).as_interned_str(),
323+
),
326324
}
327325
);
328326
}
@@ -334,8 +332,6 @@ impl<'tcx> TypeVisitor<'tcx> for BoundNamesCollector {
334332
}
335333

336334
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
337-
use syntax::symbol::Symbol;
338-
339335
match r {
340336
ty::ReLateBound(index, br) if *index == self.binder_index => {
341337
match br {
@@ -346,7 +342,7 @@ impl<'tcx> TypeVisitor<'tcx> for BoundNamesCollector {
346342
ty::BoundRegion::BrAnon(var) => {
347343
self.regions.insert(Symbol::intern(
348344
&format!("'^{}", var)
349-
).as_interned_str());
345+
));
350346
}
351347

352348
_ => (),

src/librustc/ty/context.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ use syntax::ast;
7474
use syntax::attr;
7575
use syntax::source_map::MultiSpan;
7676
use syntax::feature_gate;
77-
use syntax::symbol::{Symbol, keywords, InternedString, sym};
77+
use syntax::symbol::{Symbol, keywords, sym};
7878
use syntax_pos::Span;
7979

8080
use crate::hir;
@@ -2716,15 +2716,15 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
27162716
}
27172717

27182718
#[inline]
2719-
pub fn mk_ty_param(self, index: u32, name: InternedString) -> Ty<'tcx> {
2719+
pub fn mk_ty_param(self, index: u32, name: Symbol) -> Ty<'tcx> {
27202720
self.mk_ty(Param(ParamTy { index, name: name }))
27212721
}
27222722

27232723
#[inline]
27242724
pub fn mk_const_param(
27252725
self,
27262726
index: u32,
2727-
name: InternedString,
2727+
name: Symbol,
27282728
ty: Ty<'tcx>
27292729
) -> &'tcx Const<'tcx> {
27302730
self.mk_const(ty::Const {
@@ -2735,7 +2735,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
27352735

27362736
#[inline]
27372737
pub fn mk_self_type(self) -> Ty<'tcx> {
2738-
self.mk_ty_param(0, keywords::SelfUpper.name().as_interned_str())
2738+
self.mk_ty_param(0, keywords::SelfUpper.name())
27392739
}
27402740

27412741
pub fn mk_param_from_def(self, param: &ty::GenericParamDef) -> Kind<'tcx> {

src/librustc/ty/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ impl ty::EarlyBoundRegion {
835835
/// Does this early bound region have a name? Early bound regions normally
836836
/// always have names except when using anonymous lifetimes (`'_`).
837837
pub fn has_name(&self) -> bool {
838-
self.name != keywords::UnderscoreLifetime.name().as_interned_str()
838+
self.name != keywords::UnderscoreLifetime.name()
839839
}
840840
}
841841

@@ -852,7 +852,7 @@ pub enum GenericParamDefKind {
852852

853853
#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
854854
pub struct GenericParamDef {
855-
pub name: InternedString,
855+
pub name: Symbol,
856856
pub def_id: DefId,
857857
pub index: u32,
858858

@@ -3405,7 +3405,7 @@ impl_stable_hash_for!(struct self::SymbolName {
34053405
impl SymbolName {
34063406
pub fn new(name: &str) -> SymbolName {
34073407
SymbolName {
3408-
name: Symbol::intern(name).as_interned_str()
3408+
name: InternedString::intern(name)
34093409
}
34103410
}
34113411

src/librustc/ty/print/pretty.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::mir::interpret::ConstValue;
1010
use syntax::symbol::{keywords, Symbol};
1111

1212
use rustc_target::spec::abi::Abi;
13-
use syntax::symbol::InternedString;
1413

1514
use std::cell::Cell;
1615
use std::fmt::{self, Write as _};
@@ -818,7 +817,7 @@ pub struct FmtPrinterData<'a, 'gcx, 'tcx, F> {
818817
empty_path: bool,
819818
in_value: bool,
820819

821-
used_region_names: FxHashSet<InternedString>,
820+
used_region_names: FxHashSet<Symbol>,
822821
region_index: usize,
823822
binder_depth: usize,
824823

@@ -1142,14 +1141,16 @@ impl<F: fmt::Write> PrettyPrinter<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F>
11421141

11431142
match *region {
11441143
ty::ReEarlyBound(ref data) => {
1145-
data.name != "" && data.name != "'_"
1144+
data.name != keywords::Invalid.name() &&
1145+
data.name != keywords::UnderscoreLifetime.name()
11461146
}
11471147

11481148
ty::ReLateBound(_, br) |
11491149
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
11501150
ty::RePlaceholder(ty::Placeholder { name: br, .. }) => {
11511151
if let ty::BrNamed(_, name) = br {
1152-
if name != "" && name != "'_" {
1152+
if name != keywords::Invalid.name() &&
1153+
name != keywords::UnderscoreLifetime.name() {
11531154
return true;
11541155
}
11551156
}
@@ -1205,7 +1206,7 @@ impl<F: fmt::Write> FmtPrinter<'_, '_, '_, F> {
12051206
// `explain_region()` or `note_and_explain_region()`.
12061207
match *region {
12071208
ty::ReEarlyBound(ref data) => {
1208-
if data.name != "" {
1209+
if data.name != keywords::Invalid.name() {
12091210
p!(write("{}", data.name));
12101211
return Ok(self);
12111212
}
@@ -1214,7 +1215,8 @@ impl<F: fmt::Write> FmtPrinter<'_, '_, '_, F> {
12141215
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
12151216
ty::RePlaceholder(ty::Placeholder { name: br, .. }) => {
12161217
if let ty::BrNamed(_, name) = br {
1217-
if name != "" && name != "'_" {
1218+
if name != keywords::Invalid.name() &&
1219+
name != keywords::UnderscoreLifetime.name() {
12181220
p!(write("{}", name));
12191221
return Ok(self);
12201222
}
@@ -1283,12 +1285,12 @@ impl<F: fmt::Write> FmtPrinter<'_, 'gcx, 'tcx, F> {
12831285
) -> Result<Self, fmt::Error>
12841286
where T: Print<'gcx, 'tcx, Self, Output = Self, Error = fmt::Error> + TypeFoldable<'tcx>
12851287
{
1286-
fn name_by_region_index(index: usize) -> InternedString {
1288+
fn name_by_region_index(index: usize) -> Symbol {
12871289
match index {
12881290
0 => Symbol::intern("'r"),
12891291
1 => Symbol::intern("'s"),
12901292
i => Symbol::intern(&format!("'t{}", i-2)),
1291-
}.as_interned_str()
1293+
}
12921294
}
12931295

12941296
// Replace any anonymous late-bound regions with named
@@ -1351,7 +1353,7 @@ impl<F: fmt::Write> FmtPrinter<'_, 'gcx, 'tcx, F> {
13511353
where T: TypeFoldable<'tcx>
13521354
{
13531355

1354-
struct LateBoundRegionNameCollector<'a>(&'a mut FxHashSet<InternedString>);
1356+
struct LateBoundRegionNameCollector<'a>(&'a mut FxHashSet<Symbol>);
13551357
impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector<'_> {
13561358
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
13571359
match *r {

src/librustc/ty/query/values.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::ty::{self, Ty, TyCtxt, AdtSizedConstraint};
22
use crate::ty::util::NeedsDrop;
33

4-
use syntax::symbol::Symbol;
4+
use syntax::symbol::InternedString;
55

66
pub(super) trait Value<'tcx>: Sized {
77
fn from_cycle_error<'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Self;
@@ -28,7 +28,7 @@ impl<'tcx> Value<'tcx> for Ty<'tcx> {
2828

2929
impl<'tcx> Value<'tcx> for ty::SymbolName {
3030
fn from_cycle_error<'a>(_: TyCtxt<'a, 'tcx, 'tcx>) -> Self {
31-
ty::SymbolName { name: Symbol::intern("<error>").as_interned_str() }
31+
ty::SymbolName { name: InternedString::intern("<error>") }
3232
}
3333
}
3434

0 commit comments

Comments
 (0)