Skip to content

Commit 3beb2e9

Browse files
committed
Expand assert!(expr) to panic() function instead of panic!() macro.
The panic message might contain braces which should never be interpreted as format placeholders, which panic!() will do in a future edition.
1 parent f228efc commit 3beb2e9

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

compiler/rustc_builtin_macros/src/assert.rs

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use rustc_errors::{Applicability, DiagnosticBuilder};
22

33
use rustc_ast::ptr::P;
4-
use rustc_ast::token::{self, TokenKind};
5-
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
4+
use rustc_ast::token;
5+
use rustc_ast::tokenstream::{DelimSpan, TokenStream};
66
use rustc_ast::{self as ast, *};
77
use rustc_ast_pretty::pprust;
88
use rustc_expand::base::*;
@@ -26,31 +26,41 @@ pub fn expand_assert<'cx>(
2626
// `core::panic` and `std::panic` are different macros, so we use call-site
2727
// context to pick up whichever is currently in scope.
2828
let sp = cx.with_call_site_ctxt(sp);
29-
let tokens = custom_message.unwrap_or_else(|| {
30-
TokenStream::from(TokenTree::token(
31-
TokenKind::lit(
32-
token::Str,
33-
Symbol::intern(&format!(
34-
"assertion failed: {}",
35-
pprust::expr_to_string(&cond_expr).escape_debug()
36-
)),
37-
None,
38-
),
39-
DUMMY_SP,
40-
))
41-
});
42-
let args = P(MacArgs::Delimited(DelimSpan::from_single(sp), MacDelimiter::Parenthesis, tokens));
43-
let panic_call = MacCall {
44-
path: Path::from_ident(Ident::new(sym::panic, sp)),
45-
args,
46-
prior_type_ascription: None,
29+
30+
let panic_call = {
31+
if let Some(tokens) = custom_message {
32+
// Pass the custom message to panic!().
33+
cx.expr(
34+
sp,
35+
ExprKind::MacCall(MacCall {
36+
path: Path::from_ident(Ident::new(sym::panic, sp)),
37+
args: P(MacArgs::Delimited(
38+
DelimSpan::from_single(sp),
39+
MacDelimiter::Parenthesis,
40+
tokens,
41+
)),
42+
prior_type_ascription: None,
43+
}),
44+
)
45+
} else {
46+
// Pass our own message directly to $crate::panicking::panic(),
47+
// because it might contain `{` and `}` that should always be
48+
// passed literally.
49+
cx.expr_call_global(
50+
sp,
51+
cx.std_path(&[sym::panicking, sym::panic]),
52+
vec![cx.expr_str(
53+
DUMMY_SP,
54+
Symbol::intern(&format!(
55+
"assertion failed: {}",
56+
pprust::expr_to_string(&cond_expr).escape_debug()
57+
)),
58+
)],
59+
)
60+
}
4761
};
48-
let if_expr = cx.expr_if(
49-
sp,
50-
cx.expr(sp, ExprKind::Unary(UnOp::Not, cond_expr)),
51-
cx.expr(sp, ExprKind::MacCall(panic_call)),
52-
None,
53-
);
62+
let if_expr =
63+
cx.expr_if(sp, cx.expr(sp, ExprKind::Unary(UnOp::Not, cond_expr)), panic_call, None);
5464
MacEager::expr(if_expr)
5565
}
5666

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ symbols! {
791791
panic_runtime,
792792
panic_str,
793793
panic_unwind,
794+
panicking,
794795
param_attrs,
795796
parent_trait,
796797
partial_cmp,

library/core/src/macros/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,7 @@ pub(crate) mod builtin {
12171217
#[rustc_builtin_macro]
12181218
#[macro_export]
12191219
#[rustc_diagnostic_item = "assert_macro"]
1220+
#[allow_internal_unstable(core_panic)]
12201221
macro_rules! assert {
12211222
($cond:expr $(,)?) => {{ /* compiler built-in */ }};
12221223
($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }};

0 commit comments

Comments
 (0)