Skip to content

Commit

Permalink
Remove impls for InternedString/string equality.
Browse files Browse the repository at this point in the history
`Symbol` received the same treatment in rust-lang#60630.

Also, we can derive `PartialEq` for `InternedString`.
  • Loading branch information
nnethercote committed May 21, 2019
1 parent d35181a commit b557567
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 66 deletions.
4 changes: 2 additions & 2 deletions src/librustc/middle/intrinsicck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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>) {
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/traits/on_unimplemented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 => {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(),
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/librustc/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,14 +1140,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.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;
}
}
Expand Down Expand Up @@ -1203,7 +1205,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.as_symbol() != keywords::Invalid.name() {
p!(write("{}", data.name));
return Ok(self);
}
Expand All @@ -1212,7 +1214,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.as_symbol() != keywords::Invalid.name() &&
name.as_symbol() != keywords::UnderscoreLifetime.name() {
p!(write("{}", name));
return Ok(self);
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_utils/symbol_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 7 additions & 6 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<DefKind> {
Expand Down Expand Up @@ -568,15 +569,15 @@ 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,
item.children.decode(self).map(|index| {
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(),
Expand Down Expand Up @@ -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)),
});
Expand Down Expand Up @@ -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()
}

Expand Down
6 changes: 3 additions & 3 deletions src/librustc_mir/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/transform/rustc_peek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1702,7 +1702,7 @@ impl<'a, 'tcx> Clean<Generics> 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;
}
Expand Down
38 changes: 1 addition & 37 deletions src/libsyntax_pos/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -1211,42 +1211,6 @@ impl Ord for InternedString {
}
}

impl<T: std::ops::Deref<Target = str>> PartialEq<T> for InternedString {
fn eq(&self, other: &T) -> bool {
self.with(|string| string == other.deref())
}
}

impl PartialEq<InternedString> for InternedString {
fn eq(&self, other: &InternedString) -> bool {
self.symbol == other.symbol
}
}

impl PartialEq<InternedString> for str {
fn eq(&self, other: &InternedString) -> bool {
other.with(|string| self == string)
}
}

impl<'a> PartialEq<InternedString> for &'a str {
fn eq(&self, other: &InternedString) -> bool {
other.with(|string| *self == string)
}
}

impl PartialEq<InternedString> for String {
fn eq(&self, other: &InternedString) -> bool {
other.with(|string| self == string)
}
}

impl<'a> PartialEq<InternedString> 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))
Expand Down

0 comments on commit b557567

Please sign in to comment.