Skip to content

Commit f32a0cc

Browse files
committed
Auto merge of #78343 - camelid:macros-qualify-panic, r=m-ou-se
Qualify `panic!` as `core::panic!` in non-built-in `core` macros Fixes #78333. ----- Otherwise code like this #![no_implicit_prelude] fn main() { ::std::todo!(); ::std::unimplemented!(); } will fail to compile, which is unfortunate and presumably unintended. This changes many invocations of `panic!` in a `macro_rules!` definition to invocations of `$crate::panic!`, which makes the invocations hygienic. Note that this does not make the built-in macro `assert!` hygienic.
2 parents d9a105f + d8b1d51 commit f32a0cc

16 files changed

+465
-487
lines changed

library/core/src/macros/mod.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ macro_rules! assert_eq {
4545
// The reborrows below are intentional. Without them, the stack slot for the
4646
// borrow is initialized even before the values are compared, leading to a
4747
// noticeable slow down.
48-
panic!(r#"assertion failed: `(left == right)`
48+
$crate::panic!(r#"assertion failed: `(left == right)`
4949
left: `{:?}`,
5050
right: `{:?}`"#, &*left_val, &*right_val)
5151
}
@@ -59,7 +59,7 @@ macro_rules! assert_eq {
5959
// The reborrows below are intentional. Without them, the stack slot for the
6060
// borrow is initialized even before the values are compared, leading to a
6161
// noticeable slow down.
62-
panic!(r#"assertion failed: `(left == right)`
62+
$crate::panic!(r#"assertion failed: `(left == right)`
6363
left: `{:?}`,
6464
right: `{:?}`: {}"#, &*left_val, &*right_val,
6565
$crate::format_args!($($arg)+))
@@ -96,7 +96,7 @@ macro_rules! assert_ne {
9696
// The reborrows below are intentional. Without them, the stack slot for the
9797
// borrow is initialized even before the values are compared, leading to a
9898
// noticeable slow down.
99-
panic!(r#"assertion failed: `(left != right)`
99+
$crate::panic!(r#"assertion failed: `(left != right)`
100100
left: `{:?}`,
101101
right: `{:?}`"#, &*left_val, &*right_val)
102102
}
@@ -110,7 +110,7 @@ macro_rules! assert_ne {
110110
// The reborrows below are intentional. Without them, the stack slot for the
111111
// borrow is initialized even before the values are compared, leading to a
112112
// noticeable slow down.
113-
panic!(r#"assertion failed: `(left != right)`
113+
$crate::panic!(r#"assertion failed: `(left != right)`
114114
left: `{:?}`,
115115
right: `{:?}`: {}"#, &*left_val, &*right_val,
116116
$crate::format_args!($($arg)+))
@@ -468,7 +468,7 @@ macro_rules! writeln {
468468
///
469469
/// # Panics
470470
///
471-
/// This will always [`panic!`]
471+
/// This will always [`panic!`].
472472
///
473473
/// # Examples
474474
///
@@ -502,13 +502,13 @@ macro_rules! writeln {
502502
#[stable(feature = "rust1", since = "1.0.0")]
503503
macro_rules! unreachable {
504504
() => ({
505-
panic!("internal error: entered unreachable code")
505+
$crate::panic!("internal error: entered unreachable code")
506506
});
507507
($msg:expr $(,)?) => ({
508508
$crate::unreachable!("{}", $msg)
509509
});
510510
($fmt:expr, $($arg:tt)*) => ({
511-
panic!($crate::concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
511+
$crate::panic!($crate::concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
512512
});
513513
}
514514

@@ -517,15 +517,15 @@ macro_rules! unreachable {
517517
/// This allows your code to type-check, which is useful if you are prototyping or
518518
/// implementing a trait that requires multiple methods which you don't plan of using all of.
519519
///
520-
/// The difference between `unimplemented!` and [`todo!`](macro.todo.html) is that while `todo!`
520+
/// The difference between `unimplemented!` and [`todo!`] is that while `todo!`
521521
/// conveys an intent of implementing the functionality later and the message is "not yet
522522
/// implemented", `unimplemented!` makes no such claims. Its message is "not implemented".
523523
/// Also some IDEs will mark `todo!`s.
524524
///
525525
/// # Panics
526526
///
527-
/// This will always [panic!](macro.panic.html) because `unimplemented!` is just a
528-
/// shorthand for `panic!` with a fixed, specific message.
527+
/// This will always [`panic!`] because `unimplemented!` is just a shorthand for `panic!` with a
528+
/// fixed, specific message.
529529
///
530530
/// Like `panic!`, this macro has a second form for displaying custom values.
531531
///
@@ -586,8 +586,8 @@ macro_rules! unreachable {
586586
#[macro_export]
587587
#[stable(feature = "rust1", since = "1.0.0")]
588588
macro_rules! unimplemented {
589-
() => (panic!("not implemented"));
590-
($($arg:tt)+) => (panic!("not implemented: {}", $crate::format_args!($($arg)+)));
589+
() => ($crate::panic!("not implemented"));
590+
($($arg:tt)+) => ($crate::panic!("not implemented: {}", $crate::format_args!($($arg)+)));
591591
}
592592

593593
/// Indicates unfinished code.
@@ -602,7 +602,7 @@ macro_rules! unimplemented {
602602
///
603603
/// # Panics
604604
///
605-
/// This will always [panic!](macro.panic.html)
605+
/// This will always [`panic!`].
606606
///
607607
/// # Examples
608608
///
@@ -647,8 +647,8 @@ macro_rules! unimplemented {
647647
#[macro_export]
648648
#[stable(feature = "todo_macro", since = "1.40.0")]
649649
macro_rules! todo {
650-
() => (panic!("not yet implemented"));
651-
($($arg:tt)+) => (panic!("not yet implemented: {}", $crate::format_args!($($arg)+)));
650+
() => ($crate::panic!("not yet implemented"));
651+
($($arg:tt)+) => ($crate::panic!("not yet implemented: {}", $crate::format_args!($($arg)+)));
652652
}
653653

654654
/// Definitions of built-in macros.

0 commit comments

Comments
 (0)