Skip to content

Commit

Permalink
Rollup merge of #89622 - m-ou-se:debug-assert-2021, r=estebank
Browse files Browse the repository at this point in the history
Use correct edition for panic in [debug_]assert!().

See #88638 (comment)
  • Loading branch information
workingjubilee committed Oct 8, 2021
2 parents aed1801 + fcd9fa9 commit 30e068f
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 4 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_builtin_macros/src/assert.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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,
Expand Down
19 changes: 18 additions & 1 deletion compiler/rustc_builtin_macros/src/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ symbols! {
dyn_metadata,
dyn_trait,
edition_macro_pats,
edition_panic,
eh_catch_typeinfo,
eh_personality,
emit_enum,
Expand Down
1 change: 1 addition & 0 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)*); })
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/ui/rust-2021/panic.rs
Original file line number Diff line number Diff line change
@@ -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
}
101 changes: 101 additions & 0 deletions src/test/ui/rust-2021/panic.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 30e068f

Please sign in to comment.