Skip to content

Commit 67ffbed

Browse files
authored
Rollup merge of #83814 - petrochenkov:emptyexpr, r=davidtwco
expand: Do not ICE when a legacy AST-based macro attribute produces and empty expression Fixes #80251 The reported error is the same as for `let _ = #[cfg(FALSE)] EXPR;`
2 parents 76be7e2 + cd22425 commit 67ffbed

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

compiler/rustc_expand/src/expand.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
735735
});
736736
}
737737
};
738-
fragment_kind.expect_from_annotatables(items)
738+
if fragment_kind == AstFragmentKind::Expr && items.is_empty() {
739+
let msg =
740+
"removing an expression is not supported in this position";
741+
self.cx.span_err(span, msg);
742+
fragment_kind.dummy(span)
743+
} else {
744+
fragment_kind.expect_from_annotatables(items)
745+
}
739746
}
740747
Err(mut err) => {
741748
err.emit();

src/test/ui/macros/attr-empty-expr.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// AST-based macro attributes expanding to an empty expression produce an error and not ICE.
2+
3+
#![feature(custom_test_frameworks)]
4+
#![feature(stmt_expr_attributes)]
5+
#![feature(test)]
6+
7+
fn main() {
8+
let _ = #[test] 0; //~ ERROR removing an expression is not supported in this position
9+
let _ = #[bench] 1; //~ ERROR removing an expression is not supported in this position
10+
let _ = #[test_case] 2; //~ ERROR removing an expression is not supported in this position
11+
}
+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/attr-empty-expr.rs:8:13
3+
|
4+
LL | let _ = #[test] 0;
5+
| ^^^^^^^
6+
7+
error: removing an expression is not supported in this position
8+
--> $DIR/attr-empty-expr.rs:9:13
9+
|
10+
LL | let _ = #[bench] 1;
11+
| ^^^^^^^^
12+
13+
error: removing an expression is not supported in this position
14+
--> $DIR/attr-empty-expr.rs:10:13
15+
|
16+
LL | let _ = #[test_case] 2;
17+
| ^^^^^^^^^^^^
18+
19+
error: aborting due to 3 previous errors
20+

0 commit comments

Comments
 (0)