Skip to content

Commit 53c7f3f

Browse files
committed
Implement new panic!() behaviour for Rust 2021.
1 parent da75d32 commit 53c7f3f

File tree

9 files changed

+144
-6
lines changed

9 files changed

+144
-6
lines changed

compiler/rustc_builtin_macros/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ mod global_allocator;
3434
mod global_asm;
3535
mod llvm_asm;
3636
mod log_syntax;
37+
mod panic;
3738
mod source_util;
3839
mod test;
3940
mod trace_macros;
@@ -82,6 +83,8 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand, edition: Editi
8283
log_syntax: log_syntax::expand_log_syntax,
8384
module_path: source_util::expand_mod,
8485
option_env: env::expand_option_env,
86+
core_panic_macro: panic::expand_panic,
87+
std_panic_macro: panic::expand_panic,
8588
stringify: source_util::expand_stringify,
8689
trace_macros: trace_macros::expand_trace_macros,
8790
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use rustc_ast::ptr::P;
2+
use rustc_ast::tokenstream::{DelimSpan, TokenStream};
3+
use rustc_ast::*;
4+
use rustc_expand::base::*;
5+
use rustc_span::symbol::sym;
6+
use rustc_span::Span;
7+
8+
pub fn expand_panic<'cx>(
9+
cx: &'cx mut ExtCtxt<'_>,
10+
sp: Span,
11+
tts: TokenStream,
12+
) -> Box<dyn MacResult + 'cx> {
13+
let panic = if sp.rust_2021() { sym::panic_2021 } else { sym::panic_2015 };
14+
15+
let sp = cx.with_call_site_ctxt(sp);
16+
17+
MacEager::expr(
18+
cx.expr(
19+
sp,
20+
ExprKind::MacCall(MacCall {
21+
path: Path {
22+
span: sp,
23+
segments: cx
24+
.std_path(&[sym::panic, panic])
25+
.into_iter()
26+
.map(|ident| PathSegment::from_ident(ident))
27+
.collect(),
28+
tokens: None,
29+
},
30+
args: P(MacArgs::Delimited(
31+
DelimSpan::from_single(sp),
32+
MacDelimiter::Parenthesis,
33+
tts,
34+
)),
35+
prior_type_ascription: None,
36+
}),
37+
),
38+
)
39+
}

