diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index 3b026bc0e0f38..da810a12e1dcf 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -42,7 +42,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. - $crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::None); + $crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::None, $crate::concat!("assertion failed: `", $crate::stringify!($left), " == ", $crate::stringify!($right), "`")); } } } @@ -55,7 +55,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. - $crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+))); + $crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+)), $crate::concat!("assertion failed: `", $crate::stringify!($left), " == ", $crate::stringify!($right), "`")); } } } @@ -92,7 +92,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. - $crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::None); + $crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::None, $crate::concat!("assertion failed: `", $crate::stringify!($left), " != ", $crate::stringify!($right), "`")); } } } @@ -105,7 +105,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. - $crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+))); + $crate::panicking::assert_failed(kind, &*left_val, &*right_val, $crate::option::Option::Some($crate::format_args!($($arg)+)), $crate::concat!("assertion failed: `", $crate::stringify!($left), " != ", $crate::stringify!($right), "`")); } } } diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index 48e90e6d79400..0744d10bcf520 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -199,17 +199,53 @@ pub enum AssertKind { #[cfg_attr(feature = "panic_immediate_abort", inline)] #[track_caller] #[doc(hidden)] -pub fn assert_failed( +#[rustc_const_unstable(feature = "const_assert_eq", issue = "none")] +pub const fn assert_failed( kind: AssertKind, left: &T, right: &U, args: Option>, + string_for_const_msg: &str, ) -> ! where T: fmt::Debug + ?Sized, U: fmt::Debug + ?Sized, { - assert_failed_inner(kind, &left, &right, args) + #[track_caller] + const fn assert_failed_const_impl( + _: AssertKind, + _: &T, + _: &U, + _: Option>, + msg: &str, + ) -> ! { + panic_str(msg) + } + + #[track_caller] + #[inline] + fn assert_failed_runtime_impl( + kind: AssertKind, + left: &T, + right: &U, + args: Option>, + _: &str, + ) -> ! + where + T: fmt::Debug + ?Sized, + U: fmt::Debug + ?Sized, + { + assert_failed_inner(kind, &left, &right, args) + } + + // SAFETY: we are panicking in both branches, so this is consistent. + unsafe { + crate::intrinsics::const_eval_select( + (kind, left, right, args, string_for_const_msg), + assert_failed_const_impl, + assert_failed_runtime_impl, + ) + } } /// Internal function for `assert_match!` diff --git a/tests/mir-opt/issue_99325.main.built.after.mir b/tests/mir-opt/issue_99325.main.built.after.mir index 3e035c18db862..aab53e26a0aa8 100644 --- a/tests/mir-opt/issue_99325.main.built.after.mir +++ b/tests/mir-opt/issue_99325.main.built.after.mir @@ -27,26 +27,30 @@ fn main() -> () { let mut _20: &&[u8; 4]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL let _21: &&[u8; 4]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL let mut _22: std::option::Option>; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let _23: (); // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let mut _24: (&&[u8], &&[u8; 4]); // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let mut _25: &&[u8]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let _26: &[u8]; // in scope 0 at $DIR/issue_99325.rs:+2:16: +2:70 - let mut _27: &&[u8; 4]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let _28: &[u8; 4]; // in scope 0 at $DIR/issue_99325.rs:+2:72: +2:79 - let _29: &&[u8]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let _30: &&[u8; 4]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let mut _31: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let mut _32: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let mut _33: &&[u8]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let mut _34: &&[u8; 4]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let mut _35: !; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let _37: !; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let mut _38: core::panicking::AssertKind; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let mut _39: &&[u8]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let _40: &&[u8]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let mut _41: &&[u8; 4]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let _42: &&[u8; 4]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let mut _43: std::option::Option>; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let mut _23: &str; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let _24: &str; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let _25: (); // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let mut _26: (&&[u8], &&[u8; 4]); // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let mut _27: &&[u8]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let _28: &[u8]; // in scope 0 at $DIR/issue_99325.rs:+2:16: +2:70 + let mut _29: &&[u8; 4]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let _30: &[u8; 4]; // in scope 0 at $DIR/issue_99325.rs:+2:72: +2:79 + let _31: &&[u8]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let _32: &&[u8; 4]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let mut _33: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let mut _34: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let mut _35: &&[u8]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let mut _36: &&[u8; 4]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let mut _37: !; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let _39: !; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let mut _40: core::panicking::AssertKind; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let mut _41: &&[u8]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let _42: &&[u8]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let mut _43: &&[u8; 4]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let _44: &&[u8; 4]; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let mut _45: std::option::Option>; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let mut _46: &str; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let _47: &str; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL scope 1 { debug left_val => _8; // in scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL debug right_val => _9; // in scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL @@ -56,11 +60,11 @@ fn main() -> () { } } scope 3 { - debug left_val => _29; // in scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - debug right_val => _30; // in scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let _36: core::panicking::AssertKind; // in scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + debug left_val => _31; // in scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + debug right_val => _32; // in scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let _38: core::panicking::AssertKind; // in scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL scope 4 { - debug kind => _36; // in scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + debug kind => _38; // in scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL } } @@ -129,10 +133,17 @@ fn main() -> () { _20 = &(*_21); // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageLive(_22); // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL _22 = Option::>::None; // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _16 = core::panicking::assert_failed::<&[u8], &[u8; 4]>(move _17, move _18, move _20, move _22) -> bb19; // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageLive(_23); // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageLive(_24); // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _24 = const "assertion failed: `function_with_bytes::() == &[0x41, 0x41, 0x41, 0x41]`"; // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL // mir::Constant // + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL - // + literal: Const { ty: for<'a, 'b, 'c> fn(core::panicking::AssertKind, &'a &[u8], &'b &[u8; 4], Option>) -> ! {core::panicking::assert_failed::<&[u8], &[u8; 4]>}, val: Value() } + // + literal: Const { ty: &str, val: Value(Slice(..)) } + _23 = &(*_24); // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _16 = core::panicking::assert_failed::<&[u8], &[u8; 4]>(move _17, move _18, move _20, move _22, move _23) -> bb19; // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + // mir::Constant + // + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL + // + literal: Const { ty: for<'a, 'b, 'c, 'd> fn(core::panicking::AssertKind, &'a &[u8], &'b &[u8; 4], Option>, &'d str) -> ! {core::panicking::assert_failed::<&[u8], &[u8; 4]>}, val: Value() } } bb4: { @@ -140,10 +151,12 @@ fn main() -> () { } bb5: { + StorageDead(_23); // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_22); // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_20); // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_18); // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_17); // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageDead(_24); // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_21); // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_19); // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_16); // scope 2 at $SRC_DIR/core/src/macros/mod.rs:LL:COL @@ -173,11 +186,11 @@ fn main() -> () { StorageDead(_4); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_2); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_1); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageLive(_23); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageLive(_24); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageLive(_25); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageLive(_26); // scope 0 at $DIR/issue_99325.rs:+2:16: +2:70 - _26 = function_with_bytes::<&*b"AAAA">() -> [return: bb10, unwind: bb19]; // scope 0 at $DIR/issue_99325.rs:+2:16: +2:70 + StorageLive(_26); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageLive(_27); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageLive(_28); // scope 0 at $DIR/issue_99325.rs:+2:16: +2:70 + _28 = function_with_bytes::<&*b"AAAA">() -> [return: bb10, unwind: bb19]; // scope 0 at $DIR/issue_99325.rs:+2:16: +2:70 // mir::Constant // + span: $DIR/issue_99325.rs:11:16: 11:68 // + user_ty: UserType(1) @@ -185,63 +198,70 @@ fn main() -> () { } bb10: { - _25 = &_26; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageLive(_27); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageLive(_28); // scope 0 at $DIR/issue_99325.rs:+2:72: +2:79 - _28 = const b"AAAA"; // scope 0 at $DIR/issue_99325.rs:+2:72: +2:79 + _27 = &_28; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageLive(_29); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageLive(_30); // scope 0 at $DIR/issue_99325.rs:+2:72: +2:79 + _30 = const b"AAAA"; // scope 0 at $DIR/issue_99325.rs:+2:72: +2:79 // mir::Constant // + span: $DIR/issue_99325.rs:11:72: 11:79 // + literal: Const { ty: &[u8; 4], val: Value(Scalar(alloc4)) } - _27 = &_28; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _24 = (move _25, move _27); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _29 = &_30; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _26 = (move _27, move _29); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageDead(_29); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_27); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageDead(_25); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - FakeRead(ForMatchedPlace(None), _24); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageLive(_29); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _29 = (_24.0: &&[u8]); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageLive(_30); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _30 = (_24.1: &&[u8; 4]); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageLive(_31); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageLive(_32); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + FakeRead(ForMatchedPlace(None), _26); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageLive(_31); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _31 = (_26.0: &&[u8]); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageLive(_32); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _32 = (_26.1: &&[u8; 4]); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageLive(_33); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _33 = &(*_29); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageLive(_34); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _34 = &(*_30); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _32 = <&[u8] as PartialEq<&[u8; 4]>>::eq(move _33, move _34) -> [return: bb11, unwind: bb19]; // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageLive(_35); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _35 = &(*_31); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageLive(_36); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _36 = &(*_32); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _34 = <&[u8] as PartialEq<&[u8; 4]>>::eq(move _35, move _36) -> [return: bb11, unwind: bb19]; // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL // mir::Constant // + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL // + literal: Const { ty: for<'a, 'b> fn(&'a &[u8], &'b &[u8; 4]) -> bool {<&[u8] as PartialEq<&[u8; 4]>>::eq}, val: Value() } } bb11: { + StorageDead(_36); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageDead(_35); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _33 = Not(move _34); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_34); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageDead(_33); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _31 = Not(move _32); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageDead(_32); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - switchInt(move _31) -> [0: bb13, otherwise: bb12]; // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + switchInt(move _33) -> [0: bb13, otherwise: bb12]; // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL } bb12: { - StorageLive(_36); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _36 = core::panicking::AssertKind::Eq; // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - FakeRead(ForLet(None), _36); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageLive(_37); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageLive(_38); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _38 = move _36; // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageLive(_38); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _38 = core::panicking::AssertKind::Eq; // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + FakeRead(ForLet(None), _38); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageLive(_39); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageLive(_40); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _40 = &(*_29); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _39 = &(*_40); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _40 = move _38; // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageLive(_41); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageLive(_42); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _42 = &(*_30); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _42 = &(*_31); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL _41 = &(*_42); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageLive(_43); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _43 = Option::>::None; // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _37 = core::panicking::assert_failed::<&[u8], &[u8; 4]>(move _38, move _39, move _41, move _43) -> bb19; // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageLive(_44); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _44 = &(*_32); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _43 = &(*_44); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageLive(_45); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _45 = Option::>::None; // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageLive(_46); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageLive(_47); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _47 = const "assertion failed: `function_with_bytes::<{ &[0x41, 0x41, 0x41, 0x41] }>() == b\"AAAA\"`"; // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + // mir::Constant + // + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL + // + literal: Const { ty: &str, val: Value(Slice(..)) } + _46 = &(*_47); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _39 = core::panicking::assert_failed::<&[u8], &[u8; 4]>(move _40, move _41, move _43, move _45, move _46) -> bb19; // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL // mir::Constant // + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL - // + literal: Const { ty: for<'a, 'b, 'c> fn(core::panicking::AssertKind, &'a &[u8], &'b &[u8; 4], Option>) -> ! {core::panicking::assert_failed::<&[u8], &[u8; 4]>}, val: Value() } + // + literal: Const { ty: for<'a, 'b, 'c, 'd> fn(core::panicking::AssertKind, &'a &[u8], &'b &[u8; 4], Option>, &'d str) -> ! {core::panicking::assert_failed::<&[u8], &[u8; 4]>}, val: Value() } } bb13: { @@ -249,14 +269,16 @@ fn main() -> () { } bb14: { + StorageDead(_46); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageDead(_45); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_43); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_41); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageDead(_39); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageDead(_38); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageDead(_42); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_40); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageDead(_37); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageDead(_36); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageDead(_47); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageDead(_44); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageDead(_42); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageDead(_39); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageDead(_38); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL unreachable; // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL } @@ -265,22 +287,22 @@ fn main() -> () { } bb16: { - _23 = const (); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _25 = const (); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL goto -> bb17; // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL } bb17: { - StorageDead(_31); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageDead(_30); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageDead(_29); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageDead(_33); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageDead(_32); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageDead(_31); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL goto -> bb18; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL } bb18: { + StorageDead(_30); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_28); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageDead(_26); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageDead(_24); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - StorageDead(_23); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageDead(_25); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL _0 = const (); // scope 0 at $DIR/issue_99325.rs:+0:15: +3:2 return; // scope 0 at $DIR/issue_99325.rs:+3:2: +3:2 } diff --git a/tests/mir-opt/retag.array_casts.SimplifyCfg-elaborate-drops.after.mir b/tests/mir-opt/retag.array_casts.SimplifyCfg-elaborate-drops.after.mir index 19b726e748453..fa7edf948af68 100644 --- a/tests/mir-opt/retag.array_casts.SimplifyCfg-elaborate-drops.after.mir +++ b/tests/mir-opt/retag.array_casts.SimplifyCfg-elaborate-drops.after.mir @@ -30,6 +30,8 @@ fn array_casts() -> () { let mut _32: &usize; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL let _33: &usize; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL let mut _34: std::option::Option>; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let mut _35: &str; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let _36: &str; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL scope 1 { debug x => _1; // in scope 1 at $DIR/retag.rs:+1:9: +1:14 let _2: *mut usize; // in scope 1 at $DIR/retag.rs:+2:9: +2:10 @@ -45,7 +47,7 @@ fn array_casts() -> () { debug p => _9; // in scope 5 at $DIR/retag.rs:+6:9: +6:10 let _20: &usize; // in scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL let _21: &usize; // in scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let mut _35: &usize; // in scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let mut _37: &usize; // in scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL scope 6 { } scope 7 { @@ -116,12 +118,12 @@ fn array_casts() -> () { _15 = (*_16); // scope 6 at $DIR/retag.rs:+7:25: +7:34 _14 = &_15; // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL StorageLive(_18); // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _35 = const _; // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _37 = const _; // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL // mir::Constant // + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL // + literal: Const { ty: &usize, val: Unevaluated(array_casts, [], Some(promoted[0])) } - Retag(_35); // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _18 = &(*_35); // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + Retag(_37); // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _18 = &(*_37); // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL Deinit(_13); // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL (_13.0: &usize) = move _14; // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL (_13.1: &usize) = move _18; // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL @@ -167,10 +169,18 @@ fn array_casts() -> () { Deinit(_34); // scope 8 at $SRC_DIR/core/src/macros/mod.rs:LL:COL discriminant(_34) = 0; // scope 8 at $SRC_DIR/core/src/macros/mod.rs:LL:COL Retag(_34); // scope 8 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _28 = core::panicking::assert_failed::(move _29, move _30, move _32, move _34); // scope 8 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageLive(_35); // scope 8 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + StorageLive(_36); // scope 8 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _36 = const "assertion failed: `unsafe { *p.add(1) } == 1`"; // scope 8 at $SRC_DIR/core/src/macros/mod.rs:LL:COL // mir::Constant // + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL - // + literal: Const { ty: for<'a, 'b, 'c> fn(core::panicking::AssertKind, &'a usize, &'b usize, Option>) -> ! {core::panicking::assert_failed::}, val: Value() } + // + literal: Const { ty: &str, val: Value(Slice(..)) } + Retag(_36); // scope 8 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _35 = &(*_36); // scope 8 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _28 = core::panicking::assert_failed::(move _29, move _30, move _32, move _34, move _35); // scope 8 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + // mir::Constant + // + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL + // + literal: Const { ty: for<'a, 'b, 'c, 'd> fn(core::panicking::AssertKind, &'a usize, &'b usize, Option>, &'d str) -> ! {core::panicking::assert_failed::}, val: Value() } } bb4: { diff --git a/tests/ui/consts/assert_eq_gate.rs b/tests/ui/consts/assert_eq_gate.rs new file mode 100644 index 0000000000000..5957b32498bef --- /dev/null +++ b/tests/ui/consts/assert_eq_gate.rs @@ -0,0 +1,5 @@ +const _: () = assert_eq!(1, 1); +//~^ ERROR `core::panicking::assert_failed` is not yet stable as a const fn +//~| HELP add `#![feature(const_assert_eq)]` to the crate attributes to enable + +fn main() {} diff --git a/tests/ui/consts/assert_eq_gate.stderr b/tests/ui/consts/assert_eq_gate.stderr new file mode 100644 index 0000000000000..e684c735c0079 --- /dev/null +++ b/tests/ui/consts/assert_eq_gate.stderr @@ -0,0 +1,11 @@ +error: `core::panicking::assert_failed` is not yet stable as a const fn + --> $DIR/assert_eq_gate.rs:1:15 + | +LL | const _: () = assert_eq!(1, 1); + | ^^^^^^^^^^^^^^^^ + | + = help: add `#![feature(const_assert_eq)]` to the crate attributes to enable + = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to previous error + diff --git a/tests/ui/consts/assertions.rs b/tests/ui/consts/assertions.rs new file mode 100644 index 0000000000000..75a05a4f54fd5 --- /dev/null +++ b/tests/ui/consts/assertions.rs @@ -0,0 +1,15 @@ +#![feature(const_assert_eq)] + +const _BAD1: () = { + assert_eq!(1, 2) +}; //~^ ERROR: evaluation of constant value failed + +const _BAD2: () = { + assert_ne!(1, 1) +}; //~^ ERROR: evaluation of constant value failed + +const _BAD3: () = { + assert!(false) +}; //~^ ERROR: evaluation of constant value failed + +fn main() {} diff --git a/tests/ui/consts/assertions.stderr b/tests/ui/consts/assertions.stderr new file mode 100644 index 0000000000000..2801d0547752b --- /dev/null +++ b/tests/ui/consts/assertions.stderr @@ -0,0 +1,27 @@ +error[E0080]: evaluation of constant value failed + --> $DIR/assertions.rs:4:5 + | +LL | assert_eq!(1, 2) + | ^^^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: `1 == 2`', $DIR/assertions.rs:4:5 + | + = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0080]: evaluation of constant value failed + --> $DIR/assertions.rs:8:5 + | +LL | assert_ne!(1, 1) + | ^^^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: `1 != 1`', $DIR/assertions.rs:8:5 + | + = note: this error originates in the macro `assert_ne` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0080]: evaluation of constant value failed + --> $DIR/assertions.rs:12:5 + | +LL | assert!(false) + | ^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: false', $DIR/assertions.rs:12:5 + | + = note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0080`.