Skip to content

Commit afe5335

Browse files
committed
Use correct edition for panic in [debug_]assert!() etc.
1 parent ca8078d commit afe5335

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-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
@@ -568,6 +568,7 @@ symbols! {
568568
dyn_metadata,
569569
dyn_trait,
570570
edition_macro_pats,
571+
edition_panic,
571572
eh_catch_typeinfo,
572573
eh_personality,
573574
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
}

0 commit comments

Comments
 (0)