Skip to content

Commit

Permalink
Remove the equality operation between Symbol and strings.
Browse files Browse the repository at this point in the history
And also the equality between `Path` and strings, because `Path` is made
up of `Symbol`s.
  • Loading branch information
nnethercote committed May 12, 2019
1 parent fb084a4 commit 999c1fc
Show file tree
Hide file tree
Showing 49 changed files with 182 additions and 173 deletions.
69 changes: 35 additions & 34 deletions src/librustc/hir/lowering.rs
Expand Up @@ -73,7 +73,7 @@ use syntax_pos::Span;
const HIR_ID_COUNTER_LOCKED: u32 = 0xFFFFFFFF;

pub struct LoweringContext<'a> {
crate_root: Option<&'static str>,
crate_root: Option<Symbol>,

/// Used to assign ids to HIR nodes that do not directly correspond to an AST node.
sess: &'a Session,
Expand Down Expand Up @@ -164,8 +164,8 @@ pub trait Resolver {
fn resolve_str_path(
&mut self,
span: Span,
crate_root: Option<&str>,
components: &[&str],
crate_root: Option<Symbol>,
components: &[Symbol],
is_value: bool,
) -> hir::Path;
}
Expand Down Expand Up @@ -228,7 +228,7 @@ pub fn lower_crate(
dep_graph.assert_ignored();

LoweringContext {
crate_root: std_inject::injected_crate_name(),
crate_root: std_inject::injected_crate_name().map(Symbol::intern),
sess,
cstore,
resolver,
Expand Down Expand Up @@ -1149,7 +1149,7 @@ impl<'a> LoweringContext<'a> {
].into()),
);
let gen_future = self.expr_std_path(
unstable_span, &["future", "from_generator"], None, ThinVec::new());
unstable_span, &[sym::future, sym::from_generator], None, ThinVec::new());
hir::ExprKind::Call(P(gen_future), hir_vec![generator])
}

