Skip to content

Commit a893695

Browse files
authored
Rollup merge of rust-lang#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 rust-lang#105228
2 parents 8f2d75d + d56ce8e commit a893695

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

compiler/rustc_builtin_macros/src/cfg_eval.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ impl CfgEval<'_, '_> {
166166
))
167167
},
168168
Annotatable::Stmt(_) => |parser| {
169-
Ok(Annotatable::Stmt(P(parser.parse_stmt(ForceCollect::Yes)?.unwrap())))
169+
Ok(Annotatable::Stmt(P(parser
170+
.parse_stmt_without_recovery(false, ForceCollect::Yes)?
171+
.unwrap())))
170172
},
171173
Annotatable::Expr(_) => {
172174
|parser| Ok(Annotatable::Expr(parser.parse_expr_force_collect()?))

compiler/rustc_parse/src/parser/stmt.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ impl<'a> Parser<'a> {
4040

4141
/// If `force_collect` is [`ForceCollect::Yes`], forces collection of tokens regardless of whether
4242
/// or not we have attributes
43-
pub(crate) fn parse_stmt_without_recovery(
43+
// Public for `cfg_eval` macro expansion.
44+
pub fn parse_stmt_without_recovery(
4445
&mut self,
4546
capture_semi: bool,
4647
force_collect: ForceCollect,

tests/ui/cfg/cfg-stmt-recovery.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Verify that we do not ICE when failing to parse a statement in `cfg_eval`.
2+
3+
#![feature(cfg_eval)]
4+
#![feature(stmt_expr_attributes)]
5+
6+
#[cfg_eval]
7+
fn main() {
8+
#[cfg_eval]
9+
let _ = #[cfg(FALSE)] 0;
10+
//~^ ERROR removing an expression is not supported in this position
11+
//~| ERROR expected expression, found `;`
12+
//~| ERROR removing an expression is not supported in this position
13+
}

tests/ui/cfg/cfg-stmt-recovery.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: removing an expression is not supported in this position
2+
--> $DIR/cfg-stmt-recovery.rs:9:13
3+
|
4+
LL | let _ = #[cfg(FALSE)] 0;
5+
| ^^^^^^^^^^^^^
6+
7+
error: expected expression, found `;`
8+
--> $DIR/cfg-stmt-recovery.rs:9:28
9+
|
10+
LL | let _ = #[cfg(FALSE)] 0;
11+
| ^ expected expression
12+
13+
error: removing an expression is not supported in this position
14+
--> $DIR/cfg-stmt-recovery.rs:9:13
15+
|
16+
LL | let _ = #[cfg(FALSE)] 0;
17+
| ^^^^^^^^^^^^^
18+
19+
error: aborting due to 3 previous errors
20+

0 commit comments

Comments
 (0)