From afe5335b978bc490f27d45a42bac770383450268 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 7 Oct 2021 12:44:46 +0200 Subject: [PATCH 1/2] Use correct edition for panic in [debug_]assert!() etc. --- compiler/rustc_builtin_macros/src/assert.rs | 6 +++--- compiler/rustc_builtin_macros/src/panic.rs | 19 ++++++++++++++++++- compiler/rustc_span/src/symbol.rs | 1 + library/core/src/macros/mod.rs | 1 + 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/assert.rs b/compiler/rustc_builtin_macros/src/assert.rs index 93ba54da3424e..1e2646e4d348f 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 6f5962d435c33..54ab596bf3eb8 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 { - 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 77baf7d73810e..995c974b865ba 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -568,6 +568,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 035a748f78248..5b3e988caa506 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)*); }) } From fcd9fa9099569beba9c85c594ecbb9b07a1a7501 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Thu, 7 Oct 2021 12:45:01 +0200 Subject: [PATCH 2/2] Add tests for panic and [debug_]assert in Rust 2021. --- src/test/ui/rust-2021/panic.rs | 24 +++++++ src/test/ui/rust-2021/panic.stderr | 101 +++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 src/test/ui/rust-2021/panic.rs create mode 100644 src/test/ui/rust-2021/panic.stderr diff --git a/src/test/ui/rust-2021/panic.rs b/src/test/ui/rust-2021/panic.rs new file mode 100644 index 0000000000000..394fc3c8f8254 --- /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 0000000000000..40b62d279a509 --- /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 +