-
Notifications
You must be signed in to change notification settings - Fork 13.8k
parser: Ensure that all nonterminals have tokens after parsing #84995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,17 +94,7 @@ impl<'a> Parser<'a> { | |
|
||
/// Parses an expression, forcing tokens to be collected | ||
pub fn parse_expr_force_collect(&mut self) -> PResult<'a, P<Expr>> { | ||
// If we have outer attributes, then the call to `collect_tokens_trailing_token` | ||
// will be made for us. | ||
if matches!(self.token.kind, TokenKind::Pound | TokenKind::DocComment(..)) { | ||
self.parse_expr() | ||
} else { | ||
// If we don't have outer attributes, then we need to ensure | ||
// that collection happens by using `collect_tokens_no_attrs`. | ||
// Expression don't support custom inner attributes, so `parse_expr` | ||
// will never try to collect tokens if we don't have outer attributes. | ||
self.collect_tokens_no_attrs(|this| this.parse_expr()) | ||
} | ||
self.collect_tokens_no_attrs(|this| this.parse_expr()) | ||
|
||
} | ||
|
||
pub fn parse_anon_const_expr(&mut self) -> PResult<'a, AnonConst> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,11 @@ impl<'a> Parser<'a> { | |
// or `auto trait` items. We aim to parse an arbitrary path `a::b` but not something | ||
// that starts like a path (1 token), but it fact not a path. | ||
// Also, we avoid stealing syntax from `parse_item_`. | ||
self.parse_stmt_path_start(lo, attrs, force_collect)? | ||
if force_collect == ForceCollect::Yes { | ||
self.collect_tokens_no_attrs(|this| this.parse_stmt_path_start(lo, attrs)) | ||
} else { | ||
self.parse_stmt_path_start(lo, attrs) | ||
}? | ||
} else if let Some(item) = | ||
self.parse_item_common(attrs.clone(), false, true, |_| true, force_collect)? | ||
{ | ||
|
@@ -85,21 +89,22 @@ impl<'a> Parser<'a> { | |
self.mk_stmt(lo, StmtKind::Empty) | ||
} else if self.token != token::CloseDelim(token::Brace) { | ||
// Remainder are line-expr stmts. | ||
let e = self.parse_expr_res(Restrictions::STMT_EXPR, Some(attrs))?; | ||
let e = if force_collect == ForceCollect::Yes { | ||
self.collect_tokens_no_attrs(|this| { | ||
|
||
this.parse_expr_res(Restrictions::STMT_EXPR, Some(attrs)) | ||
}) | ||
} else { | ||
self.parse_expr_res(Restrictions::STMT_EXPR, Some(attrs)) | ||
}?; | ||
self.mk_stmt(lo.to(e.span), StmtKind::Expr(e)) | ||
} else { | ||
self.error_outer_attrs(&attrs.take_for_recovery()); | ||
return Ok(None); | ||
})) | ||
} | ||
|
||
fn parse_stmt_path_start( | ||
&mut self, | ||
lo: Span, | ||
attrs: AttrWrapper, | ||
force_collect: ForceCollect, | ||
) -> PResult<'a, Stmt> { | ||
let stmt = self.collect_tokens_trailing_token(attrs, force_collect, |this, attrs| { | ||
fn parse_stmt_path_start(&mut self, lo: Span, attrs: AttrWrapper) -> PResult<'a, Stmt> { | ||
let stmt = self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| { | ||
let path = this.parse_path(PathStyle::Expr)?; | ||
|
||
if this.eat(&token::Not) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// check-pass | ||
// aux-build:test-macros.rs | ||
|
||
#![feature(decl_macro)] | ||
#![feature(stmt_expr_attributes)] | ||
|
||
#![no_std] // Don't load unnecessary hygiene information from std | ||
extern crate std; | ||
|
||
#[macro_use] | ||
extern crate test_macros; | ||
|
||
macro mac { | ||
(expr $expr:expr) => { | ||
#[derive(Print)] | ||
enum E { | ||
V = { let _ = $expr; 0 }, | ||
} | ||
}, | ||
(stmt $stmt:stmt) => { | ||
#[derive(Print)] | ||
enum E { | ||
V = { let _ = { $stmt }; 0 }, | ||
} | ||
}, | ||
} | ||
|
||
const PATH: u8 = 2; | ||
|
||
fn main() { | ||
mac!(expr #[allow(warnings)] 0); | ||
mac!(stmt 0); | ||
mac!(stmt {}); | ||
mac!(stmt PATH); | ||
mac!(stmt 0 + 1); | ||
mac!(stmt PATH + 1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you remove this assertion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's hit in cases like
#[cfg_eval] #[rustc_dummy] 0
.Tokens for
#[rustc_dummy] 0
are collected twice due to the new behavior ofparse_expr_force_collect
, but we still get to here despite theabove because in case of
cfg_eval
theself.capture_cfg
condition is true.