Skip to content

Commit 93ca9ae

Browse files
committed
Qualify panic! as core::panic! in non-built-in core macros
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.
1 parent 31ee872 commit 93ca9ae

9 files changed

+431
-442
lines changed

library/core/src/macros/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ macro_rules! assert_eq {
4444
// The reborrows below are intentional. Without them, the stack slot for the
4545
// borrow is initialized even before the values are compared, leading to a
4646
// noticeable slow down.
47-
panic!(r#"assertion failed: `(left == right)`
47+
$crate::panic!(r#"assertion failed: `(left == right)`
4848
left: `{:?}`,
4949
right: `{:?}`"#, &*left_val, &*right_val)
5050
}
@@ -58,7 +58,7 @@ macro_rules! assert_eq {
5858
// The reborrows below are intentional. Without them, the stack slot for the
5959
// borrow is initialized even before the values are compared, leading to a
6060
// noticeable slow down.
61-
panic!(r#"assertion failed: `(left == right)`
61+
$crate::panic!(r#"assertion failed: `(left == right)`
6262
left: `{:?}`,
6363
right: `{:?}`: {}"#, &*left_val, &*right_val,
6464
$crate::format_args!($($arg)+))
@@ -95,7 +95,7 @@ macro_rules! assert_ne {
9595
// The reborrows below are intentional. Without them, the stack slot for the
9696
// borrow is initialized even before the values are compared, leading to a
9797
// noticeable slow down.
98-
panic!(r#"assertion failed: `(left != right)`
98+
$crate::panic!(r#"assertion failed: `(left != right)`
9999
left: `{:?}`,
100100
right: `{:?}`"#, &*left_val, &*right_val)
101101
}
@@ -109,7 +109,7 @@ macro_rules! assert_ne {
109109
// The reborrows below are intentional. Without them, the stack slot for the
110110
// borrow is initialized even before the values are compared, leading to a
111111
// noticeable slow down.
112-
panic!(r#"assertion failed: `(left != right)`
112+
$crate::panic!(r#"assertion failed: `(left != right)`
113113
left: `{:?}`,
114114
right: `{:?}`: {}"#, &*left_val, &*right_val,
115115
$crate::format_args!($($arg)+))
@@ -466,7 +466,7 @@ macro_rules! writeln {
466466
///
467467
/// # Panics
468468
///
469-
/// This will always [`panic!`]
469+
/// This will always [`panic!`].
470470
///
471471
/// # Examples
472472
///
@@ -500,13 +500,13 @@ macro_rules! writeln {
500500
#[stable(feature = "rust1", since = "1.0.0")]
501501
macro_rules! unreachable {
502502
() => ({
503-
panic!("internal error: entered unreachable code")
503+
$crate::panic!("internal error: entered unreachable code")
504504
});
505505
($msg:expr $(,)?) => ({
506506
$crate::unreachable!("{}", $msg)
507507
});
508508
($fmt:expr, $($arg:tt)*) => ({
509-
panic!($crate::concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
509+
$crate::panic!($crate::concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
510510
});
511511
}
512512

@@ -584,8 +584,8 @@ macro_rules! unreachable {
584584
#[macro_export]
585585
#[stable(feature = "rust1", since = "1.0.0")]
586586
macro_rules! unimplemented {
587-
() => (panic!("not implemented"));
588-
($($arg:tt)+) => (panic!("not implemented: {}", $crate::format_args!($($arg)+)));
587+
() => ($crate::panic!("not implemented"));
588+
($($arg:tt)+) => ($crate::panic!("not implemented: {}", $crate::format_args!($($arg)+)));
589589
}
590590

591591
/// Indicates unfinished code.
@@ -645,8 +645,8 @@ macro_rules! unimplemented {
645645
#[macro_export]
646646
#[stable(feature = "todo_macro", since = "1.40.0")]
647647
macro_rules! todo {
648-
() => (panic!("not yet implemented"));
649-
($($arg:tt)+) => (panic!("not yet implemented: {}", $crate::format_args!($($arg)+)));
648+
() => ($crate::panic!("not yet implemented"));
649+
($($arg:tt)+) => ($crate::panic!("not yet implemented: {}", $crate::format_args!($($arg)+)));
650650
}
651651

652652
/// Definitions of built-in macros.

src/test/mir-opt/issue_73223.main.PreCodegen.32bit.diff

Lines changed: 64 additions & 69 deletions
Large diffs are not rendered by default.

src/test/mir-opt/issue_73223.main.PreCodegen.64bit.diff

Lines changed: 64 additions & 69 deletions
Large diffs are not rendered by default.

src/test/mir-opt/issue_73223.main.SimplifyArmIdentity.32bit.diff

Lines changed: 136 additions & 142 deletions
Large diffs are not rendered by default.

src/test/mir-opt/issue_73223.main.SimplifyArmIdentity.64bit.diff

Lines changed: 136 additions & 142 deletions
Large diffs are not rendered by default.

src/test/mir-opt/issue_76432.test.SimplifyComparisonIntegral.diff

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
let mut _19: *const T; // in scope 0 at $DIR/issue_76432.rs:9:54: 9:68
2222
let mut _20: *const T; // in scope 0 at $DIR/issue_76432.rs:9:70: 9:84
2323
let mut _21: *const T; // in scope 0 at $DIR/issue_76432.rs:9:70: 9:84
24-
let mut _22: !; // in scope 0 at $SRC_DIR/std/src/macros.rs:LL:COL
24+
let mut _22: !; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
2525
scope 1 {
2626
debug v => _2; // in scope 1 at $DIR/issue_76432.rs:7:9: 7:10
2727
let _13: &T; // in scope 1 at $DIR/issue_76432.rs:9:10: 9:16
@@ -64,11 +64,11 @@
6464
}
6565

6666
bb1: {
67-
StorageLive(_22); // scope 1 at $SRC_DIR/std/src/macros.rs:LL:COL
68-
begin_panic::<&str>(const "internal error: entered unreachable code"); // scope 1 at $SRC_DIR/std/src/macros.rs:LL:COL
67+
StorageLive(_22); // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
68+
core::panicking::panic(const "internal error: entered unreachable code"); // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
6969
// mir::Constant
70-
// + span: $SRC_DIR/std/src/macros.rs:LL:COL
71-
// + literal: Const { ty: fn(&str) -> ! {std::rt::begin_panic::<&str>}, val: Value(Scalar(<ZST>)) }
70+
// + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL
71+
// + literal: Const { ty: fn(&'static str) -> ! {core::panicking::panic}, val: Value(Scalar(<ZST>)) }
7272
// ty::Const
7373
// + ty: &str
7474
// + val: Value(Slice { data: Allocation { bytes: [105, 110, 116, 101, 114, 110, 97, 108, 32, 101, 114, 114, 111, 114, 58, 32, 101, 110, 116, 101, 114, 101, 100, 32, 117, 110, 114, 101, 97, 99, 104, 97, 98, 108, 101, 32, 99, 111, 100, 101], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [1099511627775], len: Size { raw: 40 } }, size: Size { raw: 40 }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 40 })

src/test/ui/hygiene/no_implicit_prelude.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(decl_macro)]
22

33
mod foo {
4-
pub macro m() { Vec::new(); ().clone() }
4+
pub macro m() { Vec::<i32>::new(); ().clone() }
55
fn f() { ::bar::m!(); }
66
}
77

@@ -13,7 +13,7 @@ mod bar {
1313
}
1414
fn f() {
1515
::foo::m!();
16-
assert_eq!(0, 0); //~ ERROR cannot find macro `panic` in this scope
16+
assert!(true); //~ ERROR cannot find macro `panic` in this scope
1717
}
1818
}
1919

src/test/ui/hygiene/no_implicit_prelude.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: cannot find macro `panic` in this scope
22
--> $DIR/no_implicit_prelude.rs:16:9
33
|
4-
LL | assert_eq!(0, 0);
5-
| ^^^^^^^^^^^^^^^^^
4+
LL | assert!(true);
5+
| ^^^^^^^^^^^^^^
66
|
77
= note: consider importing one of these items:
88
core::panic

src/test/ui/macros/issue-78333.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// build-pass
2+
3+
#![no_implicit_prelude]
4+
5+
fn main() {
6+
::std::panic!();
7+
::std::todo!();
8+
::std::unimplemented!();
9+
::std::assert_eq!(0, 0);
10+
::std::dbg!(123);
11+
}

0 commit comments

Comments
 (0)