-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Implement the dbg!(..) macro #54317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Implement the dbg!(..) macro #54317
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
000be8f
dbg!(expr) implementation.
Centril cbb81bd
dbg_macro: feature gate + move semantics test.
Centril 6d08821
dbg_macro: Debug-required test.
Centril a9d2f38
dbg_macro: output tests.
Centril 1d2a1bf
dbg_macro: notes about VCS and log::debug!(..)
Centril 924a693
debug_macro: --bless tests.
Centril b4d6d78
dbg_macro: // ignore-wasm
Centril 3006d4e
dbg_macro: more things...
Centril 5184dcc
dbg_macro: only ignore cloudabi and emscripten
Centril e5b9331
dbg_macro: fix line numbers
Centril File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is a Stack Overflow the best kind of link for this documentation? may be worth "archiving" as a blog post somewhere on the forge for example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can always update this to something better later -- I'd welcome a follow up PR if you have ideas :)
I think it is sufficient for now.