Skip to content

Commit 5140d6d

Browse files
UrgauMark-Simulacrum
authored andcommitted
Fix invalid special casing of the unreachable! macro
1 parent 79ee164 commit 5140d6d

15 files changed

+141
-19
lines changed

compiler/rustc_builtin_macros/src/assert.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::panic::use_panic_2021;
1+
use crate::edition_panic::use_panic_2021;
22
use rustc_ast::ptr::P;
33
use rustc_ast::token;
44
use rustc_ast::tokenstream::{DelimSpan, TokenStream};

compiler/rustc_builtin_macros/src/panic.rs renamed to compiler/rustc_builtin_macros/src/edition_panic.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,29 @@ pub fn expand_panic<'cx>(
2020
sp: Span,
2121
tts: TokenStream,
2222
) -> Box<dyn MacResult + 'cx> {
23-
let panic = if use_panic_2021(sp) { sym::panic_2021 } else { sym::panic_2015 };
23+
let mac = if use_panic_2021(sp) { sym::panic_2021 } else { sym::panic_2015 };
24+
expand(mac, cx, sp, tts)
25+
}
2426

27+
// This expands to either
28+
// - `$crate::panic::unreachable_2015!(...)` or
29+
// - `$crate::panic::unreachable_2021!(...)`
30+
// depending on the edition.
31+
pub fn expand_unreachable<'cx>(
32+
cx: &'cx mut ExtCtxt<'_>,
33+
sp: Span,
34+
tts: TokenStream,
35+
) -> Box<dyn MacResult + 'cx> {
36+
let mac = if use_panic_2021(sp) { sym::unreachable_2021 } else { sym::unreachable_2015 };
37+
expand(mac, cx, sp, tts)
38+
}
39+
40+
fn expand<'cx>(
41+
mac: rustc_span::Symbol,
42+
cx: &'cx mut ExtCtxt<'_>,
43+
sp: Span,
44+
tts: TokenStream,
45+
) -> Box<dyn MacResult + 'cx> {
2546
let sp = cx.with_call_site_ctxt(sp);
2647

