Skip to content

Commit 7f2eeb1

Browse files
authored
Rollup merge of rust-lang#81647 - m-ou-se:assert-2021-fix, r=petrochenkov
Fix bug with assert!() calling the wrong edition of panic!(). The span of `panic!` produced by the `assert` macro did not carry the right edition. This changes `assert` to call the right version. Also adds tests for the 2021 edition of panic and assert, that would've caught this.
2 parents 86e23cc + ed1de99 commit 7f2eeb1

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

compiler/rustc_builtin_macros/src/assert.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ pub fn expand_assert<'cx>(
2929

3030
let panic_call = if let Some(tokens) = custom_message {
3131
let path = if span.rust_2021() {
32-
// On edition 2021, we always call `$crate::panic!()`.
32+
// On edition 2021, we always call `$crate::panic::panic_2021!()`.
3333
Path {
3434
span: sp,
3535
segments: cx
36-
.std_path(&[sym::panic])
36+
.std_path(&[sym::panic, sym::panic_2021])
3737
.into_iter()
3838
.map(|ident| PathSegment::from_ident(ident))
3939
.collect(),

library/core/src/macros/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ pub(crate) mod builtin {
12341234
#[rustc_builtin_macro]
12351235
#[macro_export]
12361236
#[rustc_diagnostic_item = "assert_macro"]
1237-
#[allow_internal_unstable(core_panic)]
1237+
#[allow_internal_unstable(core_panic, edition_panic)]
12381238
macro_rules! assert {
12391239
($cond:expr $(,)?) => {{ /* compiler built-in */ }};
12401240
($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }};

src/test/ui/panics/panic-2021.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// edition:2021
2+
3+
fn main() {
4+
panic!(123); //~ ERROR: format argument must be a string literal
5+
panic!("{}"); //~ ERROR: 1 positional argument in format string
6+
core::panic!("{}"); //~ ERROR: 1 positional argument in format string
7+
assert!(false, 123); //~ ERROR: format argument must be a string literal
8+
assert!(false, "{}"); //~ ERROR: 1 positional argument in format string
9+
}

src/test/ui/panics/panic-2021.stderr

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
error: format argument must be a string literal
2+
--> $DIR/panic-2021.rs:4:12
3+
|
4+
LL | panic!(123);
5+
| ^^^
6+
|
7+
help: you might be missing a string literal to format with
8+
|
9+
LL | panic!("{}", 123);
10+
| ^^^^^
11+
12+
error: 1 positional argument in format string, but no arguments were given
13+
--> $DIR/panic-2021.rs:5:13
14+
|
15+
LL | panic!("{}");
16+
| ^^
17+
18+
error: 1 positional argument in format string, but no arguments were given
19+
--> $DIR/panic-2021.rs:6:19
20+
|
21+
LL | core::panic!("{}");
22+
| ^^
23+
24+
error: format argument must be a string literal
25+
--> $DIR/panic-2021.rs:7:20
26+
|
27+
LL | assert!(false, 123);
28+
| ^^^
29+
|
30+
help: you might be missing a string literal to format with
31+
|
32+
LL | assert!(false, "{}", 123);
33+
| ^^^^^
34+
35+
error: 1 positional argument in format string, but no arguments were given
36+
--> $DIR/panic-2021.rs:8:21
37+
|
38+
LL | assert!(false, "{}");
39+
| ^^
40+
41+
error: aborting due to 5 previous errors
42+

0 commit comments

Comments
 (0)