Skip to content

Commit e8ef15d

Browse files
Rollup merge of #80855 - m-ou-se:assert-2021, r=petrochenkov
Expand assert!(expr, args..) to include $crate for hygiene on 2021. This makes `assert!(expr, args..)` properly hygienic in Rust 2021. This is part of rust-lang/rfcs#3007, see #80162. Before edition 2021, this was a breaking change, as `std::panic` and `core::panic` are different. In edition 2021 they will be identical, making it possible to apply proper hygiene here.
2 parents 3ed8a37 + a730970 commit e8ef15d

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

compiler/rustc_builtin_macros/src/assert.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,43 @@ use rustc_span::{Span, DUMMY_SP};
1212

1313
pub fn expand_assert<'cx>(
1414
cx: &'cx mut ExtCtxt<'_>,
15-
sp: Span,
15+
span: Span,
1616
tts: TokenStream,
1717
) -> Box<dyn MacResult + 'cx> {
18-
let Assert { cond_expr, custom_message } = match parse_assert(cx, sp, tts) {
18+
let Assert { cond_expr, custom_message } = match parse_assert(cx, span, tts) {
1919
Ok(assert) => assert,
2020
Err(mut err) => {
2121
err.emit();
22-
return DummyResult::any(sp);
22+
return DummyResult::any(span);
2323
}
2424
};
2525

2626
// `core::panic` and `std::panic` are different macros, so we use call-site
2727
// context to pick up whichever is currently in scope.
28-
let sp = cx.with_call_site_ctxt(sp);
28+
let sp = cx.with_call_site_ctxt(span);
2929

3030
let panic_call = if let Some(tokens) = custom_message {
31+
let path = if span.rust_2021() {
32+
// On edition 2021, we always call `$crate::panic!()`.
33+
Path {
34+
span: sp,
35+
segments: cx
36+
.std_path(&[sym::panic])
37+
.into_iter()
38+
.map(|ident| PathSegment::from_ident(ident))
39+
.collect(),
40+
tokens: None,
41+
}
42+
} else {
43+
// Before edition 2021, we call `panic!()` unqualified,
44+
// such that it calls either `std::panic!()` or `core::panic!()`.
45+
Path::from_ident(Ident::new(sym::panic, sp))
46+
};
3147
// Pass the custom message to panic!().
3248
cx.expr(
3349
sp,
3450
ExprKind::MacCall(MacCall {
35-
path: Path::from_ident(Ident::new(sym::panic, sp)),
51+
path,
3652
args: P(MacArgs::Delimited(
3753
DelimSpan::from_single(sp),
3854
MacDelimiter::Parenthesis,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// check-pass
2+
// edition:2021
3+
4+
#![no_implicit_prelude]
5+
6+
fn main() {
7+
assert!(true, "hoi");
8+
assert!(false, "hoi {}", 123);
9+
}

0 commit comments

Comments
 (0)