Skip to content

Commit

Permalink
Qualify panic! as core::panic! in non-built-in core macros
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
camelid committed Nov 23, 2020
1 parent a0d664b commit d37e1e1
Show file tree
Hide file tree
Showing 16 changed files with 461 additions and 483 deletions.
22 changes: 11 additions & 11 deletions library/core/src/macros/mod.rs
Expand Up @@ -45,7 +45,7 @@ macro_rules! assert_eq {
// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
panic!(r#"assertion failed: `(left == right)`
$crate::panic!(r#"assertion failed: `(left == right)`
left: `{:?}`,
right: `{:?}`"#, &*left_val, &*right_val)
}
Expand All @@ -59,7 +59,7 @@ macro_rules! assert_eq {
// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
panic!(r#"assertion failed: `(left == right)`
$crate::panic!(r#"assertion failed: `(left == right)`
left: `{:?}`,
right: `{:?}`: {}"#, &*left_val, &*right_val,
$crate::format_args!($($arg)+))
Expand Down Expand Up @@ -96,7 +96,7 @@ macro_rules! assert_ne {
// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
panic!(r#"assertion failed: `(left != right)`
$crate::panic!(r#"assertion failed: `(left != right)`
left: `{:?}`,
right: `{:?}`"#, &*left_val, &*right_val)
}
Expand All @@ -110,7 +110,7 @@ macro_rules! assert_ne {
// The reborrows below are intentional. Without them, the stack slot for the
// borrow is initialized even before the values are compared, leading to a
// noticeable slow down.
panic!(r#"assertion failed: `(left != right)`
$crate::panic!(r#"assertion failed: `(left != right)`
left: `{:?}`,
right: `{:?}`: {}"#, &*left_val, &*right_val,
$crate::format_args!($($arg)+))
Expand Down Expand Up @@ -468,7 +468,7 @@ macro_rules! writeln {
///
/// # Panics
///
/// This will always [`panic!`]
/// This will always [`panic!`].
///
/// # Examples
///
Expand Down Expand Up @@ -502,13 +502,13 @@ macro_rules! writeln {
#[stable(feature = "rust1", since = "1.0.0")]
macro_rules! unreachable {
() => ({
panic!("internal error: entered unreachable code")
$crate::panic!("internal error: entered unreachable code")
});
($msg:expr $(,)?) => ({
$crate::unreachable!("{}", $msg)
});
($fmt:expr, $($arg:tt)*) => ({
panic!($crate::concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
$crate::panic!($crate::concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
});
}

Expand Down Expand Up @@ -586,8 +586,8 @@ macro_rules! unreachable {
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
macro_rules! unimplemented {
() => (panic!("not implemented"));
($($arg:tt)+) => (panic!("not implemented: {}", $crate::format_args!($($arg)+)));
() => ($crate::panic!("not implemented"));
($($arg:tt)+) => ($crate::panic!("not implemented: {}", $crate::format_args!($($arg)+)));
}

/// Indicates unfinished code.
Expand Down Expand Up @@ -647,8 +647,8 @@ macro_rules! unimplemented {
#[macro_export]
#[stable(feature = "todo_macro", since = "1.40.0")]
macro_rules! todo {
() => (panic!("not yet implemented"));
($($arg:tt)+) => (panic!("not yet implemented: {}", $crate::format_args!($($arg)+)));
() => ($crate::panic!("not yet implemented"));
($($arg:tt)+) => ($crate::panic!("not yet implemented: {}", $crate::format_args!($($arg)+)));
}

/// Definitions of built-in macros.
Expand Down
141 changes: 68 additions & 73 deletions src/test/mir-opt/issue_73223.main.PreCodegen.32bit.diff

Large diffs are not rendered by default.

141 changes: 68 additions & 73 deletions src/test/mir-opt/issue_73223.main.PreCodegen.64bit.diff

Large diffs are not rendered by default.

294 changes: 144 additions & 150 deletions src/test/mir-opt/issue_73223.main.SimplifyArmIdentity.32bit.diff

Large diffs are not rendered by default.

294 changes: 144 additions & 150 deletions src/test/mir-opt/issue_73223.main.SimplifyArmIdentity.64bit.diff

Large diffs are not rendered by default.

Expand Up @@ -21,7 +21,7 @@
let mut _19: *const T; // in scope 0 at $DIR/issue_76432.rs:9:54: 9:68
let mut _20: *const T; // in scope 0 at $DIR/issue_76432.rs:9:70: 9:84
let mut _21: *const T; // in scope 0 at $DIR/issue_76432.rs:9:70: 9:84
let mut _22: !; // in scope 0 at $SRC_DIR/std/src/macros.rs:LL:COL
let mut _22: !; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
scope 1 {
debug v => _2; // in scope 1 at $DIR/issue_76432.rs:7:9: 7:10
let _13: &T; // in scope 1 at $DIR/issue_76432.rs:9:10: 9:16
Expand Down Expand Up @@ -64,11 +64,11 @@
}

bb1: {
StorageLive(_22); // scope 1 at $SRC_DIR/std/src/macros.rs:LL:COL
begin_panic::<&str>(const "internal error: entered unreachable code"); // scope 1 at $SRC_DIR/std/src/macros.rs:LL:COL
StorageLive(_22); // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
core::panicking::panic(const "internal error: entered unreachable code"); // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
// mir::Constant
// + span: $SRC_DIR/std/src/macros.rs:LL:COL
// + literal: Const { ty: fn(&str) -> ! {std::rt::begin_panic::<&str>}, val: Value(Scalar(<ZST>)) }
// + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL
// + literal: Const { ty: fn(&'static str) -> ! {core::panicking::panic}, val: Value(Scalar(<ZST>)) }
// ty::Const
// + ty: &str
// + 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 })
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/hygiene/no_implicit_prelude.rs
@@ -1,7 +1,7 @@
#![feature(decl_macro)]

mod foo {
pub macro m() { Vec::new(); ().clone() }
pub macro m() { Vec::<i32>::new(); ().clone() }
fn f() { ::bar::m!(); }
}

Expand All @@ -13,7 +13,7 @@ mod bar {
}
fn f() {
::foo::m!();
assert_eq!(0, 0); //~ ERROR cannot find macro `panic` in this scope
assert!(true);
}
}

Expand Down
13 changes: 1 addition & 12 deletions src/test/ui/hygiene/no_implicit_prelude.stderr
@@ -1,14 +1,3 @@
error: cannot find macro `panic` in this scope
--> $DIR/no_implicit_prelude.rs:16:9
|
LL | assert_eq!(0, 0);
| ^^^^^^^^^^^^^^^^^
|
= note: consider importing one of these items:
core::panic
std::panic
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0433]: failed to resolve: use of undeclared type `Vec`
--> $DIR/no_implicit_prelude.rs:11:9
|
Expand Down Expand Up @@ -38,7 +27,7 @@ LL | ().clone()
`use std::clone::Clone;`
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

Some errors have detailed explanations: E0433, E0599.
For more information about an error, try `rustc --explain E0433`.
1 change: 0 additions & 1 deletion src/test/ui/iterators/iter-count-overflow-ndebug.rs
Expand Up @@ -2,7 +2,6 @@
// only-32bit too impatient for 2⁶⁴ items
// compile-flags: -C debug_assertions=no -C opt-level=3

use std::panic;
use std::usize::MAX;

fn main() {
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/iterators/iter-position-overflow-ndebug.rs
Expand Up @@ -2,7 +2,6 @@
// only-32bit too impatient for 2⁶⁴ items
// compile-flags: -C debug_assertions=no -C opt-level=3

use std::panic;
use std::usize::MAX;

fn main() {
Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/macros/issue-78333.rs
@@ -0,0 +1,13 @@
// build-pass

#![no_implicit_prelude]

fn main() {
::std::panic!();
::std::todo!();
::std::unimplemented!();
::std::assert_eq!(0, 0);
::std::assert_ne!(0, 1);
::std::dbg!(123);
::std::unreachable!();
}
2 changes: 1 addition & 1 deletion src/tools/clippy/tests/ui/logic_bug.rs
@@ -1,4 +1,4 @@
#![allow(unused, clippy::many_single_char_names)]
#![allow(unused, clippy::many_single_char_names, clippy::diverging_sub_expression)]
#![warn(clippy::logic_bug)]

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/tests/ui/nonminimal_bool.rs
@@ -1,4 +1,4 @@
#![allow(unused, clippy::many_single_char_names)]
#![allow(unused, clippy::many_single_char_names, clippy::diverging_sub_expression)]
#![warn(clippy::nonminimal_bool)]

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/tests/ui/nonminimal_bool_methods.rs
@@ -1,4 +1,4 @@
#![allow(unused, clippy::many_single_char_names)]
#![allow(unused, clippy::many_single_char_names, clippy::diverging_sub_expression)]
#![warn(clippy::nonminimal_bool)]

fn methods_with_negation() {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/tests/ui/wildcard_enum_match_arm.fixed
Expand Up @@ -7,7 +7,7 @@
dead_code,
clippy::single_match,
clippy::wildcard_in_or_patterns,
clippy::unnested_or_patterns
clippy::unnested_or_patterns, clippy::diverging_sub_expression
)]

use std::io::ErrorKind;
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/tests/ui/wildcard_enum_match_arm.rs
Expand Up @@ -7,7 +7,7 @@
dead_code,
clippy::single_match,
clippy::wildcard_in_or_patterns,
clippy::unnested_or_patterns
clippy::unnested_or_patterns, clippy::diverging_sub_expression
)]

use std::io::ErrorKind;
Expand Down

0 comments on commit d37e1e1

Please sign in to comment.