Skip to content

Commit 546d062

Browse files
committed
Apply suggestions
- Move `assert_failed` to core::panicking` - Make `assert_failed` use an enum instead of a string
1 parent f138e26 commit 546d062

11 files changed

+163
-121
lines changed

library/core/src/lib.rs

-7
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,6 @@ mod macros;
173173

174174
#[macro_use]
175175
mod internal_macros;
176-
#[doc(hidden)]
177-
#[unstable(
178-
feature = "macros_internals",
179-
reason = "macros implementation detail",
180-
issue = "none"
181-
)]
182-
pub use macros::internals as macros_internals;
183176

184177
#[path = "num/shells/int_macros.rs"]
185178
#[macro_use]

library/core/src/macros/internals.rs

-35
This file was deleted.

library/core/src/macros/mod.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#[unstable(feature = "macros_internals", reason = "macros implementation detail", issue = "none")]
2-
#[doc(hidden)]
3-
pub mod internals;
4-
51
#[cfg(bootstrap)]
62
#[doc(include = "panic.md")]
73
#[macro_export]
@@ -57,16 +53,17 @@ macro_rules! panic {
5753
/// ```
5854
#[macro_export]
5955
#[stable(feature = "rust1", since = "1.0.0")]
60-
#[allow_internal_unstable(macros_internals)]
56+
#[allow_internal_unstable(core_panic)]
6157
macro_rules! assert_eq {
6258
($left:expr, $right:expr $(,)?) => ({
6359
match (&$left, &$right) {
6460
(left_val, right_val) => {
6561
if !(*left_val == *right_val) {
62+
let kind = $crate::panicking::AssertKind::Eq;
6663
// The reborrows below are intentional. Without them, the stack slot for the
6764
// borrow is initialized even before the values are compared, leading to a
6865
// noticeable slow down.
69-
$crate::macros_internals::assert_failed("==", &*left_val, &*right_val, $crate::option::Option::None);
66+
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::None);
7067
}
7168
}
7269
}
@@ -75,10 +72,11 @@ macro_rules! assert_eq {
7572
match (&$left, &$right) {
7673
(left_val, right_val) => {
7774
if !(*left_val == *right_val) {
75+
let kind = $crate::panicking::AssertKind::Eq;
7876
// The reborrows below are intentional. Without them, the stack slot for the
7977
// borrow is initialized even before the values are compared, leading to a
8078
// noticeable slow down.
81-
$crate::macros_internals::assert_failed("==", &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+)));
79+
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+)));
8280
}
8381
}
8482
}
@@ -104,16 +102,17 @@ macro_rules! assert_eq {
104102
/// ```
105103
#[macro_export]
106104
#[stable(feature = "assert_ne", since = "1.13.0")]
107-
#[allow_internal_unstable(macros_internals)]
105+
#[allow_internal_unstable(core_panic)]
108106
macro_rules! assert_ne {
109107
($left:expr, $right:expr $(,)?) => ({
110108
match (&$left, &$right) {
111109
(left_val, right_val) => {
112110
if *left_val == *right_val {
111+
let kind = $crate::panicking::AssertKind::Ne;
113112
// The reborrows below are intentional. Without them, the stack slot for the
114113
// borrow is initialized even before the values are compared, leading to a
115114
// noticeable slow down.
116-
$crate::macros_internals::assert_failed("!=", &*left_val, &*right_val, $crate::option::Option::None);
115+
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::None);
117116
}
118117
}
119118
}
@@ -122,10 +121,11 @@ macro_rules! assert_ne {
122121
match (&($left), &($right)) {
123122
(left_val, right_val) => {
124123
if *left_val == *right_val {
124+
let kind = $crate::panicking::AssertKind::Ne;
125125
// The reborrows below are intentional. Without them, the stack slot for the
126126
// borrow is initialized even before the values are compared, leading to a
127127
// noticeable slow down.
128-
$crate::macros_internals::assert_failed("!=", &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+)));
128+
$crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+)));
129129
}
130130
}
131131
}

library/core/src/panicking.rs

+49
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,52 @@ pub fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! {
9191
// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call.
9292
unsafe { panic_impl(&pi) }
9393
}
94+
95+
#[derive(Debug)]
96+
pub enum AssertKind {
97+
Eq,
98+
Ne,
99+
}
100+
101+
/// Internal function for `assert_eq!` and `assert_ne!` macros
102+
#[cold]
103+
#[track_caller]
104+
pub fn assert_failed<T, U>(
105+
kind: AssertKind,
106+
left: &T,
107+
right: &U,
108+
args: Option<fmt::Arguments<'_>>,
109+
) -> !
110+
where
111+
T: fmt::Debug + ?Sized,
112+
U: fmt::Debug + ?Sized,
113+
{
114+
#[track_caller]
115+
fn inner(
116+
kind: AssertKind,
117+
left: &dyn fmt::Debug,
118+
right: &dyn fmt::Debug,
119+
args: Option<fmt::Arguments<'_>>,
120+
) -> ! {
121+
let op = match kind {
122+
AssertKind::Eq => "==",
123+
AssertKind::Ne => "!=",
124+
};
125+
126+
match args {
127+
Some(args) => panic!(
128+
r#"assertion failed: `(left {} right)`
129+
left: `{:?}`,
130+
right: `{:?}: {}`"#,
131+
op, left, right, args
132+
),
133+
None => panic!(
134+
r#"assertion failed: `(left {} right)`
135+
left: `{:?}`,
136+
right: `{:?}`"#,
137+
op, left, right,
138+
),
139+
}
140+
}
141+
inner(kind, &left, &right, args)
142+
}

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

