Rollup merge of #111054 - cjgillot:cfg-eval-recover, r=b-naber

Do not recover when parsing stmt in cfg-eval.

`parse_stmt` does recovery on its own. When parsing the statement fails, we always get `Ok(None)` instead of an `Err` variant with the diagnostic that we can emit.

To avoid this behaviour, we need to opt-out of recovery for cfg_eval.

Fixes https://github.com/rust-lang/rust/issues/105228
This commit is contained in:
Dylan DPC 2023-05-18 17:37:08 +05:30 committed by GitHub
commit ee26abdafb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 2 deletions

View File

@ -166,7 +166,9 @@ fn configure_annotatable(&mut self, mut annotatable: Annotatable) -> Option<Anno
))
},
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)?
.unwrap())))
},
Annotatable::Expr(_) => {
|parser| Ok(Annotatable::Expr(parser.parse_expr_force_collect()?))

View File

@ -40,7 +40,8 @@ pub fn parse_stmt(&mut self, force_collect: ForceCollect) -> PResult<'a, Option<
/// 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,

View File

@ -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
}

View File

@ -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