summaryrefslogtreecommitdiffstats
path: root/tests/ui/macros/rfc-2011-nicer-assert-messages
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
commit64d98f8ee037282c35007b64c2649055c56af1db (patch)
tree5492bcf97fce41ee1c0b1cc2add283f3e66cdab0 /tests/ui/macros/rfc-2011-nicer-assert-messages
parentAdding debian version 1.67.1+dfsg1-1. (diff)
downloadrustc-64d98f8ee037282c35007b64c2649055c56af1db.tar.xz
rustc-64d98f8ee037282c35007b64c2649055c56af1db.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/macros/rfc-2011-nicer-assert-messages')
-rw-r--r--tests/ui/macros/rfc-2011-nicer-assert-messages/all-expr-kinds.rs186
-rw-r--r--tests/ui/macros/rfc-2011-nicer-assert-messages/all-not-available-cases.rs44
-rw-r--r--tests/ui/macros/rfc-2011-nicer-assert-messages/assert-with-custom-errors-does-not-create-unnecessary-code.rs13
-rw-r--r--tests/ui/macros/rfc-2011-nicer-assert-messages/assert-without-captures-does-not-create-unnecessary-code.rs15
-rw-r--r--tests/ui/macros/rfc-2011-nicer-assert-messages/auxiliary/common.rs25
-rw-r--r--tests/ui/macros/rfc-2011-nicer-assert-messages/feature-gate-generic_assert.rs26
-rw-r--r--tests/ui/macros/rfc-2011-nicer-assert-messages/non-consuming-methods-have-optimized-codegen.rs32
-rw-r--r--tests/ui/macros/rfc-2011-nicer-assert-messages/non-consuming-methods-have-optimized-codegen.stdout147
8 files changed, 488 insertions, 0 deletions
diff --git a/tests/ui/macros/rfc-2011-nicer-assert-messages/all-expr-kinds.rs b/tests/ui/macros/rfc-2011-nicer-assert-messages/all-expr-kinds.rs
new file mode 100644
index 000000000..b8b6f0846
--- /dev/null
+++ b/tests/ui/macros/rfc-2011-nicer-assert-messages/all-expr-kinds.rs
@@ -0,0 +1,186 @@
+// edition:2021
+// ignore-tidy-linelength
+// only-x86_64
+// run-pass
+// needs-unwind Asserting on contents of error message
+
+#![allow(path_statements, unused_allocation)]
+#![feature(box_syntax, core_intrinsics, generic_assert, generic_assert_internals)]
+
+macro_rules! test {
+ (
+ let mut $elem_ident:ident = $elem_expr:expr;
+ [ $($assert:tt)* ] => $msg:literal
+ ) => {
+ {
+ #[allow(unused_assignments, unused_mut, unused_variables)]
+ let rslt = std::panic::catch_unwind(|| {
+ let mut $elem_ident = $elem_expr;
+ assert!($($assert)*);
+ });
+ let err = rslt.unwrap_err();
+ if let Some(elem) = err.downcast_ref::<String>() {
+ assert_eq!(elem, &$msg);
+ }
+ else if let Some(elem) = err.downcast_ref::<&str>() {
+ assert_eq!(elem, &$msg);
+ }
+ else {
+ panic!("assert!( ... ) should return a string");
+ }
+ }
+ }
+}
+
+macro_rules! tests {
+ (
+ let mut $elem_ident:ident = $elem_expr:expr;
+
+ $(
+ [ $($elem_assert:tt)* ] => $elem_msg:literal
+ )+
+ ) => {
+ $(
+ test!(
+ let mut $elem_ident = $elem_expr;
+ [ $($elem_assert)* ] => $elem_msg
+ );
+ )+
+ }
+}
+
+const FOO: Foo = Foo { bar: 1 };
+
+#[derive(Clone, Copy, Debug, PartialEq)]
+struct Foo {
+ bar: i32
+}
+
+impl Foo {
+ fn add(&self, a: i32, b: i32) -> i32 { a + b }
+}
+
+fn add(a: i32, b: i32) -> i32 { a + b }
+
+fn main() {
+ // ***** Allowed *****
+
+ tests!(
+ let mut elem = 1i32;
+
+ // addr of
+ [ &elem == &3 ] => "Assertion failed: &elem == &3\nWith captures:\n elem = 1\n"
+
+ // array
+ [ [elem][0] == 3 ] => "Assertion failed: [elem][0] == 3\nWith captures:\n elem = 1\n"
+
+ // binary
+ [ elem + 1 == 3 ] => "Assertion failed: elem + 1 == 3\nWith captures:\n elem = 1\n"
+
+ // call
+ [ add(elem, elem) == 3 ] => "Assertion failed: add(elem, elem) == 3\nWith captures:\n elem = 1\n"
+
+ // cast
+ [ elem as i32 == 3 ] => "Assertion failed: elem as i32 == 3\nWith captures:\n elem = 1\n"
+
+ // index
+ [ [1i32, 1][elem as usize] == 3 ] => "Assertion failed: [1i32, 1][elem as usize] == 3\nWith captures:\n elem = 1\n"
+
+ // method call
+ [ FOO.add(elem, elem) == 3 ] => "Assertion failed: FOO.add(elem, elem) == 3\nWith captures:\n elem = 1\n"
+
+ // paren
+ [ (elem) == 3 ] => "Assertion failed: (elem) == 3\nWith captures:\n elem = 1\n"
+
+ // range
+ [ (0..elem) == (0..3) ] => "Assertion failed: (0..elem) == (0..3)\nWith captures:\n elem = 1\n"
+
+ // repeat
+ [ [elem; 1] == [3; 1] ] => "Assertion failed: [elem; 1] == [3; 1]\nWith captures:\n elem = 1\n"
+
+ // struct
+ [ Foo { bar: elem } == Foo { bar: 3 } ] => "Assertion failed: Foo { bar: elem } == Foo { bar: 3 }\nWith captures:\n elem = 1\n"
+
+ // tuple
+ [ (elem, 1) == (3, 3) ] => "Assertion failed: (elem, 1) == (3, 3)\nWith captures:\n elem = 1\n"
+
+ // unary
+ [ -elem == -3 ] => "Assertion failed: -elem == -3\nWith captures:\n elem = 1\n"
+ );
+
+ // ***** Disallowed *****
+
+ tests!(
+ let mut elem = 1i32;
+
+ // assign
+ [ { let local = elem; local } == 3 ] => "Assertion failed: { let local = elem; local } == 3"
+
+ // assign op
+ [ { elem += 1; elem } == 3 ] => "Assertion failed: { elem += 1; elem } == 3"
+
+ // async
+ [ { let _ = async { elem }; elem } == 3 ] => "Assertion failed: { let _ = async { elem }; elem } == 3"
+
+ // await
+
+ // block
+ [ { elem } == 3 ] => "Assertion failed: { elem } == 3"
+
+ // box
+ [ box elem == box 3 ] => "Assertion failed: box elem == box 3"
+
+ // break
+ [ loop { break elem; } == 3 ] => "Assertion failed: loop { break elem; } == 3"
+
+ // closure
+ [(|| elem)() == 3 ] => "Assertion failed: (|| elem)() == 3"
+
+ // const block
+
+ // continue
+
+ // err
+
+ // field
+ [ FOO.bar == 3 ] => "Assertion failed: FOO.bar == 3"
+
+ // for loop
+ [ { for _ in 0..elem { elem; } elem } == 3 ] => "Assertion failed: { for _ in 0..elem { elem; } elem } == 3"
+
+ // if
+ [ if true { elem } else { elem } == 3 ] => "Assertion failed: if true { elem } else { elem } == 3"
+
+ // inline asm
+
+ // let
+ [ if let true = true { elem } else { elem } == 3 ] => "Assertion failed: if let true = true { elem } else { elem } == 3"
+
+ // lit
+
+ // loop
+ [ loop { elem; break elem; } == 3 ] => "Assertion failed: loop { elem; break elem; } == 3"
+
+ // mac call
+
+ // match
+ [ match elem { _ => elem } == 3 ] => "Assertion failed: match elem { _ => elem, } == 3"
+
+ // ret
+ [ (|| { return elem; })() == 3 ] => "Assertion failed: (|| { return elem; })() == 3"
+
+ // try
+ [ (|| { Some(Some(elem)?) })() == Some(3) ] => "Assertion failed: (|| { Some(Some(elem)?) })() == Some(3)"
+
+ // try block
+
+ // underscore
+
+ // while
+ [ { while false { elem; break; } elem } == 3 ] => "Assertion failed: { while false { elem; break; } elem } == 3"
+
+ // yeet
+
+ // yield
+ );
+}
diff --git a/tests/ui/macros/rfc-2011-nicer-assert-messages/all-not-available-cases.rs b/tests/ui/macros/rfc-2011-nicer-assert-messages/all-not-available-cases.rs
new file mode 100644
index 000000000..d46f396ee
--- /dev/null
+++ b/tests/ui/macros/rfc-2011-nicer-assert-messages/all-not-available-cases.rs
@@ -0,0 +1,44 @@
+// aux-build:common.rs
+// ignore-tidy-linelength
+// only-x86_64
+// run-pass
+// needs-unwind Asserting on contents of error message
+
+#![feature(core_intrinsics, generic_assert, generic_assert_internals)]
+
+extern crate common;
+
+#[derive(Clone, Copy, PartialEq)]
+struct CopyNoDebug(i32);
+
+#[derive(Debug, PartialEq)]
+struct NoCopyDebug(i32);
+
+#[derive(PartialEq)]
+struct NoCopyNoDebug(i32);
+
+fn main() {
+ // Has Copy but does not have Debug
+ common::test!(
+ let mut copy_no_debug = CopyNoDebug(1);
+ [ copy_no_debug == CopyNoDebug(3) ] => "Assertion failed: copy_no_debug == CopyNoDebug(3)\nWith captures:\n copy_no_debug = N/A\n"
+ );
+
+ // Does not have Copy but has Debug
+ common::test!(
+ let mut no_copy_debug = NoCopyDebug(1);
+ [ no_copy_debug == NoCopyDebug(3) ] => "Assertion failed: no_copy_debug == NoCopyDebug(3)\nWith captures:\n no_copy_debug = N/A\n"
+ );
+
+ // Does not have Copy and does not have Debug
+ common::test!(
+ let mut no_copy_no_debug = NoCopyNoDebug(1);
+ [ no_copy_no_debug == NoCopyNoDebug(3) ] => "Assertion failed: no_copy_no_debug == NoCopyNoDebug(3)\nWith captures:\n no_copy_no_debug = N/A\n"
+ );
+
+ // Unevaluated (Expression short-circuited)
+ common::test!(
+ let mut elem = true;
+ [ false && elem ] => "Assertion failed: false && elem\nWith captures:\n elem = N/A\n"
+ );
+}
diff --git a/tests/ui/macros/rfc-2011-nicer-assert-messages/assert-with-custom-errors-does-not-create-unnecessary-code.rs b/tests/ui/macros/rfc-2011-nicer-assert-messages/assert-with-custom-errors-does-not-create-unnecessary-code.rs
new file mode 100644
index 000000000..6a1435f79
--- /dev/null
+++ b/tests/ui/macros/rfc-2011-nicer-assert-messages/assert-with-custom-errors-does-not-create-unnecessary-code.rs
@@ -0,0 +1,13 @@
+// compile-flags: --test
+// run-pass
+
+#![feature(core_intrinsics, generic_assert, generic_assert_internals)]
+
+#[should_panic(expected = "Custom user message")]
+#[test]
+fn test() {
+ assert!(1 == 3, "Custom user message");
+}
+
+fn main() {
+}
diff --git a/tests/ui/macros/rfc-2011-nicer-assert-messages/assert-without-captures-does-not-create-unnecessary-code.rs b/tests/ui/macros/rfc-2011-nicer-assert-messages/assert-without-captures-does-not-create-unnecessary-code.rs
new file mode 100644
index 000000000..1f5a29ab5
--- /dev/null
+++ b/tests/ui/macros/rfc-2011-nicer-assert-messages/assert-without-captures-does-not-create-unnecessary-code.rs
@@ -0,0 +1,15 @@
+// aux-build:common.rs
+// only-x86_64
+// run-pass
+// needs-unwind Asserting on contents of error message
+
+#![feature(core_intrinsics, generic_assert, generic_assert_internals)]
+
+extern crate common;
+
+fn main() {
+ common::test!(
+ let mut _nothing = ();
+ [ 1 == 3 ] => "Assertion failed: 1 == 3"
+ );
+}
diff --git a/tests/ui/macros/rfc-2011-nicer-assert-messages/auxiliary/common.rs b/tests/ui/macros/rfc-2011-nicer-assert-messages/auxiliary/common.rs
new file mode 100644
index 000000000..903ed507c
--- /dev/null
+++ b/tests/ui/macros/rfc-2011-nicer-assert-messages/auxiliary/common.rs
@@ -0,0 +1,25 @@
+#[macro_export]
+macro_rules! test {
+ (
+ let mut $elem_ident:ident = $elem_expr:expr;
+ [ $($assert:tt)* ] => $msg:literal
+ ) => {
+ {
+ #[allow(unused_assignments, unused_mut, unused_variables)]
+ let rslt = std::panic::catch_unwind(|| {
+ let mut $elem_ident = $elem_expr;
+ assert!($($assert)*);
+ });
+ let err = rslt.unwrap_err();
+ if let Some(elem) = err.downcast_ref::<String>() {
+ assert_eq!(elem, &$msg);
+ }
+ else if let Some(elem) = err.downcast_ref::<&str>() {
+ assert_eq!(elem, &$msg);
+ }
+ else {
+ panic!("assert!( ... ) should return a string");
+ }
+ }
+ }
+}
diff --git a/tests/ui/macros/rfc-2011-nicer-assert-messages/feature-gate-generic_assert.rs b/tests/ui/macros/rfc-2011-nicer-assert-messages/feature-gate-generic_assert.rs
new file mode 100644
index 000000000..01860adaa
--- /dev/null
+++ b/tests/ui/macros/rfc-2011-nicer-assert-messages/feature-gate-generic_assert.rs
@@ -0,0 +1,26 @@
+// compile-flags: --test
+// ignore-tidy-linelength
+// run-pass
+
+#![feature(core_intrinsics, generic_assert, generic_assert_internals)]
+
+use std::fmt::{Debug, Formatter};
+
+#[derive(Clone, Copy, PartialEq)]
+struct CopyDebug(i32);
+
+impl Debug for CopyDebug {
+ fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
+ f.write_str("With great power comes great electricity bills")
+ }
+}
+
+#[should_panic(expected = "Assertion failed: copy_debug == CopyDebug(3)\nWith captures:\n copy_debug = With great power comes great electricity bills\n")]
+#[test]
+fn test() {
+ let copy_debug = CopyDebug(1);
+ assert!(copy_debug == CopyDebug(3));
+}
+
+fn main() {
+}
diff --git a/tests/ui/macros/rfc-2011-nicer-assert-messages/non-consuming-methods-have-optimized-codegen.rs b/tests/ui/macros/rfc-2011-nicer-assert-messages/non-consuming-methods-have-optimized-codegen.rs
new file mode 100644
index 000000000..5ec84b08f
--- /dev/null
+++ b/tests/ui/macros/rfc-2011-nicer-assert-messages/non-consuming-methods-have-optimized-codegen.rs
@@ -0,0 +1,32 @@
+// check-pass
+// compile-flags: -Z unpretty=expanded
+
+#![feature(core_intrinsics, generic_assert, generic_assert_internals)]
+
+fn arbitrary_consuming_method_for_demonstration_purposes() {
+ let elem = 1i32;
+ assert!(elem as usize);
+}
+
+fn addr_of() {
+ let elem = 1i32;
+ assert!(&elem);
+}
+
+fn binary() {
+ let elem = 1i32;
+ assert!(elem == 1);
+ assert!(elem >= 1);
+ assert!(elem > 0);
+ assert!(elem < 3);
+ assert!(elem <= 3);
+ assert!(elem != 3);
+}
+
+fn unary() {
+ let elem = &1i32;
+ assert!(*elem);
+}
+
+fn main() {
+}
diff --git a/tests/ui/macros/rfc-2011-nicer-assert-messages/non-consuming-methods-have-optimized-codegen.stdout b/tests/ui/macros/rfc-2011-nicer-assert-messages/non-consuming-methods-have-optimized-codegen.stdout
new file mode 100644
index 000000000..90f858f80
--- /dev/null
+++ b/tests/ui/macros/rfc-2011-nicer-assert-messages/non-consuming-methods-have-optimized-codegen.stdout
@@ -0,0 +1,147 @@
+#![feature(prelude_import)]
+#![no_std]
+// check-pass
+// compile-flags: -Z unpretty=expanded
+
+#![feature(core_intrinsics, generic_assert, generic_assert_internals)]
+#[prelude_import]
+use ::std::prelude::rust_2015::*;
+#[macro_use]
+extern crate std;
+
+fn arbitrary_consuming_method_for_demonstration_purposes() {
+ let elem = 1i32;
+ {
+ #[allow(unused_imports)]
+ use ::core::asserting::{TryCaptureGeneric, TryCapturePrintable};
+ let mut __capture0 = ::core::asserting::Capture::new();
+ let __local_bind0 = &elem;
+ if ::core::intrinsics::unlikely(!(*{
+ (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
+ __local_bind0
+ } as usize)) {
+
+
+
+
+ {
+ ::std::rt::panic_fmt(::core::fmt::Arguments::new_v1(&["Assertion failed: elem as usize\nWith captures:\n elem = ",
+ "\n"], &[::core::fmt::ArgumentV1::new_debug(&__capture0)]))
+ }
+ }
+ };
+}
+fn addr_of() {
+ let elem = 1i32;
+ {
+ #[allow(unused_imports)]
+ use ::core::asserting::{TryCaptureGeneric, TryCapturePrintable};
+ let mut __capture0 = ::core::asserting::Capture::new();
+ let __local_bind0 = &elem;
+ if ::core::intrinsics::unlikely(!&*__local_bind0) {
+ (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
+ {
+ ::std::rt::panic_fmt(::core::fmt::Arguments::new_v1(&["Assertion failed: &elem\nWith captures:\n elem = ",
+ "\n"], &[::core::fmt::ArgumentV1::new_debug(&__capture0)]))
+ }
+ }
+ };
+}
+fn binary() {
+ let elem = 1i32;
+ {
+ #[allow(unused_imports)]
+ use ::core::asserting::{TryCaptureGeneric, TryCapturePrintable};
+ let mut __capture0 = ::core::asserting::Capture::new();
+ let __local_bind0 = &elem;
+ if ::core::intrinsics::unlikely(!(*__local_bind0 == 1)) {
+ (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
+ {
+ ::std::rt::panic_fmt(::core::fmt::Arguments::new_v1(&["Assertion failed: elem == 1\nWith captures:\n elem = ",
+ "\n"], &[::core::fmt::ArgumentV1::new_debug(&__capture0)]))
+ }
+ }
+ };
+ {
+ #[allow(unused_imports)]
+ use ::core::asserting::{TryCaptureGeneric, TryCapturePrintable};
+ let mut __capture0 = ::core::asserting::Capture::new();
+ let __local_bind0 = &elem;
+ if ::core::intrinsics::unlikely(!(*__local_bind0 >= 1)) {
+ (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
+ {
+ ::std::rt::panic_fmt(::core::fmt::Arguments::new_v1(&["Assertion failed: elem >= 1\nWith captures:\n elem = ",
+ "\n"], &[::core::fmt::ArgumentV1::new_debug(&__capture0)]))
+ }
+ }
+ };
+ {
+ #[allow(unused_imports)]
+ use ::core::asserting::{TryCaptureGeneric, TryCapturePrintable};
+ let mut __capture0 = ::core::asserting::Capture::new();
+ let __local_bind0 = &elem;
+ if ::core::intrinsics::unlikely(!(*__local_bind0 > 0)) {
+ (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
+ {
+ ::std::rt::panic_fmt(::core::fmt::Arguments::new_v1(&["Assertion failed: elem > 0\nWith captures:\n elem = ",
+ "\n"], &[::core::fmt::ArgumentV1::new_debug(&__capture0)]))
+ }
+ }
+ };
+ {
+ #[allow(unused_imports)]
+ use ::core::asserting::{TryCaptureGeneric, TryCapturePrintable};
+ let mut __capture0 = ::core::asserting::Capture::new();
+ let __local_bind0 = &elem;
+ if ::core::intrinsics::unlikely(!(*__local_bind0 < 3)) {
+ (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
+ {
+ ::std::rt::panic_fmt(::core::fmt::Arguments::new_v1(&["Assertion failed: elem < 3\nWith captures:\n elem = ",
+ "\n"], &[::core::fmt::ArgumentV1::new_debug(&__capture0)]))
+ }
+ }
+ };
+ {
+ #[allow(unused_imports)]
+ use ::core::asserting::{TryCaptureGeneric, TryCapturePrintable};
+ let mut __capture0 = ::core::asserting::Capture::new();
+ let __local_bind0 = &elem;
+ if ::core::intrinsics::unlikely(!(*__local_bind0 <= 3)) {
+ (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
+ {
+ ::std::rt::panic_fmt(::core::fmt::Arguments::new_v1(&["Assertion failed: elem <= 3\nWith captures:\n elem = ",
+ "\n"], &[::core::fmt::ArgumentV1::new_debug(&__capture0)]))
+ }
+ }
+ };
+ {
+ #[allow(unused_imports)]
+ use ::core::asserting::{TryCaptureGeneric, TryCapturePrintable};
+ let mut __capture0 = ::core::asserting::Capture::new();
+ let __local_bind0 = &elem;
+ if ::core::intrinsics::unlikely(!(*__local_bind0 != 3)) {
+ (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
+ {
+ ::std::rt::panic_fmt(::core::fmt::Arguments::new_v1(&["Assertion failed: elem != 3\nWith captures:\n elem = ",
+ "\n"], &[::core::fmt::ArgumentV1::new_debug(&__capture0)]))
+ }
+ }
+ };
+}
+fn unary() {
+ let elem = &1i32;
+ {
+ #[allow(unused_imports)]
+ use ::core::asserting::{TryCaptureGeneric, TryCapturePrintable};
+ let mut __capture0 = ::core::asserting::Capture::new();
+ let __local_bind0 = &elem;
+ if ::core::intrinsics::unlikely(!**__local_bind0) {
+ (&::core::asserting::Wrapper(__local_bind0)).try_capture(&mut __capture0);
+ {
+ ::std::rt::panic_fmt(::core::fmt::Arguments::new_v1(&["Assertion failed: *elem\nWith captures:\n elem = ",
+ "\n"], &[::core::fmt::ArgumentV1::new_debug(&__capture0)]))
+ }
+ }
+ };
+}
+fn main() {}