Rollup merge of #61629 - petrochenkov:stdmac, r=alexcrichton
Hygienize macros in the standard library Same as https://github.com/rust-lang/rust/pull/55597, but for all macros in the standard library. Nested macro calls will now call what they are intended to call rather than whatever is in the closest scope at call site. Technically this is a breaking change, so crater run would probably be useful. --- One exception that is not hygienized is calls to `panic!(...)`. Macros defined in libcore do not want to call `core::panic`. What they really want to call is either `std::panic` or `core::panic` depending on `no_std` settings. EDIT: After some thought, recursive calls to `panic` from `panic` itself probably do want to use `$crate` (UPDATE: done). Calling `std::panic` from macros defined in std and "whatever `panic` is in scope" from macros defined in libcore is probably even worse than always calling "whatever `panic` is in scope", so I kept the existing code. The only way to do the std/core switch correctly that I'm aware of is to define a built-in panic macro that would dispatch to `std::panic` or `core::panic` using compiler magic. Then standard library macros could delegate to this built-in macro. The macro could be named `panic` too, that would fix https://github.com/rust-lang/rust/issues/61567. (This PR doesn't do that.) --- cc https://github.com/rust-lang/rust/issues/56389 cc https://github.com/rust-lang/rust/issues/61567 Fixes https://github.com/rust-lang/rust/issues/61699 r? @alexcrichton
This commit is contained in:
commit
96b58301a3
@ -42,7 +42,7 @@ macro_rules! vec {
|
||||
($($x:expr),*) => (
|
||||
<[_]>::into_vec(box [$($x),*])
|
||||
);
|
||||
($($x:expr,)*) => (vec![$($x),*])
|
||||
($($x:expr,)*) => ($crate::vec![$($x),*])
|
||||
}
|
||||
|
||||
// HACK(japaric): with cfg(test) the inherent `[T]::into_vec` method, which is
|
||||
|
@ -6,13 +6,13 @@
|
||||
#[stable(feature = "core", since = "1.6.0")]
|
||||
macro_rules! panic {
|
||||
() => (
|
||||
panic!("explicit panic")
|
||||
$crate::panic!("explicit panic")
|
||||
);
|
||||
($msg:expr) => ({
|
||||
$crate::panicking::panic(&($msg, file!(), line!(), __rust_unstable_column!()))
|
||||
});
|
||||
($msg:expr,) => (
|
||||
panic!($msg)
|
||||
$crate::panic!($msg)
|
||||
);
|
||||
($fmt:expr, $($arg:tt)+) => ({
|
||||
$crate::panicking::panic_fmt(format_args!($fmt, $($arg)*),
|
||||
@ -58,7 +58,7 @@ macro_rules! assert_eq {
|
||||
}
|
||||
});
|
||||
($left:expr, $right:expr,) => ({
|
||||
assert_eq!($left, $right)
|
||||
$crate::assert_eq!($left, $right)
|
||||
});
|
||||
($left:expr, $right:expr, $($arg:tt)+) => ({
|
||||
match (&($left), &($right)) {
|
||||
@ -115,7 +115,7 @@ macro_rules! assert_ne {
|
||||
}
|
||||
});
|
||||
($left:expr, $right:expr,) => {
|
||||
assert_ne!($left, $right)
|
||||
$crate::assert_ne!($left, $right)
|
||||
};
|
||||
($left:expr, $right:expr, $($arg:tt)+) => ({
|
||||
match (&($left), &($right)) {
|
||||
@ -208,7 +208,7 @@ macro_rules! debug_assert {
|
||||
#[macro_export]
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
macro_rules! debug_assert_eq {
|
||||
($($arg:tt)*) => (if cfg!(debug_assertions) { assert_eq!($($arg)*); })
|
||||
($($arg:tt)*) => (if cfg!(debug_assertions) { $crate::assert_eq!($($arg)*); })
|
||||
}
|
||||
|
||||
/// Asserts that two expressions are not equal to each other.
|
||||
@ -235,7 +235,7 @@ macro_rules! debug_assert_eq {
|
||||
#[macro_export]
|
||||
#[stable(feature = "assert_ne", since = "1.13.0")]
|
||||
macro_rules! debug_assert_ne {
|
||||
($($arg:tt)*) => (if cfg!(debug_assertions) { assert_ne!($($arg)*); })
|
||||
($($arg:tt)*) => (if cfg!(debug_assertions) { $crate::assert_ne!($($arg)*); })
|
||||
}
|
||||
|
||||
/// Unwraps a result or propagates its error.
|
||||
@ -310,7 +310,7 @@ macro_rules! r#try {
|
||||
return $crate::result::Result::Err($crate::convert::From::from(err))
|
||||
}
|
||||
});
|
||||
($expr:expr,) => (r#try!($expr));
|
||||
($expr:expr,) => ($crate::r#try!($expr));
|
||||
}
|
||||
|
||||
/// Writes formatted data into a buffer.
|
||||
@ -425,10 +425,10 @@ macro_rules! write {
|
||||
#[allow_internal_unstable(format_args_nl)]
|
||||
macro_rules! writeln {
|
||||
($dst:expr) => (
|
||||
write!($dst, "\n")
|
||||
$crate::write!($dst, "\n")
|
||||
);
|
||||
($dst:expr,) => (
|
||||
writeln!($dst)
|
||||
$crate::writeln!($dst)
|
||||
);
|
||||
($dst:expr, $($arg:tt)*) => (
|
||||
$dst.write_fmt(format_args_nl!($($arg)*))
|
||||
@ -494,10 +494,10 @@ macro_rules! unreachable {
|
||||
panic!("internal error: entered unreachable code")
|
||||
});
|
||||
($msg:expr) => ({
|
||||
unreachable!("{}", $msg)
|
||||
$crate::unreachable!("{}", $msg)
|
||||
});
|
||||
($msg:expr,) => ({
|
||||
unreachable!($msg)
|
||||
$crate::unreachable!($msg)
|
||||
});
|
||||
($fmt:expr, $($arg:tt)*) => ({
|
||||
panic!(concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
|
||||
|
@ -56,13 +56,13 @@
|
||||
#[allow_internal_unstable(__rust_unstable_column, libstd_sys_internals)]
|
||||
macro_rules! panic {
|
||||
() => ({
|
||||
panic!("explicit panic")
|
||||
$crate::panic!("explicit panic")
|
||||
});
|
||||
($msg:expr) => ({
|
||||
$crate::rt::begin_panic($msg, &(file!(), line!(), __rust_unstable_column!()))
|
||||
});
|
||||
($msg:expr,) => ({
|
||||
panic!($msg)
|
||||
$crate::panic!($msg)
|
||||
});
|
||||
($fmt:expr, $($arg:tt)+) => ({
|
||||
$crate::rt::begin_panic_fmt(&format_args!($fmt, $($arg)+),
|
||||
@ -145,7 +145,7 @@ macro_rules! print {
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
#[allow_internal_unstable(print_internals, format_args_nl)]
|
||||
macro_rules! println {
|
||||
() => (print!("\n"));
|
||||
() => ($crate::print!("\n"));
|
||||
($($arg:tt)*) => ({
|
||||
$crate::io::_print(format_args_nl!($($arg)*));
|
||||
})
|
||||
@ -204,7 +204,7 @@ macro_rules! eprint {
|
||||
#[stable(feature = "eprint", since = "1.19.0")]
|
||||
#[allow_internal_unstable(print_internals, format_args_nl)]
|
||||
macro_rules! eprintln {
|
||||
() => (eprint!("\n"));
|
||||
() => ($crate::eprint!("\n"));
|
||||
($($arg:tt)*) => ({
|
||||
$crate::io::_eprint(format_args_nl!($($arg)*));
|
||||
})
|
||||
@ -337,23 +337,23 @@ macro_rules! eprintln {
|
||||
#[stable(feature = "dbg_macro", since = "1.32.0")]
|
||||
macro_rules! dbg {
|
||||
() => {
|
||||
eprintln!("[{}:{}]", file!(), line!());
|
||||
$crate::eprintln!("[{}:{}]", file!(), line!());
|
||||
};
|
||||
($val:expr) => {
|
||||
// Use of `match` here is intentional because it affects the lifetimes
|
||||
// of temporaries - https://stackoverflow.com/a/48732525/1063961
|
||||
match $val {
|
||||
tmp => {
|
||||
eprintln!("[{}:{}] {} = {:#?}",
|
||||
$crate::eprintln!("[{}:{}] {} = {:#?}",
|
||||
file!(), line!(), stringify!($val), &tmp);
|
||||
tmp
|
||||
}
|
||||
}
|
||||
};
|
||||
// Trailing comma with single argument is ignored
|
||||
($val:expr,) => { dbg!($val) };
|
||||
($val:expr,) => { $crate::dbg!($val) };
|
||||
($($val:expr),+ $(,)?) => {
|
||||
($(dbg!($val)),+,)
|
||||
($($crate::dbg!($val)),+,)
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@ mod bar {
|
||||
}
|
||||
fn f() {
|
||||
::foo::m!();
|
||||
println!(); //~ ERROR cannot find macro `print!` in this scope
|
||||
assert_eq!(0, 0); //~ ERROR cannot find macro `panic!` in this scope
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,11 +7,11 @@ LL | fn f() { ::bar::m!(); }
|
||||
LL | Vec::new();
|
||||
| ^^^ use of undeclared type or module `Vec`
|
||||
|
||||
error: cannot find macro `print!` in this scope
|
||||
error: cannot find macro `panic!` in this scope
|
||||
--> $DIR/no_implicit_prelude.rs:16:9
|
||||
|
|
||||
LL | println!();
|
||||
| ^^^^^^^^^^^
|
||||
LL | assert_eq!(0, 0);
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: have you added the `#[macro_use]` on the module/import?
|
||||
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
|
||||
|
@ -33,7 +33,6 @@ mod inner2 {
|
||||
|
||||
fn main() {
|
||||
panic!(); //~ ERROR `panic` is ambiguous
|
||||
//~| ERROR `panic` is ambiguous
|
||||
}
|
||||
|
||||
mod inner3 {
|
||||
|
@ -22,7 +22,7 @@ LL | use inner1::*;
|
||||
= help: consider adding an explicit import of `exported` to disambiguate
|
||||
|
||||
error[E0659]: `include` is ambiguous (macro-expanded name vs less macro-expanded name from outer scope during import/macro resolution)
|
||||
--> $DIR/local-modularized-tricky-fail-1.rs:47:1
|
||||
--> $DIR/local-modularized-tricky-fail-1.rs:46:1
|
||||
|
|
||||
LL | include!();
|
||||
| ^^^^^^^ ambiguous name
|
||||
@ -59,26 +59,6 @@ LL | define_panic!();
|
||||
| ---------------- in this macro invocation
|
||||
= help: use `crate::panic` to refer to this macro unambiguously
|
||||
|
||||
error[E0659]: `panic` is ambiguous (macro-expanded name vs less macro-expanded name from outer scope during import/macro resolution)
|
||||
--> $DIR/local-modularized-tricky-fail-1.rs:35:5
|
||||
|
|
||||
LL | panic!();
|
||||
| ^^^^^^^^^ ambiguous name
|
||||
|
|
||||
= note: `panic` could refer to a macro from prelude
|
||||
note: `panic` could also refer to the macro defined here
|
||||
--> $DIR/local-modularized-tricky-fail-1.rs:11:5
|
||||
|
|
||||
LL | / macro_rules! panic {
|
||||
LL | | () => ()
|
||||
LL | | }
|
||||
| |_____^
|
||||
...
|
||||
LL | define_panic!();
|
||||
| ---------------- in this macro invocation
|
||||
= help: use `crate::panic` to refer to this macro unambiguously
|
||||
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0659`.
|
||||
|
Loading…
Reference in New Issue
Block a user