Add EnteredTraceSpan::or_if_tracing_disabled

This commit is contained in:
Stypox 2025-07-31 00:12:48 +02:00
parent 188f7367bf
commit 88c9a256a9
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
3 changed files with 39 additions and 10 deletions

View File

@ -19,6 +19,7 @@ use super::{
Projectable, Provenance, ReturnAction, ReturnContinuation, Scalar, StackPopInfo, interp_ok,
throw_ub, throw_ub_custom, throw_unsup_format,
};
use crate::interpret::EnteredTraceSpan;
use crate::{enter_trace_span, fluent_generated as fluent};
/// An argument passed to a function.
@ -527,8 +528,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
unwind: mir::UnwindAction,
) -> InterpResult<'tcx> {
let _span =
enter_trace_span!(M, step::init_fn_call, tracing_separate_thread = Empty, ?fn_val);
trace!("init_fn_call: {:#?}", fn_val);
enter_trace_span!(M, step::init_fn_call, tracing_separate_thread = Empty, ?fn_val)
.or_if_tracing_disabled(|| trace!("init_fn_call: {:#?}", fn_val));
let instance = match fn_val {
FnVal::Instance(instance) => instance,

View File

@ -16,6 +16,7 @@ use super::{
FnArg, FnVal, ImmTy, Immediate, InterpCx, InterpResult, Machine, MemPlaceMeta, PlaceTy,
Projectable, Scalar, interp_ok, throw_ub, throw_unsup_format,
};
use crate::interpret::EnteredTraceSpan;
use crate::{enter_trace_span, util};
struct EvaluatedCalleeAndArgs<'tcx, M: Machine<'tcx>> {
@ -81,8 +82,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
stmt = ?stmt.kind,
span = ?stmt.source_info.span,
tracing_separate_thread = Empty,
);
info!(stmt = ?stmt.kind);
)
.or_if_tracing_disabled(|| info!(stmt = ?stmt.kind));
use rustc_middle::mir::StatementKind::*;
@ -470,8 +471,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
terminator = ?terminator.kind,
span = ?terminator.source_info.span,
tracing_separate_thread = Empty,
);
info!(terminator = ?terminator.kind);
)
.or_if_tracing_disabled(|| info!(terminator = ?terminator.kind));
use rustc_middle::mir::TerminatorKind::*;
match terminator.kind {

View File

@ -48,10 +48,24 @@ pub(crate) fn create_static_alloc<'tcx>(
/// A marker trait returned by [crate::interpret::Machine::enter_trace_span], identifying either a
/// real [tracing::span::EnteredSpan] in case tracing is enabled, or the dummy type `()` when
/// tracing is disabled.
pub trait EnteredTraceSpan {}
impl EnteredTraceSpan for () {}
impl EnteredTraceSpan for tracing::span::EnteredSpan {}
/// tracing is disabled. Also see [crate::enter_trace_span!] below.
pub trait EnteredTraceSpan {
/// Allows executing an alternative function when tracing is disabled. Useful for example if you
/// want to open a trace span when tracing is enabled, and alternatively just log a line when
/// tracing is disabled.
fn or_if_tracing_disabled(self, f: impl FnOnce()) -> Self;
}
impl EnteredTraceSpan for () {
fn or_if_tracing_disabled(self, f: impl FnOnce()) -> Self {
f(); // tracing is disabled, execute the function
self
}
}
impl EnteredTraceSpan for tracing::span::EnteredSpan {
fn or_if_tracing_disabled(self, _f: impl FnOnce()) -> Self {
self // tracing is enabled, don't execute anything
}
}
/// Shortand for calling [crate::interpret::Machine::enter_trace_span] on a [tracing::info_span!].
/// 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 {}
/// # type M = rustc_const_eval::const_eval::CompileTimeMachine<'static>;
/// let _span = enter_trace_span!(M, step::eval_statement, tracing_separate_thread = tracing::field::Empty);
/// ```
///
/// ### Executing something else when tracing is disabled
///
/// [crate::interpret::Machine::enter_trace_span] returns [EnteredTraceSpan], on which you can call
/// [EnteredTraceSpan::or_if_tracing_disabled], to e.g. log a line as an alternative to the tracing
/// span for when tracing is disabled. For example:
/// ```rust
/// # use rustc_const_eval::enter_trace_span;
/// # use rustc_const_eval::interpret::EnteredTraceSpan;
/// # type M = rustc_const_eval::const_eval::CompileTimeMachine<'static>;
/// let _span = enter_trace_span!(M, step::eval_statement)
/// .or_if_tracing_disabled(|| tracing::info!("eval_statement"));
/// ```
#[macro_export]
macro_rules! enter_trace_span {
($machine:ty, $name:ident :: $subname:ident $($tt:tt)*) => {