compiler/rustc_lint/src/panic_fmt.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ declare_lint! {
1919
///
2020
/// ### Explanation
2121
///
22-
/// `panic!("{}")` panics with the message `"{}"`, as a `panic!()` invocation
23-
/// with a single argument does not use `format_args!()`.
24-
/// A future edition of Rust will interpret this string as format string,
25-
/// which would break this.
22+
/// In Rust 2018 and earlier, `panic!("{}")` panics with the message `"{}"`,
23+
/// as a `panic!()` invocation with a single argument does not use `format_args!()`.
24+
/// Rust 2021 interprets this string as format string, which breaks this.
2625
PANIC_FMT,
2726
Warn,
2827
"detect braces in single-argument panic!() invocations",
@@ -50,8 +49,8 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
5049
if let ast::LitKind::Str(sym, _) = lit.node {
5150
let mut expn = f.span.ctxt().outer_expn_data();
5251
if let Some(id) = expn.macro_def_id {
53-
if cx.tcx.is_diagnostic_item(sym::std_panic_macro, id)
54-
|| cx.tcx.is_diagnostic_item(sym::core_panic_macro, id)
52+
if cx.tcx.is_diagnostic_item(sym::std_panic_2015_macro, id)
53+
|| cx.tcx.is_diagnostic_item(sym::core_panic_2015_macro, id)
5554
{
5655
let fmt = sym.as_str();
5756
if !fmt.contains(&['{', '}'][..]) {

compiler/rustc_span/src/symbol.rs

+4
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ symbols! {
398398
copysignf64,
399399
core,
400400
core_intrinsics,
401+
core_panic_2015_macro,
401402
core_panic_macro,
402403
cosf32,
403404
cosf64,
@@ -794,6 +795,8 @@ symbols! {
794795
owned_box,
795796
packed,
796797
panic,
798+
panic_2015,
799+
panic_2021,
797800
panic_abort,
798801
panic_bounds_check,
799802
panic_handler,
@@ -1095,6 +1098,7 @@ symbols! {
10951098
staticlib,
10961099
std,
10971100
std_inject,
1101+
std_panic_2015_macro,
10981102
std_panic_macro,
10991103
stmt,
11001104
stmt_expr_attributes,

library/core/src/macros/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[cfg(bootstrap)]
12
#[doc(include = "panic.md")]
23
#[macro_export]
34
#[allow_internal_unstable(core_panic)]
@@ -18,6 +19,19 @@ macro_rules! panic {
1819
);
1920
}
2021

22+
#[cfg(not(bootstrap))]
23+
#[doc(include = "panic.md")]
24+
#[macro_export]
25+
#[rustc_builtin_macro = "core_panic_macro"]
26+
#[allow_internal_unstable(edition_panic)]
27+
#[stable(feature = "core", since = "1.6.0")]
28+
#[rustc_diagnostic_item = "core_panic_macro"]
29+
macro_rules! panic {
30+
// Expands to either `$crate::panic_2015` or `$crate::panic_2021`
31+
// depending on the edition of the caller.
32+
($($arg:tt)*) => { /* compiler built-in */ };
33+
}
34+
2135
/// Asserts that two expressions are equal to each other (using [`PartialEq`]).
2236
///
2337
/// On panic, this macro will print the values of the expressions with their

library/core/src/panic.rs

+34
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,40 @@
55
use crate::any::Any;
66
use crate::fmt;
77

8+
#[doc(hidden)]
9+
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
10+
#[allow_internal_unstable(core_panic)]
11+
#[rustc_diagnostic_item = "core_panic_2015_macro"]
12+
#[rustc_macro_transparency = "semitransparent"]
13+
pub macro panic_2015 {
14+
() => (
15+
$crate::panicking::panic("explicit panic")
16+
),
17+
($msg:literal $(,)?) => (
18+
$crate::panicking::panic($msg)
19+
),
20+
($msg:expr $(,)?) => (
21+
$crate::panicking::panic_str($msg)
22+
),
23+
($fmt:expr, $($arg:tt)+) => (
24+
$crate::panicking::panic_fmt($crate::format_args!($fmt, $($arg)+))
25+
),
26+
}
27+
28+
#[doc(hidden)]
29+
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
30+
#[allow_internal_unstable(core_panic)]
31+
#[rustc_diagnostic_item = "core_panic_2021_macro"]
32+
#[rustc_macro_transparency = "semitransparent"]
33+
pub macro panic_2021 {
34+
() => (
35+
$crate::panicking::panic("explicit panic")
36+
),
37+
($($t:tt)+) => (
38+
$crate::panicking::panic_fmt($crate::format_args!($($t)+))
39+
),
40+
}
41+
842
/// A struct providing information about a panic.
943
///
1044
/// `PanicInfo` structure is passed to a panic hook set by the [`set_hook`]

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@
258258
#![feature(dropck_eyepatch)]
259259
#![feature(duration_constants)]
260260
#![feature(duration_zero)]
261+
#![feature(edition_panic)]
261262
#![feature(exact_size_is_empty)]
262263
#![feature(exhaustive_patterns)]
263264
#![feature(extend_one)]

library/std/src/macros.rs

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! library. Each macro is available for use when linking against the standard
55
//! library.
66
7+
#[cfg(bootstrap)]
78
#[doc(include = "../../core/src/macros/panic.md")]
89
#[macro_export]
910
#[stable(feature = "rust1", since = "1.0.0")]
@@ -17,6 +18,19 @@ macro_rules! panic {
1718
});
1819
}
1920

21+
#[cfg(not(bootstrap))]
22+
#[doc(include = "../../core/src/macros/panic.md")]
23+
#[macro_export]
24+
#[rustc_builtin_macro = "std_panic_macro"]
25+
#[stable(feature = "rust1", since = "1.0.0")]
26+
#[allow_internal_unstable(edition_panic)]
27+
#[cfg_attr(not(test), rustc_diagnostic_item = "std_panic_macro")]
28+
macro_rules! panic {
29+
// Expands to either `$crate::panic_2015` or `$crate::panic_2021`
30+
// depending on the edition of the caller.
31+
($($arg:tt)*) => { /* compiler built-in */ };
32+
}
33+
2034
/// Prints to the standard output.
2135
///
2236
/// Equivalent to the [`println!`] macro except that a newline is not printed at

library/std/src/panic.rs

+30
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,36 @@ use crate::sync::{Arc, Mutex, RwLock};
1717
use crate::task::{Context, Poll};
1818
use crate::thread::Result;
1919

20+
#[doc(hidden)]
21+
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
22+
#[allow_internal_unstable(libstd_sys_internals)]
23+
#[cfg_attr(not(test), rustc_diagnostic_item = "std_panic_2015_macro")]
24+
#[rustc_macro_transparency = "semitransparent"]
25+
pub macro panic_2015 {
26+
() => ({
27+
$crate::rt::begin_panic("explicit panic")
28+
}),
29+
($msg:expr $(,)?) => ({
30+
$crate::rt::begin_panic($msg)
31+
}),
32+
($fmt:expr, $($arg:tt)+) => ({
33+
$crate::rt::begin_panic_fmt(&$crate::format_args!($fmt, $($arg)+))
34+
}),
35+
}
36+
37+
#[doc(hidden)]
38+
#[unstable(feature = "edition_panic", issue = "none", reason = "use panic!() instead")]
39+
#[allow_internal_unstable(libstd_sys_internals)]
40+
#[rustc_macro_transparency = "semitransparent"]
41+
pub macro panic_2021 {
42+
() => ({
43+
$crate::rt::begin_panic("explicit panic")
44+
}),
45+
($($t:tt)+) => ({
46+
$crate::rt::begin_panic_fmt(&$crate::format_args!($($t)+))
47+
}),
48+
}
49+
2050
#[stable(feature = "panic_hooks", since = "1.10.0")]
2151
pub use crate::panicking::{set_hook, take_hook};
2252

0 commit comments

Comments
 (0)