From 565710b33cb20c901b8b3371d1364cf7fb11e79b Mon Sep 17 00:00:00 2001 From: Loïc BRANSTETT Date: Fri, 21 Jan 2022 23:04:06 +0100 Subject: Fix invalid special casing of the unreachable! macro --- compiler/rustc_builtin_macros/src/assert.rs | 2 +- compiler/rustc_builtin_macros/src/edition_panic.rs | 86 ++++++++++++++++++++++ compiler/rustc_builtin_macros/src/lib.rs | 7 +- compiler/rustc_builtin_macros/src/panic.rs | 65 ---------------- compiler/rustc_span/src/symbol.rs | 5 ++ 5 files changed, 96 insertions(+), 69 deletions(-) create mode 100644 compiler/rustc_builtin_macros/src/edition_panic.rs delete mode 100644 compiler/rustc_builtin_macros/src/panic.rs (limited to 'compiler') diff --git a/compiler/rustc_builtin_macros/src/assert.rs b/compiler/rustc_builtin_macros/src/assert.rs index 1e2646e4d34..9a45dec55f3 100644 --- a/compiler/rustc_builtin_macros/src/assert.rs +++ b/compiler/rustc_builtin_macros/src/assert.rs @@ -1,4 +1,4 @@ -use crate::panic::use_panic_2021; +use crate::edition_panic::use_panic_2021; use rustc_ast::ptr::P; use rustc_ast::token; use rustc_ast::tokenstream::{DelimSpan, TokenStream}; diff --git a/compiler/rustc_builtin_macros/src/edition_panic.rs b/compiler/rustc_builtin_macros/src/edition_panic.rs new file mode 100644 index 00000000000..518b88dec6a --- /dev/null +++ b/compiler/rustc_builtin_macros/src/edition_panic.rs @@ -0,0 +1,86 @@ +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; + +// This expands to either +// - `$crate::panic::panic_2015!(...)` or +// - `$crate::panic::panic_2021!(...)` +// depending on the edition. +// +// This is used for both std::panic!() and core::panic!(). +// +// `$crate` will refer to either the `std` or `core` crate depending on which +// one we're expanding from. +pub fn expand_panic<'cx>( + cx: &'cx mut ExtCtxt<'_>, + sp: Span, + tts: TokenStream, +) -> Box { + let mac = if use_panic_2021(sp) { sym::panic_2021 } else { sym::panic_2015 }; + expand(mac, cx, sp, tts) +} + +// This expands to either +// - `$crate::panic::unreachable_2015!(...)` or +// - `$crate::panic::unreachable_2021!(...)` +// depending on the edition. +pub fn expand_unreachable<'cx>( + cx: &'cx mut ExtCtxt<'_>, + sp: Span, + tts: TokenStream, +) -> Box { + let mac = if use_panic_2021(sp) { sym::unreachable_2021 } else { sym::unreachable_2015 }; + expand(mac, cx, sp, tts) +} + +fn expand<'cx>( + mac: rustc_span::Symbol, + cx: &'cx mut ExtCtxt<'_>, + sp: Span, + tts: TokenStream, +) -> Box { + let sp = cx.with_call_site_ctxt(sp); + + MacEager::expr( + cx.expr( + sp, + ExprKind::MacCall(MacCall { + path: Path { + span: sp, + segments: cx + .std_path(&[sym::panic, mac]) + .into_iter() + .map(|ident| PathSegment::from_ident(ident)) + .collect(), + tokens: None, + }, + args: P(MacArgs::Delimited( + DelimSpan::from_single(sp), + MacDelimiter::Parenthesis, + tts, + )), + prior_type_ascription: None, + }), + ), + ) +} + +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_builtin_macros/src/lib.rs b/compiler/rustc_builtin_macros/src/lib.rs index 6c16c285492..98c94dfc686 100644 --- a/compiler/rustc_builtin_macros/src/lib.rs +++ b/compiler/rustc_builtin_macros/src/lib.rs @@ -31,12 +31,12 @@ mod concat_bytes; mod concat_idents; mod derive; mod deriving; +mod edition_panic; mod env; mod format; mod format_foreign; mod global_allocator; mod log_syntax; -mod panic; mod source_util; mod test; mod trace_macros; @@ -82,8 +82,9 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) { log_syntax: log_syntax::expand_log_syntax, module_path: source_util::expand_mod, option_env: env::expand_option_env, - core_panic: panic::expand_panic, - std_panic: panic::expand_panic, + core_panic: edition_panic::expand_panic, + std_panic: edition_panic::expand_panic, + unreachable: edition_panic::expand_unreachable, stringify: source_util::expand_stringify, trace_macros: trace_macros::expand_trace_macros, } diff --git a/compiler/rustc_builtin_macros/src/panic.rs b/compiler/rustc_builtin_macros/src/panic.rs deleted file mode 100644 index 54ab596bf3e..00000000000 --- a/compiler/rustc_builtin_macros/src/panic.rs +++ /dev/null @@ -1,65 +0,0 @@ -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; - -// This expands to either -// - `$crate::panic::panic_2015!(...)` or -// - `$crate::panic::panic_2021!(...)` -// depending on the edition. -// -// This is used for both std::panic!() and core::panic!(). -// -// `$crate` will refer to either the `std` or `core` crate depending on which -// one we're expanding from. -pub fn expand_panic<'cx>( - cx: &'cx mut ExtCtxt<'_>, - sp: Span, - tts: TokenStream, -) -> Box { - let panic = if use_panic_2021(sp) { sym::panic_2021 } else { sym::panic_2015 }; - - let sp = cx.with_call_site_ctxt(sp); - - MacEager::expr( - cx.expr( - sp, - ExprKind::MacCall(MacCall { - path: Path { - span: sp, - segments: cx - .std_path(&[sym::panic, panic]) - .into_iter() - .map(|ident| PathSegment::from_ident(ident)) - .collect(), - tokens: None, - }, - args: P(MacArgs::Delimited( - DelimSpan::from_single(sp), - MacDelimiter::Parenthesis, - tts, - )), - prior_type_ascription: None, - }), - ), - ) -} - -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 757c430e799..7068b86572f 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1437,7 +1437,12 @@ symbols! { unmarked_api, unpin, unreachable, + unreachable_2015, + unreachable_2015_macro, + unreachable_2021, + unreachable_2021_macro, unreachable_code, + unreachable_display, unreachable_macro, unrestricted_attribute_tokens, unsafe_block_in_unsafe_fn, -- cgit 1.4.1-3-g733a5