diff options
| author | Jubilee <46493976+workingjubilee@users.noreply.github.com> | 2021-10-07 20:26:15 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-07 20:26:15 -0700 |
| commit | 30e068f58b46d42b0e71b5112ceb2193f376ecc2 (patch) | |
| tree | 87a0b8e620cbce6d2a0880a6b84c6bac22cfcb41 | |
| parent | aed18018410ea09de7924938e15f877bc3df4a71 (diff) | |
| parent | fcd9fa9099569beba9c85c594ecbb9b07a1a7501 (diff) | |
| download | rust-30e068f58b46d42b0e71b5112ceb2193f376ecc2.tar.gz rust-30e068f58b46d42b0e71b5112ceb2193f376ecc2.zip | |
Rollup merge of #89622 - m-ou-se:debug-assert-2021, r=estebank
Use correct edition for panic in [debug_]assert!(). See https://github.com/rust-lang/rust/issues/88638#issuecomment-915472783
| -rw-r--r-- | compiler/rustc_builtin_macros/src/assert.rs | 6 | ||||
| -rw-r--r-- | compiler/rustc_builtin_macros/src/panic.rs | 19 | ||||
| -rw-r--r-- | compiler/rustc_span/src/symbol.rs | 1 | ||||
| -rw-r--r-- | library/core/src/macros/mod.rs | 1 | ||||
| -rw-r--r-- | src/test/ui/rust-2021/panic.rs | 24 | ||||
| -rw-r--r-- | src/test/ui/rust-2021/panic.stderr | 101 |
6 files changed, 148 insertions, 4 deletions
diff --git a/compiler/rustc_builtin_macros/src/assert.rs b/compiler/rustc_builtin_macros/src/assert.rs index 93ba54da342..1e2646e4d34 100644 --- a/compiler/rustc_builtin_macros/src/assert.rs +++ b/compiler/rustc_builtin_macros/src/assert.rs @@ -1,10 +1,10 @@ -use rustc_errors::{Applicability, DiagnosticBuilder}; - +use crate::panic::use_panic_2021; use rustc_ast::ptr::P; use rustc_ast::token; use rustc_ast::tokenstream::{DelimSpan, TokenStream}; use rustc_ast::{self as ast, *}; use rustc_ast_pretty::pprust; +use rustc_errors::{Applicability, DiagnosticBuilder}; use rustc_expand::base::*; use rustc_parse::parser::Parser; use rustc_span::symbol::{sym, Ident, Symbol}; @@ -28,7 +28,7 @@ pub fn expand_assert<'cx>( let sp = cx.with_call_site_ctxt(span); let panic_call = if let Some(tokens) = custom_message { - let path = if span.rust_2021() { + let path = if use_panic_2021(span) { // On edition 2021, we always call `$crate::panic::panic_2021!()`. Path { span: sp, diff --git a/compiler/rustc_builtin_macros/src/panic.rs b/compiler/rustc_builtin_macros/src/panic.rs index 6f5962d435c..54ab596bf3e 100644 --- a/compiler/rustc_builtin_macros/src/panic.rs +++ b/compiler/rustc_builtin_macros/src/panic.rs @@ -2,6 +2,7 @@ use rustc_ast::ptr::P; use rustc_ast::tokenstream::{DelimSpan, TokenStream}; use rustc_ast::*; use rustc_expand::base::*; +use rustc_span::edition::Edition; use rustc_span::symbol::sym; use rustc_span::Span; @@ -19,7 +20,7 @@ pub fn expand_panic<'cx>( sp: Span, tts: TokenStream, ) -> Box<dyn MacResult + 'cx> { - let panic = if sp.rust_2021() { sym::panic_2021 } else { sym::panic_2015 }; + let panic = if use_panic_2021(sp) { sym::panic_2021 } else { sym::panic_2015 }; let sp = cx.with_call_site_ctxt(sp); @@ -46,3 +47,19 @@ pub fn expand_panic<'cx>( ), ) } + +pub fn use_panic_2021(mut span: Span) -> bool { + // To determine the editon, we check the first span up the expansion + // stack that does not have #[allow_internal_unstable(edition_panic)]. + // (To avoid using the edition of e.g. the assert!() or debug_assert!() definition.) + loop { + let expn = span.ctxt().outer_expn_data(); + if let Some(features) = expn.allow_internal_unstable { + if features.iter().any(|&f| f == sym::edition_panic) { + span = expn.call_site; + continue; + } + } + break expn.edition >= Edition::Edition2021; + } +} diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 382dbc377d6..0e30e154ee5 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -570,6 +570,7 @@ symbols! { dyn_metadata, dyn_trait, edition_macro_pats, + edition_panic, eh_catch_typeinfo, eh_personality, emit_enum, diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index 035a748f782..5b3e988caa5 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -210,6 +210,7 @@ pub macro assert_matches { #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_diagnostic_item = "debug_assert_macro"] +#[allow_internal_unstable(edition_panic)] macro_rules! debug_assert { ($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert!($($arg)*); }) } diff --git a/src/test/ui/rust-2021/panic.rs b/src/test/ui/rust-2021/panic.rs new file mode 100644 index 00000000000..394fc3c8f82 --- /dev/null +++ b/src/test/ui/rust-2021/panic.rs @@ -0,0 +1,24 @@ +// edition:2021 + +fn main() { + debug_assert!(false, 123); + //~^ ERROR must be a string literal + assert!(false, 123); + //~^ ERROR must be a string literal + panic!(false, 123); + //~^ ERROR must be a string literal + + std::debug_assert!(false, 123); + //~^ ERROR must be a string literal + std::assert!(false, 123); + //~^ ERROR must be a string literal + std::panic!(false, 123); + //~^ ERROR must be a string literal + + core::debug_assert!(false, 123); + //~^ ERROR must be a string literal + core::assert!(false, 123); + //~^ ERROR must be a string literal + core::panic!(false, 123); + //~^ ERROR must be a string literal +} diff --git a/src/test/ui/rust-2021/panic.stderr b/src/test/ui/rust-2021/panic.stderr new file mode 100644 index 00000000000..40b62d279a5 --- /dev/null +++ b/src/test/ui/rust-2021/panic.stderr @@ -0,0 +1,101 @@ +error: format argument must be a string literal + --> $DIR/panic.rs:4:26 + | +LL | debug_assert!(false, 123); + | ^^^ + | +help: you might be missing a string literal to format with + | +LL | debug_assert!(false, "{}", 123); + | +++++ + +error: format argument must be a string literal + --> $DIR/panic.rs:6:20 + | +LL | assert!(false, 123); + | ^^^ + | +help: you might be missing a string literal to format with + | +LL | assert!(false, "{}", 123); + | +++++ + +error: format argument must be a string literal + --> $DIR/panic.rs:8:12 + | +LL | panic!(false, 123); + | ^^^^^ + | +help: you might be missing a string literal to format with + | +LL | panic!("{} {}", false, 123); + | ++++++++ + +error: format argument must be a string literal + --> $DIR/panic.rs:11:31 + | +LL | std::debug_assert!(false, 123); + | ^^^ + | +help: you might be missing a string literal to format with + | +LL | std::debug_assert!(false, "{}", 123); + | +++++ + +error: format argument must be a string literal + --> $DIR/panic.rs:13:25 + | +LL | std::assert!(false, 123); + | ^^^ + | +help: you might be missing a string literal to format with + | +LL | std::assert!(false, "{}", 123); + | +++++ + +error: format argument must be a string literal + --> $DIR/panic.rs:15:17 + | +LL | std::panic!(false, 123); + | ^^^^^ + | +help: you might be missing a string literal to format with + | +LL | std::panic!("{} {}", false, 123); + | ++++++++ + +error: format argument must be a string literal + --> $DIR/panic.rs:18:32 + | +LL | core::debug_assert!(false, 123); + | ^^^ + | +help: you might be missing a string literal to format with + | +LL | core::debug_assert!(false, "{}", 123); + | +++++ + +error: format argument must be a string literal + --> $DIR/panic.rs:20:26 + | +LL | core::assert!(false, 123); + | ^^^ + | +help: you might be missing a string literal to format with + | +LL | core::assert!(false, "{}", 123); + | +++++ + +error: format argument must be a string literal + --> $DIR/panic.rs:22:18 + | +LL | core::panic!(false, 123); + | ^^^^^ + | +help: you might be missing a string literal to format with + | +LL | core::panic!("{} {}", false, 123); + | ++++++++ + +error: aborting due to 9 previous errors + |
