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.
This commit is contained in:
commit
e8ef15d9d1
2 changed files with 30 additions and 5 deletions
|
@ -12,27 +12,43 @@ use rustc_span::{Span, DUMMY_SP};
|
||||||
|
|
||||||
pub fn expand_assert<'cx>(
|
pub fn expand_assert<'cx>(
|
||||||
cx: &'cx mut ExtCtxt<'_>,
|
cx: &'cx mut ExtCtxt<'_>,
|
||||||
sp: Span,
|
span: Span,
|
||||||
tts: TokenStream,
|
tts: TokenStream,
|
||||||
) -> Box<dyn MacResult + 'cx> {
|
) -> Box<dyn MacResult + 'cx> {
|
||||||
let Assert { cond_expr, custom_message } = match parse_assert(cx, sp, tts) {
|
let Assert { cond_expr, custom_message } = match parse_assert(cx, span, tts) {
|
||||||
Ok(assert) => assert,
|
Ok(assert) => assert,
|
||||||
Err(mut err) => {
|
Err(mut err) => {
|
||||||
err.emit();
|
err.emit();
|
||||||
return DummyResult::any(sp);
|
return DummyResult::any(span);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// `core::panic` and `std::panic` are different macros, so we use call-site
|
// `core::panic` and `std::panic` are different macros, so we use call-site
|
||||||
// context to pick up whichever is currently in scope.
|
// context to pick up whichever is currently in scope.
|
||||||
let sp = cx.with_call_site_ctxt(sp);
|
let sp = cx.with_call_site_ctxt(span);
|
||||||
|
|
||||||
let panic_call = if let Some(tokens) = custom_message {
|
let panic_call = if let Some(tokens) = custom_message {
|
||||||
|
let path = if span.rust_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!().
|
// Pass the custom message to panic!().
|
||||||
cx.expr(
|
cx.expr(
|
||||||
sp,
|
sp,
|
||||||
ExprKind::MacCall(MacCall {
|
ExprKind::MacCall(MacCall {
|
||||||
path: Path::from_ident(Ident::new(sym::panic, sp)),
|
path,
|
||||||
args: P(MacArgs::Delimited(
|
args: P(MacArgs::Delimited(
|
||||||
DelimSpan::from_single(sp),
|
DelimSpan::from_single(sp),
|
||||||
MacDelimiter::Parenthesis,
|
MacDelimiter::Parenthesis,
|
||||||
|
|
9
src/test/ui/hygiene/no_implicit_prelude-2021.rs
Normal file
9
src/test/ui/hygiene/no_implicit_prelude-2021.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
// check-pass
|
||||||
|
// edition:2021
|
||||||
|
|
||||||
|
#![no_implicit_prelude]
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
assert!(true, "hoi");
|
||||||
|
assert!(false, "hoi {}", 123);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue