@@ -48,10 +48,24 @@ pub(crate) fn create_static_alloc<'tcx>(
4848
4949/// A marker trait returned by [crate::interpret::Machine::enter_trace_span], identifying either a
5050/// real [tracing::span::EnteredSpan] in case tracing is enabled, or the dummy type `()` when
51- /// tracing is disabled.
52- pub trait EnteredTraceSpan { }
53- impl EnteredTraceSpan for ( ) { }
54- impl EnteredTraceSpan for tracing:: span:: EnteredSpan { }
51+ /// tracing is disabled. Also see [enter_trace_span!] below.
52+ pub trait EnteredTraceSpan {
53+ /// Allows executing an alternative function when tracing is disabled. Useful for example if you
54+ /// want to open a trace span when tracing is enabled, and alternatively just log a line when
55+ /// tracing is disabled.
56+ fn or_if_tracing_disabled ( self , f : impl FnOnce ( ) ) -> Self ;
57+ }
58+ impl EnteredTraceSpan for ( ) {
59+ fn or_if_tracing_disabled ( self , f : impl FnOnce ( ) ) -> Self {
60+ f ( ) ; // tracing is disabled, execute the function
61+ self
62+ }
63+ }
64+ impl EnteredTraceSpan for tracing:: span:: EnteredSpan {
65+ fn or_if_tracing_disabled ( self , _f : impl FnOnce ( ) ) -> Self {
66+ self // tracing is enabled, don't execute anything
67+ }
68+ }
5569
5670/// Shortand for calling [crate::interpret::Machine::enter_trace_span] on a [tracing::info_span!].
5771/// This is supposed to be compiled out when [crate::interpret::Machine::enter_trace_span] has the
@@ -112,6 +126,19 @@ impl EnteredTraceSpan for tracing::span::EnteredSpan {}
112126/// # type M = rustc_const_eval::const_eval::CompileTimeMachine<'static>;
113127/// let _span = enter_trace_span!(M, step::eval_statement, tracing_separate_thread = tracing::field::Empty);
114128/// ```
129+ ///
130+ /// ### Executing something else when tracing is disabled
131+ ///
132+ /// [crate::interpret::Machine::enter_trace_span] returns [EnteredTraceSpan], on which you can call
133+ /// [EnteredTraceSpan::or_if_tracing_disabled], to e.g. log a line as an alternative to the tracing
134+ /// span for when tracing is disabled. For example:
135+ /// ```rust
136+ /// # use rustc_const_eval::enter_trace_span;
137+ /// # use rustc_const_eval::interpret::EnteredTraceSpan;
138+ /// # type M = rustc_const_eval::const_eval::CompileTimeMachine<'static>;
139+ /// let _span = enter_trace_span!(M, step::eval_statement)
140+ /// .or_if_tracing_disabled(|| tracing::info!("eval_statement"));
141+ /// ```
115142#[ macro_export]
116143macro_rules! enter_trace_span {
117144 ( $machine: ident, $name: ident :: $subname: ident $( $tt: tt) * ) => { {
0 commit comments