+14-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
let mut _7: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
1212
let mut _8: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
1313
let mut _9: i32; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
14-
let mut _10: &str; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
1514
let mut _11: &i32; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
1615
let mut _12: &i32; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
1716
let mut _13: std::option::Option<std::fmt::Arguments>; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
@@ -23,6 +22,10 @@
2322
scope 4 {
2423
debug left_val => _11; // in scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
2524
debug right_val => _12; // in scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
25+
let _10: core::panicking::AssertKind; // in scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
26+
scope 5 {
27+
debug kind => _10; // in scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
28+
}
2629
}
2730
}
2831
}
@@ -65,19 +68,20 @@
6568
}
6669

6770
bb1: {
68-
_10 = const "=="; // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
69-
// ty::Const
70-
// + ty: &str
71-
// + val: Value(Slice { data: Allocation { bytes: [61, 61], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [3], len: Size { raw: 2 } }, size: Size { raw: 2 }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 2 })
71+
StorageLive(_10); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
72+
discriminant(_10) = 0; // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
73+
StorageLive(_13); // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
74+
discriminant(_13) = 0; // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
75+
core::panicking::assert_failed::<i32, i32>(const core::panicking::AssertKind::Eq, move _11, move _12, move _13); // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
7276
// mir::Constant
7377
// + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL
74-
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [61, 61], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [3], len: Size { raw: 2 } }, size: Size { raw: 2 }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 2 }) }
75-
StorageLive(_13); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
76-
discriminant(_13) = 0; // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
77-
core::macros::internals::assert_failed::<i32, i32>(move _10, move _11, move _12, move _13); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
78+
// + literal: Const { ty: for<'r, 's, 't0> fn(core::panicking::AssertKind, &'r i32, &'s i32, std::option::Option<std::fmt::Arguments<'t0>>) -> ! {core::panicking::assert_failed::<i32, i32>}, val: Value(Scalar(<ZST>)) }
79+
// ty::Const
80+
// + ty: core::panicking::AssertKind
81+
// + val: Value(Scalar(0x00))
7882
// mir::Constant
7983
// + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL
80-
// + literal: Const { ty: for<'r, 's, 't0, 't1> fn(&'r str, &'s i32, &'t0 i32, std::option::Option<std::fmt::Arguments<'t1>>) -> ! {core::macros::internals::assert_failed::<i32, i32>}, val: Value(Scalar(<ZST>)) }
84+
// + literal: Const { ty: core::panicking::AssertKind, val: Value(Scalar(0x00)) }
8185
}
8286

8387
bb2: {

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

+15-8
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
let mut _7: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
1212
let mut _8: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
1313
let mut _9: i32; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
14-
let mut _10: &str; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
1514
let mut _11: &i32; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
1615
let mut _12: &i32; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
16+
let mut _13: std::option::Option<std::fmt::Arguments>; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
1717
scope 1 {
1818
debug split => _1; // in scope 1 at $DIR/issue-73223.rs:2:9: 2:14
1919
let _4: std::option::Option<i32>; // in scope 1 at $DIR/issue-73223.rs:7:9: 7:14
@@ -22,6 +22,10 @@
2222
scope 4 {
2323
debug left_val => _11; // in scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
2424
debug right_val => _12; // in scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
25+
let _10: core::panicking::AssertKind; // in scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
26+
scope 5 {
27+
debug kind => _10; // in scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
28+
}
2529
}
2630
}
2731
}
@@ -64,17 +68,20 @@
6468
}
6569

6670
bb1: {
67-
_10 = const "=="; // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
68-
// ty::Const
69-
// + ty: &str
70-
// + val: Value(Slice { data: Allocation { bytes: [61, 61], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [3], len: Size { raw: 2 } }, size: Size { raw: 2 }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 2 })
71+
StorageLive(_10); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
72+
discriminant(_10) = 0; // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
73+
StorageLive(_13); // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
74+
discriminant(_13) = 0; // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
75+
core::panicking::assert_failed::<i32, i32>(const core::panicking::AssertKind::Eq, move _11, move _12, move _13); // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
7176
// mir::Constant
7277
// + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL
73-
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [61, 61], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [3], len: Size { raw: 2 } }, size: Size { raw: 2 }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 2 }) }
74-
core::macros::internals::assert_failed::<i32, i32>(move _10, move _11, move _12); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
78+
// + literal: Const { ty: for<'r, 's, 't0> fn(core::panicking::AssertKind, &'r i32, &'s i32, std::option::Option<std::fmt::Arguments<'t0>>) -> ! {core::panicking::assert_failed::<i32, i32>}, val: Value(Scalar(<ZST>)) }
79+
// ty::Const
80+
// + ty: core::panicking::AssertKind
81+
// + val: Value(Scalar(0x00))
7582
// mir::Constant
7683
// + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL
77-
// + literal: Const { ty: for<'r, 's, 't0> fn(&'r str, &'s i32, &'t0 i32) -> ! {core::macros::internals::assert_failed::<i32, i32>}, val: Value(Scalar(<ZST>)) }
84+
// + literal: Const { ty: core::panicking::AssertKind, val: Value(Scalar(0x00)) }
7885
}
7986

8087
bb2: {

0 commit comments

Comments
 (0)