Skip to content

Commit

Permalink
Rollup merge of #83814 - petrochenkov:emptyexpr, r=davidtwco
Browse files Browse the repository at this point in the history
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;`
  • Loading branch information
JohnTitor authored Apr 5, 2021
2 parents 76be7e2 + cd22425 commit 67ffbed
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
9 changes: 8 additions & 1 deletion compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
});
}
};
fragment_kind.expect_from_annotatables(items)
if fragment_kind == AstFragmentKind::Expr && items.is_empty() {
let msg =
"removing an expression is not supported in this position";
self.cx.span_err(span, msg);
fragment_kind.dummy(span)
} else {
fragment_kind.expect_from_annotatables(items)
}
}
Err(mut err) => {
err.emit();
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/macros/attr-empty-expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// AST-based macro attributes expanding to an empty expression produce an error and not ICE.

#![feature(custom_test_frameworks)]
#![feature(stmt_expr_attributes)]
#![feature(test)]

fn main() {
let _ = #[test] 0; //~ ERROR removing an expression is not supported in this position
let _ = #[bench] 1; //~ ERROR removing an expression is not supported in this position
let _ = #[test_case] 2; //~ ERROR removing an expression is not supported in this position
}
20 changes: 20 additions & 0 deletions src/test/ui/macros/attr-empty-expr.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/attr-empty-expr.rs:8:13
|
LL | let _ = #[test] 0;
| ^^^^^^^

error: removing an expression is not supported in this position
--> $DIR/attr-empty-expr.rs:9:13
|
LL | let _ = #[bench] 1;
| ^^^^^^^^

error: removing an expression is not supported in this position
--> $DIR/attr-empty-expr.rs:10:13
|
LL | let _ = #[test_case] 2;
| ^^^^^^^^^^^^

error: aborting due to 3 previous errors

0 comments on commit 67ffbed

Please sign in to comment.