2748
MacEager::expr(
@@ -31,7 +52,7 @@ pub fn expand_panic<'cx>(
3152
path: Path {
3253
span: sp,
3354
segments: cx
34-
.std_path(&[sym::panic, panic])
55+
.std_path(&[sym::panic, mac])
3556
.into_iter()
3657
.map(|ident| PathSegment::from_ident(ident))
3758
.collect(),

compiler/rustc_builtin_macros/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ mod concat_bytes;
2929
mod concat_idents;
3030
mod derive;
3131
mod deriving;
32+
mod edition_panic;
3233
mod env;
3334
mod format;
3435
mod format_foreign;
3536
mod global_allocator;
3637
mod llvm_asm;
3738
mod log_syntax;
38-
mod panic;
3939
mod source_util;
4040
mod test;
4141
mod trace_macros;
@@ -82,8 +82,9 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
8282
log_syntax: log_syntax::expand_log_syntax,
8383
module_path: source_util::expand_mod,
8484
option_env: env::expand_option_env,
85-
core_panic: panic::expand_panic,
86-
std_panic: panic::expand_panic,
85+
core_panic: edition_panic::expand_panic,
86+
std_panic: edition_panic::expand_panic,
87+
unreachable: edition_panic::expand_unreachable,
8788
stringify: source_util::expand_stringify,
8889
trace_macros: trace_macros::expand_trace_macros,
8990
}

compiler/rustc_span/src/symbol.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,13 @@ symbols! {
13881388
unmarked_api,
13891389
unpin,
13901390
unreachable,
1391+
unreachable_2015,
1392+
unreachable_2015_macro,
1393+
unreachable_2021,
1394+
unreachable_2021_macro,
13911395
unreachable_code,
1396+
unreachable_display,
1397+
unreachable_macro,
13921398
unrestricted_attribute_tokens,
13931399
unsafe_block_in_unsafe_fn,
13941400
unsafe_cell,

library/core/src/macros/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,22 @@ macro_rules! writeln {
587587
/// unreachable!("The loop should always return");
588588
/// }
589589
/// ```
590+
#[cfg(not(bootstrap))]
591+
#[macro_export]
592+
#[rustc_builtin_macro(unreachable)]
593+
#[allow_internal_unstable(edition_panic)]
594+
#[stable(feature = "rust1", since = "1.0.0")]
595+
#[cfg_attr(not(test), rustc_diagnostic_item = "unreachable_macro")]
596+
macro_rules! unreachable {
597+
// Expands to either `$crate::panic::unreachable_2015` or `$crate::panic::unreachable_2021`
598+
// depending on the edition of the caller.
599+
($($arg:tt)*) => {
600+
/* compiler built-in */
601+
};
602+
}
603+
604+
/// unreachable!() macro
605+
#[cfg(bootstrap)]
590606
#[macro_export]
591607
#[stable(feature = "rust1", since = "1.0.0")]
592608
#[allow_internal_unstable(core_panic)]

library/core/src/panic.rs

+33
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,39 @@ pub macro panic_2021 {
5858
),
5959
}
6060

61+
#[doc(hidden)]
62+
#[unstable(feature = "edition_panic", issue = "none", reason = "use unreachable!() instead")]
63+
#[allow_internal_unstable(core_panic)]
64+
#[rustc_diagnostic_item = "unreachable_2015_macro"]
65+
#[rustc_macro_transparency = "semitransparent"]
66+
pub macro unreachable_2015 {
67+
() => (
68+
$crate::panicking::panic("internal error: entered unreachable code")
69+
),
70+
// Use of `unreachable_display` for non_fmt_panic lint.
71+
// NOTE: the message ("internal error ...") is embeded directly in unreachable_display
72+
($msg:expr $(,)?) => (
73+
$crate::panicking::unreachable_display(&$msg)
74+
),
75+
($fmt:expr, $($arg:tt)*) => (
76+
$crate::panic!($crate::concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
77+
),
78+
}
79+
80+
#[doc(hidden)]
81+
#[unstable(feature = "edition_panic", issue = "none", reason = "use unreachable!() instead")]
82+
#[allow_internal_unstable(core_panic)]
83+
#[rustc_diagnostic_item = "unreachable_2021_macro"]
84+
#[rustc_macro_transparency = "semitransparent"]
85+
pub macro unreachable_2021 {
86+
() => (
87+
$crate::panicking::panic("internal error: entered unreachable code")
88+
),
89+
($($t:tt)+) => (
90+
$crate::panic!("internal error: entered unreachable code: {}", $crate::format_args!($($t)+))
91+
),
92+
}
93+
6194
/// An internal trait used by libstd to pass data from libstd to `panic_unwind`
6295
/// and other panic runtimes. Not intended to be stabilized any time soon, do
6396
/// not use.

library/core/src/panicking.rs

+8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ pub const fn panic_str(expr: &str) -> ! {
5555
panic_display(&expr);
5656
}
5757

58+
#[cfg(not(bootstrap))]
59+
#[inline]
60+
#[track_caller]
61+
#[rustc_diagnostic_item = "unreachable_display"] // needed for `non-fmt-panics` lint
62+
pub fn unreachable_display<T: fmt::Display>(x: &T) -> ! {
63+
panic_fmt(format_args!("internal error: entered unreachable code: {}", *x));
64+
}
65+
5866
#[inline]
5967
#[track_caller]
6068
#[lang = "panic_display"] // needed for const-evaluated panics

src/test/mir-opt/issue_76432.test.SimplifyComparisonIntegral.diff

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
let mut _19: *const T; // in scope 0 at $DIR/issue_76432.rs:9:54: 9:68
2222
let mut _20: *const T; // in scope 0 at $DIR/issue_76432.rs:9:70: 9:84
2323
let mut _21: *const T; // in scope 0 at $DIR/issue_76432.rs:9:70: 9:84
24-
let mut _22: !; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
24+
let mut _22: !; // in scope 0 at $SRC_DIR/core/src/panic.rs:LL:COL
2525
let mut _23: &[T; 3]; // in scope 0 at $DIR/issue_76432.rs:7:19: 7:29
2626
scope 1 {
2727
debug v => _2; // in scope 1 at $DIR/issue_76432.rs:7:9: 7:10
@@ -66,16 +66,16 @@
6666
}
6767

6868
bb1: {
69-
StorageLive(_22); // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
70-
core::panicking::panic(const "internal error: entered unreachable code"); // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
69+
StorageLive(_22); // scope 1 at $SRC_DIR/core/src/panic.rs:LL:COL
70+
core::panicking::panic(const "internal error: entered unreachable code"); // scope 1 at $SRC_DIR/core/src/panic.rs:LL:COL
7171
// mir::Constant
72-
// + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL
72+
// + span: $SRC_DIR/core/src/panic.rs:LL:COL
7373
// + literal: Const { ty: fn(&'static str) -> ! {core::panicking::panic}, val: Value(Scalar(<ZST>)) }
7474
// ty::Const
7575
// + ty: &str
7676
// + val: Value(Slice { data: Allocation { bytes: [105, 110, 116, 101, 114, 110, 97, 108, 32, 101, 114, 114, 111, 114, 58, 32, 101, 110, 116, 101, 114, 101, 100, 32, 117, 110, 114, 101, 97, 99, 104, 97, 98, 108, 101, 32, 99, 111, 100, 101], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [1099511627775], len: Size { raw: 40 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 40 })
7777
// mir::Constant
78-
// + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL
78+
// + span: $SRC_DIR/core/src/panic.rs:LL:COL
7979
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [105, 110, 116, 101, 114, 110, 97, 108, 32, 101, 114, 114, 111, 114, 58, 32, 101, 110, 116, 101, 114, 101, 100, 32, 117, 110, 114, 101, 97, 99, 104, 97, 98, 108, 101, 32, 99, 111, 100, 101], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [1099511627775], len: Size { raw: 40 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 40 }) }
8080
}
8181

src/test/ui/consts/const-eval/const_panic.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ error[E0080]: evaluation of constant value failed
2020
LL | const Y: () = std::unreachable!();
2121
| ^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:12:15
2222
|
23-
= note: this error originates in the macro `std::unreachable` (in Nightly builds, run with -Z macro-backtrace for more info)
23+
= note: this error originates in the macro `$crate::panic::unreachable_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
2424

2525
error[E0080]: evaluation of constant value failed
2626
--> $DIR/const_panic.rs:15:15
@@ -68,7 +68,7 @@ error[E0080]: evaluation of constant value failed
6868
LL | const Y_CORE: () = core::unreachable!();
6969
| ^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic.rs:30:20
7070
|
71-
= note: this error originates in the macro `core::unreachable` (in Nightly builds, run with -Z macro-backtrace for more info)
71+
= note: this error originates in the macro `$crate::panic::unreachable_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
7272

7373
error[E0080]: evaluation of constant value failed
7474
--> $DIR/const_panic.rs:33:20

src/test/ui/consts/const-eval/const_panic_2021.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ error[E0080]: evaluation of constant value failed
2020
LL | const C: () = std::unreachable!();
2121
| ^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic_2021.rs:12:15
2222
|
23-
= note: this error originates in the macro `std::unreachable` (in Nightly builds, run with -Z macro-backtrace for more info)
23+
= note: this error originates in the macro `$crate::panic::unreachable_2021` (in Nightly builds, run with -Z macro-backtrace for more info)
2424

2525
error[E0080]: evaluation of constant value failed
2626
--> $DIR/const_panic_2021.rs:15:15
@@ -60,7 +60,7 @@ error[E0080]: evaluation of constant value failed
6060
LL | const C_CORE: () = core::unreachable!();
6161
| ^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic_2021.rs:27:20
6262
|
63-
= note: this error originates in the macro `core::unreachable` (in Nightly builds, run with -Z macro-backtrace for more info)
63+
= note: this error originates in the macro `$crate::panic::unreachable_2021` (in Nightly builds, run with -Z macro-backtrace for more info)
6464

6565
error[E0080]: evaluation of constant value failed
6666
--> $DIR/const_panic_2021.rs:30:20

src/test/ui/consts/const-eval/const_panic_libcore_bin.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ error[E0080]: evaluation of constant value failed
1212
LL | const Y: () = unreachable!();
1313
| ^^^^^^^^^^^^^^ the evaluated program panicked at 'internal error: entered unreachable code', $DIR/const_panic_libcore_bin.rs:11:15
1414
|
15-
= note: this error originates in the macro `unreachable` (in Nightly builds, run with -Z macro-backtrace for more info)
15+
= note: this error originates in the macro `$crate::panic::unreachable_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
1616

1717
error[E0080]: evaluation of constant value failed
1818
--> $DIR/const_panic_libcore_bin.rs:14:15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-fail
2+
// ignore-emscripten no processes
3+
4+
// revisions: edition_2015 edition_2021
5+
// [edition_2015]edition:2015
6+
// [edition_2021]edition:2021
7+
// [edition_2015]error-pattern:internal error: entered unreachable code: x is {x}
8+
// [edition_2021]error-pattern:internal error: entered unreachable code: x is 5
9+
10+
fn main() {
11+
let x = 5;
12+
unreachable!("x is {x}");
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: there is no argument named `x`
2+
--> $DIR/unreachable-format-args.rs:13:5
3+
|
4+
LL | unreachable!("x is {x} and y is {y}", y = 0);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: did you intend to capture a variable `x` from the surrounding scope?
8+
= note: to avoid ambiguity, `format_args!` cannot capture variables when the format string is expanded from a macro
9+
= note: this error originates in the macro `$crate::concat` (in Nightly builds, run with -Z macro-backtrace for more info)
10+
11+
error: aborting due to previous error
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// ignore-emscripten no processes
2+
3+
// revisions: edition_2015 edition_2021
4+
// [edition_2015]edition:2015
5+
// [edition_2021]edition:2021
6+
// [edition_2015]check-fail
7+
// [edition_2021]run-fail
8+
// [edition_2015]error-pattern:there is no argument named `x`
9+
// [edition_2021]error-pattern:internal error: entered unreachable code: x is 5 and y is 0
10+
11+
fn main() {
12+
let x = 5;
13+
unreachable!("x is {x} and y is {y}", y = 0);
14+
}

src/test/ui/proc-macro/quote-debug.stdout

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ fn main() {
3535
lit.set_span(crate::Span::recover_proc_macro_span(2));
3636
lit
3737
} else {
38-
{
39-
::core::panicking::panic("internal error: entered unreachable code")
40-
}
38+
::core::panicking::panic("internal error: entered unreachable code")
4139
}
4240
})),
4341
crate::TokenStream::from(crate::TokenTree::Punct(crate::Punct::new('\u{3b}',

0 commit comments

Comments
 (0)