diff options
| author | Mara Bos <m-ou.se@m-ou.se> | 2021-01-09 20:53:24 +0100 |
|---|---|---|
| committer | Mara Bos <m-ou.se@m-ou.se> | 2021-01-09 20:53:24 +0100 |
| commit | a1c41e9ca7245e7d8ccd72c16c1e36b9d7e729f3 (patch) | |
| tree | 46076464caaf2fd77b341efc65036439912d273b /compiler/rustc_builtin_macros/src/assert.rs | |
| parent | 1f9dc9a1821d55b1641c517feac7fcd6ac76aadc (diff) | |
| download | rust-a1c41e9ca7245e7d8ccd72c16c1e36b9d7e729f3.tar.gz rust-a1c41e9ca7245e7d8ccd72c16c1e36b9d7e729f3.zip | |
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.
Diffstat (limited to 'compiler/rustc_builtin_macros/src/assert.rs')
| -rw-r--r-- | compiler/rustc_builtin_macros/src/assert.rs | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/compiler/rustc_builtin_macros/src/assert.rs b/compiler/rustc_builtin_macros/src/assert.rs index bb6d3f6a007..3cffc7a75ee 100644 --- a/compiler/rustc_builtin_macros/src/assert.rs +++ b/compiler/rustc_builtin_macros/src/assert.rs @@ -23,16 +23,34 @@ pub fn expand_assert<'cx>( } }; + let is_2021 = sp.rust_2021(); + // `core::panic` and `std::panic` are different macros, so we use call-site // context to pick up whichever is currently in scope. let sp = cx.with_call_site_ctxt(sp); let panic_call = if let Some(tokens) = custom_message { + let path = if is_2021 { + // On edition 2021, we always call `$crate::panic!()`. + Path { + span: sp, + segments: cx + .std_path(&[sym::panic]) + .into_iter() + .map(|ident| PathSegment::from_ident(ident)) + .collect(), + tokens: None, + } + } else { + // Before edition 2021, we call `panic!()` unqualified, + // such that it calls either `std::panic!()` or `core::panic!()`. + Path::from_ident(Ident::new(sym::panic, sp)) + }; // Pass the custom message to panic!(). cx.expr( sp, ExprKind::MacCall(MacCall { - path: Path::from_ident(Ident::new(sym::panic, sp)), + path, args: P(MacArgs::Delimited( DelimSpan::from_single(sp), MacDelimiter::Parenthesis, |
