@@ -350,76 +350,37 @@ macro_rules! eprintln {
350350/// [`debug!`]: https://docs.rs/log/*/log/macro.debug.html
351351/// [`log`]: https://crates.io/crates/log
352352#[ macro_export]
353- #[ allow_internal_unstable( std_internals) ]
354353#[ cfg_attr( not( test) , rustc_diagnostic_item = "dbg_macro" ) ]
355354#[ stable( feature = "dbg_macro" , since = "1.32.0" ) ]
356355macro_rules! dbg {
356+ // NOTE: We cannot use `concat!` to make a static string as a format argument
357+ // of `eprintln!` because `file!` could contain a `{` or
358+ // `$val` expression could be a block (`{ .. }`), in which case the `eprintln!`
359+ // will be malformed.
357360 ( ) => {
358361 $crate:: eprintln!( "[{}:{}:{}]" , $crate:: file!( ) , $crate:: line!( ) , $crate:: column!( ) )
359362 } ;
360- ( $( $val: expr) ,+ $( , ) ?) => {
361- $crate:: macros:: dbg_internal!( ( ) ( ) ( $( $val) ,+) )
362- } ;
363- }
364-
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.
368- ///
369- /// E.g. `dbg_internal!(() () (1, 2))` expands into
370- /// ```rust, ignore
371- /// match (1, 2) {
372- /// args => {
373- /// let (tmp_1, tmp_2) = args;
374- /// eprint!("...", &tmp_1, &tmp_2, /* some other arguments */);
375- /// (tmp_1, tmp_2)
376- /// }
377- /// }
378- /// ```
379- ///
380- /// This is necessary so that `dbg!` outputs don't get torn, see #136703.
381- #[ doc( hidden) ]
382- #[ rustc_macro_transparency = "semiopaque" ]
383- pub macro dbg_internal {
384- ( ( $( $piece: literal) , +) ( $( $processed: expr => $bound: ident) , +) ( ) ) => {
363+ ( $val: expr $( , ) ?) => {
385364 // Use of `match` here is intentional because it affects the lifetimes
386365 // of temporaries - https://stackoverflow.com/a/48732525/1063961
387- // Always put the arguments in a tuple to avoid an unused parens lint on the pattern.
388- match ( $( $processed, ) +) {
389- // Move the entire tuple so it doesn't stick around as a temporary (#154988).
390- args => {
391- let ( $( $bound, ) +) = args;
392- $crate:: eprint!(
393- $crate :: concat!( $( $piece) , +) ,
394- $(
395- $crate :: stringify!( $processed) ,
396- // The `&T: Debug` check happens here (not in the format literal desugaring)
397- // to avoid format literal related messages and suggestions.
398- &&$bound as & dyn $crate :: fmt:: Debug
399- ) , +,
400- // The location returned here is that of the macro invocation, so
401- // it will be the same for all expressions. Thus, label these
402- // arguments so that they can be reused in every piece of the
403- // formatting template.
404- file=$crate :: file!( ) ,
405- line=$crate :: line!( ) ,
406- column=$crate :: column!( )
366+ match $val {
367+ tmp => {
368+ $crate:: eprintln!( "[{}:{}:{}] {} = {:#?}" ,
369+ $crate:: file!( ) ,
370+ $crate:: line!( ) ,
371+ $crate:: column!( ) ,
372+ $crate:: stringify!( $val) ,
373+ // The `&T: Debug` check happens here (not in the format literal desugaring)
374+ // to avoid format literal related messages and suggestions.
375+ &&tmp as & dyn $crate:: fmt:: Debug ,
407376 ) ;
408- // Comma separate the variables only when necessary so that this will
409- // not yield a tuple for a single expression, but rather just parenthesize
410- // the expression.
411- ( $( $bound) , +)
412-
377+ tmp
413378 }
414379 }
415- } ,
416- ( ( $( $piece: literal) , * ) ( $( $processed: expr => $bound: ident) , * ) ( $val: expr $( , $rest: expr) * ) ) => {
417- $crate:: macros:: dbg_internal!(
418- ( $( $piece, ) * "[{file}:{line}:{column}] {} = {:#?}\n " )
419- ( $( $processed => $bound, ) * $val => tmp)
420- ( $( $rest) , * )
421- )
422- } ,
380+ } ;
381+ ( $( $val: expr) ,+ $( , ) ?) => {
382+ ( $( $crate:: dbg!( $val) ) ,+, )
383+ } ;
423384}
424385
425386#[ doc( hidden) ]
0 commit comments