Skip to content

Do not recover when parsing stmt in cfg-eval. #111054

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

Merged
merged 1 commit into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compiler/rustc_builtin_macros/src/cfg_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ impl CfgEval<'_, '_> {
))
},
Annotatable::Stmt(_) => |parser| {
Ok(Annotatable::Stmt(P(parser.parse_stmt(ForceCollect::Yes)?.unwrap())))
Ok(Annotatable::Stmt(P(parser
.parse_stmt_without_recovery(false, ForceCollect::Yes)?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that familiar with the macro expansion code, so feel free to re-assign.

But I'm wondering why we even get into the expand call if the parse for the Annotatable failed in the first place. If I understand this correctly this parse is a re-parse (we also get a duplicated error message in the test). Why can't we prevent this to be called when the first parse failed?

.unwrap())))
},
Annotatable::Expr(_) => {
|parser| Ok(Annotatable::Expr(parser.parse_expr_force_collect()?))
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ impl<'a> Parser<'a> {

/// If `force_collect` is [`ForceCollect::Yes`], forces collection of tokens regardless of whether
/// or not we have attributes
pub(crate) fn parse_stmt_without_recovery(
// Public for `cfg_eval` macro expansion.
pub fn parse_stmt_without_recovery(
&mut self,
capture_semi: bool,
force_collect: ForceCollect,
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/cfg/cfg-stmt-recovery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Verify that we do not ICE when failing to parse a statement in `cfg_eval`.

#![feature(cfg_eval)]
#![feature(stmt_expr_attributes)]

#[cfg_eval]
fn main() {
#[cfg_eval]
let _ = #[cfg(FALSE)] 0;
//~^ ERROR removing an expression is not supported in this position
//~| ERROR expected expression, found `;`
//~| ERROR removing an expression is not supported in this position
}
20 changes: 20 additions & 0 deletions tests/ui/cfg/cfg-stmt-recovery.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: removing an expression is not supported in this position
--> $DIR/cfg-stmt-recovery.rs:9:13
|
LL | let _ = #[cfg(FALSE)] 0;
| ^^^^^^^^^^^^^

error: expected expression, found `;`
--> $DIR/cfg-stmt-recovery.rs:9:28
|
LL | let _ = #[cfg(FALSE)] 0;
| ^ expected expression

error: removing an expression is not supported in this position
--> $DIR/cfg-stmt-recovery.rs:9:13
|
LL | let _ = #[cfg(FALSE)] 0;
| ^^^^^^^^^^^^^

error: aborting due to 3 previous errors