Skip to content

Commit f221d7c

Browse files
committed
Add EnteredTraceSpan::or_if_tracing_disabled
1 parent 6490878 commit f221d7c

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

compiler/rustc_const_eval/src/interpret/call.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use super::{
1919
Projectable, Provenance, ReturnAction, ReturnContinuation, Scalar, StackPopInfo, interp_ok,
2020
throw_ub, throw_ub_custom, throw_unsup_format,
2121
};
22+
use crate::interpret::EnteredTraceSpan;
2223
use crate::{enter_trace_span, fluent_generated as fluent};
2324

2425
/// An argument passed to a function.
@@ -527,8 +528,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
527528
unwind: mir::UnwindAction,
528529
) -> InterpResult<'tcx> {
529530
let _span =
530-
enter_trace_span!(M, step::init_fn_call, tracing_separate_thread = Empty, ?fn_val);
531-
trace!("init_fn_call: {:#?}", fn_val);
531+
enter_trace_span!(M, step::init_fn_call, tracing_separate_thread = Empty, ?fn_val)
532+
.or_if_tracing_disabled(|| trace!("init_fn_call: {:#?}", fn_val));
532533

533534
let instance = match fn_val {
534535
FnVal::Instance(instance) => instance,

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use super::{
1616
FnArg, FnVal, ImmTy, Immediate, InterpCx, InterpResult, Machine, MemPlaceMeta, PlaceTy,
1717
Projectable, Scalar, interp_ok, throw_ub, throw_unsup_format,
1818
};
19+
use crate::interpret::EnteredTraceSpan;
1920
use crate::{enter_trace_span, util};
2021

2122
struct EvaluatedCalleeAndArgs<'tcx, M: Machine<'tcx>> {
@@ -75,8 +76,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
7576
///
7677
/// This does NOT move the statement counter forward, the caller has to do that!
7778
pub fn eval_statement(&mut self, stmt: &mir::Statement<'tcx>) -> InterpResult<'tcx> {
78-
let _span = enter_trace_span!(M, step::eval_statement, stmt = ?stmt, tracing_separate_thread = Empty);
79-
info!(?stmt);
79+
let _span = enter_trace_span!(M, step::eval_statement, stmt = ?stmt, tracing_separate_thread = Empty)
80+
.or_if_tracing_disabled(|| info!(?stmt));
8081

8182
use rustc_middle::mir::StatementKind::*;
8283

@@ -473,8 +474,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
473474
}
474475

475476
fn eval_terminator(&mut self, terminator: &mir::Terminator<'tcx>) -> InterpResult<'tcx> {
476-
let _span = enter_trace_span!(M, step::eval_terminator, terminator = ?terminator.kind, tracing_separate_thread = Empty);
477-
info!("{:?}", terminator.kind);
477+
let _span = enter_trace_span!(M, step::eval_terminator, terminator = ?terminator.kind, tracing_separate_thread = Empty)
478+
.or_if_tracing_disabled(|| info!("{:?}", terminator.kind));
478479

479480
use rustc_middle::mir::TerminatorKind::*;
480481
match terminator.kind {

compiler/rustc_const_eval/src/interpret/util.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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 [crate::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]
116143
macro_rules! enter_trace_span {
117144
($machine:ident, $name:ident :: $subname:ident $($tt:tt)*) => {{

0 commit comments

Comments
 (0)