diff options
| author | Jack Huey <31162821+jackh726@users.noreply.github.com> | 2021-02-02 16:01:46 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-02 16:01:46 -0500 |
| commit | 7f2eeb10c76c8a6f36fd1a273a99f42b8e5222d4 (patch) | |
| tree | 0e498d70274abe417e77bdec539376c47c026fe7 | |
| parent | 86e23cc9f1537a15612810619331953c41dec226 (diff) | |
| parent | ed1de99b4f45032fd049a55bdfae6ee928beecd5 (diff) | |
Rollup merge of #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.
| -rw-r--r-- | compiler/rustc_builtin_macros/src/assert.rs | 4 | ||||
| -rw-r--r-- | library/core/src/macros/mod.rs | 2 | ||||
| -rw-r--r-- | src/test/ui/panics/panic-2021.rs | 9 | ||||
| -rw-r--r-- | src/test/ui/panics/panic-2021.stderr | 42 |
4 files changed, 54 insertions, 3 deletions
diff --git a/compiler/rustc_builtin_macros/src/assert.rs b/compiler/rustc_builtin_macros/src/assert.rs index f82269c4eee..93ba54da342 100644 --- a/compiler/rustc_builtin_macros/src/assert.rs +++ b/compiler/rustc_builtin_macros/src/assert.rs @@ -29,11 +29,11 @@ pub fn expand_assert<'cx>( let panic_call = if let Some(tokens) = custom_message { let path = if span.rust_2021() { - // On edition 2021, we always call `$crate::panic!()`. + // On edition 2021, we always call `$crate::panic::panic_2021!()`. Path { span: sp, segments: cx - .std_path(&[sym::panic]) + .std_path(&[sym::panic, sym::panic_2021]) .into_iter() .map(|ident| PathSegment::from_ident(ident)) .collect(), diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index 10d30609aca..6a7e4b2ba25 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -1234,7 +1234,7 @@ pub(crate) mod builtin { #[rustc_builtin_macro] #[macro_export] #[rustc_diagnostic_item = "assert_macro"] - #[allow_internal_unstable(core_panic)] + #[allow_internal_unstable(core_panic, edition_panic)] macro_rules! assert { ($cond:expr $(,)?) => {{ /* compiler built-in */ }}; ($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }}; diff --git a/src/test/ui/panics/panic-2021.rs b/src/test/ui/panics/panic-2021.rs new file mode 100644 index 00000000000..e606612e108 --- /dev/null +++ b/src/test/ui/panics/panic-2021.rs @@ -0,0 +1,9 @@ +// edition:2021 + +fn main() { + panic!(123); //~ ERROR: format argument must be a string literal + panic!("{}"); //~ ERROR: 1 positional argument in format string + core::panic!("{}"); //~ ERROR: 1 positional argument in format string + assert!(false, 123); //~ ERROR: format argument must be a string literal + assert!(false, "{}"); //~ ERROR: 1 positional argument in format string +} diff --git a/src/test/ui/panics/panic-2021.stderr b/src/test/ui/panics/panic-2021.stderr new file mode 100644 index 00000000000..59b1e4f7a98 --- /dev/null +++ b/src/test/ui/panics/panic-2021.stderr @@ -0,0 +1,42 @@ +error: format argument must be a string literal + --> $DIR/panic-2021.rs:4:12 + | +LL | panic!(123); + | ^^^ + | +help: you might be missing a string literal to format with + | +LL | panic!("{}", 123); + | ^^^^^ + +error: 1 positional argument in format string, but no arguments were given + --> $DIR/panic-2021.rs:5:13 + | +LL | panic!("{}"); + | ^^ + +error: 1 positional argument in format string, but no arguments were given + --> $DIR/panic-2021.rs:6:19 + | +LL | core::panic!("{}"); + | ^^ + +error: format argument must be a string literal + --> $DIR/panic-2021.rs:7:20 + | +LL | assert!(false, 123); + | ^^^ + | +help: you might be missing a string literal to format with + | +LL | assert!(false, "{}", 123); + | ^^^^^ + +error: 1 positional argument in format string, but no arguments were given + --> $DIR/panic-2021.rs:8:21 + | +LL | assert!(false, "{}"); + | ^^ + +error: aborting due to 5 previous errors + |
