Auto merge of #54317 - Centril:feature/dbg_macro, r=SimonSapin

Implement the dbg!(..) macro

Implements the `dbg!(..)` macro due to #54306.
cc https://github.com/rust-lang/rfcs/pull/2361

r? @alexcrichton
This commit is contained in:
bors 2018-09-25 07:06:15 +00:00
commit e7416d5650
9 changed files with 332 additions and 0 deletions

View file

@ -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);
}
}

View file

@ -0,0 +1,5 @@
// Feature gate test for `dbg!(..)`.
fn main() {
dbg!(1);
}

View file

@ -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`.

View file

@ -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`.

View file

@ -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);
}

View file

@ -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`.

View file

@ -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);
}

View file

@ -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`.