Skip to content

Commit b641fd3

Browse files
committed
extend ui test
1 parent 08a4628 commit b641fd3

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/libstd/macros.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,15 @@ macro_rules! eprintln {
321321
/// assert_eq!(dbg!(1usize, 2u32), (1, 2));
322322
/// ```
323323
///
324+
/// However, a single argument with a trailing comma will still not be treated
325+
/// as a tuple, following the convention of ignoring trailing commas in macro
326+
/// invocations. You can use a 1-tuple directly if you need one:
327+
///
328+
/// ```
329+
/// assert_eq!(1, dbg!(1u32,)); // trailing comma ignored
330+
/// assert_eq!((1,), dbg!((1u32,))); // 1-tuple
331+
/// ```
332+
///
324333
/// [stderr]: https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)
325334
/// [`debug!`]: https://docs.rs/log/*/log/macro.debug.html
326335
/// [`log`]: https://crates.io/crates/log
@@ -341,9 +350,11 @@ macro_rules! dbg {
341350
}
342351
}
343352
};
353+
// Trailing comma with single argument is ignored
354+
($val:expr,) => { dbg!($val) };
344355
($($val:expr),+ $(,)?) => {
345356
($(dbg!($val)),+,)
346-
}
357+
};
347358
}
348359

349360
/// Awaits the completion of an async call.

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

+22
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ fn test() {
5454
7331
5555
}));
5656
assert_eq!(foo, 42);
57+
58+
// Test trailing comma:
59+
assert_eq!(("Yeah",), dbg!(("Yeah",)));
60+
61+
// Test multiple arguments:
62+
assert_eq!((1u8, 2u32), dbg!(1,
63+
2));
64+
65+
// Test multiple arguments + trailing comma:
66+
assert_eq!((1u8, 2u32, "Yeah"), dbg!(1u8, 2u32,
67+
"Yeah",));
5768
}
5869

5970
fn validate_stderr(stderr: Vec<String>) {
@@ -85,6 +96,17 @@ fn validate_stderr(stderr: Vec<String>) {
8596

8697
"before",
8798
":51] { foo += 1; eprintln!(\"before\"); 7331 } = 7331",
99+
100+
":59] (\"Yeah\",) = (",
101+
" \"Yeah\",",
102+
")",
103+
104+
":62] 1 = 1",
105+
":62] 2 = 2",
106+
107+
":66] 1u8 = 1",
108+
":66] 2u32 = 2",
109+
":66] \"Yeah\" = \"Yeah\"",
88110
]);
89111
}
90112

0 commit comments

Comments
 (0)