Skip to content

Commit

Permalink
Auto merge of #60815 - nnethercote:use-Symbol-more-2, r=petrochenkov
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed May 20, 2019
2 parents 128b4c8 + c06cdbe commit caef1e8
Show file tree
Hide file tree
Showing 20 changed files with 96 additions and 90 deletions.
4 changes: 2 additions & 2 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,9 @@ impl Generics {
own_counts
}

pub fn get_named(&self, name: &InternedString) -> Option<&GenericParam> {
pub fn get_named(&self, name: InternedString) -> Option<&GenericParam> {
for param in &self.params {
if *name == param.name.ident().as_interned_str() {
if name == param.name.ident().as_interned_str() {
return Some(param);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,20 +194,20 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
let mut sp = cm.def_span(self.hir().span_by_hir_id(node));
if let Some(param) = self.hir()
.get_generics(scope)
.and_then(|generics| generics.get_named(&br.name))
.and_then(|generics| generics.get_named(br.name))
{
sp = param.span;
}
(format!("the lifetime {} as defined on", br.name), sp)
}
ty::ReFree(ty::FreeRegion {
bound_region: ty::BoundRegion::BrNamed(_, ref name),
bound_region: ty::BoundRegion::BrNamed(_, name),
..
}) => {
let mut sp = cm.def_span(self.hir().span_by_hir_id(node));
if let Some(param) = self.hir()
.get_generics(scope)
.and_then(|generics| generics.get_named(&name))
.and_then(|generics| generics.get_named(name))
{
sp = param.span;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
) -> Option<AnonymousArgInfo<'_>> {
let (id, bound_region) = match *anon_region {
ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
ty::ReEarlyBound(ref ebr) => (
ty::ReEarlyBound(ebr) => (
self.tcx().parent(ebr.def_id).unwrap(),
ty::BoundRegion::BrNamed(ebr.def_id, ebr.name),
),
Expand Down
9 changes: 5 additions & 4 deletions src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,8 +828,8 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {

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

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

Ok(path)
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/mir/mono.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::hir::def_id::{DefId, CrateNum, LOCAL_CRATE};
use crate::hir::HirId;
use syntax::symbol::{Symbol, InternedString};
use syntax::symbol::InternedString;
use crate::ty::{Instance, TyCtxt};
use crate::util::nodemap::FxHashMap;
use rustc_data_structures::base_n;
Expand Down Expand Up @@ -280,7 +280,7 @@ impl<'a, 'gcx: 'tcx, 'tcx: 'a> CodegenUnitNameBuilder<'a, 'gcx, 'tcx> {
cgu_name
} else {
let cgu_name = &cgu_name.as_str()[..];
Symbol::intern(&CodegenUnit::mangle_name(cgu_name)).as_interned_str()
InternedString::intern(&CodegenUnit::mangle_name(cgu_name))
}
}

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

Symbol::intern(&cgu_name[..]).as_interned_str()
InternedString::intern(&cgu_name[..])
}
}
5 changes: 3 additions & 2 deletions src/librustc/traits/object_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use crate::ty::{self, Ty, TyCtxt, TypeFoldable, Predicate, ToPredicate};
use crate::ty::subst::{Subst, InternalSubsts};
use std::borrow::Cow;
use std::iter::{self};
use syntax::ast::{self, Name};
use syntax::ast::{self};
use syntax::symbol::InternedString;
use syntax_pos::Span;

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

// `Receiver[Self => U]`
Expand Down
14 changes: 4 additions & 10 deletions src/librustc/traits/structural_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,17 +312,15 @@ impl<'tcx> TypeVisitor<'tcx> for BoundNamesCollector {
}

fn visit_ty(&mut self, t: Ty<'tcx>) -> bool {
use syntax::symbol::Symbol;

match t.sty {
ty::Bound(debruijn, bound_ty) if debruijn == self.binder_index => {
self.types.insert(
bound_ty.var.as_u32(),
match bound_ty.kind {
ty::BoundTyKind::Param(name) => name,
ty::BoundTyKind::Anon => Symbol::intern(
&format!("^{}", bound_ty.var.as_u32())
).as_interned_str(),
ty::BoundTyKind::Anon =>
InternedString::intern(&format!("^{}", bound_ty.var.as_u32()),
),
}
);
}
Expand All @@ -334,8 +332,6 @@ impl<'tcx> TypeVisitor<'tcx> for BoundNamesCollector {
}

fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
use syntax::symbol::Symbol;

match r {
ty::ReLateBound(index, br) if *index == self.binder_index => {
match br {
Expand All @@ -344,9 +340,7 @@ impl<'tcx> TypeVisitor<'tcx> for BoundNamesCollector {
}

ty::BoundRegion::BrAnon(var) => {
self.regions.insert(Symbol::intern(
&format!("'^{}", var)
).as_interned_str());
self.regions.insert(InternedString::intern(&format!("'^{}", var)));
}

_ => (),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3405,7 +3405,7 @@ impl_stable_hash_for!(struct self::SymbolName {
impl SymbolName {
pub fn new(name: &str) -> SymbolName {
SymbolName {
name: Symbol::intern(name).as_interned_str()
name: InternedString::intern(name)
}
}

Expand Down
12 changes: 5 additions & 7 deletions src/librustc/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ use crate::middle::region;
use crate::ty::{self, DefIdTree, ParamConst, Ty, TyCtxt, TypeFoldable};
use crate::ty::subst::{Kind, Subst, UnpackedKind};
use crate::mir::interpret::ConstValue;
use syntax::symbol::{keywords, Symbol};

use rustc_target::spec::abi::Abi;
use syntax::symbol::InternedString;
use syntax::symbol::{keywords, InternedString};

use std::cell::Cell;
use std::fmt::{self, Write as _};
Expand Down Expand Up @@ -1285,10 +1283,10 @@ impl<F: fmt::Write> FmtPrinter<'_, 'gcx, 'tcx, F> {
{
fn name_by_region_index(index: usize) -> InternedString {
match index {
0 => Symbol::intern("'r"),
1 => Symbol::intern("'s"),
i => Symbol::intern(&format!("'t{}", i-2)),
}.as_interned_str()
0 => InternedString::intern("'r"),
1 => InternedString::intern("'s"),
i => InternedString::intern(&format!("'t{}", i-2)),
}
}

// Replace any anonymous late-bound regions with named
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/query/values.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ty::{self, Ty, TyCtxt, AdtSizedConstraint};
use crate::ty::util::NeedsDrop;

use syntax::symbol::Symbol;
use syntax::symbol::InternedString;

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ impl<'a, 'gcx, 'tcx> ParamTy {
// FIXME(#50125): Ignoring `Self` with `index != 0` might lead to weird behavior elsewhere,
// but this should only be possible when using `-Z continue-parse-after-error` like
// `compile-fail/issue-36638.rs`.
self.name == keywords::SelfUpper.name().as_str() && self.index == 0
self.name.as_symbol() == keywords::SelfUpper.name() && self.index == 0
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_llvm/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use rustc::ty::layout::{self, LayoutOf, HasTyCtxt, Primitive};
use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
use rustc::hir;
use syntax::ast::{self, FloatTy};
use syntax::symbol::Symbol;
use syntax::symbol::LocalInternedString;

use rustc_codegen_ssa::traits::*;

Expand Down Expand Up @@ -213,7 +213,7 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
}
"type_name" => {
let tp_ty = substs.type_at(0);
let ty_name = Symbol::intern(&tp_ty.to_string()).as_str();
let ty_name = LocalInternedString::intern(&tp_ty.to_string());
self.const_str_slice(ty_name)
}
"type_id" => {
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_codegen_ssa/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::traits::*;

use std::borrow::Cow;

use syntax::symbol::Symbol;
use syntax::symbol::LocalInternedString;
use syntax_pos::Pos;

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

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

Expand All @@ -423,7 +423,7 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
}
_ => {
let str = msg.description();
let msg_str = Symbol::intern(str).as_str();
let msg_str = LocalInternedString::intern(str);
let msg_file_line_col = bx.static_panic_msg(
Some(msg_str),
filename,
Expand Down Expand Up @@ -535,15 +535,15 @@ impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let layout = bx.layout_of(ty);
if layout.abi.is_uninhabited() {
let loc = bx.sess().source_map().lookup_char_pos(span.lo());
let filename = Symbol::intern(&loc.file.name.to_string()).as_str();
let filename = LocalInternedString::intern(&loc.file.name.to_string());
let line = bx.const_u32(loc.line as u32);
let col = bx.const_u32(loc.col.to_usize() as u32 + 1);

let str = format!(
"Attempted to instantiate uninhabited type {}",
ty
);
let msg_str = Symbol::intern(&str).as_str();
let msg_str = LocalInternedString::intern(&str);
let msg_file_line_col = bx.static_panic_msg(
Some(msg_str),
filename,
Expand Down
12 changes: 6 additions & 6 deletions src/librustc_codegen_utils/symbol_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_mir::monomorphize::item::{InstantiationMode, MonoItem, MonoItemExt};
use rustc_mir::monomorphize::Instance;

use syntax_pos::symbol::{Symbol, InternedString};
use syntax_pos::symbol::InternedString;

use log::debug;

Expand Down Expand Up @@ -238,13 +238,13 @@ fn compute_symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) ->
if def_id.is_local() {
if tcx.plugin_registrar_fn(LOCAL_CRATE) == Some(def_id) {
let disambiguator = tcx.sess.local_crate_disambiguator();
return Symbol::intern(&tcx.sess.generate_plugin_registrar_symbol(disambiguator))
.as_interned_str();
return
InternedString::intern(&tcx.sess.generate_plugin_registrar_symbol(disambiguator));
}
if tcx.proc_macro_decls_static(LOCAL_CRATE) == Some(def_id) {
let disambiguator = tcx.sess.local_crate_disambiguator();
return Symbol::intern(&tcx.sess.generate_proc_macro_decls_symbol(disambiguator))
.as_interned_str();
return
InternedString::intern(&tcx.sess.generate_proc_macro_decls_symbol(disambiguator));
}
}

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

Symbol::intern(&printer.path.finish(hash)).as_interned_str()
InternedString::intern(&printer.path.finish(hash))
}

// Follow C++ namespace-mangling style, see
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use rustc::ty::subst::{SubstsRef, UnpackedKind};
use rustc::ty::{self, RegionKind, RegionVid, Ty, TyCtxt};
use rustc::ty::print::RegionHighlightMode;
use rustc_errors::DiagnosticBuilder;
use syntax::ast::Name;
use syntax::symbol::keywords;
use syntax_pos::Span;
use syntax_pos::symbol::InternedString;
Expand Down Expand Up @@ -60,8 +59,8 @@ impl RegionName {
}

#[allow(dead_code)]
crate fn name(&self) -> &InternedString {
&self.name
crate fn name(&self) -> InternedString {
self.name
}

crate fn highlight_region_name(
Expand Down Expand Up @@ -206,7 +205,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
match error_region {
ty::ReEarlyBound(ebr) => {
if ebr.has_name() {
let span = self.get_named_span(tcx, error_region, &ebr.name);
let span = self.get_named_span(tcx, error_region, ebr.name);
Some(RegionName {
name: ebr.name,
source: RegionNameSource::NamedEarlyBoundRegion(span)
Expand All @@ -223,7 +222,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {

ty::ReFree(free_region) => match free_region.bound_region {
ty::BoundRegion::BrNamed(_, name) => {
let span = self.get_named_span(tcx, error_region, &name);
let span = self.get_named_span(tcx, error_region, name);
Some(RegionName {
name,
source: RegionNameSource::NamedFreeRegion(span),
Expand Down Expand Up @@ -306,7 +305,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
&self,
tcx: TyCtxt<'_, '_, 'tcx>,
error_region: &RegionKind,
name: &InternedString,
name: InternedString,
) -> Span {
let scope = error_region.free_region_binding_scope(tcx);
let node = tcx.hir().as_local_hir_id(scope).unwrap_or(hir::DUMMY_HIR_ID);
Expand Down Expand Up @@ -791,6 +790,6 @@ impl<'tcx> RegionInferenceContext<'tcx> {
let c = *counter;
*counter += 1;

Name::intern(&format!("'{:?}", c)).as_interned_str()
InternedString::intern(&format!("'{:?}", c))
}
}
4 changes: 2 additions & 2 deletions src/librustc_mir/monomorphize/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use syntax::attr::InlineAttr;
use std::fmt::{self, Write};
use std::iter;
use rustc::mir::mono::Linkage;
use syntax_pos::symbol::Symbol;
use syntax_pos::symbol::InternedString;
use syntax::source_map::Span;
pub use rustc::mir::mono::MonoItem;

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

0 comments on commit caef1e8

Please sign in to comment.