-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #54317 - Centril:feature/dbg_macro, r=SimonSapin
Implement the dbg!(..) macro Implements the `dbg!(..)` macro due to #54306. cc rust-lang/rfcs#2361 r? @alexcrichton
- Loading branch information
Showing
9 changed files
with
332 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/test/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// run-pass | ||
// ignore-cloudabi no processes | ||
// ignore-emscripten no processes | ||
|
||
// Tests ensuring that `dbg!(expr)` has the expected run-time behavior. | ||
// as well as some compile time properties we expect. | ||
|
||
#![feature(dbg_macro)] | ||
|
||
#[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 test() { | ||
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); | ||
|
||
// 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); | ||
} | ||
|
||
fn validate_stderr(stderr: Vec<String>) { | ||
assert_eq!(stderr, &[ | ||
":23] Unit = Unit", | ||
|
||
":24] a = Unit", | ||
|
||
":30] Point{x: 42, y: 24,} = Point {", | ||
" x: 42,", | ||
" y: 24", | ||
"}", | ||
|
||
":31] b = Point {", | ||
" x: 42,", | ||
" y: 24", | ||
"}", | ||
|
||
":40] &a = NoCopy(", | ||
" 1337", | ||
")", | ||
|
||
":40] dbg!(& a) = NoCopy(", | ||
" 1337", | ||
")", | ||
":45] f(&42) = 42", | ||
|
||
"before", | ||
":50] { foo += 1; eprintln!(\"before\"); 7331 } = 7331", | ||
]); | ||
} | ||
|
||
fn main() { | ||
// The following is a hack to deal with compiletest's inability | ||
// to check the output (to stdout) of run-pass tests. | ||
use std::env; | ||
use std::process::Command; | ||
|
||
let mut args = env::args(); | ||
let prog = args.next().unwrap(); | ||
let child = args.next(); | ||
if let Some("child") = child.as_ref().map(|s| &**s) { | ||
// Only run the test if we've been spawned as 'child' | ||
test() | ||
} else { | ||
// This essentially spawns as 'child' to run the tests | ||
// and then it collects output of stderr and checks the output | ||
// against what we expect. | ||
let out = Command::new(&prog).arg("child").output().unwrap(); | ||
assert!(out.status.success()); | ||
assert!(out.stdout.is_empty()); | ||
|
||
let stderr = String::from_utf8(out.stderr).unwrap(); | ||
let stderr = stderr.lines().map(|mut s| { | ||
if s.starts_with("[") { | ||
// Strip `[` and file path: | ||
s = s.trim_start_matches("["); | ||
assert!(s.starts_with(file!())); | ||
s = s.trim_start_matches(file!()); | ||
} | ||
s.to_owned() | ||
}).collect(); | ||
|
||
validate_stderr(stderr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Feature gate test for `dbg!(..)`. | ||
|
||
fn main() { | ||
dbg!(1); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/test/ui/rfc-2361-dbg-macro/dbg-macro-feature-gate.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error[E0658]: macro dbg! is unstable (see issue #54306) | ||
--> $DIR/dbg-macro-feature-gate.rs:4:5 | ||
| | ||
LL | dbg!(1); | ||
| ^^^^^^^^ | ||
| | ||
= help: add #![feature(dbg_macro)] to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
14 changes: 14 additions & 0 deletions
14
src/test/ui/rfc-2361-dbg-macro/dbg-macro-move-semantics.nll.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error[E0382]: use of moved value: `a` | ||
--> $DIR/dbg-macro-move-semantics.rs:11:18 | ||
| | ||
LL | let _ = dbg!(a); | ||
| ------- value moved here | ||
LL | let _ = dbg!(a); | ||
| ^ value used here after move | ||
| | ||
= note: move occurs because `a` has type `NoCopy`, which does not implement the `Copy` trait | ||
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0382`. |
12 changes: 12 additions & 0 deletions
12
src/test/ui/rfc-2361-dbg-macro/dbg-macro-move-semantics.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Test ensuring that `dbg!(expr)` will take ownership of the argument. | ||
|
||
#![feature(dbg_macro)] | ||
|
||
#[derive(Debug)] | ||
struct NoCopy(usize); | ||
|
||
fn main() { | ||
let a = NoCopy(0); | ||
let _ = dbg!(a); | ||
let _ = dbg!(a); | ||
} |
25 changes: 25 additions & 0 deletions
25
src/test/ui/rfc-2361-dbg-macro/dbg-macro-move-semantics.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
error[E0382]: use of moved value: `a` | ||
--> $DIR/dbg-macro-move-semantics.rs:11:18 | ||
| | ||
LL | let _ = dbg!(a); | ||
| ------- value moved here | ||
LL | let _ = dbg!(a); | ||
| ^ value used here after move | ||
| | ||
= note: move occurs because `a` has type `NoCopy`, which does not implement the `Copy` trait | ||
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) | ||
|
||
error[E0382]: use of moved value: `a` | ||
--> $DIR/dbg-macro-move-semantics.rs:11:13 | ||
| | ||
LL | let _ = dbg!(a); | ||
| ------- value moved here | ||
LL | let _ = dbg!(a); | ||
| ^^^^^^^ value used here after move | ||
| | ||
= note: move occurs because `a` has type `NoCopy`, which does not implement the `Copy` trait | ||
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0382`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Test ensuring that `dbg!(expr)` requires the passed type to implement `Debug`. | ||
|
||
#![feature(dbg_macro)] | ||
|
||
struct NotDebug; | ||
|
||
fn main() { | ||
let _: NotDebug = dbg!(NotDebug); | ||
} |
15 changes: 15 additions & 0 deletions
15
src/test/ui/rfc-2361-dbg-macro/dbg-macro-requires-debug.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error[E0277]: `NotDebug` doesn't implement `std::fmt::Debug` | ||
--> $DIR/dbg-macro-requires-debug.rs:8:23 | ||
| | ||
LL | let _: NotDebug = dbg!(NotDebug); | ||
| ^^^^^^^^^^^^^^ `NotDebug` cannot be formatted using `{:?}` | ||
| | ||
= help: the trait `std::fmt::Debug` is not implemented for `NotDebug` | ||
= note: add `#[derive(Debug)]` or manually implement `std::fmt::Debug` | ||
= note: required because of the requirements on the impl of `std::fmt::Debug` for `&NotDebug` | ||
= note: required by `std::fmt::Debug::fmt` | ||
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |