Skip to content

Commit 1dce36e

Browse files
authored
Rollup merge of rust-lang#57847 - clarcharr:dbg_no_params, r=Centril
dbg!() without parameters Fixes rust-lang#57845.
2 parents 8ddcc75 + ea9e2c4 commit 1dce36e

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/libstd/macros.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,16 @@ macro_rules! eprintln {
305305
/// let _ = dbg!(a); // <-- `a` is moved again; error!
306306
/// ```
307307
///
308+
/// You can also use `dbg!()` without a value to just print the
309+
/// file and line whenever it's reached.
310+
///
308311
/// [stderr]: https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)
309312
#[macro_export]
310313
#[stable(feature = "dbg_macro", since = "1.32.0")]
311314
macro_rules! dbg {
315+
() => {
316+
eprintln!("[{}:{}]", file!(), line!());
317+
};
312318
($val:expr) => {
313319
// Use of `match` here is intentional because it affects the lifetimes
314320
// of temporaries - https://stackoverflow.com/a/48732525/1063961

src/test/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ fn test() {
3333
// We can move `b` because it's Copy.
3434
drop(b);
3535

36+
// Without parameters works as expected.
37+
let _: () = dbg!();
38+
3639
// Test that we can borrow and that successive applications is still identity.
3740
let a = NoCopy(1337);
3841
let b: &NoCopy = dbg!(dbg!(&a));
@@ -69,17 +72,19 @@ fn validate_stderr(stderr: Vec<String>) {
6972
" y: 24",
7073
"}",
7174

72-
":38] &a = NoCopy(",
75+
":37]",
76+
77+
":41] &a = NoCopy(",
7378
" 1337",
7479
")",
7580

76-
":38] dbg!(& a) = NoCopy(",
81+
":41] dbg!(& a) = NoCopy(",
7782
" 1337",
7883
")",
79-
":43] f(&42) = 42",
84+
":46] f(&42) = 42",
8085

8186
"before",
82-
":48] { foo += 1; eprintln!(\"before\"); 7331 } = 7331",
87+
":51] { foo += 1; eprintln!(\"before\"); 7331 } = 7331",
8388
]);
8489
}
8590

0 commit comments

Comments
 (0)