Expand Down Expand Up @@ -2548,7 +2548,7 @@ impl<'a> LoweringContext<'a> {

// ::std::future::Future<future_params>
let future_path =
self.std_path(span, &["future", "Future"], Some(future_params), false);
self.std_path(span, &[sym::future, sym::Future], Some(future_params), false);

hir::GenericBound::Trait(
hir::PolyTraitRef {
Expand Down Expand Up @@ -4194,7 +4194,7 @@ impl<'a> LoweringContext<'a> {
|x: P<hir::Expr>| x.into_inner(),
);
block.expr = Some(this.wrap_in_try_constructor(
"from_ok", tail, unstable_span));
sym::from_ok, tail, unstable_span));
hir::ExprKind::Block(P(block), None)
})
}
Expand Down Expand Up @@ -4336,7 +4336,7 @@ impl<'a> LoweringContext<'a> {
self.expr_call_std_assoc_fn(
id,
e.span,
&["ops", "RangeInclusive"],
&[sym::ops, sym::RangeInclusive],
"new",
hir_vec![e1, e2],
)
Expand All @@ -4345,11 +4345,11 @@ impl<'a> LoweringContext<'a> {
use syntax::ast::RangeLimits::*;

let path = match (e1, e2, lims) {
(&None, &None, HalfOpen) => "RangeFull",
(&Some(..), &None, HalfOpen) => "RangeFrom",
(&None, &Some(..), HalfOpen) => "RangeTo",
(&Some(..), &Some(..), HalfOpen) => "Range",
(&None, &Some(..), Closed) => "RangeToInclusive",
(&None, &None, HalfOpen) => sym::RangeFull,
(&Some(..), &None, HalfOpen) => sym::RangeFrom,
(&None, &Some(..), HalfOpen) => sym::RangeTo,
(&Some(..), &Some(..), HalfOpen) => sym::Range,
(&None, &Some(..), Closed) => sym::RangeToInclusive,
(&Some(..), &Some(..), Closed) => unreachable!(),
(_, &None, Closed) => self.diagnostic()
.span_fatal(e.span, "inclusive range with no end")
Expand All @@ -4367,7 +4367,7 @@ impl<'a> LoweringContext<'a> {
.collect::<P<[hir::Field]>>();

let is_unit = fields.is_empty();
let struct_path = ["ops", path];
let struct_path = [sym::ops, path];
let struct_path = self.std_path(e.span, &struct_path, None, is_unit);
let struct_path = hir::QPath::Resolved(None, P(struct_path));

Expand Down Expand Up @@ -4656,7 +4656,7 @@ impl<'a> LoweringContext<'a> {
let match_expr = {
let iter = P(self.expr_ident(head_sp, iter, iter_pat_nid));
let ref_mut_iter = self.expr_mut_addr_of(head_sp, iter);
let next_path = &["iter", "Iterator", "next"];
let next_path = &[sym::iter, sym::Iterator, sym::next];
let next_expr = P(self.expr_call_std_path(
head_sp,
next_path,
Expand Down Expand Up @@ -4723,7 +4723,8 @@ impl<'a> LoweringContext<'a> {

// `match ::std::iter::IntoIterator::into_iter(<head>) { ... }`
let into_iter_expr = {
let into_iter_path = &["iter", "IntoIterator", "into_iter"];
let into_iter_path =
&[sym::iter, sym::IntoIterator, sym::into_iter];
P(self.expr_call_std_path(
head_sp,
into_iter_path,
Expand Down Expand Up @@ -4780,7 +4781,7 @@ impl<'a> LoweringContext<'a> {
// expand <expr>
let sub_expr = self.lower_expr(sub_expr);

let path = &["ops", "Try", "into_result"];
let path = &[sym::ops, sym::Try, sym::into_result];
P(self.expr_call_std_path(
unstable_span,
path,
Expand Down Expand Up @@ -4822,12 +4823,12 @@ impl<'a> LoweringContext<'a> {
let err_ident = self.str_to_ident("err");
let (err_local, err_local_nid) = self.pat_ident(try_span, err_ident);
let from_expr = {
let from_path = &["convert", "From", "from"];
let from_path = &[sym::convert, sym::From, sym::from];
let err_expr = self.expr_ident(try_span, err_ident, err_local_nid);
self.expr_call_std_path(try_span, from_path, hir_vec![err_expr])
};
let from_err_expr =
self.wrap_in_try_constructor("from_error", from_expr, unstable_span);
self.wrap_in_try_constructor(sym::from_error, from_expr, unstable_span);
let thin_attrs = ThinVec::from(attrs);
let catch_scope = self.catch_scopes.last().map(|x| *x);
let ret_expr = if let Some(catch_node) = catch_scope {
Expand Down Expand Up @@ -5057,7 +5058,7 @@ impl<'a> LoweringContext<'a> {
fn expr_call_std_path(
&mut self,
span: Span,
path_components: &[&str],
path_components: &[Symbol],
args: hir::HirVec<hir::Expr>,
) -> hir::Expr {
let path = P(self.expr_std_path(span, path_components, None, ThinVec::new()));
Expand All @@ -5077,7 +5078,7 @@ impl<'a> LoweringContext<'a> {
&mut self,
ty_path_id: hir::HirId,
span: Span,
ty_path_components: &[&str],
ty_path_components: &[Symbol],
assoc_fn_name: &str,
args: hir::HirVec<hir::Expr>,
) -> hir::ExprKind {
Expand Down Expand Up @@ -5119,7 +5120,7 @@ impl<'a> LoweringContext<'a> {
fn expr_std_path(
&mut self,
span: Span,
components: &[&str],
components: &[Symbol],
params: Option<P<hir::GenericArgs>>,
attrs: ThinVec<Attribute>,
) -> hir::Expr {
Expand Down Expand Up @@ -5250,25 +5251,25 @@ impl<'a> LoweringContext<'a> {
}

fn pat_ok(&mut self, span: Span, pat: P<hir::Pat>) -> P<hir::Pat> {
self.pat_std_enum(span, &["result", "Result", "Ok"], hir_vec![pat])
self.pat_std_enum(span, &[sym::result, sym::Result, sym::Ok], hir_vec![pat])
}

fn pat_err(&mut self, span: Span, pat: P<hir::Pat>) -> P<hir::Pat> {
self.pat_std_enum(span, &["result", "Result", "Err"], hir_vec![pat])
self.pat_std_enum(span, &[sym::result, sym::Result, sym::Err], hir_vec![pat])
}

fn pat_some(&mut self, span: Span, pat: P<hir::Pat>) -> P<hir::Pat> {
self.pat_std_enum(span, &["option", "Option", "Some"], hir_vec![pat])
self.pat_std_enum(span, &[sym::option, sym::Option, sym::Some], hir_vec![pat])
}

fn pat_none(&mut self, span: Span) -> P<hir::Pat> {
self.pat_std_enum(span, &["option", "Option", "None"], hir_vec![])
self.pat_std_enum(span, &[sym::option, sym::Option, sym::None], hir_vec![])
}

fn pat_std_enum(
&mut self,
span: Span,
components: &[&str],
components: &[Symbol],
subpats: hir::HirVec<P<hir::Pat>>,
) -> P<hir::Pat> {
let path = self.std_path(span, components, None, true);
Expand Down Expand Up @@ -5321,7 +5322,7 @@ impl<'a> LoweringContext<'a> {
fn std_path(
&mut self,
span: Span,
components: &[&str],
components: &[Symbol],
params: Option<P<hir::GenericArgs>>,
is_value: bool,
) -> hir::Path {
Expand Down Expand Up @@ -5520,11 +5521,11 @@ impl<'a> LoweringContext<'a> {

fn wrap_in_try_constructor(
&mut self,
method: &'static str,
method: Symbol,
e: hir::Expr,
unstable_span: Span,
) -> P<hir::Expr> {
let path = &["ops", "Try", method];
let path = &[sym::ops, sym::Try, method];
let from_err = P(self.expr_std_path(unstable_span, path, None,
ThinVec::new()));
P(self.expr_call(e.span, from_err, hir_vec![e]))
Expand Down Expand Up @@ -5594,15 +5595,15 @@ impl<'a> LoweringContext<'a> {
let new_unchecked_expr_kind = self.expr_call_std_assoc_fn(
pin_ty_id,
span,
&["pin", "Pin"],
&[sym::pin, sym::Pin],
"new_unchecked",
hir_vec![ref_mut_pinned],
);
let new_unchecked = P(self.expr(span, new_unchecked_expr_kind, ThinVec::new()));
let unsafe_expr = self.expr_unsafe(new_unchecked);
P(self.expr_call_std_path(
gen_future_span,
&["future", "poll_with_tls_context"],
&[sym::future, sym::poll_with_tls_context],
hir_vec![unsafe_expr],
))
};
Expand All @@ -5616,7 +5617,7 @@ impl<'a> LoweringContext<'a> {
let x_expr = P(self.expr_ident(span, x_ident, x_pat_hid));
let ready_pat = self.pat_std_enum(
span,
&["task", "Poll", "Ready"],
&[sym::task, sym::Poll, sym::Ready],
hir_vec![x_pat],
);
let break_x = self.with_loop_scope(loop_node_id, |this| {
Expand All @@ -5633,7 +5634,7 @@ impl<'a> LoweringContext<'a> {
let pending_arm = {
let pending_pat = self.pat_std_enum(
span,
&["task", "Poll", "Pending"],
&[sym::task, sym::Poll, sym::Pending],
hir_vec![],
);
let empty_block = P(self.expr_block_empty(span));
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/map/mod.rs
Expand Up @@ -1146,7 +1146,7 @@ impl<'a> NodesMatchingSuffix<'a> {
None => return false,
Some((node_id, name)) => (node_id, name),
};
if mod_name != &**part {
if mod_name.as_str() != *part {
return false;
}
cursor = self.map.get_parent_item(mod_id);
Expand Down Expand Up @@ -1183,7 +1183,7 @@ impl<'a> NodesMatchingSuffix<'a> {
// We are looking at some node `n` with a given name and parent
// id; do their names match what I am seeking?
fn matches_names(&self, parent_of_n: HirId, name: Name) -> bool {
name == &**self.item_name && self.suffix_matches(parent_of_n)
name.as_str() == *self.item_name && self.suffix_matches(parent_of_n)
}

fn matches_suffix(&self, hir: HirId) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/lint/levels.rs
Expand Up @@ -221,7 +221,7 @@ impl<'a> LintLevelsBuilder<'a> {
match item.node {
ast::MetaItemKind::Word => {} // actual lint names handled later
ast::MetaItemKind::NameValue(ref name_value) => {
if item.path == "reason" {
if item.path == sym::reason {
// found reason, reslice meta list to exclude it
metas = &metas[0..metas.len()-1];
// FIXME (#55112): issue unused-attributes lint if we thereby
Expand Down Expand Up @@ -261,7 +261,7 @@ impl<'a> LintLevelsBuilder<'a> {
let mut err = bad_attr(li.span());
if let Some(item) = li.meta_item() {
if let ast::MetaItemKind::NameValue(_) = item.node {
if item.path == "reason" {
if item.path == sym::reason {
err.help("reason in lint attribute must come last");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/entry.rs
Expand Up @@ -86,7 +86,7 @@ fn entry_point_type(item: &Item, at_root: bool) -> EntryPointType {
EntryPointType::Start
} else if attr::contains_name(&item.attrs, sym::main) {
EntryPointType::MainAttr
} else if item.ident.name == "main" {
} else if item.ident.name == sym::main {
if at_root {
// This is a top-level function so can be 'main'.
EntryPointType::MainNamed
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/lib_features.rs
Expand Up @@ -76,7 +76,7 @@ impl<'a, 'tcx> LibFeatureCollector<'a, 'tcx> {
// This additional check for stability is to make sure we
// don't emit additional, irrelevant errors for malformed
// attributes.
if *stab_attr != "stable" || since.is_some() {
if *stab_attr != sym::stable || since.is_some() {
return Some((feature, since, attr.span));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/stability.rs
Expand Up @@ -686,7 +686,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
// the `-Z force-unstable-if-unmarked` flag present (we're
// compiling a compiler crate), then let this missing feature
// annotation slide.
if feature == "rustc_private" && issue == 27812 {
if feature == sym::rustc_private && issue == 27812 {
if self.sess.opts.debugging_opts.force_unstable_if_unmarked {
return EvalResult::Allow;
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/weak_lang_items.rs
Expand Up @@ -6,7 +6,7 @@ use crate::middle::lang_items;
use rustc_data_structures::fx::FxHashSet;
use rustc_target::spec::PanicStrategy;
use syntax::ast;
use syntax::symbol::Symbol;
use syntax::symbol::{Symbol, sym};
use syntax_pos::Span;
use crate::hir::def_id::DefId;
use crate::hir::intravisit::{Visitor, NestedVisitorMap};
Expand Down Expand Up @@ -46,8 +46,8 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

pub fn link_name(attrs: &[ast::Attribute]) -> Option<Symbol> {
lang_items::extract(attrs).and_then(|(name, _)| {
$(if name == stringify!($name) {
Some(Symbol::intern(stringify!($sym)))
$(if name == sym::$name {
Some(sym::$sym)
} else)* {
None
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/session/config.rs
Expand Up @@ -2753,6 +2753,7 @@ mod tests {
// another --cfg test
#[test]
fn test_switch_implies_cfg_test_unless_cfg_test() {
use syntax::symbol::sym;
syntax::with_globals(|| {
let matches = &match optgroups().parse(&["--test".to_string(),
"--cfg=test".to_string()]) {
Expand All @@ -2763,7 +2764,7 @@ mod tests {
let (sessopts, cfg) = build_session_options_and_crate_config(matches);
let sess = build_session(sessopts, None, registry);
let cfg = build_configuration(&sess, to_crate_config(cfg));
let mut test_items = cfg.iter().filter(|&&(name, _)| name == "test");
let mut test_items = cfg.iter().filter(|&&(name, _)| name == sym::test);
assert!(test_items.next().is_some());
assert!(test_items.next().is_none());
});
Expand Down
5 changes: 3 additions & 2 deletions src/librustc/traits/project.rs
Expand Up @@ -19,6 +19,7 @@ use crate::mir::interpret::{GlobalId, ConstValue};
use rustc_data_structures::snapshot_map::{Snapshot, SnapshotMap};
use rustc_macros::HashStable;
use syntax::ast::Ident;
use syntax::symbol::sym;
use crate::ty::subst::{Subst, InternalSubsts};
use crate::ty::{self, ToPredicate, ToPolyTraitRef, Ty, TyCtxt};
use crate::ty::fold::{TypeFoldable, TypeFolder};
Expand Down Expand Up @@ -1318,9 +1319,9 @@ fn confirm_generator_candidate<'cx, 'gcx, 'tcx>(
gen_sig)
.map_bound(|(trait_ref, yield_ty, return_ty)| {
let name = tcx.associated_item(obligation.predicate.item_def_id).ident.name;
let ty = if name == "Return" {
let ty = if name == sym::Return {
return_ty
} else if name == "Yield" {
} else if name == sym::Yield {
yield_ty
} else {
bug!()
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/back/write.rs
Expand Up @@ -386,7 +386,7 @@ pub fn start_async_codegen<B: ExtraBackendMethods>(
let subsystem = attr::first_attr_value_str_by_name(&tcx.hir().krate().attrs,
sym::windows_subsystem);
let windows_subsystem = subsystem.map(|subsystem| {
if subsystem != "windows" && subsystem != "console" {
if subsystem != sym::windows && subsystem != sym::console {
tcx.sess.fatal(&format!("invalid windows subsystem `{}`, only \
`windows` and `console` are allowed",
subsystem));
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_utils/link.rs
Expand Up @@ -56,7 +56,7 @@ pub fn find_crate_name(sess: Option<&Session>,
if let Some(sess) = sess {
if let Some(ref s) = sess.opts.crate_name {
if let Some((attr, name)) = attr_crate_name {
if name != &**s {
if name.as_str() != *s {
let msg = format!("--crate-name and #[crate_name] are \
required to match, but `{}` != `{}`",
s, name);
Expand Down

0 comments on commit 999c1fc

Please sign in to comment.