From c389a39c9728d5c912a9ce1bc4c04eb1a3f31fe8 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 11 May 2019 19:08:09 +0300 Subject: [PATCH] Eliminate unnecessary `Ident::with_empty_ctxt`s --- src/librustc/hir/lowering.rs | 2 +- src/librustc/hir/map/def_collector.rs | 2 +- src/librustc/hir/mod.rs | 3 +-- src/librustc/middle/resolve_lifetime.rs | 2 +- src/librustc_resolve/build_reduced_graph.rs | 2 +- src/librustc_resolve/diagnostics.rs | 2 +- src/librustc_resolve/lib.rs | 2 +- src/librustc_resolve/resolve_imports.rs | 2 +- src/libsyntax/ext/build.rs | 2 +- src/libsyntax/ext/expand.rs | 8 ++++---- src/libsyntax/ext/placeholders.rs | 3 +-- src/libsyntax/ext/tt/quoted.rs | 4 ++-- src/libsyntax/mut_visit.rs | 3 +-- src/libsyntax/parse/parser.rs | 18 ++++++++---------- src/libsyntax/std_inject.rs | 2 +- src/libsyntax_ext/deriving/generic/mod.rs | 2 +- src/libsyntax_ext/global_asm.rs | 2 +- src/libsyntax_pos/symbol.rs | 5 +++++ 18 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 7cf5b4a9021..90dff5e3fc4 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -1614,7 +1614,7 @@ fn generate_existential_type( trace!("registering existential type with id {:#?}", exist_ty_id); let exist_ty_item = hir::Item { hir_id: exist_ty_id, - ident: Ident::with_empty_ctxt(kw::Invalid), + ident: Ident::invalid(), attrs: Default::default(), node: exist_ty_item_kind, vis: respan(span.shrink_to_lo(), hir::VisibilityKind::Inherited), diff --git a/src/librustc/hir/map/def_collector.rs b/src/librustc/hir/map/def_collector.rs index 77fd8cc062f..bb9e76f0262 100644 --- a/src/librustc/hir/map/def_collector.rs +++ b/src/librustc/hir/map/def_collector.rs @@ -138,7 +138,7 @@ fn visit_item(&mut self, i: &'a Item) { // information we encapsulate into, the better let def_data = match i.node { ItemKind::Impl(..) => DefPathData::Impl, - ItemKind::Mod(..) if i.ident == Ident::with_empty_ctxt(kw::Invalid) => { + ItemKind::Mod(..) if i.ident.name == kw::Invalid => { return visit::walk_item(self, i); } ItemKind::Mod(..) | ItemKind::Trait(..) | ItemKind::TraitAlias(..) | diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index e5e6c408a06..e3e930f5d1d 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -234,8 +234,7 @@ pub enum LifetimeName { impl LifetimeName { pub fn ident(&self) -> Ident { match *self { - LifetimeName::Implicit => Ident::with_empty_ctxt(kw::Invalid), - LifetimeName::Error => Ident::with_empty_ctxt(kw::Invalid), + LifetimeName::Implicit | LifetimeName::Error => Ident::invalid(), LifetimeName::Underscore => Ident::with_empty_ctxt(kw::UnderscoreLifetime), LifetimeName::Static => Ident::with_empty_ctxt(kw::StaticLifetime), LifetimeName::Param(param_name) => param_name.ident(), diff --git a/src/librustc/middle/resolve_lifetime.rs b/src/librustc/middle/resolve_lifetime.rs index 6a4d2e0966b..419cc593686 100644 --- a/src/librustc/middle/resolve_lifetime.rs +++ b/src/librustc/middle/resolve_lifetime.rs @@ -1602,7 +1602,7 @@ fn check_uses_for_lifetimes_defined_by_scope(&mut self) { } { debug!("id = {:?} span = {:?} name = {:?}", id, span, name); - if name == ast::Ident::with_empty_ctxt(kw::UnderscoreLifetime) { + if name.name == kw::UnderscoreLifetime { continue; } diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index 782f36a77da..8c516a4da4e 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -420,7 +420,7 @@ fn build_reduced_graph_for_item(&mut self, item: &Item, parent_scope: ParentScop ItemKind::GlobalAsm(..) => {} - ItemKind::Mod(..) if ident == Ident::with_empty_ctxt(kw::Invalid) => {} // Crate root + ItemKind::Mod(..) if ident.name == kw::Invalid => {} // Crate root ItemKind::Mod(..) => { let def_id = self.definitions.local_def_id(item.id); diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 730927d2bb5..8b5e2b86d5e 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -460,7 +460,7 @@ pub(crate) fn make_path_suggestion( (Some(fst), _) if fst.ident.span.rust_2018() && !fst.ident.is_path_segment_keyword() => { // Insert a placeholder that's later replaced by `self`/`super`/etc. - path.insert(0, Segment::from_ident(Ident::with_empty_ctxt(kw::Invalid))); + path.insert(0, Segment::from_ident(Ident::invalid())); } _ => return None, } diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 748cf691f8d..a7097a9475e 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -4669,7 +4669,7 @@ fn lookup_import_candidates_from_module(&mut self, { let mut candidates = Vec::new(); let mut seen_modules = FxHashSet::default(); - let not_local_module = crate_name != Ident::with_empty_ctxt(kw::Crate); + let not_local_module = crate_name.name != kw::Crate; let mut worklist = vec![(start_module, Vec::::new(), not_local_module)]; while let Some((in_module, diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index ab873ea2701..ef71bd51167 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -992,7 +992,7 @@ fn finalize_import( // HACK(eddyb) `lint_if_path_starts_with_module` needs at least // 2 segments, so the `resolve_path` above won't trigger it. let mut full_path = directive.module_path.clone(); - full_path.push(Segment::from_ident(Ident::with_empty_ctxt(kw::Invalid))); + full_path.push(Segment::from_ident(Ident::invalid())); self.lint_if_path_starts_with_module( directive.crate_lint(), &full_path, diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 77d5fa238f2..466715e69fd 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -1175,7 +1175,7 @@ fn item_use(&self, sp: Span, vis: ast::Visibility, vp: P) -> P { P(ast::Item { id: ast::DUMMY_NODE_ID, - ident: Ident::with_empty_ctxt(kw::Invalid), + ident: Ident::invalid(), attrs: vec![], node: ast::ItemKind::Use(vp), vis, diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 5f9c84c6126..fbe052252a1 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -271,7 +271,7 @@ pub fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate { attrs: krate.attrs, span: krate.span, node: ast::ItemKind::Mod(krate.module), - ident: Ident::with_empty_ctxt(kw::Invalid), + ident: Ident::invalid(), id: ast::DUMMY_NODE_ID, vis: respan(krate.span.shrink_to_lo(), ast::VisibilityKind::Public), tokens: None, @@ -708,7 +708,7 @@ fn expand_bang_invoc(&mut self, }; let path = &mac.node.path; - let ident = ident.unwrap_or_else(|| Ident::with_empty_ctxt(kw::Invalid)); + let ident = ident.unwrap_or_else(|| Ident::invalid()); let validate_and_set_expn_info = |this: &mut Self, // arg instead of capture def_site_span: Option, allow_internal_unstable, @@ -929,7 +929,7 @@ fn expand_derive_invoc(&mut self, invoc.expansion_data.mark.set_expn_info(expn_info); let span = span.with_ctxt(self.cx.backtrace()); let dummy = ast::MetaItem { // FIXME(jseyfried) avoid this - path: Path::from_ident(Ident::with_empty_ctxt(kw::Invalid)), + path: Path::from_ident(Ident::invalid()), span: DUMMY_SP, node: ast::MetaItemKind::Word, }; @@ -1338,7 +1338,7 @@ fn visit_block(&mut self, block: &mut P) { }) } ast::ItemKind::Mod(ast::Mod { inner, .. }) => { - if item.ident == Ident::with_empty_ctxt(kw::Invalid) { + if item.ident == Ident::invalid() { return noop_flat_map_item(item, self); } diff --git a/src/libsyntax/ext/placeholders.rs b/src/libsyntax/ext/placeholders.rs index 9ba1ff0ec7e..8f24d11cfd5 100644 --- a/src/libsyntax/ext/placeholders.rs +++ b/src/libsyntax/ext/placeholders.rs @@ -6,7 +6,6 @@ use crate::tokenstream::TokenStream; use crate::mut_visit::*; use crate::ptr::P; -use crate::symbol::kw; use crate::ThinVec; use smallvec::{smallvec, SmallVec}; @@ -22,7 +21,7 @@ fn mac_placeholder() -> ast::Mac { }) } - let ident = ast::Ident::with_empty_ctxt(kw::Invalid); + let ident = ast::Ident::invalid(); let attrs = Vec::new(); let generics = ast::Generics::default(); let vis = dummy_spanned(ast::VisibilityKind::Inherited); diff --git a/src/libsyntax/ext/tt/quoted.rs b/src/libsyntax/ext/tt/quoted.rs index 2f1a9834674..a029c654659 100644 --- a/src/libsyntax/ext/tt/quoted.rs +++ b/src/libsyntax/ext/tt/quoted.rs @@ -228,7 +228,7 @@ pub fn parse( result.push(TokenTree::MetaVarDecl( span, ident, - ast::Ident::with_empty_ctxt(kw::Invalid), + ast::Ident::invalid(), )); } @@ -334,7 +334,7 @@ fn parse_tree( pprust::token_to_string(&tok) ); sess.span_diagnostic.span_err(span, &msg); - TokenTree::MetaVar(span, ast::Ident::with_empty_ctxt(kw::Invalid)) + TokenTree::MetaVar(span, ast::Ident::invalid()) } // There are no more tokens. Just return the `$` we already have. diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs index 8dae4756c82..a6564de3b98 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -11,7 +11,6 @@ use crate::source_map::{Spanned, respan}; use crate::parse::token::{self, Token}; use crate::ptr::P; -use crate::symbol::kw; use crate::ThinVec; use crate::tokenstream::*; use crate::util::map_in_place::MapInPlace; @@ -977,7 +976,7 @@ pub fn noop_visit_mod(Mod { inner, items, inline: _ }: &mut Mod, pub fn noop_visit_crate(krate: &mut Crate, vis: &mut T) { visit_clobber(krate, |Crate { module, attrs, span }| { let item = P(Item { - ident: Ident::with_empty_ctxt(kw::Invalid), + ident: Ident::invalid(), attrs, id: DUMMY_NODE_ID, vis: respan(span.shrink_to_lo(), VisibilityKind::Public), diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 0099dd3d3c8..6c1f1d51a07 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1480,9 +1480,7 @@ fn parse_trait_item_(&mut self, (ident, TraitItemKind::Const(ty, default), ast::Generics::default()) } else if let Some(mac) = self.parse_assoc_macro_invoc("trait", None, &mut false)? { // trait item macro. - (Ident::with_empty_ctxt(kw::Invalid), - ast::TraitItemKind::Macro(mac), - ast::Generics::default()) + (Ident::invalid(), ast::TraitItemKind::Macro(mac), ast::Generics::default()) } else { let (constness, unsafety, mut asyncness, abi) = self.parse_fn_front_matter()?; @@ -4988,7 +4986,7 @@ fn parse_stmt_without_recovery(&mut self, // it's a macro invocation let id = match self.token { - token::OpenDelim(_) => Ident::with_empty_ctxt(kw::Invalid), // no special identifier + token::OpenDelim(_) => Ident::invalid(), // no special identifier _ => self.parse_ident()?, }; @@ -6396,7 +6394,7 @@ fn parse_impl_method(&mut self, vis: &Visibility, at_end: &mut bool) // code copied from parse_macro_use_or_failure... abstraction! if let Some(mac) = self.parse_assoc_macro_invoc("impl", Some(vis), at_end)? { // method macro - Ok((Ident::with_empty_ctxt(kw::Invalid), vec![], ast::Generics::default(), + Ok((Ident::invalid(), vec![], ast::Generics::default(), ast::ImplItemKind::Macro(mac))) } else { let (constness, unsafety, mut asyncness, abi) = self.parse_fn_front_matter()?; @@ -6616,7 +6614,7 @@ fn parse_item_impl(&mut self, unsafety: Unsafety, defaultness: Defaultness) } }; - Ok((Ident::with_empty_ctxt(kw::Invalid), item_kind, Some(attrs))) + Ok((Ident::invalid(), item_kind, Some(attrs))) } fn parse_late_bound_lifetime_defs(&mut self) -> PResult<'a, Vec> { @@ -7414,7 +7412,7 @@ fn parse_item_foreign_mod(&mut self, abi, items: foreign_items }; - let invalid = Ident::with_empty_ctxt(kw::Invalid); + let invalid = Ident::invalid(); Ok(self.mk_item(lo.to(prev_span), invalid, ItemKind::ForeignMod(m), visibility, attrs)) } @@ -7662,7 +7660,7 @@ fn parse_item_implementation( let span = lo.to(self.prev_span); let item = - self.mk_item(span, Ident::with_empty_ctxt(kw::Invalid), item_, visibility, attrs); + self.mk_item(span, Ident::invalid(), item_, visibility, attrs); return Ok(Some(item)); } @@ -8108,7 +8106,7 @@ fn parse_item_implementation( Some(mac) => { Ok( ForeignItem { - ident: Ident::with_empty_ctxt(kw::Invalid), + ident: Ident::invalid(), span: lo.to(self.prev_span), id: ast::DUMMY_NODE_ID, attrs, @@ -8155,7 +8153,7 @@ fn parse_macro_use_or_failure( let id = if self.token.is_ident() { self.parse_ident()? } else { - Ident::with_empty_ctxt(kw::Invalid) // no special identifier + Ident::invalid() // no special identifier }; // eat a matched-delimiter token tree: let (delim, tts) = self.expect_delimited_token_tree()?; diff --git a/src/libsyntax/std_inject.rs b/src/libsyntax/std_inject.rs index 5f6cf944061..398705857bb 100644 --- a/src/libsyntax/std_inject.rs +++ b/src/libsyntax/std_inject.rs @@ -118,7 +118,7 @@ pub fn maybe_inject_crates_ref( span, })), id: ast::DUMMY_NODE_ID, - ident: ast::Ident::with_empty_ctxt(kw::Invalid), + ident: ast::Ident::invalid(), span, tokens: None, })); diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs index 5b4bf6b8618..0689eb50f9c 100644 --- a/src/libsyntax_ext/deriving/generic/mod.rs +++ b/src/libsyntax_ext/deriving/generic/mod.rs @@ -686,7 +686,7 @@ fn create_derived_impl(&self, }; cx.item(self.span, - Ident::with_empty_ctxt(kw::Invalid), + Ident::invalid(), a, ast::ItemKind::Impl(unsafety, ast::ImplPolarity::Positive, diff --git a/src/libsyntax_ext/global_asm.rs b/src/libsyntax_ext/global_asm.rs index 3f2853e4b0e..5220143a3cc 100644 --- a/src/libsyntax_ext/global_asm.rs +++ b/src/libsyntax_ext/global_asm.rs @@ -37,7 +37,7 @@ pub fn expand_global_asm<'cx>(cx: &'cx mut ExtCtxt<'_>, match parse_global_asm(cx, sp, tts) { Ok(Some(global_asm)) => { MacEager::items(smallvec![P(ast::Item { - ident: ast::Ident::with_empty_ctxt(Symbol::intern("")), + ident: ast::Ident::invalid(), attrs: Vec::new(), id: ast::DUMMY_NODE_ID, node: ast::ItemKind::GlobalAsm(P(global_asm)), diff --git a/src/libsyntax_pos/symbol.rs b/src/libsyntax_pos/symbol.rs index 6bcf83869a9..f9f0b097f7a 100644 --- a/src/libsyntax_pos/symbol.rs +++ b/src/libsyntax_pos/symbol.rs @@ -641,6 +641,11 @@ pub const fn with_empty_ctxt(name: Symbol) -> Ident { Ident::new(name, DUMMY_SP) } + #[inline] + pub fn invalid() -> Ident { + Ident::with_empty_ctxt(kw::Invalid) + } + /// Maps an interned string to an identifier with an empty syntax context. pub fn from_interned_str(string: InternedString) -> Ident { Ident::with_empty_ctxt(string.as_symbol()) -- 2.44.0