@@ -63,66 +63,59 @@ thread_local! {
63
63
static NO_VISIBLE_PATH : Cell <bool > = const { Cell :: new( false ) } ;
64
64
}
65
65
66
- /// Avoids running any queries during any prints that occur
67
- /// during the closure. This may alter the appearance of some
68
- /// types (e.g. forcing verbose printing for opaque types).
69
- /// This method is used during some queries (e.g. `explicit_item_bounds`
70
- /// for opaque types), to ensure that any debug printing that
71
- /// occurs during the query computation does not end up recursively
72
- /// calling the same query.
73
- pub fn with_no_queries < F : FnOnce ( ) -> R , R > ( f : F ) -> R {
74
- NO_QUERIES . with ( |no_queries| {
75
- let old = no_queries. replace ( true ) ;
76
- let result = f ( ) ;
77
- no_queries. set ( old) ;
78
- result
79
- } )
80
- }
81
-
82
- /// Force us to name impls with just the filename/line number. We
83
- /// normally try to use types. But at some points, notably while printing
84
- /// cycle errors, this can result in extra or suboptimal error output,
85
- /// so this variable disables that check.
86
- pub fn with_forced_impl_filename_line < F : FnOnce ( ) -> R , R > ( f : F ) -> R {
87
- FORCE_IMPL_FILENAME_LINE . with ( |force| {
88
- let old = force. replace ( true ) ;
89
- let result = f ( ) ;
90
- force. set ( old) ;
91
- result
92
- } )
93
- }
66
+ macro_rules! define_helper {
67
+ ( $( $( #[ $a: meta] ) * fn $name: ident( $helper: ident, $tl: ident) ; ) +) => {
68
+ $(
69
+ #[ must_use]
70
+ pub struct $helper( bool ) ;
71
+
72
+ impl $helper {
73
+ pub fn new( ) -> $helper {
74
+ $helper( $tl. with( |c| c. replace( true ) ) )
75
+ }
76
+ }
94
77
95
- /// Adds the `crate::` prefix to paths where appropriate.
96
- pub fn with_crate_prefix < F : FnOnce ( ) -> R , R > ( f : F ) -> R {
97
- SHOULD_PREFIX_WITH_CRATE . with ( |flag| {
98
- let old = flag. replace ( true ) ;
99
- let result = f ( ) ;
100
- flag. set ( old) ;
101
- result
102
- } )
103
- }
78
+ $( #[ $a] ) *
79
+ pub macro $name( $e: expr) {
80
+ {
81
+ let _guard = $helper:: new( ) ;
82
+ $e
83
+ }
84
+ }
104
85
105
- /// Prevent path trimming if it is turned on. Path trimming affects `Display` impl
106
- /// of various rustc types, for example `std::vec::Vec` would be trimmed to `Vec`,
107
- /// if no other `Vec` is found.
108
- pub fn with_no_trimmed_paths < F : FnOnce ( ) -> R , R > ( f : F ) -> R {
109
- NO_TRIMMED_PATH . with ( |flag| {
110
- let old = flag. replace ( true ) ;
111
- let result = f ( ) ;
112
- flag. set ( old) ;
113
- result
114
- } )
86
+ impl Drop for $helper {
87
+ fn drop( & mut self ) {
88
+ $tl. with( |c| c. set( self . 0 ) )
89
+ }
90
+ }
91
+ ) +
92
+ }
115
93
}
116
94
117
- /// Prevent selection of visible paths. `Display` impl of DefId will prefer visible (public) reexports of types as paths.
118
- pub fn with_no_visible_paths < F : FnOnce ( ) -> R , R > ( f : F ) -> R {
119
- NO_VISIBLE_PATH . with ( |flag| {
120
- let old = flag. replace ( true ) ;
121
- let result = f ( ) ;
122
- flag. set ( old) ;
123
- result
124
- } )
125
- }
95
+ define_helper ! (
96
+ /// Avoids running any queries during any prints that occur
97
+ /// during the closure. This may alter the appearance of some
98
+ /// types (e.g. forcing verbose printing for opaque types).
99
+ /// This method is used during some queries (e.g. `explicit_item_bounds`
100
+ /// for opaque types), to ensure that any debug printing that
101
+ /// occurs during the query computation does not end up recursively
102
+ /// calling the same query.
103
+ fn with_no_queries( NoQueriesGuard , NO_QUERIES ) ;
104
+ /// Force us to name impls with just the filename/line number. We
105
+ /// normally try to use types. But at some points, notably while printing
106
+ /// cycle errors, this can result in extra or suboptimal error output,
107
+ /// so this variable disables that check.
108
+ fn with_forced_impl_filename_line( ForcedImplGuard , FORCE_IMPL_FILENAME_LINE ) ;
109
+ /// Adds the `crate::` prefix to paths where appropriate.
110
+ fn with_crate_prefix( CratePrefixGuard , SHOULD_PREFIX_WITH_CRATE ) ;
111
+ /// Prevent path trimming if it is turned on. Path trimming affects `Display` impl
112
+ /// of various rustc types, for example `std::vec::Vec` would be trimmed to `Vec`,
113
+ /// if no other `Vec` is found.
114
+ fn with_no_trimmed_paths( NoTrimmedGuard , NO_TRIMMED_PATH ) ;
115
+ /// Prevent selection of visible paths. `Display` impl of DefId will prefer
116
+ /// visible (public) reexports of types as paths.
117
+ fn with_no_visible_paths( NoVisibleGuard , NO_VISIBLE_PATH ) ;
118
+ ) ;
126
119
127
120
/// The "region highlights" are used to control region printing during
128
121
/// specific error messages. When a "region highlight" is enabled, it
@@ -379,7 +372,7 @@ pub trait PrettyPrinter<'tcx>:
379
372
// in cases where the `extern crate foo` has non-trivial
380
373
// parents, e.g. it's nested in `impl foo::Trait for Bar`
381
374
// (see also issues #55779 and #87932).
382
- self = with_no_visible_paths ( || self . print_def_path ( def_id, & [ ] ) ) ? ;
375
+ self = with_no_visible_paths ! ( self . print_def_path( def_id, & [ ] ) ? ) ;
383
376
384
377
return Ok ( ( self , true ) ) ;
385
378
}
@@ -654,7 +647,7 @@ pub trait PrettyPrinter<'tcx>:
654
647
return Ok ( self ) ;
655
648
}
656
649
657
- return with_no_queries ( || {
650
+ return with_no_queries ! ( {
658
651
let def_key = self . tcx( ) . def_key( def_id) ;
659
652
if let Some ( name) = def_key. disambiguated_data. data. get_opt_name( ) {
660
653
p!( write( "{}" , name) ) ;
0 commit comments