Skip to content

Commit 414ca04

Browse files
authored
Rollup merge of #89622 - m-ou-se:debug-assert-2021, r=estebank
Use correct edition for panic in [debug_]assert!(). See #88638 (comment)
2 parents 8437cef + fcd9fa9 commit 414ca04

File tree

6 files changed

+148
-4
lines changed

6 files changed

+148
-4
lines changed

compiler/rustc_builtin_macros/src/assert.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use rustc_errors::{Applicability, DiagnosticBuilder};
2-
1+
use crate::panic::use_panic_2021;
32
use rustc_ast::ptr::P;
43
use rustc_ast::token;
54
use rustc_ast::tokenstream::{DelimSpan, TokenStream};
65
use rustc_ast::{self as ast, *};
76
use rustc_ast_pretty::pprust;
7+
use rustc_errors::{Applicability, DiagnosticBuilder};
88
use rustc_expand::base::*;
99
use rustc_parse::parser::Parser;
1010
use rustc_span::symbol::{sym, Ident, Symbol};
@@ -28,7 +28,7 @@ pub fn expand_assert<'cx>(
2828
let sp = cx.with_call_site_ctxt(span);
2929

3030
let panic_call = if let Some(tokens) = custom_message {
31-
let path = if span.rust_2021() {
31+
let path = if use_panic_2021(span) {
3232
// On edition 2021, we always call `$crate::panic::panic_2021!()`.
3333
Path {
3434
span: sp,

compiler/rustc_builtin_macros/src/panic.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rustc_ast::ptr::P;
22
use rustc_ast::tokenstream::{DelimSpan, TokenStream};
33
use rustc_ast::*;
44
use rustc_expand::base::*;
5+
use rustc_span::edition::Edition;
56
use rustc_span::symbol::sym;
67
use rustc_span::Span;
78

@@ -19,7 +20,7 @@ pub fn expand_panic<'cx>(
1920
sp: Span,
2021
tts: TokenStream,
2122
) -> Box<dyn MacResult + 'cx> {
22-
let panic = if sp.rust_2021() { sym::panic_2021 } else { sym::panic_2015 };
23+
let panic = if use_panic_2021(sp) { sym::panic_2021 } else { sym::panic_2015 };
2324

2425
let sp = cx.with_call_site_ctxt(sp);
2526

@@ -46,3 +47,19 @@ pub fn expand_panic<'cx>(
4647
),
4748
)
4849
}
50+
51+
pub fn use_panic_2021(mut span: Span) -> bool {
52+
// To determine the editon, we check the first span up the expansion
53+
// stack that does not have #[allow_internal_unstable(edition_panic)].
54+
// (To avoid using the edition of e.g. the assert!() or debug_assert!() definition.)
55+
loop {
56+
let expn = span.ctxt().outer_expn_data();
57+
if let Some(features) = expn.allow_internal_unstable {
58+
if features.iter().any(|&f| f == sym::edition_panic) {
59+
span = expn.call_site;
60+
continue;
61+
}
62+
}
63+
break expn.edition >= Edition::Edition2021;
64+
}
65+
}

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ symbols! {
570570
dyn_metadata,
571571
dyn_trait,
572572
edition_macro_pats,
573+
edition_panic,
573574
eh_catch_typeinfo,
574575
eh_personality,
575576
emit_enum,

library/core/src/macros/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ pub macro assert_matches {
210210
#[macro_export]
211211
#[stable(feature = "rust1", since = "1.0.0")]
212212
#[rustc_diagnostic_item = "debug_assert_macro"]
213+
#[allow_internal_unstable(edition_panic)]
213214
macro_rules! debug_assert {
214215
($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert!($($arg)*); })
215216
}

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

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// edition:2021
2+
3+
fn main() {
4+
debug_assert!(false, 123);
5+
//~^ ERROR must be a string literal
6+
assert!(false, 123);
7+
//~^ ERROR must be a string literal
8+
panic!(false, 123);
9+
//~^ ERROR must be a string literal
10+
11+
std::debug_assert!(false, 123);
12+
//~^ ERROR must be a string literal
13+
std::assert!(false, 123);
14+
//~^ ERROR must be a string literal
15+
std::panic!(false, 123);
16+
//~^ ERROR must be a string literal
17+
18+
core::debug_assert!(false, 123);
19+
//~^ ERROR must be a string literal
20+
core::assert!(false, 123);
21+
//~^ ERROR must be a string literal
22+
core::panic!(false, 123);
23+
//~^ ERROR must be a string literal
24+
}

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

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
error: format argument must be a string literal
2+
--> $DIR/panic.rs:4:26
3+
|
4+
LL | debug_assert!(false, 123);
5+
| ^^^
6+
|
7+
help: you might be missing a string literal to format with
8+
|
9+
LL | debug_assert!(false, "{}", 123);
10+
| +++++
11+
12+
error: format argument must be a string literal
13+
--> $DIR/panic.rs:6:20
14+
|
15+
LL | assert!(false, 123);
16+
| ^^^
17+
|
18+
help: you might be missing a string literal to format with
19+
|
20+
LL | assert!(false, "{}", 123);
21+
| +++++
22+
23+
error: format argument must be a string literal
24+
--> $DIR/panic.rs:8:12
25+
|
26+
LL | panic!(false, 123);
27+
| ^^^^^
28+
|
29+
help: you might be missing a string literal to format with
30+
|
31+
LL | panic!("{} {}", false, 123);
32+
| ++++++++
33+
34+
error: format argument must be a string literal
35+
--> $DIR/panic.rs:11:31
36+
|
37+
LL | std::debug_assert!(false, 123);
38+
| ^^^
39+
|
40+
help: you might be missing a string literal to format with
41+
|
42+
LL | std::debug_assert!(false, "{}", 123);
43+
| +++++
44+
45+
error: format argument must be a string literal
46+
--> $DIR/panic.rs:13:25
47+
|
48+
LL | std::assert!(false, 123);
49+
| ^^^
50+
|
51+
help: you might be missing a string literal to format with
52+
|
53+
LL | std::assert!(false, "{}", 123);
54+
| +++++
55+
56+
error: format argument must be a string literal
57+
--> $DIR/panic.rs:15:17
58+
|
59+
LL | std::panic!(false, 123);
60+
| ^^^^^
61+
|
62+
help: you might be missing a string literal to format with
63+
|
64+
LL | std::panic!("{} {}", false, 123);
65+
| ++++++++
66+
67+
error: format argument must be a string literal
68+
--> $DIR/panic.rs:18:32
69+
|
70+
LL | core::debug_assert!(false, 123);
71+
| ^^^
72+
|
73+
help: you might be missing a string literal to format with
74+
|
75+
LL | core::debug_assert!(false, "{}", 123);
76+
| +++++
77+
78+
error: format argument must be a string literal
79+
--> $DIR/panic.rs:20:26
80+
|
81+
LL | core::assert!(false, 123);
82+
| ^^^
83+
|
84+
help: you might be missing a string literal to format with
85+
|
86+
LL | core::assert!(false, "{}", 123);
87+
| +++++
88+
89+
error: format argument must be a string literal
90+
--> $DIR/panic.rs:22:18
91+
|
92+
LL | core::panic!(false, 123);
93+
| ^^^^^
94+
|
95+
help: you might be missing a string literal to format with
96+
|
97+
LL | core::panic!("{} {}", false, 123);
98+
| ++++++++
99+
100+
error: aborting due to 9 previous errors
101+

0 commit comments

Comments
 (0)