Skip to content

Commit a1c41e9

Browse files
committed
Expand assert!(expr, args..) to include $crate for hygiene on 2021.
Before 2021, this was a breaking change, as std::panic and core::panic are different. In edition 2021 they will be identical, making it possible again to apply proper hygiene here.
1 parent 1f9dc9a commit a1c41e9

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

compiler/rustc_builtin_macros/src/assert.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,34 @@ pub fn expand_assert<'cx>(
2323
}
2424
};
2525

26+
let is_2021 = sp.rust_2021();
27+
2628
// `core::panic` and `std::panic` are different macros, so we use call-site
2729
// context to pick up whichever is currently in scope.
2830
let sp = cx.with_call_site_ctxt(sp);
2931

3032
let panic_call = if let Some(tokens) = custom_message {
33+
let path = if is_2021 {
34+
// On edition 2021, we always call `$crate::panic!()`.
35+
Path {
36+
span: sp,
37+
segments: cx
38+
.std_path(&[sym::panic])
39+
.into_iter()
40+
.map(|ident| PathSegment::from_ident(ident))
41+
.collect(),
42+
tokens: None,
43+
}
44+
} else {
45+
// Before edition 2021, we call `panic!()` unqualified,
46+
// such that it calls either `std::panic!()` or `core::panic!()`.
47+
Path::from_ident(Ident::new(sym::panic, sp))
48+
};
3149
// Pass the custom message to panic!().
3250
cx.expr(
3351
sp,
3452
ExprKind::MacCall(MacCall {
35-
path: Path::from_ident(Ident::new(sym::panic, sp)),
53+
path,
3654
args: P(MacArgs::Delimited(
3755
DelimSpan::from_single(sp),
3856
MacDelimiter::Parenthesis,

0 commit comments

Comments
 (0)