Skip to content

Commit 55df67d

Browse files
authored
Rollup merge of rust-lang#61629 - petrochenkov:stdmac, r=alexcrichton
Hygienize macros in the standard library Same as rust-lang#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 rust-lang#61567. (This PR doesn't do that.) --- cc rust-lang#56389 cc rust-lang#61567 Fixes rust-lang#61699 r? @alexcrichton
2 parents 648a529 + eb09daa commit 55df67d

File tree

7 files changed

+26
-47
lines changed

7 files changed

+26
-47
lines changed

src/liballoc/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ macro_rules! vec {
4242
($($x:expr),*) => (
4343
<[_]>::into_vec(box [$($x),*])
4444
);
45-
($($x:expr,)*) => (vec![$($x),*])
45+
($($x:expr,)*) => ($crate::vec![$($x),*])
4646
}
4747

4848
// HACK(japaric): with cfg(test) the inherent `[T]::into_vec` method, which is

src/libcore/macros.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
#[stable(feature = "core", since = "1.6.0")]
77
macro_rules! panic {
88
() => (
9-
panic!("explicit panic")
9+
$crate::panic!("explicit panic")
1010
);
1111
($msg:expr) => ({
1212
$crate::panicking::panic(&($msg, file!(), line!(), __rust_unstable_column!()))
1313
});
1414
($msg:expr,) => (
15-
panic!($msg)
15+
$crate::panic!($msg)
1616
);
1717
($fmt:expr, $($arg:tt)+) => ({
1818
$crate::panicking::panic_fmt(format_args!($fmt, $($arg)*),
@@ -58,7 +58,7 @@ macro_rules! assert_eq {
5858
}
5959
});
6060
($left:expr, $right:expr,) => ({
61-
assert_eq!($left, $right)
61+
$crate::assert_eq!($left, $right)
6262
});
6363
($left:expr, $right:expr, $($arg:tt)+) => ({
6464
match (&($left), &($right)) {
@@ -115,7 +115,7 @@ macro_rules! assert_ne {
115115
}
116116
});
117117
($left:expr, $right:expr,) => {
118-
assert_ne!($left, $right)
118+
$crate::assert_ne!($left, $right)
119119
};
120120
($left:expr, $right:expr, $($arg:tt)+) => ({
121121
match (&($left), &($right)) {
@@ -208,7 +208,7 @@ macro_rules! debug_assert {
208208
#[macro_export]
209209
#[stable(feature = "rust1", since = "1.0.0")]
210210
macro_rules! debug_assert_eq {
211-
($($arg:tt)*) => (if cfg!(debug_assertions) { assert_eq!($($arg)*); })
211+
($($arg:tt)*) => (if cfg!(debug_assertions) { $crate::assert_eq!($($arg)*); })
212212
}
213213

214214
/// Asserts that two expressions are not equal to each other.
@@ -235,7 +235,7 @@ macro_rules! debug_assert_eq {
235235
#[macro_export]
236236
#[stable(feature = "assert_ne", since = "1.13.0")]
237237
macro_rules! debug_assert_ne {
238-
($($arg:tt)*) => (if cfg!(debug_assertions) { assert_ne!($($arg)*); })
238+
($($arg:tt)*) => (if cfg!(debug_assertions) { $crate::assert_ne!($($arg)*); })
239239
}
240240

241241
/// Unwraps a result or propagates its error.
@@ -310,7 +310,7 @@ macro_rules! r#try {
310310
return $crate::result::Result::Err($crate::convert::From::from(err))
311311
}
312312
});
313-
($expr:expr,) => (r#try!($expr));
313+
($expr:expr,) => ($crate::r#try!($expr));
314314
}
315315

316316
/// Writes formatted data into a buffer.
@@ -425,10 +425,10 @@ macro_rules! write {
425425
#[allow_internal_unstable(format_args_nl)]
426426
macro_rules! writeln {
427427
($dst:expr) => (
428-
write!($dst, "\n")
428+
$crate::write!($dst, "\n")
429429
);
430430
($dst:expr,) => (
431-
writeln!($dst)
431+
$crate::writeln!($dst)
432432
);
433433
($dst:expr, $($arg:tt)*) => (
434434
$dst.write_fmt(format_args_nl!($($arg)*))
@@ -494,10 +494,10 @@ macro_rules! unreachable {
494494
panic!("internal error: entered unreachable code")
495495
});
496496
($msg:expr) => ({
497-
unreachable!("{}", $msg)
497+
$crate::unreachable!("{}", $msg)
498498
});
499499
($msg:expr,) => ({
500-
unreachable!($msg)
500+
$crate::unreachable!($msg)
501501
});
502502
($fmt:expr, $($arg:tt)*) => ({
503503
panic!(concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)

src/libstd/macros.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@
5656
#[allow_internal_unstable(__rust_unstable_column, libstd_sys_internals)]
5757
macro_rules! panic {
5858
() => ({
59-
panic!("explicit panic")
59+
$crate::panic!("explicit panic")
6060
});
6161
($msg:expr) => ({
6262
$crate::rt::begin_panic($msg, &(file!(), line!(), __rust_unstable_column!()))
6363
});
6464
($msg:expr,) => ({
65-
panic!($msg)
65+
$crate::panic!($msg)
6666
});
6767
($fmt:expr, $($arg:tt)+) => ({
6868
$crate::rt::begin_panic_fmt(&format_args!($fmt, $($arg)+),
@@ -145,7 +145,7 @@ macro_rules! print {
145145
#[stable(feature = "rust1", since = "1.0.0")]
146146
#[allow_internal_unstable(print_internals, format_args_nl)]
147147
macro_rules! println {
148-
() => (print!("\n"));
148+
() => ($crate::print!("\n"));
149149
($($arg:tt)*) => ({
150150
$crate::io::_print(format_args_nl!($($arg)*));
151151
})
@@ -204,7 +204,7 @@ macro_rules! eprint {
204204
#[stable(feature = "eprint", since = "1.19.0")]
205205
#[allow_internal_unstable(print_internals, format_args_nl)]
206206
macro_rules! eprintln {
207-
() => (eprint!("\n"));
207+
() => ($crate::eprint!("\n"));
208208
($($arg:tt)*) => ({
209209
$crate::io::_eprint(format_args_nl!($($arg)*));
210210
})
@@ -337,23 +337,23 @@ macro_rules! eprintln {
337337
#[stable(feature = "dbg_macro", since = "1.32.0")]
338338
macro_rules! dbg {
339339
() => {
340-
eprintln!("[{}:{}]", file!(), line!());
340+
$crate::eprintln!("[{}:{}]", file!(), line!());
341341
};
342342
($val:expr) => {
343343
// Use of `match` here is intentional because it affects the lifetimes
344344
// of temporaries - https://stackoverflow.com/a/48732525/1063961
345345
match $val {
346346
tmp => {
347-
eprintln!("[{}:{}] {} = {:#?}",
347+
$crate::eprintln!("[{}:{}] {} = {:#?}",
348348
file!(), line!(), stringify!($val), &tmp);
349349
tmp
350350
}
351351
}
352352
};
353353
// Trailing comma with single argument is ignored
354-
($val:expr,) => { dbg!($val) };
354+
($val:expr,) => { $crate::dbg!($val) };
355355
($($val:expr),+ $(,)?) => {
356-
($(dbg!($val)),+,)
356+
($($crate::dbg!($val)),+,)
357357
};
358358
}
359359

src/test/ui/hygiene/no_implicit_prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod bar {
1313
}
1414
fn f() {
1515
::foo::m!();
16-
println!(); //~ ERROR cannot find macro `print!` in this scope
16+
assert_eq!(0, 0); //~ ERROR cannot find macro `panic!` in this scope
1717
}
1818
}
1919

src/test/ui/hygiene/no_implicit_prelude.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ LL | fn f() { ::bar::m!(); }
77
LL | Vec::new();
88
| ^^^ use of undeclared type or module `Vec`
99

10-
error: cannot find macro `print!` in this scope
10+
error: cannot find macro `panic!` in this scope
1111
--> $DIR/no_implicit_prelude.rs:16:9
1212
|
13-
LL | println!();
14-
| ^^^^^^^^^^^
13+
LL | assert_eq!(0, 0);
14+
| ^^^^^^^^^^^^^^^^^
1515
|
1616
= help: have you added the `#[macro_use]` on the module/import?
1717
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

src/test/ui/imports/local-modularized-tricky-fail-1.rs

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ mod inner2 {
3333

3434
fn main() {
3535
panic!(); //~ ERROR `panic` is ambiguous
36-
//~| ERROR `panic` is ambiguous
3736
}
3837

3938
mod inner3 {

src/test/ui/imports/local-modularized-tricky-fail-1.stderr

+2-22
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ LL | use inner1::*;
2222
= help: consider adding an explicit import of `exported` to disambiguate
2323

2424
error[E0659]: `include` is ambiguous (macro-expanded name vs less macro-expanded name from outer scope during import/macro resolution)
25-
--> $DIR/local-modularized-tricky-fail-1.rs:47:1
25+
--> $DIR/local-modularized-tricky-fail-1.rs:46:1
2626
|
2727
LL | include!();
2828
| ^^^^^^^ ambiguous name
@@ -59,26 +59,6 @@ LL | define_panic!();
5959
| ---------------- in this macro invocation
6060
= help: use `crate::panic` to refer to this macro unambiguously
6161

62-
error[E0659]: `panic` is ambiguous (macro-expanded name vs less macro-expanded name from outer scope during import/macro resolution)
63-
--> $DIR/local-modularized-tricky-fail-1.rs:35:5
64-
|
65-
LL | panic!();
66-
| ^^^^^^^^^ ambiguous name
67-
|
68-
= note: `panic` could refer to a macro from prelude
69-
note: `panic` could also refer to the macro defined here
70-
--> $DIR/local-modularized-tricky-fail-1.rs:11:5
71-
|
72-
LL | / macro_rules! panic {
73-
LL | | () => ()
74-
LL | | }
75-
| |_____^
76-
...
77-
LL | define_panic!();
78-
| ---------------- in this macro invocation
79-
= help: use `crate::panic` to refer to this macro unambiguously
80-
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
81-
82-
error: aborting due to 4 previous errors
62+
error: aborting due to 3 previous errors
8363

8464
For more information about this error, try `rustc --explain E0659`.

0 commit comments

Comments
 (0)