From b557567c35cbb0c84c536e62df8dbfd1694314ef Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 15 May 2019 06:32:44 +1000 Subject: [PATCH] Remove impls for `InternedString`/string equality. `Symbol` received the same treatment in #60630. Also, we can derive `PartialEq` for `InternedString`. --- src/librustc/middle/intrinsicck.rs | 4 +-- src/librustc/traits/on_unimplemented.rs | 10 +++--- src/librustc/ty/mod.rs | 6 ++-- src/librustc/ty/print/pretty.rs | 11 ++++--- src/librustc_codegen_utils/symbol_names.rs | 4 +-- src/librustc_lint/builtin.rs | 2 +- src/librustc_metadata/cstore_impl.rs | 2 +- src/librustc_metadata/decoder.rs | 13 ++++---- src/librustc_mir/shim.rs | 6 ++-- src/librustc_mir/transform/rustc_peek.rs | 2 +- src/librustdoc/clean/mod.rs | 2 +- src/libsyntax_pos/symbol.rs | 38 +--------------------- 12 files changed, 34 insertions(+), 66 deletions(-) diff --git a/src/librustc/middle/intrinsicck.rs b/src/librustc/middle/intrinsicck.rs index 5c28d480571e9..1f25dba2915c4 100644 --- a/src/librustc/middle/intrinsicck.rs +++ b/src/librustc/middle/intrinsicck.rs @@ -6,7 +6,7 @@ use crate::ty::query::Providers; use rustc_target::spec::abi::Abi::RustIntrinsic; use rustc_data_structures::indexed_vec::Idx; -use syntax_pos::Span; +use syntax_pos::{Span, sym}; use crate::hir::intravisit::{self, Visitor, NestedVisitorMap}; use crate::hir; @@ -69,7 +69,7 @@ fn unpack_option_like<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, impl<'a, 'tcx> ExprVisitor<'a, 'tcx> { fn def_id_is_transmute(&self, def_id: DefId) -> bool { self.tcx.fn_sig(def_id).abi() == RustIntrinsic && - self.tcx.item_name(def_id) == "transmute" + self.tcx.item_name(def_id) == sym::transmute } fn check_transmute(&self, span: Span, from: Ty<'tcx>, to: Ty<'tcx>) { diff --git a/src/librustc/traits/on_unimplemented.rs b/src/librustc/traits/on_unimplemented.rs index 7ba7429f465a6..f9ceeb5bfc01b 100644 --- a/src/librustc/traits/on_unimplemented.rs +++ b/src/librustc/traits/on_unimplemented.rs @@ -243,15 +243,15 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedFormatString { // `{Self}` is allowed Position::ArgumentNamed(s) if s == "Self" => (), // `{ThisTraitsName}` is allowed - Position::ArgumentNamed(s) if s == name => (), + Position::ArgumentNamed(s) if s == name.as_str() => (), // `{from_method}` is allowed Position::ArgumentNamed(s) if s == "from_method" => (), // `{from_desugaring}` is allowed 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 - ) { + Position::ArgumentNamed(s) => match generics.params.iter().find(|param| { + param.name.as_str() == s + }) { Some(_) => (), None => { span_err!(tcx.sess, span, E0230, @@ -301,7 +301,7 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedFormatString { Piece::NextArgument(a) => match a.position { Position::ArgumentNamed(s) => match generic_map.get(s) { Some(val) => val, - None if s == name => { + None if s == name.as_str() => { &trait_str } None => { diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index e1c432d5b6da1..6629a190c1571 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -2981,9 +2981,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { } } - pub fn item_name(self, id: DefId) -> InternedString { + pub fn item_name(self, id: DefId) -> Symbol { if id.index == CRATE_DEF_INDEX { - self.original_crate_name(id.krate).as_interned_str() + self.original_crate_name(id.krate) } else { let def_key = self.def_key(id); match def_key.disambiguated_data.data { @@ -2995,7 +2995,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> { }), _ => def_key.disambiguated_data.data.get_opt_name().unwrap_or_else(|| { bug!("item_name: no name for {:?}", self.def_path(id)); - }), + }).as_symbol(), } } } diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs index 91b708d7dbe10..c18852a832fce 100644 --- a/src/librustc/ty/print/pretty.rs +++ b/src/librustc/ty/print/pretty.rs @@ -1140,14 +1140,16 @@ impl PrettyPrinter<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> match *region { ty::ReEarlyBound(ref data) => { - data.name != "" && data.name != "'_" + data.name.as_symbol() != keywords::Invalid.name() && + data.name.as_symbol() != 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.as_symbol() != keywords::Invalid.name() && + name.as_symbol() != keywords::UnderscoreLifetime.name() { return true; } } @@ -1203,7 +1205,7 @@ impl FmtPrinter<'_, '_, '_, F> { // `explain_region()` or `note_and_explain_region()`. match *region { ty::ReEarlyBound(ref data) => { - if data.name != "" { + if data.name.as_symbol() != keywords::Invalid.name() { p!(write("{}", data.name)); return Ok(self); } @@ -1212,7 +1214,8 @@ impl 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.as_symbol() != keywords::Invalid.name() && + name.as_symbol() != keywords::UnderscoreLifetime.name() { p!(write("{}", name)); return Ok(self); } diff --git a/src/librustc_codegen_utils/symbol_names.rs b/src/librustc_codegen_utils/symbol_names.rs index 6915687ceba93..1a8647ed197b5 100644 --- a/src/librustc_codegen_utils/symbol_names.rs +++ b/src/librustc_codegen_utils/symbol_names.rs @@ -264,7 +264,7 @@ fn compute_symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) -> return name.as_interned_str(); } // Don't mangle foreign items. - return tcx.item_name(def_id); + return tcx.item_name(def_id).as_interned_str(); } if let Some(name) = &attrs.export_name { @@ -274,7 +274,7 @@ fn compute_symbol_name(tcx: TyCtxt<'_, 'tcx, 'tcx>, instance: Instance<'tcx>) -> if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) { // Don't mangle - return tcx.item_name(def_id); + return tcx.item_name(def_id).as_interned_str(); } // We want to compute the "type" of this item. Unfortunately, some diff --git a/src/librustc_lint/builtin.rs b/src/librustc_lint/builtin.rs index af4f1b88b0fb0..d17a92efb066b 100644 --- a/src/librustc_lint/builtin.rs +++ b/src/librustc_lint/builtin.rs @@ -929,7 +929,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MutableTransmutes { fn def_id_is_transmute(cx: &LateContext<'_, '_>, def_id: DefId) -> bool { cx.tcx.fn_sig(def_id).abi() == RustIntrinsic && - cx.tcx.item_name(def_id) == "transmute" + cx.tcx.item_name(def_id) == sym::transmute } } } diff --git a/src/librustc_metadata/cstore_impl.rs b/src/librustc_metadata/cstore_impl.rs index 087256a971056..f9b4784bb220c 100644 --- a/src/librustc_metadata/cstore_impl.rs +++ b/src/librustc_metadata/cstore_impl.rs @@ -432,7 +432,7 @@ impl cstore::CStore { let data = self.get_crate_data(id.krate); if let Some(ref proc_macros) = data.proc_macros { return LoadedMacro::ProcMacro(proc_macros[id.index.to_proc_macro_index()].1.clone()); - } else if data.name == sym::proc_macro && data.item_name(id.index) == "quote" { + } else if data.name == sym::proc_macro && data.item_name(id.index) == sym::quote { use syntax::ext::base::SyntaxExtension; use syntax_ext::proc_macro_impl::BangProcMacro; diff --git a/src/librustc_metadata/decoder.rs b/src/librustc_metadata/decoder.rs index d882fe6f27ecc..d2ba82b5a1c9f 100644 --- a/src/librustc_metadata/decoder.rs +++ b/src/librustc_metadata/decoder.rs @@ -29,7 +29,7 @@ use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque}; use syntax::attr; use syntax::ast::{self, Ident}; use syntax::source_map; -use syntax::symbol::{InternedString, sym}; +use syntax::symbol::{Symbol, sym}; use syntax::ext::base::{MacroKind, SyntaxExtension}; use syntax::ext::hygiene::Mark; use syntax_pos::{self, Span, BytePos, Pos, DUMMY_SP, NO_EXPANSION}; @@ -497,12 +497,13 @@ impl<'a, 'tcx> CrateMetadata { } } - pub fn item_name(&self, item_index: DefIndex) -> InternedString { + pub fn item_name(&self, item_index: DefIndex) -> Symbol { self.def_key(item_index) .disambiguated_data .data .get_opt_name() .expect("no name in item_name") + .as_symbol() } pub fn def_kind(&self, index: DefIndex) -> Option { @@ -568,7 +569,7 @@ impl<'a, 'tcx> CrateMetadata { ty::VariantDef::new( tcx, - Ident::from_interned_str(self.item_name(index)), + Ident::with_empty_ctxt(self.item_name(index)), variant_did, ctor_did, data.discr, @@ -576,7 +577,7 @@ impl<'a, 'tcx> CrateMetadata { let f = self.entry(index); ty::FieldDef { did: self.local_def_id(index), - ident: Ident::from_interned_str(self.item_name(index)), + ident: Ident::with_empty_ctxt(self.item_name(index)), vis: f.visibility.decode(self) } }).collect(), @@ -787,7 +788,7 @@ impl<'a, 'tcx> CrateMetadata { if let Some(kind) = self.def_kind(child_index) { callback(def::Export { res: Res::Def(kind, self.local_def_id(child_index)), - ident: Ident::from_interned_str(self.item_name(child_index)), + ident: Ident::with_empty_ctxt(self.item_name(child_index)), vis: self.get_visibility(child_index), span: self.entry(child_index).span.decode((self, sess)), }); @@ -982,7 +983,7 @@ impl<'a, 'tcx> CrateMetadata { self.entry(id) .children .decode(self) - .map(|index| self.item_name(index).as_symbol()) + .map(|index| self.item_name(index)) .collect() } diff --git a/src/librustc_mir/shim.rs b/src/librustc_mir/shim.rs index 5bd3a1714887d..b9224d973fe7b 100644 --- a/src/librustc_mir/shim.rs +++ b/src/librustc_mir/shim.rs @@ -10,7 +10,7 @@ use rustc::ty::query::Providers; use rustc_data_structures::indexed_vec::{IndexVec, Idx}; use rustc_target::spec::abi::Abi; -use syntax_pos::Span; +use syntax_pos::{Span, sym}; use std::fmt; use std::iter; @@ -100,9 +100,9 @@ fn make_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, } ty::InstanceDef::CloneShim(def_id, ty) => { let name = tcx.item_name(def_id); - if name == "clone" { + if name == sym::clone { build_clone_shim(tcx, def_id, ty) - } else if name == "clone_from" { + } else if name == sym::clone_from { debug!("make_shim({:?}: using default trait implementation", instance); return tcx.optimized_mir(def_id); } else { diff --git a/src/librustc_mir/transform/rustc_peek.rs b/src/librustc_mir/transform/rustc_peek.rs index 815821f6ff033..7d2dbff996d08 100644 --- a/src/librustc_mir/transform/rustc_peek.rs +++ b/src/librustc_mir/transform/rustc_peek.rs @@ -223,7 +223,7 @@ fn is_rustc_peek<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, if let ty::FnDef(def_id, _) = func.ty.sty { let abi = tcx.fn_sig(def_id).abi(); let name = tcx.item_name(def_id); - if abi == Abi::RustIntrinsic && name == "rustc_peek" { + if abi == Abi::RustIntrinsic && name == sym::rustc_peek { return Some((args, source_info.span)); } } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index e434623d4a127..6d03a5ad63bbb 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1702,7 +1702,7 @@ impl<'a, 'tcx> Clean for (&'a ty::Generics, let stripped_typarams = gens.params.iter().filter_map(|param| match param.kind { ty::GenericParamDefKind::Lifetime => None, ty::GenericParamDefKind::Type { .. } => { - if param.name == keywords::SelfUpper.name().as_str() { + if param.name.as_symbol() == keywords::SelfUpper.name() { assert_eq!(param.index, 0); return None; } diff --git a/src/libsyntax_pos/symbol.rs b/src/libsyntax_pos/symbol.rs index aa9028d4a6bad..ed2edaef14e21 100644 --- a/src/libsyntax_pos/symbol.rs +++ b/src/libsyntax_pos/symbol.rs @@ -1146,7 +1146,7 @@ impl Encodable for LocalInternedString { /// assert_ne!(Symbol::gensym("x"), Symbol::gensym("x")) /// assert_eq!(Symbol::gensym("x").as_interned_str(), Symbol::gensym("x").as_interned_str()) /// ``` -#[derive(Clone, Copy, Eq)] +#[derive(Clone, Copy, PartialEq, Eq)] pub struct InternedString { symbol: Symbol, } @@ -1211,42 +1211,6 @@ impl Ord for InternedString { } } -impl> PartialEq for InternedString { - fn eq(&self, other: &T) -> bool { - self.with(|string| string == other.deref()) - } -} - -impl PartialEq for InternedString { - fn eq(&self, other: &InternedString) -> bool { - self.symbol == other.symbol - } -} - -impl PartialEq for str { - fn eq(&self, other: &InternedString) -> bool { - other.with(|string| self == string) - } -} - -impl<'a> PartialEq for &'a str { - fn eq(&self, other: &InternedString) -> bool { - other.with(|string| *self == string) - } -} - -impl PartialEq for String { - fn eq(&self, other: &InternedString) -> bool { - other.with(|string| self == string) - } -} - -impl<'a> PartialEq for &'a String { - fn eq(&self, other: &InternedString) -> bool { - other.with(|string| *self == string) - } -} - impl fmt::Debug for InternedString { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.with(|str| fmt::Debug::fmt(&str, f))