Replace some bool
params with an enum
This commit is contained in:
parent
38b0865248
commit
d86f9cd464
7 changed files with 63 additions and 47 deletions
|
@ -8,6 +8,7 @@ use rustc_ast::ast::*;
|
|||
use rustc_ast::ptr::P;
|
||||
use rustc_ast::token::{self, Delimiter, TokenKind};
|
||||
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
|
||||
use rustc_ast::util::case::Case;
|
||||
use rustc_ast::{self as ast, AttrVec, Attribute, DUMMY_NODE_ID};
|
||||
use rustc_ast::{Async, Const, Defaultness, IsAuto, Mutability, Unsafe, UseTree, UseTreeKind};
|
||||
use rustc_ast::{BindingAnnotation, Block, FnDecl, FnSig, Param, SelfKind};
|
||||
|
@ -34,7 +35,7 @@ impl<'a> Parser<'a> {
|
|||
|
||||
/// Parses a `mod <foo> { ... }` or `mod <foo>;` item.
|
||||
fn parse_item_mod(&mut self, attrs: &mut AttrVec) -> PResult<'a, ItemInfo> {
|
||||
let unsafety = self.parse_unsafety(false);
|
||||
let unsafety = self.parse_unsafety(Case::Sensitive);
|
||||
self.expect_keyword(kw::Mod)?;
|
||||
let id = self.parse_ident()?;
|
||||
let mod_kind = if self.eat(&token::Semi) {
|
||||
|
@ -150,7 +151,7 @@ impl<'a> Parser<'a> {
|
|||
&vis,
|
||||
&mut def,
|
||||
fn_parse_mode,
|
||||
false,
|
||||
Case::Sensitive,
|
||||
)?;
|
||||
if let Some((ident, kind)) = kind {
|
||||
self.error_on_unconsumed_default(def, &kind);
|
||||
|
@ -212,14 +213,14 @@ impl<'a> Parser<'a> {
|
|||
vis: &Visibility,
|
||||
def: &mut Defaultness,
|
||||
fn_parse_mode: FnParseMode,
|
||||
kw_case_insensitive: bool,
|
||||
case: Case,
|
||||
) -> PResult<'a, Option<ItemInfo>> {
|
||||
let def_final = def == &Defaultness::Final;
|
||||
let mut def_ = || mem::replace(def, Defaultness::Final);
|
||||
|
||||
let info = if self.eat_keyword_case(kw::Use, kw_case_insensitive) {
|
||||
let info = if self.eat_keyword_case(kw::Use, case) {
|
||||
self.parse_use_item()?
|
||||
} else if self.check_fn_front_matter(def_final, kw_case_insensitive) {
|
||||
} else if self.check_fn_front_matter(def_final, case) {
|
||||
// FUNCTION ITEM
|
||||
let (ident, sig, generics, body) = self.parse_fn(attrs, fn_parse_mode, lo, vis)?;
|
||||
(ident, ItemKind::Fn(Box::new(Fn { defaultness: def_(), sig, generics, body })))
|
||||
|
@ -233,7 +234,7 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
} else if self.is_unsafe_foreign_mod() {
|
||||
// EXTERN BLOCK
|
||||
let unsafety = self.parse_unsafety(false);
|
||||
let unsafety = self.parse_unsafety(Case::Sensitive);
|
||||
self.expect_keyword(kw::Extern)?;
|
||||
self.parse_item_foreign_mod(attrs, unsafety)?
|
||||
} else if self.is_static_global() {
|
||||
|
@ -242,7 +243,7 @@ impl<'a> Parser<'a> {
|
|||
let m = self.parse_mutability();
|
||||
let (ident, ty, expr) = self.parse_item_global(Some(m))?;
|
||||
(ident, ItemKind::Static(ty, m, expr))
|
||||
} else if let Const::Yes(const_span) = self.parse_constness(false) {
|
||||
} else if let Const::Yes(const_span) = self.parse_constness(Case::Sensitive) {
|
||||
// CONST ITEM
|
||||
if self.token.is_keyword(kw::Impl) {
|
||||
// recover from `const impl`, suggest `impl const`
|
||||
|
@ -294,11 +295,19 @@ impl<'a> Parser<'a> {
|
|||
} else if self.isnt_macro_invocation() && vis.kind.is_pub() {
|
||||
self.recover_missing_kw_before_item()?;
|
||||
return Ok(None);
|
||||
} else if self.isnt_macro_invocation() && !kw_case_insensitive {
|
||||
} else if self.isnt_macro_invocation() && case == Case::Sensitive {
|
||||
_ = def_;
|
||||
|
||||
// Recover wrong cased keywords
|
||||
return self.parse_item_kind(attrs, macros_allowed, lo, vis, def, fn_parse_mode, true);
|
||||
return self.parse_item_kind(
|
||||
attrs,
|
||||
macros_allowed,
|
||||
lo,
|
||||
vis,
|
||||
def,
|
||||
fn_parse_mode,
|
||||
Case::Insensitive,
|
||||
);
|
||||
} else if macros_allowed && self.check_path() {
|
||||
// MACRO INVOCATION ITEM
|
||||
(Ident::empty(), ItemKind::MacCall(P(self.parse_item_macro(vis)?)))
|
||||
|
@ -551,7 +560,7 @@ impl<'a> Parser<'a> {
|
|||
attrs: &mut AttrVec,
|
||||
defaultness: Defaultness,
|
||||
) -> PResult<'a, ItemInfo> {
|
||||
let unsafety = self.parse_unsafety(false);
|
||||
let unsafety = self.parse_unsafety(Case::Sensitive);
|
||||
self.expect_keyword(kw::Impl)?;
|
||||
|
||||
// First, parse generic parameters if necessary.
|
||||
|
@ -565,7 +574,7 @@ impl<'a> Parser<'a> {
|
|||
generics
|
||||
};
|
||||
|
||||
let constness = self.parse_constness(false);
|
||||
let constness = self.parse_constness(Case::Sensitive);
|
||||
if let Const::Yes(span) = constness {
|
||||
self.sess.gated_spans.gate(sym::const_trait_impl, span);
|
||||
}
|
||||
|
@ -809,7 +818,7 @@ impl<'a> Parser<'a> {
|
|||
|
||||
/// Parses `unsafe? auto? trait Foo { ... }` or `trait Foo = Bar;`.
|
||||
fn parse_item_trait(&mut self, attrs: &mut AttrVec, lo: Span) -> PResult<'a, ItemInfo> {
|
||||
let unsafety = self.parse_unsafety(false);
|
||||
let unsafety = self.parse_unsafety(Case::Sensitive);
|
||||
// Parse optional `auto` prefix.
|
||||
let is_auto = if self.eat_keyword(kw::Auto) { IsAuto::Yes } else { IsAuto::No };
|
||||
|
||||
|
@ -1758,7 +1767,7 @@ impl<'a> Parser<'a> {
|
|||
let (ident, is_raw) = self.ident_or_err()?;
|
||||
if !is_raw && ident.is_reserved() {
|
||||
let snapshot = self.create_snapshot_for_diagnostic();
|
||||
let err = if self.check_fn_front_matter(false, false) {
|
||||
let err = if self.check_fn_front_matter(false, Case::Sensitive) {
|
||||
let inherited_vis = Visibility {
|
||||
span: rustc_span::DUMMY_SP,
|
||||
kind: VisibilityKind::Inherited,
|
||||
|
@ -2147,11 +2156,7 @@ impl<'a> Parser<'a> {
|
|||
///
|
||||
/// `check_pub` adds additional `pub` to the checks in case users place it
|
||||
/// wrongly, can be used to ensure `pub` never comes after `default`.
|
||||
pub(super) fn check_fn_front_matter(
|
||||
&mut self,
|
||||
check_pub: bool,
|
||||
kw_case_insensitive: bool,
|
||||
) -> bool {
|
||||
pub(super) fn check_fn_front_matter(&mut self, check_pub: bool, case: Case) -> bool {
|
||||
// We use an over-approximation here.
|
||||
// `const const`, `fn const` won't parse, but we're not stepping over other syntax either.
|
||||
// `pub` is added in case users got confused with the ordering like `async pub fn`,
|
||||
|
@ -2161,12 +2166,12 @@ impl<'a> Parser<'a> {
|
|||
} else {
|
||||
&[kw::Const, kw::Async, kw::Unsafe, kw::Extern]
|
||||
};
|
||||
self.check_keyword_case(kw::Fn, kw_case_insensitive) // Definitely an `fn`.
|
||||
self.check_keyword_case(kw::Fn, case) // Definitely an `fn`.
|
||||
// `$qual fn` or `$qual $qual`:
|
||||
|| quals.iter().any(|&kw| self.check_keyword_case(kw, kw_case_insensitive))
|
||||
|| quals.iter().any(|&kw| self.check_keyword_case(kw, case))
|
||||
&& self.look_ahead(1, |t| {
|
||||
// `$qual fn`, e.g. `const fn` or `async fn`.
|
||||
t.is_keyword_case(kw::Fn, kw_case_insensitive)
|
||||
t.is_keyword_case(kw::Fn, case)
|
||||
// Two qualifiers `$qual $qual` is enough, e.g. `async unsafe`.
|
||||
|| (
|
||||
(
|
||||
|
@ -2175,16 +2180,16 @@ impl<'a> Parser<'a> {
|
|||
// Rule out 2015 `const async: T = val`.
|
||||
&& i.is_reserved()
|
||||
)
|
||||
|| kw_case_insensitive
|
||||
|| case == Case::Insensitive
|
||||
&& t.is_non_raw_ident_where(|i| quals.iter().any(|qual| qual.as_str() == i.name.as_str().to_lowercase()))
|
||||
)
|
||||
// Rule out unsafe extern block.
|
||||
&& !self.is_unsafe_foreign_mod())
|
||||
})
|
||||
// `extern ABI fn`
|
||||
|| self.check_keyword_case(kw::Extern, kw_case_insensitive)
|
||||
|| self.check_keyword_case(kw::Extern, case)
|
||||
&& self.look_ahead(1, |t| t.can_begin_literal_maybe_minus())
|
||||
&& self.look_ahead(2, |t| t.is_keyword_case(kw::Fn, kw_case_insensitive))
|
||||
&& self.look_ahead(2, |t| t.is_keyword_case(kw::Fn, case))
|
||||
}
|
||||
|
||||
/// Parses all the "front matter" (or "qualifiers") for a `fn` declaration,
|
||||
|
@ -2200,22 +2205,22 @@ impl<'a> Parser<'a> {
|
|||
/// `Visibility::Inherited` when no visibility is known.
|
||||
pub(super) fn parse_fn_front_matter(&mut self, orig_vis: &Visibility) -> PResult<'a, FnHeader> {
|
||||
let sp_start = self.token.span;
|
||||
let constness = self.parse_constness(true);
|
||||
let constness = self.parse_constness(Case::Insensitive);
|
||||
|
||||
let async_start_sp = self.token.span;
|
||||
let asyncness = self.parse_asyncness(true);
|
||||
let asyncness = self.parse_asyncness(Case::Insensitive);
|
||||
|
||||
let unsafe_start_sp = self.token.span;
|
||||
let unsafety = self.parse_unsafety(true);
|
||||
let unsafety = self.parse_unsafety(Case::Insensitive);
|
||||
|
||||
let ext_start_sp = self.token.span;
|
||||
let ext = self.parse_extern(true);
|
||||
let ext = self.parse_extern(Case::Insensitive);
|
||||
|
||||
if let Async::Yes { span, .. } = asyncness {
|
||||
self.ban_async_in_2015(span);
|
||||
}
|
||||
|
||||
if !self.eat_keyword_case(kw::Fn, true) {
|
||||
if !self.eat_keyword_case(kw::Fn, Case::Insensitive) {
|
||||
// It is possible for `expect_one_of` to recover given the contents of
|
||||
// `self.expected_tokens`, therefore, do not use `self.unexpected()` which doesn't
|
||||
// account for this.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue