Skip to content

Commit caef1e8

Browse files
committed
Auto merge of #60815 - nnethercote:use-Symbol-more-2, r=petrochenkov
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 128b4c8 + c06cdbe commit caef1e8

File tree

20 files changed

+96
-90
lines changed

20 files changed

+96
-90
lines changed

src/librustc/hir/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -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: InternedString) -> Option<&GenericParam> {
613613
for param in &self.params {
614-
if *name == param.name.ident().as_interned_str() {
614+
if name == param.name.ident().as_interned_str() {
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/error_reporting/nice_region_error/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
4343
) -> Option<AnonymousArgInfo<'_>> {
4444
let (id, bound_region) = match *anon_region {
4545
ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
46-
ty::ReEarlyBound(ref ebr) => (
46+
ty::ReEarlyBound(ebr) => (
4747
self.tcx().parent(ebr.def_id).unwrap(),
4848
ty::BoundRegion::BrNamed(ebr.def_id, ebr.name),
4949
),

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use crate::ty::{self, Ty, TyCtxt, TypeFoldable, Predicate, ToPredicate};
1818
use crate::ty::subst::{Subst, InternalSubsts};
1919
use std::borrow::Cow;
2020
use std::iter::{self};
21-
use syntax::ast::{self, Name};
21+
use syntax::ast::{self};
22+
use syntax::symbol::InternedString;
2223
use syntax_pos::Span;
2324

2425
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
@@ -539,7 +540,7 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
539540
// are implemented
540541
let unsized_self_ty: Ty<'tcx> = self.mk_ty_param(
541542
::std::u32::MAX,
542-
Name::intern("RustaceansAreAwesome").as_interned_str(),
543+
InternedString::intern("RustaceansAreAwesome"),
543544
);
544545

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

src/librustc/traits/structural_impls.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -312,17 +312,15 @@ 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(
320318
bound_ty.var.as_u32(),
321319
match bound_ty.kind {
322320
ty::BoundTyKind::Param(name) => name,
323-
ty::BoundTyKind::Anon => Symbol::intern(
324-
&format!("^{}", bound_ty.var.as_u32())
325-
).as_interned_str(),
321+
ty::BoundTyKind::Anon =>
322+
InternedString::intern(&format!("^{}", bound_ty.var.as_u32()),
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 {
@@ -344,9 +340,7 @@ impl<'tcx> TypeVisitor<'tcx> for BoundNamesCollector {
344340
}
345341

346342
ty::BoundRegion::BrAnon(var) => {
347-
self.regions.insert(Symbol::intern(
348-
&format!("'^{}", var)
349-
).as_interned_str());
343+
self.regions.insert(InternedString::intern(&format!("'^{}", var)));
350344
}
351345

352346
_ => (),

src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -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

+5-7
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ use crate::middle::region;
77
use crate::ty::{self, DefIdTree, ParamConst, Ty, TyCtxt, TypeFoldable};
88
use crate::ty::subst::{Kind, Subst, UnpackedKind};
99
use crate::mir::interpret::ConstValue;
10-
use syntax::symbol::{keywords, Symbol};
11-
1210
use rustc_target::spec::abi::Abi;
13-
use syntax::symbol::InternedString;
11+
use syntax::symbol::{keywords, InternedString};
1412

1513
use std::cell::Cell;
1614
use std::fmt::{self, Write as _};
@@ -1285,10 +1283,10 @@ impl<F: fmt::Write> FmtPrinter<'_, 'gcx, 'tcx, F> {
12851283
{
12861284
fn name_by_region_index(index: usize) -> InternedString {
12871285
match index {
1288-
0 => Symbol::intern("'r"),
1289-
1 => Symbol::intern("'s"),
1290-
i => Symbol::intern(&format!("'t{}", i-2)),
1291-
}.as_interned_str()
1286+
0 => InternedString::intern("'r"),
1287+
1 => InternedString::intern("'s"),
1288+
i => InternedString::intern(&format!("'t{}", i-2)),
1289+
}
12921290
}
12931291

12941292
// Replace any anonymous late-bound regions with named

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

src/librustc/ty/sty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ impl<'a, 'gcx, 'tcx> ParamTy {
11361136
// FIXME(#50125): Ignoring `Self` with `index != 0` might lead to weird behavior elsewhere,
11371137
// but this should only be possible when using `-Z continue-parse-after-error` like
11381138
// `compile-fail/issue-36638.rs`.
1139-
self.name == keywords::SelfUpper.name().as_str() && self.index == 0
1139+
self.name.as_symbol() == keywords::SelfUpper.name() && self.index == 0
11401140
}
11411141
}
11421142

src/librustc_codegen_llvm/intrinsic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc::ty::layout::{self, LayoutOf, HasTyCtxt, Primitive};
2020
use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
2121
use rustc::hir;
2222
use syntax::ast::{self, FloatTy};
23-
use syntax::symbol::Symbol;
23+
use syntax::symbol::LocalInternedString;
2424

2525
use rustc_codegen_ssa::traits::*;
2626

@@ -213,7 +213,7 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
213213
}
214214
"type_name" => {
215215
let tp_ty = substs.type_at(0);
216-
let ty_name = Symbol::intern(&tp_ty.to_string()).as_str();
216+
let ty_name = LocalInternedString::intern(&tp_ty.to_string());
217217
self.const_str_slice(ty_name)
218218
}
219219
"type_id" => {

src/librustc_codegen_ssa/mir/block.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::traits::*;
1515

1616
use std::borrow::Cow;
1717

18-
use syntax::symbol::Symbol;
18+
use syntax::symbol::LocalInternedString;
1919
use syntax_pos::Pos;
2020

2121
use super::{FunctionCx, LocalRef};
@@ -401,7 +401,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
401401

402402
// Get the location information.
403403
let loc = bx.sess().source_map().lookup_char_pos(span.lo());
404-
let filename = Symbol::intern(&loc.file.name.to_string()).as_str();
404+
let filename = LocalInternedString::intern(&loc.file.name.to_string());
405405
let line = bx.const_u32(loc.line as u32);
406406
let col = bx.const_u32(loc.col.to_usize() as u32 + 1);
407407

@@ -423,7 +423,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
423423
}
424424
_ => {
425425
let str = msg.description();
426-
let msg_str = Symbol::intern(str).as_str();
426+
let msg_str = LocalInternedString::intern(str);
427427
let msg_file_line_col = bx.static_panic_msg(
428428
Some(msg_str),
429429
filename,
@@ -535,15 +535,15 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
535535
let layout = bx.layout_of(ty);
536536
if layout.abi.is_uninhabited() {
537537
let loc = bx.sess().source_map().lookup_char_pos(span.lo());
538-
let filename = Symbol::intern(&loc.file.name.to_string()).as_str();
538+
let filename = LocalInternedString::intern(&loc.file.name.to_string());
539539
let line = bx.const_u32(loc.line as u32);
540540
let col = bx.const_u32(loc.col.to_usize() as u32 + 1);
541541

542542
let str = format!(
543543
"Attempted to instantiate uninhabited type {}",
544544
ty
545545
);
546-
let msg_str = Symbol::intern(&str).as_str();
546+
let msg_str = LocalInternedString::intern(&str);
547547
let msg_file_line_col = bx.static_panic_msg(
548548
Some(msg_str),
549549
filename,

src/librustc_codegen_utils/symbol_names.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
101101
use rustc_mir::monomorphize::item::{InstantiationMode, MonoItem, MonoItemExt};
102102
use rustc_mir::monomorphize::Instance;
103103

104-
use syntax_pos::symbol::{Symbol, InternedString};
104+
use syntax_pos::symbol::InternedString;
105105

106106
use log::debug;
107107

@@ -238,13 +238,13 @@ fn compute_symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) ->
238238
if def_id.is_local() {
239239
if tcx.plugin_registrar_fn(LOCAL_CRATE) == Some(def_id) {
240240
let disambiguator = tcx.sess.local_crate_disambiguator();
241-
return Symbol::intern(&tcx.sess.generate_plugin_registrar_symbol(disambiguator))
242-
.as_interned_str();
241+
return
242+
InternedString::intern(&tcx.sess.generate_plugin_registrar_symbol(disambiguator));
243243
}
244244
if tcx.proc_macro_decls_static(LOCAL_CRATE) == Some(def_id) {
245245
let disambiguator = tcx.sess.local_crate_disambiguator();
246-
return Symbol::intern(&tcx.sess.generate_proc_macro_decls_symbol(disambiguator))
247-
.as_interned_str();
246+
return
247+
InternedString::intern(&tcx.sess.generate_proc_macro_decls_symbol(disambiguator));
248248
}
249249
}
250250

@@ -322,7 +322,7 @@ fn compute_symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) ->
322322
let _ = printer.write_str("{{vtable-shim}}");
323323
}
324324

325-
Symbol::intern(&printer.path.finish(hash)).as_interned_str()
325+
InternedString::intern(&printer.path.finish(hash))
326326
}
327327

328328
// Follow C++ namespace-mangling style, see

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use rustc::ty::subst::{SubstsRef, UnpackedKind};
1212
use rustc::ty::{self, RegionKind, RegionVid, Ty, TyCtxt};
1313
use rustc::ty::print::RegionHighlightMode;
1414
use rustc_errors::DiagnosticBuilder;
15-
use syntax::ast::Name;
1615
use syntax::symbol::keywords;
1716
use syntax_pos::Span;
1817
use syntax_pos::symbol::InternedString;
@@ -60,8 +59,8 @@ impl RegionName {
6059
}
6160

6261
#[allow(dead_code)]
63-
crate fn name(&self) -> &InternedString {
64-
&self.name
62+
crate fn name(&self) -> InternedString {
63+
self.name
6564
}
6665

6766
crate fn highlight_region_name(
@@ -206,7 +205,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
206205
match error_region {
207206
ty::ReEarlyBound(ebr) => {
208207
if ebr.has_name() {
209-
let span = self.get_named_span(tcx, error_region, &ebr.name);
208+
let span = self.get_named_span(tcx, error_region, ebr.name);
210209
Some(RegionName {
211210
name: ebr.name,
212211
source: RegionNameSource::NamedEarlyBoundRegion(span)
@@ -223,7 +222,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
223222

224223
ty::ReFree(free_region) => match free_region.bound_region {
225224
ty::BoundRegion::BrNamed(_, name) => {
226-
let span = self.get_named_span(tcx, error_region, &name);
225+
let span = self.get_named_span(tcx, error_region, name);
227226
Some(RegionName {
228227
name,
229228
source: RegionNameSource::NamedFreeRegion(span),
@@ -306,7 +305,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
306305
&self,
307306
tcx: TyCtxt<'_, '_, 'tcx>,
308307
error_region: &RegionKind,
309-
name: &InternedString,
308+
name: InternedString,
310309
) -> Span {
311310
let scope = error_region.free_region_binding_scope(tcx);
312311
let node = tcx.hir().as_local_hir_id(scope).unwrap_or(hir::DUMMY_HIR_ID);
@@ -791,6 +790,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
791790
let c = *counter;
792791
*counter += 1;
793792

794-
Name::intern(&format!("'{:?}", c)).as_interned_str()
793+
InternedString::intern(&format!("'{:?}", c))
795794
}
796795
}

src/librustc_mir/monomorphize/item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use syntax::attr::InlineAttr;
1010
use std::fmt::{self, Write};
1111
use std::iter;
1212
use rustc::mir::mono::Linkage;
13-
use syntax_pos::symbol::Symbol;
13+
use syntax_pos::symbol::InternedString;
1414
use syntax::source_map::Span;
1515
pub use rustc::mir::mono::MonoItem;
1616

@@ -61,7 +61,7 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug {
6161
MonoItem::GlobalAsm(hir_id) => {
6262
let def_id = tcx.hir().local_def_id_from_hir_id(hir_id);
6363
ty::SymbolName {
64-
name: Symbol::intern(&format!("global_asm_{:?}", def_id)).as_interned_str()
64+
name: InternedString::intern(&format!("global_asm_{:?}", def_id))
6565
}
6666
}
6767
}

0 commit comments

Comments
 (0)