Skip to content

Commit a367cec

Browse files
committed
emit error with span for empty asserts
Fixes #55547.
1 parent 9cd3bef commit a367cec

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/libsyntax_ext/assert.rs

+8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ pub fn expand_assert<'cx>(
2424
tts: &[TokenTree],
2525
) -> Box<dyn MacResult + 'cx> {
2626
let mut parser = cx.new_parser_from_tts(tts);
27+
28+
if parser.token == token::Eof {
29+
cx.struct_span_err(sp, "macro requires a boolean expression as an argument")
30+
.span_label(sp, "boolean expression required")
31+
.emit();
32+
return DummyResult::expr(sp);
33+
}
34+
2735
let cond_expr = panictry!(parser.parse_expr());
2836
let custom_msg_args = if parser.eat(&token::Comma) {
2937
let ts = parser.parse_tokens();

src/test/ui/macros/assert.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
assert!(); //~ ERROR requires a boolean expression
3+
debug_assert!(); //~ ERROR requires a boolean expression
4+
}

src/test/ui/macros/assert.stderr

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: macro requires a boolean expression as an argument
2+
--> $DIR/assert.rs:2:5
3+
|
4+
LL | assert!(); //~ ERROR requires a boolean expression
5+
| ^^^^^^^^^^ boolean expression required
6+
7+
error: macro requires a boolean expression as an argument
8+
--> $DIR/assert.rs:3:5
9+
|
10+
LL | debug_assert!(); //~ ERROR requires a boolean expression
11+
| ^^^^^^^^^^^^^^^^ boolean expression required
12+
|
13+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)