Skip to content

Commit d37e1e1

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 a0d664b commit d37e1e1

16 files changed

+461
-483
lines changed

library/core/src/macros/mod.rs

Lines changed: 11 additions & 11 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

@@ -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.
@@ -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.

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

Lines changed: 68 additions & 73 deletions
Large diffs are not rendered by default.

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

Lines changed: 68 additions & 73 deletions
Large diffs are not rendered by default.

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

Lines changed: 144 additions & 150 deletions
Large diffs are not rendered by default.

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

Lines changed: 144 additions & 150 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);
1717
}
1818
}
1919

src/test/ui/hygiene/no_implicit_prelude.stderr

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
error: cannot find macro `panic` in this scope
2-
--> $DIR/no_implicit_prelude.rs:16:9
3-
|
4-
LL | assert_eq!(0, 0);
5-
| ^^^^^^^^^^^^^^^^^
6-
|
7-
= note: consider importing one of these items:
8-
core::panic
9-
std::panic
10-
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
11-
121
error[E0433]: failed to resolve: use of undeclared type `Vec`
132
--> $DIR/no_implicit_prelude.rs:11:9
143
|
@@ -38,7 +27,7 @@ LL | ().clone()
3827
`use std::clone::Clone;`
3928
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
4029

41-
error: aborting due to 3 previous errors
30+
error: aborting due to 2 previous errors
4231

4332
Some errors have detailed explanations: E0433, E0599.
4433
For more information about an error, try `rustc --explain E0433`.

src/test/ui/iterators/iter-count-overflow-ndebug.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// only-32bit too impatient for 2⁶⁴ items
33
// compile-flags: -C debug_assertions=no -C opt-level=3
44

5-
use std::panic;
65
use std::usize::MAX;
76

87
fn main() {

src/test/ui/iterators/iter-position-overflow-ndebug.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// only-32bit too impatient for 2⁶⁴ items
33
// compile-flags: -C debug_assertions=no -C opt-level=3
44

5-
use std::panic;
65
use std::usize::MAX;
76

87
fn main() {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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::assert_ne!(0, 1);
11+
::std::dbg!(123);
12+
::std::unreachable!();
13+
}

src/tools/clippy/tests/ui/logic_bug.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(unused, clippy::many_single_char_names)]
1+
#![allow(unused, clippy::many_single_char_names, clippy::diverging_sub_expression)]
22
#![warn(clippy::logic_bug)]
33

44
fn main() {

src/tools/clippy/tests/ui/nonminimal_bool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(unused, clippy::many_single_char_names)]
1+
#![allow(unused, clippy::many_single_char_names, clippy::diverging_sub_expression)]
22
#![warn(clippy::nonminimal_bool)]
33

44
fn main() {

src/tools/clippy/tests/ui/nonminimal_bool_methods.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(unused, clippy::many_single_char_names)]
1+
#![allow(unused, clippy::many_single_char_names, clippy::diverging_sub_expression)]
22
#![warn(clippy::nonminimal_bool)]
33

44
fn methods_with_negation() {

src/tools/clippy/tests/ui/wildcard_enum_match_arm.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
dead_code,
88
clippy::single_match,
99
clippy::wildcard_in_or_patterns,
10-
clippy::unnested_or_patterns
10+
clippy::unnested_or_patterns, clippy::diverging_sub_expression
1111
)]
1212

1313
use std::io::ErrorKind;

src/tools/clippy/tests/ui/wildcard_enum_match_arm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
dead_code,
88
clippy::single_match,
99
clippy::wildcard_in_or_patterns,
10-
clippy::unnested_or_patterns
10+
clippy::unnested_or_patterns, clippy::diverging_sub_expression
1111
)]
1212

1313
use std::io::ErrorKind;

0 commit comments

Comments
 (0)