summaryrefslogtreecommitdiffstats
path: root/tests/ui/rfc-2361-dbg-macro
diff options
context:
space:
mode:
Diffstat (limited to 'tests/ui/rfc-2361-dbg-macro')
-rw-r--r--tests/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.rs67
-rw-r--r--tests/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.run.stderr28
-rw-r--r--tests/ui/rfc-2361-dbg-macro/dbg-macro-move-semantics.rs10
-rw-r--r--tests/ui/rfc-2361-dbg-macro/dbg-macro-move-semantics.stderr14
-rw-r--r--tests/ui/rfc-2361-dbg-macro/dbg-macro-requires-debug.rs7
-rw-r--r--tests/ui/rfc-2361-dbg-macro/dbg-macro-requires-debug.stderr17
6 files changed, 143 insertions, 0 deletions
diff --git a/tests/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.rs b/tests/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.rs
new file mode 100644
index 000000000..04d924a9a
--- /dev/null
+++ b/tests/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.rs
@@ -0,0 +1,67 @@
+// run-pass
+// check-run-results
+
+// Tests ensuring that `dbg!(expr)` has the expected run-time behavior.
+// as well as some compile time properties we expect.
+
+#[derive(Copy, Clone, Debug)]
+struct Unit;
+
+#[derive(Copy, Clone, Debug, PartialEq)]
+struct Point<T> {
+ x: T,
+ y: T,
+}
+
+#[derive(Debug, PartialEq)]
+struct NoCopy(usize);
+
+fn main() {
+ let a: Unit = dbg!(Unit);
+ let _: Unit = dbg!(a);
+ // We can move `a` because it's Copy.
+ drop(a);
+
+ // `Point<T>` will be faithfully formatted according to `{:#?}`.
+ let a = Point { x: 42, y: 24 };
+ let b: Point<u8> = dbg!(Point { x: 42, y: 24 }); // test stringify!(..)
+ let c: Point<u8> = dbg!(b);
+ // Identity conversion:
+ assert_eq!(a, b);
+ assert_eq!(a, c);
+ // We can move `b` because it's Copy.
+ drop(b);
+
+ // Without parameters works as expected.
+ let _: () = dbg!();
+
+ // Test that we can borrow and that successive applications is still identity.
+ let a = NoCopy(1337);
+ let b: &NoCopy = dbg!(dbg!(&a));
+ assert_eq!(&a, b);
+
+ // Test involving lifetimes of temporaries:
+ fn f<'a>(x: &'a u8) -> &'a u8 { x }
+ let a: &u8 = dbg!(f(&42));
+ assert_eq!(a, &42);
+
+ // Test side effects:
+ let mut foo = 41;
+ assert_eq!(7331, dbg!({
+ foo += 1;
+ eprintln!("before");
+ 7331
+ }));
+ assert_eq!(foo, 42);
+
+ // Test trailing comma:
+ assert_eq!(("Yeah",), dbg!(("Yeah",)));
+
+ // Test multiple arguments:
+ assert_eq!((1u8, 2u32), dbg!(1,
+ 2));
+
+ // Test multiple arguments + trailing comma:
+ assert_eq!((1u8, 2u32, "Yeah"), dbg!(1u8, 2u32,
+ "Yeah",));
+}
diff --git a/tests/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.run.stderr b/tests/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.run.stderr
new file mode 100644
index 000000000..49d72158e
--- /dev/null
+++ b/tests/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.run.stderr
@@ -0,0 +1,28 @@
+[$DIR/dbg-macro-expected-behavior.rs:20] Unit = Unit
+[$DIR/dbg-macro-expected-behavior.rs:21] a = Unit
+[$DIR/dbg-macro-expected-behavior.rs:27] Point { x: 42, y: 24 } = Point {
+ x: 42,
+ y: 24,
+}
+[$DIR/dbg-macro-expected-behavior.rs:28] b = Point {
+ x: 42,
+ y: 24,
+}
+[$DIR/dbg-macro-expected-behavior.rs:36]
+[$DIR/dbg-macro-expected-behavior.rs:40] &a = NoCopy(
+ 1337,
+)
+[$DIR/dbg-macro-expected-behavior.rs:40] dbg!(& a) = NoCopy(
+ 1337,
+)
+[$DIR/dbg-macro-expected-behavior.rs:45] f(&42) = 42
+before
+[$DIR/dbg-macro-expected-behavior.rs:50] { foo += 1; eprintln!("before"); 7331 } = 7331
+[$DIR/dbg-macro-expected-behavior.rs:58] ("Yeah",) = (
+ "Yeah",
+)
+[$DIR/dbg-macro-expected-behavior.rs:61] 1 = 1
+[$DIR/dbg-macro-expected-behavior.rs:61] 2 = 2
+[$DIR/dbg-macro-expected-behavior.rs:65] 1u8 = 1
+[$DIR/dbg-macro-expected-behavior.rs:65] 2u32 = 2
+[$DIR/dbg-macro-expected-behavior.rs:65] "Yeah" = "Yeah"
diff --git a/tests/ui/rfc-2361-dbg-macro/dbg-macro-move-semantics.rs b/tests/ui/rfc-2361-dbg-macro/dbg-macro-move-semantics.rs
new file mode 100644
index 000000000..9f3c567b6
--- /dev/null
+++ b/tests/ui/rfc-2361-dbg-macro/dbg-macro-move-semantics.rs
@@ -0,0 +1,10 @@
+// Test ensuring that `dbg!(expr)` will take ownership of the argument.
+
+#[derive(Debug)]
+struct NoCopy(usize);
+
+fn main() {
+ let a = NoCopy(0);
+ let _ = dbg!(a);
+ let _ = dbg!(a); //~ ERROR use of moved value
+}
diff --git a/tests/ui/rfc-2361-dbg-macro/dbg-macro-move-semantics.stderr b/tests/ui/rfc-2361-dbg-macro/dbg-macro-move-semantics.stderr
new file mode 100644
index 000000000..e97fdcce1
--- /dev/null
+++ b/tests/ui/rfc-2361-dbg-macro/dbg-macro-move-semantics.stderr
@@ -0,0 +1,14 @@
+error[E0382]: use of moved value: `a`
+ --> $DIR/dbg-macro-move-semantics.rs:9:18
+ |
+LL | let a = NoCopy(0);
+ | - move occurs because `a` has type `NoCopy`, which does not implement the `Copy` trait
+LL | let _ = dbg!(a);
+ | ------- value moved here
+LL | let _ = dbg!(a);
+ | ^ value used here after move
+ |
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0382`.
diff --git a/tests/ui/rfc-2361-dbg-macro/dbg-macro-requires-debug.rs b/tests/ui/rfc-2361-dbg-macro/dbg-macro-requires-debug.rs
new file mode 100644
index 000000000..f2fb62d76
--- /dev/null
+++ b/tests/ui/rfc-2361-dbg-macro/dbg-macro-requires-debug.rs
@@ -0,0 +1,7 @@
+// Test ensuring that `dbg!(expr)` requires the passed type to implement `Debug`.
+
+struct NotDebug;
+
+fn main() {
+ let _: NotDebug = dbg!(NotDebug); //~ ERROR `NotDebug` doesn't implement `Debug`
+}
diff --git a/tests/ui/rfc-2361-dbg-macro/dbg-macro-requires-debug.stderr b/tests/ui/rfc-2361-dbg-macro/dbg-macro-requires-debug.stderr
new file mode 100644
index 000000000..d8b5a9e63
--- /dev/null
+++ b/tests/ui/rfc-2361-dbg-macro/dbg-macro-requires-debug.stderr
@@ -0,0 +1,17 @@
+error[E0277]: `NotDebug` doesn't implement `Debug`
+ --> $DIR/dbg-macro-requires-debug.rs:6:23
+ |
+LL | let _: NotDebug = dbg!(NotDebug);
+ | ^^^^^^^^^^^^^^ `NotDebug` cannot be formatted using `{:?}`
+ |
+ = help: the trait `Debug` is not implemented for `NotDebug`
+ = note: add `#[derive(Debug)]` to `NotDebug` or manually `impl Debug for NotDebug`
+ = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
+help: consider annotating `NotDebug` with `#[derive(Debug)]`
+ |
+LL | #[derive(Debug)]
+ |
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0277`.