From 59a382122fb09e2a9b4629e36efbc63a321eab6a Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 11 May 2019 17:41:37 +0300 Subject: Simplify use of keyword symbols --- src/libsyntax_pos/hygiene.rs | 14 +++++------ src/libsyntax_pos/symbol.rs | 55 +++++++++++++++----------------------------- 2 files changed, 26 insertions(+), 43 deletions(-) (limited to 'src/libsyntax_pos') diff --git a/src/libsyntax_pos/hygiene.rs b/src/libsyntax_pos/hygiene.rs index 6e787c08504..e8dcd8171c9 100644 --- a/src/libsyntax_pos/hygiene.rs +++ b/src/libsyntax_pos/hygiene.rs @@ -8,7 +8,7 @@ use crate::GLOBALS; use crate::Span; use crate::edition::Edition; -use crate::symbol::{keywords, Symbol}; +use crate::symbol::{kw, Symbol}; use serialize::{Encodable, Decodable, Encoder, Decoder}; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; @@ -192,7 +192,7 @@ impl HygieneData { prev_ctxt: SyntaxContext(0), opaque: SyntaxContext(0), opaque_and_semitransparent: SyntaxContext(0), - dollar_crate_name: keywords::DollarCrate.name(), + dollar_crate_name: kw::DollarCrate, }], markings: FxHashMap::default(), } @@ -245,7 +245,7 @@ impl SyntaxContext { prev_ctxt: SyntaxContext::empty(), opaque: SyntaxContext::empty(), opaque_and_semitransparent: SyntaxContext::empty(), - dollar_crate_name: keywords::DollarCrate.name(), + dollar_crate_name: kw::DollarCrate, }); SyntaxContext(data.syntax_contexts.len() as u32 - 1) }) @@ -312,7 +312,7 @@ impl SyntaxContext { prev_ctxt, opaque: new_opaque, opaque_and_semitransparent: new_opaque, - dollar_crate_name: keywords::DollarCrate.name(), + dollar_crate_name: kw::DollarCrate, }); new_opaque }); @@ -330,7 +330,7 @@ impl SyntaxContext { prev_ctxt, opaque, opaque_and_semitransparent: new_opaque_and_semitransparent, - dollar_crate_name: keywords::DollarCrate.name(), + dollar_crate_name: kw::DollarCrate, }); new_opaque_and_semitransparent }); @@ -346,7 +346,7 @@ impl SyntaxContext { prev_ctxt, opaque, opaque_and_semitransparent, - dollar_crate_name: keywords::DollarCrate.name(), + dollar_crate_name: kw::DollarCrate, }); new_opaque_and_semitransparent_and_transparent }) @@ -512,7 +512,7 @@ impl SyntaxContext { &mut data.syntax_contexts[self.0 as usize].dollar_crate_name, dollar_crate_name ); assert!(dollar_crate_name == prev_dollar_crate_name || - prev_dollar_crate_name == keywords::DollarCrate.name(), + prev_dollar_crate_name == kw::DollarCrate, "$crate name is reset for a syntax context"); }) } diff --git a/src/libsyntax_pos/symbol.rs b/src/libsyntax_pos/symbol.rs index 8b07e81e586..6bcf83869a9 100644 --- a/src/libsyntax_pos/symbol.rs +++ b/src/libsyntax_pos/symbol.rs @@ -692,7 +692,7 @@ impl Ident { /// Transforms an underscore identifier into one with the same name, but /// gensymed. Leaves non-underscore identifiers unchanged. pub fn gensym_if_underscore(self) -> Ident { - if self.name == keywords::Underscore.name() { self.gensym() } else { self } + if self.name == kw::Underscore { self.gensym() } else { self } } // WARNING: this function is deprecated and will be removed in the future. @@ -864,8 +864,8 @@ impl Interner { this.strings.reserve(init.len()); // We can't allocate empty strings in the arena, so handle this here. - assert!(keywords::Invalid.name().as_u32() == 0 && init[0].is_empty()); - this.names.insert("", keywords::Invalid.name()); + assert!(kw::Invalid.as_u32() == 0 && init[0].is_empty()); + this.names.insert("", kw::Invalid); this.strings.push(""); for string in &init[1..] { @@ -926,26 +926,9 @@ impl Interner { } } -pub mod keywords { - use super::{Symbol, Ident}; - - #[derive(Clone, Copy, PartialEq, Eq)] - pub struct Keyword { - ident: Ident, - } - - impl Keyword { - #[inline] - pub fn ident(self) -> Ident { - self.ident - } - - #[inline] - pub fn name(self) -> Symbol { - self.ident.name - } - } - +// This module has a very short name because it's used a lot. +pub mod kw { + use super::Symbol; keywords!(); } @@ -957,11 +940,11 @@ pub mod sym { impl Symbol { fn is_used_keyword_2018(self) -> bool { - self == keywords::Dyn.name() + self == kw::Dyn } fn is_unused_keyword_2018(self) -> bool { - self >= keywords::Async.name() && self <= keywords::Try.name() + self >= kw::Async && self <= kw::Try } } @@ -969,20 +952,20 @@ impl Ident { // Returns `true` for reserved identifiers used internally for elided lifetimes, // unnamed method parameters, crate root module, error recovery etc. pub fn is_special(self) -> bool { - self.name <= keywords::Underscore.name() + self.name <= kw::Underscore } /// Returns `true` if the token is a keyword used in the language. pub fn is_used_keyword(self) -> bool { // Note: `span.edition()` is relatively expensive, don't call it unless necessary. - self.name >= keywords::As.name() && self.name <= keywords::While.name() || + self.name >= kw::As && self.name <= kw::While || self.name.is_used_keyword_2018() && self.span.rust_2018() } /// Returns `true` if the token is a keyword reserved for possible future use. pub fn is_unused_keyword(self) -> bool { // Note: `span.edition()` is relatively expensive, don't call it unless necessary. - self.name >= keywords::Abstract.name() && self.name <= keywords::Yield.name() || + self.name >= kw::Abstract && self.name <= kw::Yield || self.name.is_unused_keyword_2018() && self.span.rust_2018() } @@ -993,17 +976,17 @@ impl Ident { /// A keyword or reserved identifier that can be used as a path segment. pub fn is_path_segment_keyword(self) -> bool { - self.name == keywords::Super.name() || - self.name == keywords::SelfLower.name() || - self.name == keywords::SelfUpper.name() || - self.name == keywords::Crate.name() || - self.name == keywords::PathRoot.name() || - self.name == keywords::DollarCrate.name() + self.name == kw::Super || + self.name == kw::SelfLower || + self.name == kw::SelfUpper || + self.name == kw::Crate || + self.name == kw::PathRoot || + self.name == kw::DollarCrate } /// This identifier can be a raw identifier. pub fn can_be_raw(self) -> bool { - self.name != keywords::Invalid.name() && self.name != keywords::Underscore.name() && + self.name != kw::Invalid && self.name != kw::Underscore && !self.is_path_segment_keyword() } @@ -1267,7 +1250,7 @@ mod tests { fn without_first_quote_test() { GLOBALS.set(&Globals::new(edition::DEFAULT_EDITION), || { let i = Ident::from_str("'break"); - assert_eq!(i.without_first_quote().name, keywords::Break.name()); + assert_eq!(i.without_first_quote().name, kw::Break); }); } } -- cgit 1.4.1-3-g733a5 From c389a39c9728d5c912a9ce1bc4c04eb1a3f31fe8 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 11 May 2019 19:08:09 +0300 Subject: 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(-) (limited to 'src/libsyntax_pos') 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 @@ impl<'a> LoweringContext<'a> { 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 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> { // 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 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> { } { 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 @@ impl<'a> Resolver<'a> { 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 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { (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 @@ impl<'a> Resolver<'a> { { 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 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { // 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 @@ impl<'a> AstBuilder for ExtCtxt<'a> { 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 @@ impl<'a, 'b> MacroExpander<'a, 'b> { 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 @@ impl<'a, 'b> MacroExpander<'a, 'b> { }; 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 @@ impl<'a, 'b> MacroExpander<'a, 'b> { 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 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> { }) } 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::ext::hygiene::Mark; 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 @@ pub fn placeholder(kind: AstFragmentKind, id: ast::NodeId) -> AstFragment { }) } - 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 @@ where 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::ast::*; 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 @@ impl<'a> Parser<'a> { (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 @@ impl<'a> Parser<'a> { // 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 @@ impl<'a> Parser<'a> { // 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 @@ impl<'a> Parser<'a> { } }; - 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 @@ impl<'a> Parser<'a> { 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 @@ impl<'a> Parser<'a> { 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 @@ impl<'a> Parser<'a> { 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 @@ impl<'a> Parser<'a> { 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 @@ impl<'a> TraitDef<'a> { }; 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 @@ impl 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()) -- cgit 1.4.1-3-g733a5 From a1885cdba38a63448ceec02f951ddc0844d0ff38 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Mon, 13 May 2019 22:46:20 +0300 Subject: Restore the old behavior of the rustdoc keyword check + Fix rebase --- src/librustc/ty/print/pretty.rs | 16 ++++++++-------- src/librustc_allocator/expand.rs | 5 ++--- src/librustc_plugin/load.rs | 4 ++-- src/librustc_resolve/resolve_imports.rs | 2 +- src/librustdoc/html/highlight.rs | 14 +++++++------- src/librustdoc/html/layout.rs | 2 +- src/libsyntax/attr/mod.rs | 4 ++-- src/libsyntax/ext/tt/macro_rules.rs | 12 ++++++------ src/libsyntax/feature_gate.rs | 4 ++-- src/libsyntax/parse/diagnostics.rs | 4 ++-- src/libsyntax/parse/literal.rs | 11 +++++------ src/libsyntax/test.rs | 2 +- src/libsyntax_pos/symbol.rs | 5 +++++ 13 files changed, 44 insertions(+), 41 deletions(-) (limited to 'src/libsyntax_pos') diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs index 5199da8fbbc..7a8d5d3bb9a 100644 --- a/src/librustc/ty/print/pretty.rs +++ b/src/librustc/ty/print/pretty.rs @@ -8,7 +8,7 @@ use crate::ty::{self, DefIdTree, ParamConst, Ty, TyCtxt, TypeFoldable}; use crate::ty::subst::{Kind, Subst, UnpackedKind}; use crate::mir::interpret::ConstValue; use rustc_target::spec::abi::Abi; -use syntax::symbol::{keywords, InternedString}; +use syntax::symbol::{kw, InternedString}; use std::cell::Cell; use std::fmt::{self, Write as _}; @@ -1140,16 +1140,16 @@ impl PrettyPrinter<'gcx, 'tcx> for FmtPrinter<'_, 'gcx, 'tcx, F> match *region { ty::ReEarlyBound(ref data) => { - data.name.as_symbol() != keywords::Invalid.name() && - data.name.as_symbol() != keywords::UnderscoreLifetime.name() + data.name.as_symbol() != kw::Invalid && + data.name.as_symbol() != kw::UnderscoreLifetime } ty::ReLateBound(_, br) | ty::ReFree(ty::FreeRegion { bound_region: br, .. }) | ty::RePlaceholder(ty::Placeholder { name: br, .. }) => { if let ty::BrNamed(_, name) = br { - if name.as_symbol() != keywords::Invalid.name() && - name.as_symbol() != keywords::UnderscoreLifetime.name() { + if name.as_symbol() != kw::Invalid && + name.as_symbol() != kw::UnderscoreLifetime { return true; } } @@ -1205,7 +1205,7 @@ impl FmtPrinter<'_, '_, '_, F> { // `explain_region()` or `note_and_explain_region()`. match *region { ty::ReEarlyBound(ref data) => { - if data.name.as_symbol() != keywords::Invalid.name() { + if data.name.as_symbol() != kw::Invalid { p!(write("{}", data.name)); return Ok(self); } @@ -1214,8 +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.as_symbol() != keywords::Invalid.name() && - name.as_symbol() != keywords::UnderscoreLifetime.name() { + if name.as_symbol() != kw::Invalid && + name.as_symbol() != kw::UnderscoreLifetime { p!(write("{}", name)); return Ok(self); } diff --git a/src/librustc_allocator/expand.rs b/src/librustc_allocator/expand.rs index 0c19d770194..b9cd30694f6 100644 --- a/src/librustc_allocator/expand.rs +++ b/src/librustc_allocator/expand.rs @@ -19,7 +19,7 @@ use syntax::{ mut_visit::{self, MutVisitor}, parse::ParseSess, ptr::P, - symbol::{keywords, Symbol, sym} + symbol::{kw, sym, Symbol} }; use syntax_pos::Span; @@ -116,8 +116,7 @@ impl MutVisitor for ExpandAllocatorDirectives<'_> { // We will generate a new submodule. To `use` the static from that module, we need to get // the `super::...` path. - let super_path = - f.cx.path(f.span, vec![Ident::with_empty_ctxt(keywords::Super.name()), f.global]); + let super_path = f.cx.path(f.span, vec![Ident::with_empty_ctxt(kw::Super), f.global]); // Generate the items in the submodule let mut items = vec![ diff --git a/src/librustc_plugin/load.rs b/src/librustc_plugin/load.rs index 680bdcc4bbe..46cd66fe585 100644 --- a/src/librustc_plugin/load.rs +++ b/src/librustc_plugin/load.rs @@ -11,7 +11,7 @@ use std::mem; use std::path::PathBuf; use syntax::ast; use syntax::span_err; -use syntax::symbol::{Symbol, keywords, sym}; +use syntax::symbol::{Symbol, kw, sym}; use syntax_pos::{Span, DUMMY_SP}; /// Pointer to a registrar function. @@ -58,7 +58,7 @@ pub fn load_plugins(sess: &Session, for plugin in plugins { // plugins must have a name and can't be key = value let name = plugin.name_or_empty(); - if name != keywords::Invalid.name() && !plugin.is_value_str() { + if name != kw::Invalid && !plugin.is_value_str() { let args = plugin.meta_item_list().map(ToOwned::to_owned); loader.load_plugin(plugin.span(), name, args.unwrap_or_default()); } else { diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index ef71bd51167..c0ff7b310b5 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -707,7 +707,7 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> { has_errors = true; if let SingleImport { source, ref source_bindings, .. } = import.subclass { - if source.name == keywords::SelfLower.name() { + if source.name == kw::SelfLower { // Silence `unresolved import` error if E0429 is already emitted if let Err(Determined) = source_bindings.value_ns.get() { continue; diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 5bb06516ac4..e5b44077fc9 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -15,6 +15,7 @@ use syntax::source_map::{SourceMap, FilePathMapping}; use syntax::parse::lexer::{self, TokenAndSpan}; use syntax::parse::token; use syntax::parse; +use syntax::symbol::{kw, sym}; use syntax_pos::{Span, FileName}; /// Highlights `src`, returning the HTML output. @@ -325,16 +326,15 @@ impl<'a> Classifier<'a> { // Keywords are also included in the identifier set. token::Ident(ident, is_raw) => { - match &*ident.as_str() { - "ref" | "mut" if !is_raw => Class::RefKeyWord, + match ident.name { + kw::Ref | kw::Mut if !is_raw => Class::RefKeyWord, - "self" | "Self" => Class::Self_, - "false" | "true" if !is_raw => Class::Bool, + kw::SelfLower | kw::SelfUpper => Class::Self_, + kw::False | kw::True if !is_raw => Class::Bool, - "Option" | "Result" => Class::PreludeTy, - "Some" | "None" | "Ok" | "Err" => Class::PreludeVal, + sym::Option | sym::Result => Class::PreludeTy, + sym::Some | sym::None | sym::Ok | sym::Err => Class::PreludeVal, - "$crate" => Class::KeyWord, _ if tas.tok.is_reserved_ident() => Class::KeyWord, _ => { diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index 71d8665f3b3..ae0bd1aafa8 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -44,7 +44,7 @@ pub fn render( \ \ \ - \ + \ {title}\ \ Symbol { - self.ident().unwrap_or(Ident.invalid()).name + self.ident().unwrap_or(Ident::invalid()).name } // #[attribute(name = "value")] diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 8adee4be75f..37c49112dca 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -13,7 +13,7 @@ use crate::parse::{Directory, ParseSess}; use crate::parse::parser::Parser; use crate::parse::token::{self, NtTT}; use crate::parse::token::Token::*; -use crate::symbol::{Symbol, keywords, sym}; +use crate::symbol::{Symbol, kw, sym}; use crate::tokenstream::{DelimSpan, TokenStream, TokenTree}; use errors::FatalError; @@ -1046,8 +1046,8 @@ fn is_in_follow(tok: "ed::TokenTree, frag: &str) -> IsInFollow { match *tok { TokenTree::Token(_, ref tok) => match *tok { FatArrow | Comma | Eq | BinOp(token::Or) => IsInFollow::Yes, - Ident(i, false) if i.name == keywords::If.name() || - i.name == keywords::In.name() => IsInFollow::Yes, + Ident(i, false) if i.name == kw::If || + i.name == kw::In => IsInFollow::Yes, _ => IsInFollow::No(tokens), }, _ => IsInFollow::No(tokens), @@ -1064,8 +1064,8 @@ fn is_in_follow(tok: "ed::TokenTree, frag: &str) -> IsInFollow { OpenDelim(token::DelimToken::Bracket) | Comma | FatArrow | Colon | Eq | Gt | BinOp(token::Shr) | Semi | BinOp(token::Or) => IsInFollow::Yes, - Ident(i, false) if i.name == keywords::As.name() || - i.name == keywords::Where.name() => IsInFollow::Yes, + Ident(i, false) if i.name == kw::As || + i.name == kw::Where => IsInFollow::Yes, _ => IsInFollow::No(tokens), }, TokenTree::MetaVarDecl(_, _, frag) if frag.name == sym::block => @@ -1092,7 +1092,7 @@ fn is_in_follow(tok: "ed::TokenTree, frag: &str) -> IsInFollow { match *tok { TokenTree::Token(_, ref tok) => match *tok { Comma => IsInFollow::Yes, - Ident(i, is_raw) if is_raw || i.name != keywords::Priv.name() => + Ident(i, is_raw) if is_raw || i.name != kw::Priv => IsInFollow::Yes, ref tok => if tok.can_begin_type() { IsInFollow::Yes diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 5b1a9bb739f..57a6656140f 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -22,7 +22,7 @@ use crate::source_map::Spanned; use crate::edition::{ALL_EDITIONS, Edition}; use crate::visit::{self, FnKind, Visitor}; use crate::parse::{token, ParseSess}; -use crate::symbol::{Symbol, keywords, sym}; +use crate::symbol::{Symbol, kw, sym}; use crate::tokenstream::TokenTree; use errors::{DiagnosticBuilder, Handler}; @@ -1948,7 +1948,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { fn visit_item(&mut self, i: &'a ast::Item) { match i.node { ast::ItemKind::Const(_,_) => { - if i.ident.name == keywords::Underscore.name() { + if i.ident.name == kw::Underscore { gate_feature_post!(&self, underscore_const_names, i.span, "naming constants with `_` is unstable"); } diff --git a/src/libsyntax/parse/diagnostics.rs b/src/libsyntax/parse/diagnostics.rs index 1a2393be806..d48fcbbd672 100644 --- a/src/libsyntax/parse/diagnostics.rs +++ b/src/libsyntax/parse/diagnostics.rs @@ -6,7 +6,7 @@ use crate::parse::PResult; use crate::parse::Parser; use crate::print::pprust; use crate::ptr::P; -use crate::symbol::keywords; +use crate::symbol::kw; use crate::ThinVec; use errors::{Applicability, DiagnosticBuilder}; use syntax_pos::Span; @@ -405,7 +405,7 @@ impl<'a> Parser<'a> { /// Recover from `pub` keyword in places where it seems _reasonable_ but isn't valid. crate fn eat_bad_pub(&mut self) { - if self.token.is_keyword(keywords::Pub) { + if self.token.is_keyword(kw::Pub) { match self.parse_visibility(false) { Ok(vis) => { self.diagnostic() diff --git a/src/libsyntax/parse/literal.rs b/src/libsyntax/parse/literal.rs index 6db1a669493..f277f0522b8 100644 --- a/src/libsyntax/parse/literal.rs +++ b/src/libsyntax/parse/literal.rs @@ -43,8 +43,8 @@ impl LitKind { Some(match lit { token::Bool(i) => { - assert!(i == keywords::True.name() || i == keywords::False.name()); - LitKind::Bool(i == keywords::True.name()) + assert!(i == kw::True || i == kw::False); + LitKind::Bool(i == kw::True) } token::Byte(i) => { match unescape_byte(&i.as_str()) { @@ -156,8 +156,8 @@ impl LitKind { } LitKind::FloatUnsuffixed(symbol) => (token::Lit::Float(symbol), None), LitKind::Bool(value) => { - let kw = if value { keywords::True } else { keywords::False }; - (token::Lit::Bool(kw.name()), None) + let kw = if value { kw::True } else { kw::False }; + (token::Lit::Bool(kw), None) } LitKind::Err(val) => (token::Lit::Err(val), None), } @@ -175,8 +175,7 @@ impl Lit { diag: Option<(Span, &Handler)>, ) -> Option { let (token, suffix) = match *token { - token::Ident(ident, false) if ident.name == keywords::True.name() || - ident.name == keywords::False.name() => + token::Ident(ident, false) if ident.name == kw::True || ident.name == kw::False => (token::Bool(ident.name), None), token::Literal(token, suffix) => (token, suffix), diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs index 206320f58b7..d1e11da4e7c 100644 --- a/src/libsyntax/test.rs +++ b/src/libsyntax/test.rs @@ -215,7 +215,7 @@ fn mk_reexport_mod(cx: &mut TestCtxt<'_>, tests: Vec, tested_submods: Vec<(Ident, Ident)>) -> (P, Ident) { - let super_ = Ident::with_empty_ctxt(keywords::Super.name()); + let super_ = Ident::with_empty_ctxt(kw::Super); let items = tests.into_iter().map(|r| { cx.ext_cx.item_use_simple(DUMMY_SP, dummy_spanned(ast::VisibilityKind::Public), diff --git a/src/libsyntax_pos/symbol.rs b/src/libsyntax_pos/symbol.rs index f9f0b097f7a..070bb1d8ef7 100644 --- a/src/libsyntax_pos/symbol.rs +++ b/src/libsyntax_pos/symbol.rs @@ -951,6 +951,11 @@ impl Symbol { fn is_unused_keyword_2018(self) -> bool { self >= kw::Async && self <= kw::Try } + + /// Used for sanity checking rustdoc keyword sections. + pub fn is_doc_keyword(self) -> bool { + self <= kw::Union + } } impl Ident { -- cgit 1.4.1-3-g733a5