Skip to content

Commit 6009a68

Browse files
committed
Optimize in macro call directly
This excludes `panic_2015!` as calling it in the same manner results in triggering the edition lint in a few places.
1 parent e890c3b commit 6009a68

File tree

8 files changed

+26
-77
lines changed

8 files changed

+26
-77
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3509,7 +3509,6 @@ dependencies = [
35093509
name = "rustc_builtin_macros"
35103510
version = "0.0.0"
35113511
dependencies = [
3512-
"rustc-literal-escaper",
35133512
"rustc_ast",
35143513
"rustc_ast_pretty",
35153514
"rustc_attr_parsing",

compiler/rustc_builtin_macros/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ doctest = false
88

99
[dependencies]
1010
# tidy-alphabetical-start
11-
rustc-literal-escaper = "0.0.5"
1211
rustc_ast = { path = "../rustc_ast" }
1312
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1413
rustc_attr_parsing = { path = "../rustc_attr_parsing" }

compiler/rustc_builtin_macros/src/edition_panic.rs

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use rustc_ast::token::{Delimiter, Lit, LitKind, TokenKind};
2-
use rustc_ast::tokenstream::{TokenStream, TokenTree};
1+
use rustc_ast::token::Delimiter;
2+
use rustc_ast::tokenstream::TokenStream;
33
use rustc_ast::*;
44
use rustc_expand::base::*;
55
use rustc_span::edition::Edition;
6-
use rustc_span::{Ident, Span, Symbol, sym};
6+
use rustc_span::{Span, Symbol, sym};
77

88
// Use an enum to ensure that no new macro calls are added without also updating the message in the
99
// optimized path below.
@@ -67,55 +67,6 @@ fn expand<'cx>(
6767
) -> MacroExpanderResult<'cx> {
6868
let sp = cx.with_call_site_ctxt(sp);
6969

70-
// If the call is of the form `panic!(<string literal>)` and there are no formatting arguments
71-
// in the string literal, we can call `core::panicking::panic` to centralize the panic logic.
72-
if tts.len() == 1
73-
&& let Some(TokenTree::Token(token, _)) = tts.get(0)
74-
&& let TokenKind::Literal(lit) = &token.kind
75-
&& let Lit { kind: LitKind::Str | LitKind::StrRaw(_), symbol, .. } = lit
76-
&& let msg = symbol.as_str()
77-
&& !msg.contains(|c| c == '{' || c == '}')
78-
{
79-
let msg = match mac {
80-
InnerCall::Panic2015 | InnerCall::Panic2021 => cx.expr(sp, ExprKind::Lit(*lit)),
81-
InnerCall::Unreachable2015 | InnerCall::Unreachable2021 => {
82-
let msg = if msg.contains('\\') {
83-
let mut buf = String::with_capacity(msg.len());
84-
// Force-inlining here is aggressive but the closure is
85-
// called on every char in the string, so it can be hot in
86-
// programs with many long strings containing escapes.
87-
rustc_literal_escaper::unescape_str(
88-
msg,
89-
#[inline(always)]
90-
|_, res| match res {
91-
Ok(c) => buf.push(c),
92-
Err(err) => {
93-
assert!(!err.is_fatal(), "failed to unescape string literal")
94-
}
95-
},
96-
);
97-
buf
98-
} else {
99-
msg.to_owned()
100-
};
101-
102-
cx.expr_str(
103-
sp,
104-
Symbol::intern(&format!("internal error: entered unreachable code: {msg}")),
105-
)
106-
}
107-
};
108-
109-
return ExpandResult::Ready(MacEager::expr(cx.expr_call(
110-
sp,
111-
cx.expr_path(cx.path_global(
112-
sp,
113-
[sym::core, sym::panicking, sym::panic].map(|sym| Ident::new(sym, sp)).to_vec(),
114-
)),
115-
[msg].into(),
116-
)));
117-
}
118-
11970
ExpandResult::Ready(MacEager::expr(
12071
cx.expr_macro_call(
12172
sp,

library/core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@
184184
#![feature(staged_api)]
185185
#![feature(stmt_expr_attributes)]
186186
#![feature(strict_provenance_lints)]
187+
#![feature(super_let)]
187188
#![feature(trait_alias)]
188189
#![feature(transparent_unions)]
189190
#![feature(try_blocks)]

library/core/src/panic.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ pub macro panic_2021 {
5959
($($t:tt)+) => ({
6060
// Semicolon to prevent temporaries inside the formatting machinery from
6161
// being considered alive in the caller after the panic_fmt call.
62-
$crate::panicking::panic_fmt($crate::const_format_args!($($t)+));
62+
super let args = $crate::const_format_args!($($t)+);
63+
if let $crate::option::Option::Some(s) = args.as_str() {
64+
$crate::panicking::panic(s);
65+
}
66+
$crate::panicking::panic_fmt(args);
6367
}),
6468
}
6569

tests/ui/consts/const-eval/const_panic-normalize-tabs-115498.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
struct Bug([u8; panic!{"\t"}]);
44
//~^ ERROR evaluation panicked
55
//~| NOTE: in this expansion of panic!
6-
//~| NOTE: failed here

tests/ui/panics/short-ice-remove-middle-frames-2.run.stderr

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
thread 'main' ($TID) panicked at $DIR/short-ice-remove-middle-frames-2.rs:62:5:
33
debug!!!
44
stack backtrace:
5-
0: __rustc::rust_begin_unwind
6-
1: core::panicking::panic_fmt
7-
2: core::panicking::panic
8-
3: short_ice_remove_middle_frames_2::eight
9-
4: short_ice_remove_middle_frames_2::seven::{{closure}}
5+
0: std::panicking::begin_panic
6+
1: short_ice_remove_middle_frames_2::eight
7+
2: short_ice_remove_middle_frames_2::seven::{{closure}}
108
[... omitted 3 frames ...]
11-
5: short_ice_remove_middle_frames_2::fifth
12-
6: short_ice_remove_middle_frames_2::fourth::{{closure}}
9+
3: short_ice_remove_middle_frames_2::fifth
10+
4: short_ice_remove_middle_frames_2::fourth::{{closure}}
1311
[... omitted 4 frames ...]
14-
7: short_ice_remove_middle_frames_2::first
15-
8: short_ice_remove_middle_frames_2::main
16-
9: core::ops::function::FnOnce::call_once
12+
5: short_ice_remove_middle_frames_2::first
13+
6: short_ice_remove_middle_frames_2::main
14+
7: core::ops::function::FnOnce::call_once
1715
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

tests/ui/panics/short-ice-remove-middle-frames.run.stderr

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
thread 'main' ($TID) panicked at $DIR/short-ice-remove-middle-frames.rs:58:5:
33
debug!!!
44
stack backtrace:
5-
0: __rustc::rust_begin_unwind
6-
1: core::panicking::panic_fmt
7-
2: core::panicking::panic
8-
3: short_ice_remove_middle_frames::seven
9-
4: short_ice_remove_middle_frames::sixth
10-
5: short_ice_remove_middle_frames::fifth::{{closure}}
5+
0: std::panicking::begin_panic
6+
1: short_ice_remove_middle_frames::seven
7+
2: short_ice_remove_middle_frames::sixth
8+
3: short_ice_remove_middle_frames::fifth::{{closure}}
119
[... omitted 4 frames ...]
12-
6: short_ice_remove_middle_frames::second
13-
7: short_ice_remove_middle_frames::first::{{closure}}
14-
8: short_ice_remove_middle_frames::first
15-
9: short_ice_remove_middle_frames::main
16-
10: core::ops::function::FnOnce::call_once
10+
4: short_ice_remove_middle_frames::second
11+
5: short_ice_remove_middle_frames::first::{{closure}}
12+
6: short_ice_remove_middle_frames::first
13+
7: short_ice_remove_middle_frames::main
14+
8: core::ops::function::FnOnce::call_once
1715
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

0 commit comments

Comments
 (0)