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/ast.rs | 12 +- src/libsyntax/attr/mod.rs | 8 +- src/libsyntax/diagnostics/plugin.rs | 4 +- src/libsyntax/ext/base.rs | 4 +- src/libsyntax/ext/build.rs | 6 +- src/libsyntax/ext/expand.rs | 18 +- src/libsyntax/ext/placeholders.rs | 4 +- src/libsyntax/ext/tt/macro_parser.rs | 8 +- src/libsyntax/ext/tt/macro_rules.rs | 2 +- src/libsyntax/ext/tt/quoted.rs | 10 +- src/libsyntax/mut_visit.rs | 4 +- src/libsyntax/parse/literal.rs | 2 +- src/libsyntax/parse/parser.rs | 415 ++++++++++++++++++----------------- src/libsyntax/parse/token.rs | 74 +++---- src/libsyntax/print/pprust.rs | 14 +- src/libsyntax/std_inject.rs | 6 +- src/libsyntax/test.rs | 6 +- src/libsyntax/util/parser.rs | 4 +- 18 files changed, 302 insertions(+), 299 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index da45bc1d9f5..0df00e7f441 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -10,7 +10,7 @@ use crate::parse::token; use crate::print::pprust; use crate::ptr::P; use crate::source_map::{dummy_spanned, respan, Spanned}; -use crate::symbol::{keywords, Symbol}; +use crate::symbol::{kw, Symbol}; use crate::tokenstream::TokenStream; use crate::ThinVec; @@ -65,7 +65,7 @@ impl fmt::Debug for Lifetime { pub struct Path { pub span: Span, /// The segments in the path: the things separated by `::`. - /// Global paths begin with `keywords::PathRoot`. + /// Global paths begin with `kw::PathRoot`. pub segments: Vec, } @@ -100,7 +100,7 @@ impl Path { } pub fn is_global(&self) -> bool { - !self.segments.is_empty() && self.segments[0].ident.name == keywords::PathRoot.name() + !self.segments.is_empty() && self.segments[0].ident.name == kw::PathRoot } } @@ -128,7 +128,7 @@ impl PathSegment { PathSegment { ident, id: DUMMY_NODE_ID, args: None } } pub fn path_root(span: Span) -> Self { - PathSegment::from_ident(Ident::new(keywords::PathRoot.name(), span)) + PathSegment::from_ident(Ident::new(kw::PathRoot, span)) } } @@ -1782,7 +1782,7 @@ pub type ExplicitSelf = Spanned; impl Arg { pub fn to_self(&self) -> Option { if let PatKind::Ident(BindingMode::ByValue(mutbl), ident, _) = self.pat.node { - if ident.name == keywords::SelfLower.name() { + if ident.name == kw::SelfLower { return match self.ty.node { TyKind::ImplicitSelf => Some(respan(self.pat.span, SelfKind::Value(mutbl))), TyKind::Rptr(lt, MutTy { ref ty, mutbl }) if ty.node.is_implicit_self() => { @@ -1800,7 +1800,7 @@ impl Arg { pub fn is_self(&self) -> bool { if let PatKind::Ident(_, ident, _) = self.pat.node { - ident.name == keywords::SelfLower.name() + ident.name == kw::SelfLower } else { false } diff --git a/src/libsyntax/attr/mod.rs b/src/libsyntax/attr/mod.rs index 592b40df176..3e93ac23a53 100644 --- a/src/libsyntax/attr/mod.rs +++ b/src/libsyntax/attr/mod.rs @@ -22,7 +22,7 @@ use crate::parse::parser::Parser; use crate::parse::{self, ParseSess, PResult}; use crate::parse::token::{self, Token}; use crate::ptr::P; -use crate::symbol::{keywords, Symbol, sym}; +use crate::symbol::{kw, sym, Symbol}; use crate::ThinVec; use crate::tokenstream::{TokenStream, TokenTree, DelimSpan}; use crate::GLOBALS; @@ -90,7 +90,7 @@ impl NestedMetaItem { self.meta_item().and_then(|meta_item| meta_item.ident()) } pub fn name_or_empty(&self) -> Symbol { - self.ident().unwrap_or(keywords::Invalid.ident()).name + self.ident().unwrap_or(Ident::invalid()).name } /// Gets the string value if self is a MetaItem and the MetaItem is a @@ -168,7 +168,7 @@ impl Attribute { } } pub fn name_or_empty(&self) -> Symbol { - self.ident().unwrap_or(keywords::Invalid.ident()).name + self.ident().unwrap_or(Ident::invalid()).name } pub fn value_str(&self) -> Option { @@ -206,7 +206,7 @@ impl MetaItem { } } pub fn name_or_empty(&self) -> Symbol { - self.ident().unwrap_or(keywords::Invalid.ident()).name + self.ident().unwrap_or(Ident.invalid()).name } // #[attribute(name = "value")] diff --git a/src/libsyntax/diagnostics/plugin.rs b/src/libsyntax/diagnostics/plugin.rs index c988dc61bec..e9476e2fdfd 100644 --- a/src/libsyntax/diagnostics/plugin.rs +++ b/src/libsyntax/diagnostics/plugin.rs @@ -7,7 +7,7 @@ use crate::ext::base::{ExtCtxt, MacEager, MacResult}; use crate::ext::build::AstBuilder; use crate::parse::token; use crate::ptr::P; -use crate::symbol::keywords; +use crate::symbol::kw; use crate::tokenstream::{TokenTree}; use smallvec::smallvec; @@ -185,7 +185,7 @@ pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt<'_>, (descriptions.len(), ecx.expr_vec(span, descriptions)) }); - let static_ = ecx.lifetime(span, keywords::StaticLifetime.ident()); + let static_ = ecx.lifetime(span, Ident::with_empty_ctxt(kw::StaticLifetime)); let ty_str = ecx.ty_rptr( span, ecx.ty_ident(span, ecx.ident_of("str")), diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 489fac4f1ca..f1a20d54065 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -10,7 +10,7 @@ use crate::mut_visit::{self, MutVisitor}; use crate::parse::{self, parser, DirectoryOwnership}; use crate::parse::token; use crate::ptr::P; -use crate::symbol::{keywords, Ident, Symbol, sym}; +use crate::symbol::{kw, sym, Ident, Symbol}; use crate::ThinVec; use crate::tokenstream::{self, TokenStream}; @@ -971,7 +971,7 @@ impl<'a> ExtCtxt<'a> { } pub fn std_path(&self, components: &[&str]) -> Vec { let def_site = DUMMY_SP.apply_mark(self.current_expansion.mark); - iter::once(Ident::new(keywords::DollarCrate.name(), def_site)) + iter::once(Ident::new(kw::DollarCrate, def_site)) .chain(components.iter().map(|s| self.ident_of(s))) .collect() } diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index d24106f697e..77d5fa238f2 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -3,7 +3,7 @@ use crate::attr; use crate::source_map::{dummy_spanned, respan, Spanned}; use crate::ext::base::ExtCtxt; use crate::ptr::P; -use crate::symbol::{Symbol, keywords}; +use crate::symbol::{Symbol, kw}; use crate::ThinVec; use rustc_target::spec::abi::Abi; @@ -628,7 +628,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { self.expr_path(self.path_ident(span, id)) } fn expr_self(&self, span: Span) -> P { - self.expr_ident(span, keywords::SelfLower.ident()) + self.expr_ident(span, Ident::with_empty_ctxt(kw::SelfLower)) } fn expr_binary(&self, sp: Span, op: ast::BinOpKind, @@ -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: keywords::Invalid.ident(), + ident: Ident::with_empty_ctxt(kw::Invalid), attrs: vec![], node: ast::ItemKind::Use(vp), vis, diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 478ae4de82b..5f9c84c6126 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -14,7 +14,7 @@ use crate::parse::token::{self, Token}; use crate::parse::parser::Parser; use crate::ptr::P; use crate::symbol::Symbol; -use crate::symbol::{keywords, sym}; +use crate::symbol::{kw, sym}; use crate::tokenstream::{TokenStream, TokenTree}; use crate::visit::{self, Visitor}; use crate::util::map_in_place::MapInPlace; @@ -198,7 +198,7 @@ fn macro_bang_format(path: &ast::Path) -> ExpnFormat { if i != 0 { path_str.push_str("::"); } - if segment.ident.name != keywords::PathRoot.name() { + if segment.ident.name != kw::PathRoot { path_str.push_str(&segment.ident.as_str()) } } @@ -271,7 +271,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { attrs: krate.attrs, span: krate.span, node: ast::ItemKind::Mod(krate.module), - ident: keywords::Invalid.ident(), + ident: Ident::with_empty_ctxt(kw::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(|| keywords::Invalid.ident()); + let ident = ident.unwrap_or_else(|| Ident::with_empty_ctxt(kw::Invalid)); let validate_and_set_expn_info = |this: &mut Self, // arg instead of capture def_site_span: Option, allow_internal_unstable, @@ -736,7 +736,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } } - if ident.name != keywords::Invalid.name() { + if ident.name != kw::Invalid { let msg = format!("macro {}! expects no ident argument, given '{}'", path, ident); this.cx.span_err(path.span, &msg); this.cx.trace_macros_diag(); @@ -792,7 +792,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } IdentTT { ref expander, span: tt_span, ref allow_internal_unstable } => { - if ident.name == keywords::Invalid.name() { + if ident.name == kw::Invalid { self.cx.span_err(path.span, &format!("macro {}! expects an ident argument", path)); self.cx.trace_macros_diag(); @@ -828,7 +828,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { } SyntaxExtension::ProcMacro { ref expander, ref allow_internal_unstable, edition } => { - if ident.name != keywords::Invalid.name() { + if ident.name != kw::Invalid { let msg = format!("macro {}! expects no ident argument, given '{}'", path, ident); self.cx.span_err(path.span, &msg); @@ -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(keywords::Invalid.ident()), + path: Path::from_ident(Ident::with_empty_ctxt(kw::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 == keywords::Invalid.ident() { + if item.ident == Ident::with_empty_ctxt(kw::Invalid) { return noop_flat_map_item(item, self); } diff --git a/src/libsyntax/ext/placeholders.rs b/src/libsyntax/ext/placeholders.rs index f5e18e98436..9ba1ff0ec7e 100644 --- a/src/libsyntax/ext/placeholders.rs +++ b/src/libsyntax/ext/placeholders.rs @@ -6,7 +6,7 @@ use crate::ext::hygiene::Mark; use crate::tokenstream::TokenStream; use crate::mut_visit::*; use crate::ptr::P; -use crate::symbol::keywords; +use crate::symbol::kw; use crate::ThinVec; use smallvec::{smallvec, SmallVec}; @@ -22,7 +22,7 @@ pub fn placeholder(kind: AstFragmentKind, id: ast::NodeId) -> AstFragment { }) } - let ident = keywords::Invalid.ident(); + let ident = ast::Ident::with_empty_ctxt(kw::Invalid); let attrs = Vec::new(); let generics = ast::Generics::default(); let vis = dummy_spanned(ast::VisibilityKind::Inherited); diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 084a69f4cda..fa1f85c0e7b 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -80,7 +80,7 @@ use crate::parse::{Directory, ParseSess}; use crate::parse::parser::{Parser, PathStyle}; use crate::parse::token::{self, DocComment, Nonterminal, Token}; use crate::print::pprust; -use crate::symbol::keywords; +use crate::symbol::kw; use crate::tokenstream::{DelimSpan, TokenStream}; use errors::FatalError; @@ -382,7 +382,7 @@ fn nameize>( TokenTree::Delimited(_, ref delim) => for next_m in &delim.tts { n_rec(sess, next_m, res.by_ref(), ret_val)?; }, - TokenTree::MetaVarDecl(span, _, id) if id.name == keywords::Invalid.name() => { + TokenTree::MetaVarDecl(span, _, id) if id.name == kw::Invalid => { if sess.missing_fragment_specifiers.borrow_mut().remove(&span) { return Err((span, "missing fragment specifier".to_string())); } @@ -587,7 +587,7 @@ fn inner_parse_loop<'root, 'tt>( } // We need to match a metavar (but the identifier is invalid)... this is an error - TokenTree::MetaVarDecl(span, _, id) if id.name == keywords::Invalid.name() => { + TokenTree::MetaVarDecl(span, _, id) if id.name == kw::Invalid => { if sess.missing_fragment_specifiers.borrow_mut().remove(&span) { return Error(span, "missing fragment specifier".to_string()); } @@ -802,7 +802,7 @@ pub fn parse( /// We prohibit passing `_` to macros expecting `ident` for now. fn get_macro_ident(token: &Token) -> Option<(Ident, bool)> { match *token { - token::Ident(ident, is_raw) if ident.name != keywords::Underscore.name() => + token::Ident(ident, is_raw) if ident.name != kw::Underscore => Some((ident, is_raw)), _ => None, } diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 672b7b42855..8adee4be75f 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -1107,7 +1107,7 @@ fn is_in_follow(tok: "ed::TokenTree, frag: &str) -> IsInFollow { _ => IsInFollow::No(tokens), } }, - "" => IsInFollow::Yes, // keywords::Invalid + "" => IsInFollow::Yes, // kw::Invalid _ => IsInFollow::Invalid(format!("invalid fragment specifier `{}`", frag), VALID_FRAGMENT_NAMES_MSG), } diff --git a/src/libsyntax/ext/tt/quoted.rs b/src/libsyntax/ext/tt/quoted.rs index ed8395f11ad..2f1a9834674 100644 --- a/src/libsyntax/ext/tt/quoted.rs +++ b/src/libsyntax/ext/tt/quoted.rs @@ -6,7 +6,7 @@ use crate::parse::{token, ParseSess}; use crate::print::pprust; use crate::tokenstream::{self, DelimSpan}; use crate::ast; -use crate::symbol::keywords; +use crate::symbol::kw; use syntax_pos::{edition::Edition, BytePos, Span}; @@ -228,7 +228,7 @@ pub fn parse( result.push(TokenTree::MetaVarDecl( span, ident, - keywords::Invalid.ident(), + ast::Ident::with_empty_ctxt(kw::Invalid), )); } @@ -319,8 +319,8 @@ where Some(tokenstream::TokenTree::Token(ident_span, ref token)) if token.is_ident() => { let (ident, is_raw) = token.ident().unwrap(); let span = ident_span.with_lo(span.lo()); - if ident.name == keywords::Crate.name() && !is_raw { - let ident = ast::Ident::new(keywords::DollarCrate.name(), ident.span); + if ident.name == kw::Crate && !is_raw { + let ident = ast::Ident::new(kw::DollarCrate, ident.span); TokenTree::Token(span, token::Ident(ident, is_raw)) } else { TokenTree::MetaVar(span, ident) @@ -334,7 +334,7 @@ where pprust::token_to_string(&tok) ); sess.span_diagnostic.span_err(span, &msg); - TokenTree::MetaVar(span, keywords::Invalid.ident()) + TokenTree::MetaVar(span, ast::Ident::with_empty_ctxt(kw::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 8fdd15a029f..8dae4756c82 100644 --- a/src/libsyntax/mut_visit.rs +++ b/src/libsyntax/mut_visit.rs @@ -11,7 +11,7 @@ use crate::ast::*; use crate::source_map::{Spanned, respan}; use crate::parse::token::{self, Token}; use crate::ptr::P; -use crate::symbol::keywords; +use crate::symbol::kw; use crate::ThinVec; use crate::tokenstream::*; use crate::util::map_in_place::MapInPlace; @@ -977,7 +977,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: keywords::Invalid.ident(), + ident: Ident::with_empty_ctxt(kw::Invalid), attrs, id: DUMMY_NODE_ID, vis: respan(span.shrink_to_lo(), VisibilityKind::Public), diff --git a/src/libsyntax/parse/literal.rs b/src/libsyntax/parse/literal.rs index 53195421ddc..6db1a669493 100644 --- a/src/libsyntax/parse/literal.rs +++ b/src/libsyntax/parse/literal.rs @@ -6,7 +6,7 @@ use crate::parse::PResult; use crate::parse::token::{self, Token}; use crate::parse::unescape::{unescape_str, unescape_char, unescape_byte_str, unescape_byte}; use crate::print::pprust; -use crate::symbol::{keywords, Symbol}; +use crate::symbol::{kw, Symbol}; use crate::tokenstream::{TokenStream, TokenTree}; use errors::{Applicability, Handler}; diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 24d120376de..0099dd3d3c8 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -46,7 +46,7 @@ use crate::ptr::P; use crate::parse::PResult; use crate::ThinVec; use crate::tokenstream::{self, DelimSpan, TokenTree, TokenStream, TreeAndJoint}; -use crate::symbol::{keywords, sym, Symbol}; +use crate::symbol::{kw, sym, Symbol}; use errors::{Applicability, DiagnosticBuilder, DiagnosticId, FatalError}; use rustc_target::spec::abi::{self, Abi}; @@ -379,7 +379,7 @@ impl TokenCursor { #[derive(Clone, PartialEq)] crate enum TokenType { Token(token::Token), - Keyword(keywords::Keyword), + Keyword(Symbol), Operator, Lifetime, Ident, @@ -392,7 +392,7 @@ impl TokenType { crate fn to_string(&self) -> String { match *self { TokenType::Token(ref t) => format!("`{}`", pprust::token_to_string(t)), - TokenType::Keyword(kw) => format!("`{}`", kw.name()), + TokenType::Keyword(kw) => format!("`{}`", kw), TokenType::Operator => "an operator".to_string(), TokenType::Lifetime => "lifetime".to_string(), TokenType::Ident => "identifier".to_string(), @@ -510,7 +510,7 @@ impl From> for LhsExpr { /// Creates a placeholder argument. fn dummy_arg(span: Span) -> Arg { - let ident = Ident::new(keywords::Invalid.name(), span); + let ident = Ident::new(kw::Invalid, span); let pat = P(Pat { id: ast::DUMMY_NODE_ID, node: PatKind::Ident(BindingMode::ByValue(Mutability::Immutable), ident, None), @@ -771,15 +771,15 @@ impl<'a> Parser<'a> { TokenType::Token(token::Semi) => true, // we expect a `;` here _ => false, }) && ( // a `;` would be expected before the current keyword - self.token.is_keyword(keywords::Break) || - self.token.is_keyword(keywords::Continue) || - self.token.is_keyword(keywords::For) || - self.token.is_keyword(keywords::If) || - self.token.is_keyword(keywords::Let) || - self.token.is_keyword(keywords::Loop) || - self.token.is_keyword(keywords::Match) || - self.token.is_keyword(keywords::Return) || - self.token.is_keyword(keywords::While) + self.token.is_keyword(kw::Break) || + self.token.is_keyword(kw::Continue) || + self.token.is_keyword(kw::For) || + self.token.is_keyword(kw::If) || + self.token.is_keyword(kw::Let) || + self.token.is_keyword(kw::Loop) || + self.token.is_keyword(kw::Match) || + self.token.is_keyword(kw::Return) || + self.token.is_keyword(kw::While) ); let cm = self.sess.source_map(); match (cm.lookup_line(self.span.lo()), cm.lookup_line(sp.lo())) { @@ -913,14 +913,14 @@ impl<'a> Parser<'a> { is_present } - fn check_keyword(&mut self, kw: keywords::Keyword) -> bool { + fn check_keyword(&mut self, kw: Symbol) -> bool { self.expected_tokens.push(TokenType::Keyword(kw)); self.token.is_keyword(kw) } /// If the next token is the given keyword, eats it and returns /// `true`. Otherwise, returns `false`. - pub fn eat_keyword(&mut self, kw: keywords::Keyword) -> bool { + pub fn eat_keyword(&mut self, kw: Symbol) -> bool { if self.check_keyword(kw) { self.bump(); true @@ -929,7 +929,7 @@ impl<'a> Parser<'a> { } } - fn eat_keyword_noexpect(&mut self, kw: keywords::Keyword) -> bool { + fn eat_keyword_noexpect(&mut self, kw: Symbol) -> bool { if self.token.is_keyword(kw) { self.bump(); true @@ -941,7 +941,7 @@ impl<'a> Parser<'a> { /// If the given word is not a keyword, signals an error. /// If the next token is not the given word, signals an error. /// Otherwise, eats it. - fn expect_keyword(&mut self, kw: keywords::Keyword) -> PResult<'a, ()> { + fn expect_keyword(&mut self, kw: Symbol) -> PResult<'a, ()> { if !self.eat_keyword(kw) { self.unexpected() } else { @@ -1375,9 +1375,9 @@ impl<'a> Parser<'a> { /// Is the current token one of the keywords that signals a bare function type? fn token_is_bare_fn_keyword(&mut self) -> bool { - self.check_keyword(keywords::Fn) || - self.check_keyword(keywords::Unsafe) || - self.check_keyword(keywords::Extern) + self.check_keyword(kw::Fn) || + self.check_keyword(kw::Unsafe) || + self.check_keyword(kw::Extern) } /// Parses a `TyKind::BareFn` type. @@ -1395,13 +1395,13 @@ impl<'a> Parser<'a> { */ let unsafety = self.parse_unsafety(); - let abi = if self.eat_keyword(keywords::Extern) { + let abi = if self.eat_keyword(kw::Extern) { self.parse_opt_abi()?.unwrap_or(Abi::C) } else { Abi::Rust }; - self.expect_keyword(keywords::Fn)?; + self.expect_keyword(kw::Fn)?; let (inputs, c_variadic) = self.parse_fn_args(false, true)?; let ret_ty = self.parse_ret_ty(false)?; let decl = P(FnDecl { @@ -1419,7 +1419,7 @@ impl<'a> Parser<'a> { /// Parses asyncness: `async` or nothing. fn parse_asyncness(&mut self) -> IsAsync { - if self.eat_keyword(keywords::Async) { + if self.eat_keyword(kw::Async) { IsAsync::Async { closure_id: ast::DUMMY_NODE_ID, return_impl_trait_id: ast::DUMMY_NODE_ID, @@ -1432,7 +1432,7 @@ impl<'a> Parser<'a> { /// Parses unsafety: `unsafe` or nothing. fn parse_unsafety(&mut self) -> Unsafety { - if self.eat_keyword(keywords::Unsafe) { + if self.eat_keyword(kw::Unsafe) { Unsafety::Unsafe } else { Unsafety::Normal @@ -1462,10 +1462,10 @@ impl<'a> Parser<'a> { mut attrs: Vec) -> PResult<'a, TraitItem> { let lo = self.span; self.eat_bad_pub(); - let (name, node, generics) = if self.eat_keyword(keywords::Type) { + let (name, node, generics) = if self.eat_keyword(kw::Type) { self.parse_trait_item_assoc_ty()? } else if self.is_const_item() { - self.expect_keyword(keywords::Const)?; + self.expect_keyword(kw::Const)?; let ident = self.parse_ident()?; self.expect(&token::Colon)?; let ty = self.parse_ty()?; @@ -1480,7 +1480,9 @@ 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. - (keywords::Invalid.ident(), ast::TraitItemKind::Macro(mac), ast::Generics::default()) + (Ident::with_empty_ctxt(kw::Invalid), + ast::TraitItemKind::Macro(mac), + ast::Generics::default()) } else { let (constness, unsafety, mut asyncness, abi) = self.parse_fn_front_matter()?; @@ -1654,7 +1656,7 @@ impl<'a> Parser<'a> { // Reference self.expect_and()?; self.parse_borrowed_pointee()? - } else if self.eat_keyword_noexpect(keywords::Typeof) { + } else if self.eat_keyword_noexpect(kw::Typeof) { // `typeof(EXPR)` // In order to not be ambiguous, the type must be surrounded by parens. self.expect(&token::OpenDelim(token::Paren))?; @@ -1664,13 +1666,13 @@ impl<'a> Parser<'a> { }; self.expect(&token::CloseDelim(token::Paren))?; TyKind::Typeof(e) - } else if self.eat_keyword(keywords::Underscore) { + } else if self.eat_keyword(kw::Underscore) { // A type to be inferred `_` TyKind::Infer } else if self.token_is_bare_fn_keyword() { // Function pointer type self.parse_ty_bare_fn(Vec::new())? - } else if self.check_keyword(keywords::For) { + } else if self.check_keyword(kw::For) { // Function pointer type or bound list (trait object type) starting with a poly-trait. // `for<'lt> [unsafe] [extern "ABI"] fn (&'lt S) -> T` // `for<'lt> Trait1<'lt> + Trait2 + 'a` @@ -1683,12 +1685,12 @@ impl<'a> Parser<'a> { let parse_plus = allow_plus && self.check_plus(); self.parse_remaining_bounds(lifetime_defs, path, lo, parse_plus)? } - } else if self.eat_keyword(keywords::Impl) { + } else if self.eat_keyword(kw::Impl) { // Always parse bounds greedily for better error recovery. let bounds = self.parse_generic_bounds(None)?; impl_dyn_multi = bounds.len() > 1 || self.prev_token_kind == PrevTokenKind::Plus; TyKind::ImplTrait(ast::DUMMY_NODE_ID, bounds) - } else if self.check_keyword(keywords::Dyn) && + } else if self.check_keyword(kw::Dyn) && (self.span.rust_2018() || self.look_ahead(1, |t| t.can_begin_bound() && !can_continue_type_after_non_fn_ident(t))) { @@ -1766,9 +1768,9 @@ impl<'a> Parser<'a> { } fn parse_ptr(&mut self) -> PResult<'a, MutTy> { - let mutbl = if self.eat_keyword(keywords::Mut) { + let mutbl = if self.eat_keyword(kw::Mut) { Mutability::Mutable - } else if self.eat_keyword(keywords::Const) { + } else if self.eat_keyword(kw::Const) { Mutability::Immutable } else { let span = self.prev_span; @@ -1790,7 +1792,7 @@ impl<'a> Parser<'a> { _ => 0, } token::BinOp(token::And) | token::AndAnd => 1, - _ if self.token.is_keyword(keywords::Mut) => 1, + _ if self.token.is_keyword(kw::Mut) => 1, _ => 0, }; @@ -1891,7 +1893,7 @@ impl<'a> Parser<'a> { } match ty { Ok(ty) => { - let ident = Ident::new(keywords::Invalid.name(), self.prev_span); + let ident = Ident::new(kw::Invalid, self.prev_span); let pat = P(Pat { id: ast::DUMMY_NODE_ID, node: PatKind::Ident( @@ -2006,7 +2008,7 @@ impl<'a> Parser<'a> { fn parse_ident_or_underscore(&mut self) -> PResult<'a, ast::Ident> { match self.token { - token::Ident(ident, false) if ident.name == keywords::Underscore.name() => { + token::Ident(ident, false) if ident.name == kw::Underscore => { let span = self.span; self.bump(); Ok(Ident::new(ident.name, span)) @@ -2034,7 +2036,7 @@ impl<'a> Parser<'a> { // above). `path_span` has the span of that path, or an empty // span in the case of something like `::Bar`. let (mut path, path_span); - if self.eat_keyword(keywords::As) { + if self.eat_keyword(kw::As) { let path_lo = self.span; path = self.parse_path(PathStyle::Type)?; path_span = path_lo.to(self.prev_span); @@ -2233,7 +2235,7 @@ impl<'a> Parser<'a> { /// Parses mutability (`mut` or nothing). fn parse_mutability(&mut self) -> Mutability { - if self.eat_keyword(keywords::Mut) { + if self.eat_keyword(kw::Mut) { Mutability::Mutable } else { Mutability::Immutable @@ -2465,37 +2467,37 @@ impl<'a> Parser<'a> { hi = path.span; return Ok(self.mk_expr(lo.to(hi), ExprKind::Path(Some(qself), path), attrs)); } - if self.span.rust_2018() && self.check_keyword(keywords::Async) { + if self.span.rust_2018() && self.check_keyword(kw::Async) { return if self.is_async_block() { // check for `async {` and `async move {` self.parse_async_block(attrs) } else { self.parse_lambda_expr(attrs) }; } - if self.check_keyword(keywords::Move) || self.check_keyword(keywords::Static) { + if self.check_keyword(kw::Move) || self.check_keyword(kw::Static) { return self.parse_lambda_expr(attrs); } - if self.eat_keyword(keywords::If) { + if self.eat_keyword(kw::If) { return self.parse_if_expr(attrs); } - if self.eat_keyword(keywords::For) { + if self.eat_keyword(kw::For) { let lo = self.prev_span; return self.parse_for_expr(None, lo, attrs); } - if self.eat_keyword(keywords::While) { + if self.eat_keyword(kw::While) { let lo = self.prev_span; return self.parse_while_expr(None, lo, attrs); } if let Some(label) = self.eat_label() { let lo = label.ident.span; self.expect(&token::Colon)?; - if self.eat_keyword(keywords::While) { + if self.eat_keyword(kw::While) { return self.parse_while_expr(Some(label), lo, attrs) } - if self.eat_keyword(keywords::For) { + if self.eat_keyword(kw::For) { return self.parse_for_expr(Some(label), lo, attrs) } - if self.eat_keyword(keywords::Loop) { + if self.eat_keyword(kw::Loop) { return self.parse_loop_expr(Some(label), lo, attrs) } if self.token == token::OpenDelim(token::Brace) { @@ -2509,24 +2511,24 @@ impl<'a> Parser<'a> { err.span_label(self.span, msg); return Err(err); } - if self.eat_keyword(keywords::Loop) { + if self.eat_keyword(kw::Loop) { let lo = self.prev_span; return self.parse_loop_expr(None, lo, attrs); } - if self.eat_keyword(keywords::Continue) { + if self.eat_keyword(kw::Continue) { let label = self.eat_label(); let ex = ExprKind::Continue(label); let hi = self.prev_span; return Ok(self.mk_expr(lo.to(hi), ex, attrs)); } - if self.eat_keyword(keywords::Match) { + if self.eat_keyword(kw::Match) { let match_sp = self.prev_span; return self.parse_match_expr(attrs).map_err(|mut err| { err.span_label(match_sp, "while parsing this match expression"); err }); } - if self.eat_keyword(keywords::Unsafe) { + if self.eat_keyword(kw::Unsafe) { return self.parse_block_expr( None, lo, @@ -2540,10 +2542,10 @@ impl<'a> Parser<'a> { } if self.is_try_block() { let lo = self.span; - assert!(self.eat_keyword(keywords::Try)); + assert!(self.eat_keyword(kw::Try)); return self.parse_try_block(lo, attrs); } - if self.eat_keyword(keywords::Return) { + if self.eat_keyword(kw::Return) { if self.token.can_begin_expr() { let e = self.parse_expr()?; hi = e.span; @@ -2551,7 +2553,7 @@ impl<'a> Parser<'a> { } else { ex = ExprKind::Ret(None); } - } else if self.eat_keyword(keywords::Break) { + } else if self.eat_keyword(kw::Break) { let label = self.eat_label(); let e = if self.token.can_begin_expr() && !(self.token == token::OpenDelim(token::Brace) @@ -2563,7 +2565,7 @@ impl<'a> Parser<'a> { }; ex = ExprKind::Break(label, e); hi = self.prev_span; - } else if self.eat_keyword(keywords::Yield) { + } else if self.eat_keyword(kw::Yield) { if self.token.can_begin_expr() { let e = self.parse_expr()?; hi = e.span; @@ -2571,14 +2573,14 @@ impl<'a> Parser<'a> { } else { ex = ExprKind::Yield(None); } - } else if self.token.is_keyword(keywords::Let) { + } else if self.token.is_keyword(kw::Let) { // Catch this syntax error here, instead of in `parse_ident`, so // that we can explicitly mention that let is not to be used as an expression let mut db = self.fatal("expected expression, found statement (`let`)"); db.span_label(self.span, "expected expression"); db.note("variable declaration using `let` is a statement"); return Err(db); - } else if self.span.rust_2018() && self.eat_keyword(keywords::Await) { + } else if self.span.rust_2018() && self.eat_keyword(kw::Await) { let (await_hi, e_kind) = self.parse_await_macro_or_alt(lo, self.prev_span)?; hi = await_hi; ex = e_kind; @@ -2879,7 +2881,7 @@ impl<'a> Parser<'a> { // Assuming we have just parsed `.`, continue parsing into an expression. fn parse_dot_suffix(&mut self, self_arg: P, lo: Span) -> PResult<'a, P> { - if self.span.rust_2018() && self.eat_keyword(keywords::Await) { + if self.span.rust_2018() && self.eat_keyword(kw::Await) { let span = lo.to(self.prev_span); let await_expr = self.mk_expr( span, @@ -3250,7 +3252,7 @@ impl<'a> Parser<'a> { let (span, e) = self.interpolated_or_expr_span(e)?; (lo.to(span), ExprKind::AddrOf(m, e)) } - token::Ident(..) if self.token.is_keyword(keywords::In) => { + token::Ident(..) if self.token.is_keyword(kw::In) => { self.bump(); let place = self.parse_expr_res( Restrictions::NO_STRUCT_LITERAL, @@ -3261,7 +3263,7 @@ impl<'a> Parser<'a> { let blk_expr = self.mk_expr(span, ExprKind::Block(blk, None), ThinVec::new()); (lo.to(span), ExprKind::ObsoleteInPlace(place, blk_expr)) } - token::Ident(..) if self.token.is_keyword(keywords::Box) => { + token::Ident(..) if self.token.is_keyword(kw::Box) => { self.bump(); let e = self.parse_prefix_expr(None); let (span, e) = self.interpolated_or_expr_span(e)?; @@ -3683,7 +3685,7 @@ impl<'a> Parser<'a> { /// Parses an `if` or `if let` expression (`if` token already eaten). fn parse_if_expr(&mut self, attrs: ThinVec) -> PResult<'a, P> { - if self.check_keyword(keywords::Let) { + if self.check_keyword(kw::Let) { return self.parse_if_let_expr(attrs); } let lo = self.prev_span; @@ -3693,7 +3695,7 @@ impl<'a> Parser<'a> { // verify that the last statement is either an implicit return (no `;`) or an explicit // return. This won't catch blocks with an explicit `return`, but that would be caught by // the dead code lint. - if self.eat_keyword(keywords::Else) || !cond.returns() { + if self.eat_keyword(kw::Else) || !cond.returns() { let sp = self.sess.source_map().next_point(lo); let mut err = self.diagnostic() .struct_span_err(sp, "missing condition for `if` statemement"); @@ -3709,7 +3711,7 @@ impl<'a> Parser<'a> { })?; let mut els: Option> = None; let mut hi = thn.span; - if self.eat_keyword(keywords::Else) { + if self.eat_keyword(kw::Else) { let elexpr = self.parse_else_expr()?; hi = elexpr.span; els = Some(elexpr); @@ -3721,12 +3723,12 @@ impl<'a> Parser<'a> { fn parse_if_let_expr(&mut self, attrs: ThinVec) -> PResult<'a, P> { let lo = self.prev_span; - self.expect_keyword(keywords::Let)?; + self.expect_keyword(kw::Let)?; let pats = self.parse_pats()?; self.expect(&token::Eq)?; let expr = self.parse_expr_res(Restrictions::NO_STRUCT_LITERAL, None)?; let thn = self.parse_block()?; - let (hi, els) = if self.eat_keyword(keywords::Else) { + let (hi, els) = if self.eat_keyword(kw::Else) { let expr = self.parse_else_expr()?; (expr.span, Some(expr)) } else { @@ -3741,7 +3743,7 @@ impl<'a> Parser<'a> { -> PResult<'a, P> { let lo = self.span; - let movability = if self.eat_keyword(keywords::Static) { + let movability = if self.eat_keyword(kw::Static) { Movability::Static } else { Movability::Movable @@ -3751,7 +3753,7 @@ impl<'a> Parser<'a> { } else { IsAsync::NotAsync }; - let capture_clause = if self.eat_keyword(keywords::Move) { + let capture_clause = if self.eat_keyword(kw::Move) { CaptureBy::Value } else { CaptureBy::Ref @@ -3779,7 +3781,7 @@ impl<'a> Parser<'a> { // `else` token already eaten fn parse_else_expr(&mut self) -> PResult<'a, P> { - if self.eat_keyword(keywords::If) { + if self.eat_keyword(kw::If) { return self.parse_if_expr(ThinVec::new()); } else { let blk = self.parse_block()?; @@ -3794,7 +3796,7 @@ impl<'a> Parser<'a> { // Parse: `for in ` let pat = self.parse_top_level_pat()?; - if !self.eat_keyword(keywords::In) { + if !self.eat_keyword(kw::In) { let in_span = self.prev_span.between(self.span); let mut err = self.sess.span_diagnostic .struct_span_err(in_span, "missing `in` in `for` loop"); @@ -3806,7 +3808,7 @@ impl<'a> Parser<'a> { err.emit(); } let in_span = self.prev_span; - if self.eat_keyword(keywords::In) { + if self.eat_keyword(kw::In) { // a common typo: `for _ in in bar {}` let mut err = self.sess.span_diagnostic.struct_span_err( self.prev_span, @@ -3835,7 +3837,7 @@ impl<'a> Parser<'a> { fn parse_while_expr(&mut self, opt_label: Option