From 8816a9ac1fbb2c35128ab2e1e3fd1032178bacc3 Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Tue, 9 Apr 2019 23:00:23 +0200 Subject: [PATCH 1/3] allow multiple args to `dbg!(..)` --- src/libstd/macros.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index 14b266a43441d..c1cd6b8429b5c 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -314,6 +314,13 @@ macro_rules! eprintln { /// You can also use `dbg!()` without a value to just print the /// file and line whenever it's reached. /// +/// Finally, if you want to `dbg!(..)` multiple values, it will treat them as +/// a tuple (and return it, too): +/// +/// ``` +/// assert_eq!(dbg!(1usize, 2u32), (1, 2)); +/// ``` +/// /// [stderr]: https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr) /// [`debug!`]: https://docs.rs/log/*/log/macro.debug.html /// [`log`]: https://crates.io/crates/log @@ -333,6 +340,9 @@ macro_rules! dbg { tmp } } + }; + ($val:expr, $($more:expr),+) => { + dbg!(($val, $($more),*)) } } From 08a4628bf6691067f28f387a2141265676c11d38 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Wed, 10 Apr 2019 05:47:44 +0200 Subject: [PATCH 2/3] Apply suggestions from code review Use dbg! recursively on each item Co-Authored-By: llogiq --- src/libstd/macros.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index c1cd6b8429b5c..e245a048955f6 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -341,8 +341,8 @@ macro_rules! dbg { } } }; - ($val:expr, $($more:expr),+) => { - dbg!(($val, $($more),*)) + ($($val:expr),+ $(,)?) => { + ($(dbg!($val)),+,) } } From b641fd374e82fc8e3cf6b876fa57270f2de39b32 Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Wed, 10 Apr 2019 06:00:35 +0200 Subject: [PATCH 3/3] extend ui test --- src/libstd/macros.rs | 13 ++++++++++- .../dbg-macro-expected-behavior.rs | 22 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index e245a048955f6..9eba76cc04a64 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -321,6 +321,15 @@ macro_rules! eprintln { /// assert_eq!(dbg!(1usize, 2u32), (1, 2)); /// ``` /// +/// However, a single argument with a trailing comma will still not be treated +/// as a tuple, following the convention of ignoring trailing commas in macro +/// invocations. You can use a 1-tuple directly if you need one: +/// +/// ``` +/// assert_eq!(1, dbg!(1u32,)); // trailing comma ignored +/// assert_eq!((1,), dbg!((1u32,))); // 1-tuple +/// ``` +/// /// [stderr]: https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr) /// [`debug!`]: https://docs.rs/log/*/log/macro.debug.html /// [`log`]: https://crates.io/crates/log @@ -341,9 +350,11 @@ macro_rules! dbg { } } }; + // Trailing comma with single argument is ignored + ($val:expr,) => { dbg!($val) }; ($($val:expr),+ $(,)?) => { ($(dbg!($val)),+,) - } + }; } /// Awaits the completion of an async call. diff --git a/src/test/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.rs b/src/test/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.rs index f1b73ee115db1..e2a7ac349ecd5 100644 --- a/src/test/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.rs +++ b/src/test/ui/rfc-2361-dbg-macro/dbg-macro-expected-behavior.rs @@ -54,6 +54,17 @@ fn test() { 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",)); } fn validate_stderr(stderr: Vec) { @@ -85,6 +96,17 @@ fn validate_stderr(stderr: Vec) { "before", ":51] { foo += 1; eprintln!(\"before\"); 7331 } = 7331", + + ":59] (\"Yeah\",) = (", + " \"Yeah\",", + ")", + + ":62] 1 = 1", + ":62] 2 = 2", + + ":66] 1u8 = 1", + ":66] 2u32 = 2", + ":66] \"Yeah\" = \"Yeah\"", ]); }