about summary refs log tree commit diff
path: root/compiler/rustc_builtin_macros/src
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_builtin_macros/src')
-rw-r--r--compiler/rustc_builtin_macros/src/assert.rs6
-rw-r--r--compiler/rustc_builtin_macros/src/panic.rs19
2 files changed, 21 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;
+    }
+}