55//! library.
66// ignore-tidy-dbg
77
8+ #[ cfg( test) ]
9+ mod tests;
10+
811#[ doc = include_str ! ( "../../core/src/macros/panic.md" ) ]
912#[ macro_export]
1013#[ rustc_builtin_macro( std_panic) ]
@@ -359,19 +362,16 @@ macro_rules! dbg {
359362 } ;
360363}
361364
362- /// Internal macro that processes a list of expressions and produces a chain of
363- /// nested `match`es, one for each expression, before finally calling `eprint!`
364- /// with the collected information and returning all the evaluated expressions
365- /// in a tuple.
365+ /// Internal macro that processes a list of expressions, binds their results
366+ /// with `match`, calls `eprint!` with the collected information, and returns
367+ /// all the evaluated expressions in a tuple.
366368///
367369/// E.g. `dbg_internal!(() () (1, 2))` expands into
368370/// ```rust, ignore
369- /// match 1 {
370- /// tmp_1 => match 2 {
371- /// tmp_2 => {
372- /// eprint!("...", &tmp_1, &tmp_2, /* some other arguments */);
373- /// (tmp_1, tmp_2)
374- /// }
371+ /// match (1, 2) {
372+ /// (tmp_1, tmp_2) => {
373+ /// eprint!("...", &tmp_1, &tmp_2, /* some other arguments */);
374+ /// (tmp_1, tmp_2)
375375/// }
376376/// }
377377/// ```
@@ -380,37 +380,41 @@ macro_rules! dbg {
380380#[ doc( hidden) ]
381381#[ rustc_macro_transparency = "semiopaque" ]
382382pub macro dbg_internal {
383- ( ( $( $piece: literal) , +) ( $( $processed: expr => $bound: expr) , +) ( ) ) => { {
384- $crate:: eprint!(
385- $crate :: concat!( $( $piece) , +) ,
386- $(
387- $crate :: stringify!( $processed) ,
388- // The `&T: Debug` check happens here (not in the format literal desugaring)
389- // to avoid format literal related messages and suggestions.
390- &&$bound as & dyn $crate :: fmt:: Debug
391- ) , +,
392- // The location returned here is that of the macro invocation, so
393- // it will be the same for all expressions. Thus, label these
394- // arguments so that they can be reused in every piece of the
395- // formatting template.
396- file=$crate :: file!( ) ,
397- line=$crate :: line!( ) ,
398- column=$crate :: column!( )
399- ) ;
400- // Comma separate the variables only when necessary so that this will
401- // not yield a tuple for a single expression, but rather just parenthesize
402- // the expression.
403- ( $( $bound) , +)
404- } } ,
405- ( ( $( $piece: literal) , * ) ( $( $processed: expr => $bound: expr) , * ) ( $val: expr $( , $rest: expr) * ) ) => {
383+ ( ( $( $piece: literal) , +) ( $( $processed: expr => $bound: ident) , +) ( ) ) => {
406384 // Use of `match` here is intentional because it affects the lifetimes
407385 // of temporaries - https://stackoverflow.com/a/48732525/1063961
408- match $val {
409- tmp => $crate:: macros:: dbg_internal!(
410- ( $( $piece, ) * "[{file}:{line}:{column}] {} = {:#?}\n " )
411- ( $( $processed => $bound, ) * $val => tmp)
412- ( $( $rest) , * )
413- ) ,
386+ // Always put the arguments in a tuple to avoid an unused parens lint on the pattern.
387+ match ( $( $processed, ) +) {
388+ ( $( $bound, ) +) => {
389+ $crate:: eprint!(
390+ $crate :: concat!( $( $piece) , +) ,
391+ $(
392+ $crate :: stringify!( $processed) ,
393+ // The `&T: Debug` check happens here (not in the format literal desugaring)
394+ // to avoid format literal related messages and suggestions.
395+ &&$bound as & dyn $crate :: fmt:: Debug
396+ ) , +,
397+ // The location returned here is that of the macro invocation, so
398+ // it will be the same for all expressions. Thus, label these
399+ // arguments so that they can be reused in every piece of the
400+ // formatting template.
401+ file=$crate :: file!( ) ,
402+ line=$crate :: line!( ) ,
403+ column=$crate :: column!( )
404+ ) ;
405+ // Comma separate the variables only when necessary so that this will
406+ // not yield a tuple for a single expression, but rather just parenthesize
407+ // the expression.
408+ ( $( $bound) , +)
409+
410+ }
414411 }
415412 } ,
413+ ( ( $( $piece: literal) , * ) ( $( $processed: expr => $bound: ident) , * ) ( $val: expr $( , $rest: expr) * ) ) => {
414+ $crate:: macros:: dbg_internal!(
415+ ( $( $piece, ) * "[{file}:{line}:{column}] {} = {:#?}\n " )
416+ ( $( $processed => $bound, ) * $val => tmp)
417+ ( $( $rest) , * )
418+ )
419+ } ,
416420}
0 commit comments