Skip to content

Commit

Permalink
Auto merge of #60815 - nnethercote:use-Symbol-more-2, r=<try>
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 14, 2019
2 parents 80e7cde + 67b969e commit 8e849d8
Show file tree
Hide file tree
Showing 31 changed files with 127 additions and 110 deletions.
2 changes: 1 addition & 1 deletion src/librustc/hir/map/definitions.rs
Expand Up @@ -593,7 +593,7 @@ impl DefPathData {
ImplTrait => "{{opaque}}",
};

Symbol::intern(s).as_interned_str()
InternedString::intern(s)
}

pub fn to_string(&self) -> String {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/hir/mod.rs
Expand Up @@ -16,7 +16,7 @@ use crate::util::nodemap::{NodeMap, FxHashSet};
use crate::mir::mono::Linkage;

use errors::FatalError;
use syntax_pos::{Span, DUMMY_SP, symbol::InternedString};
use syntax_pos::{Span, DUMMY_SP};
use syntax::source_map::Spanned;
use rustc_target::spec::abi::Abi;
use syntax::ast::{self, CrateSugar, Ident, Name, NodeId, AsmDialect};
Expand Down 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: Symbol) -> Option<&GenericParam> {
for param in &self.params {
if *name == param.name.ident().as_interned_str() {
if name == param.name.ident().name {
return Some(param);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/infer/error_reporting/mod.rs
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
4 changes: 2 additions & 2 deletions src/librustc/infer/mod.rs
Expand Up @@ -32,7 +32,7 @@ use std::cell::{Cell, Ref, RefCell, RefMut};
use std::collections::BTreeMap;
use std::fmt;
use syntax::ast;
use syntax_pos::symbol::InternedString;
use syntax_pos::symbol::Symbol;
use syntax_pos::Span;

use self::combine::CombineFields;
Expand Down Expand Up @@ -383,7 +383,7 @@ pub enum RegionVariableOrigin {
Coercion(Span),

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

/// Region variables created for bound regions
/// in a function or method that is called
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/infer/type_variable.rs
@@ -1,4 +1,4 @@
use syntax::symbol::InternedString;
use syntax::symbol::Symbol;
use syntax_pos::Span;
use crate::ty::{self, Ty, TyVid};

Expand Down Expand Up @@ -43,7 +43,7 @@ pub enum TypeVariableOrigin {
MiscVariable(Span),
NormalizeProjectionType(Span),
TypeInference(Span),
TypeParameterDefinition(Span, InternedString),
TypeParameterDefinition(Span, Symbol),

/// one of the upvars or closure kind parameters in a `ClosureSubsts`
/// (before it has been determined)
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/infer/unify_key.rs
Expand Up @@ -3,7 +3,7 @@ use crate::mir::interpret::ConstValue;
use rustc_data_structures::unify::{NoError, EqUnifyValue, UnifyKey, UnifyValue, UnificationTable};
use rustc_data_structures::unify::InPlace;
use syntax_pos::{Span, DUMMY_SP};
use syntax::symbol::InternedString;
use syntax::symbol::Symbol;

use std::cmp;
use std::marker::PhantomData;
Expand Down Expand Up @@ -84,7 +84,7 @@ impl ToType for FloatVarValue {
pub enum ConstVariableOrigin {
MiscVariable(Span),
ConstInference(Span),
ConstParameterDefinition(Span, InternedString),
ConstParameterDefinition(Span, Symbol),
SubstitutionPlaceholder(Span),
}

Expand Down
9 changes: 5 additions & 4 deletions src/librustc/lint/context.rs
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
@@ -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[..])
}
}
2 changes: 1 addition & 1 deletion src/librustc/traits/object_safety.rs
Expand Up @@ -539,7 +539,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(),
Name::intern("RustaceansAreAwesome"),
);

// `Receiver[Self => U]`
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/on_unimplemented.rs
Expand Up @@ -250,7 +250,7 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedFormatString {
Position::ArgumentNamed(s) if s == "from_desugaring" => (),
// So is `{A}` if A is a type parameter
Position::ArgumentNamed(s) => match generics.params.iter().find(|param|
param.name == s
param.name.as_str() == s
) {
Some(_) => (),
None => {
Expand Down
14 changes: 5 additions & 9 deletions src/librustc/traits/structural_impls.rs
Expand Up @@ -4,7 +4,7 @@ use crate::traits;
use crate::traits::project::Normalized;
use crate::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
use crate::ty::{self, Lift, Ty, TyCtxt};
use syntax::symbol::InternedString;
use syntax::symbol::Symbol;

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

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

binder_index: ty::DebruijnIndex,
}
Expand Down Expand Up @@ -312,8 +312,6 @@ 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(
Expand All @@ -322,7 +320,7 @@ impl<'tcx> TypeVisitor<'tcx> for BoundNamesCollector {
ty::BoundTyKind::Param(name) => name,
ty::BoundTyKind::Anon => Symbol::intern(
&format!("^{}", bound_ty.var.as_u32())
).as_interned_str(),
),
}
);
}
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 @@ -346,7 +342,7 @@ impl<'tcx> TypeVisitor<'tcx> for BoundNamesCollector {
ty::BoundRegion::BrAnon(var) => {
self.regions.insert(Symbol::intern(
&format!("'^{}", var)
).as_interned_str());
));
}

_ => (),
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/ty/context.rs
Expand Up @@ -74,7 +74,7 @@ use syntax::ast;
use syntax::attr;
use syntax::source_map::MultiSpan;
use syntax::feature_gate;
use syntax::symbol::{Symbol, keywords, InternedString, sym};
use syntax::symbol::{Symbol, keywords, sym};
use syntax_pos::Span;

use crate::hir;
Expand Down Expand Up @@ -2716,15 +2716,15 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}

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

#[inline]
pub fn mk_const_param(
self,
index: u32,
name: InternedString,
name: Symbol,
ty: Ty<'tcx>
) -> &'tcx Const<'tcx> {
self.mk_const(ty::Const {
Expand All @@ -2735,7 +2735,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {

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

pub fn mk_param_from_def(self, param: &ty::GenericParamDef) -> Kind<'tcx> {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/ty/mod.rs
Expand Up @@ -835,7 +835,7 @@ impl ty::EarlyBoundRegion {
/// Does this early bound region have a name? Early bound regions normally
/// always have names except when using anonymous lifetimes (`'_`).
pub fn has_name(&self) -> bool {
self.name != keywords::UnderscoreLifetime.name().as_interned_str()
self.name != keywords::UnderscoreLifetime.name()
}
}

Expand All @@ -852,7 +852,7 @@ pub enum GenericParamDefKind {

#[derive(Clone, RustcEncodable, RustcDecodable, HashStable)]
pub struct GenericParamDef {
pub name: InternedString,
pub name: Symbol,
pub def_id: DefId,
pub index: u32,

Expand Down 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
20 changes: 11 additions & 9 deletions src/librustc/ty/print/pretty.rs
Expand Up @@ -10,7 +10,6 @@ use crate::mir::interpret::ConstValue;
use syntax::symbol::{keywords, Symbol};

use rustc_target::spec::abi::Abi;
use syntax::symbol::InternedString;

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

used_region_names: FxHashSet<InternedString>,
used_region_names: FxHashSet<Symbol>,
region_index: usize,
binder_depth: usize,

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

match *region {
ty::ReEarlyBound(ref data) => {
data.name != "" && data.name != "'_"
data.name != keywords::Invalid.name() &&
data.name != keywords::UnderscoreLifetime.name()
}

ty::ReLateBound(_, br) |
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
ty::RePlaceholder(ty::Placeholder { name: br, .. }) => {
if let ty::BrNamed(_, name) = br {
if name != "" && name != "'_" {
if name != keywords::Invalid.name() &&
name != keywords::UnderscoreLifetime.name() {
return true;
}
}
Expand Down Expand Up @@ -1205,7 +1206,7 @@ impl<F: fmt::Write> FmtPrinter<'_, '_, '_, F> {
// `explain_region()` or `note_and_explain_region()`.
match *region {
ty::ReEarlyBound(ref data) => {
if data.name != "" {
if data.name != keywords::Invalid.name() {
p!(write("{}", data.name));
return Ok(self);
}
Expand All @@ -1214,7 +1215,8 @@ impl<F: fmt::Write> FmtPrinter<'_, '_, '_, F> {
ty::ReFree(ty::FreeRegion { bound_region: br, .. }) |
ty::RePlaceholder(ty::Placeholder { name: br, .. }) => {
if let ty::BrNamed(_, name) = br {
if name != "" && name != "'_" {
if name != keywords::Invalid.name() &&
name != keywords::UnderscoreLifetime.name() {
p!(write("{}", name));
return Ok(self);
}
Expand Down Expand Up @@ -1283,12 +1285,12 @@ impl<F: fmt::Write> FmtPrinter<'_, 'gcx, 'tcx, F> {
) -> Result<Self, fmt::Error>
where T: Print<'gcx, 'tcx, Self, Output = Self, Error = fmt::Error> + TypeFoldable<'tcx>
{
fn name_by_region_index(index: usize) -> InternedString {
fn name_by_region_index(index: usize) -> Symbol {
match index {
0 => Symbol::intern("'r"),
1 => Symbol::intern("'s"),
i => Symbol::intern(&format!("'t{}", i-2)),
}.as_interned_str()
}
}

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

struct LateBoundRegionNameCollector<'a>(&'a mut FxHashSet<InternedString>);
struct LateBoundRegionNameCollector<'a>(&'a mut FxHashSet<Symbol>);
impl<'tcx> ty::fold::TypeVisitor<'tcx> for LateBoundRegionNameCollector<'_> {
fn visit_region(&mut self, r: ty::Region<'tcx>) -> bool {
match *r {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/query/values.rs
@@ -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

0 comments on commit 8e849d8

Please sign in to comment.