@@ -1431,3 +1431,44 @@ impl ToU64 for usize {
1431
1431
self . try_into ( ) . unwrap ( )
1432
1432
}
1433
1433
}
1434
+
1435
+ /// This struct is needed to enforce `#[must_use]` on values produced by [enter_trace_span] even
1436
+ /// when the "tracing" feature is not enabled.
1437
+ #[ must_use]
1438
+ pub struct MaybeEnteredTraceSpan {
1439
+ #[ cfg( feature = "tracing" ) ]
1440
+ pub _entered_span : tracing:: span:: EnteredSpan ,
1441
+ }
1442
+
1443
+ /// Enters a [tracing::info_span] only if the "tracing" feature is enabled, otherwise does nothing.
1444
+ /// This is like [rustc_const_eval::enter_trace_span] except that it does not depend on the
1445
+ /// [Machine] trait to check if tracing is enabled, because from the Miri codebase we can directly
1446
+ /// check whether the "tracing" feature is enabled, unlike from the rustc_const_eval codebase.
1447
+ ///
1448
+ /// In addition to the syntax accepted by [tracing::span!], this macro optionally allows passing
1449
+ /// the span name (i.e. the first macro argument) in the form `NAME::SUBNAME` (without quotes) to
1450
+ /// indicate that the span has name "NAME" (usually the name of the component) and has an additional
1451
+ /// more specific name "SUBNAME" (usually the function name). The latter is passed to the [tracing]
1452
+ /// infrastructure as a span field with the name "NAME". This allows not being distracted by
1453
+ /// subnames when looking at the trace in <https://ui.perfetto.dev>, but when deeper introspection
1454
+ /// is needed within a component, it's still possible to view the subnames directly in the UI by
1455
+ /// selecting a span, clicking on the "NAME" argument on the right, and clicking on "Visualize
1456
+ /// argument values".
1457
+ /// ```rust
1458
+ /// // for example, the first will expand to the second
1459
+ /// enter_trace_span!(borrow_tracker::on_stack_pop, /* ... */)
1460
+ /// enter_trace_span!("borrow_tracker", borrow_tracker = "on_stack_pop", /* ... */)
1461
+ /// ```
1462
+ #[ macro_export]
1463
+ macro_rules! enter_trace_span {
1464
+ ( $name: ident :: $subname: ident $( $tt: tt) * ) => { {
1465
+ enter_trace_span!( stringify!( $name) , $name = %stringify!( subname) $( $tt) * )
1466
+ } } ;
1467
+
1468
+ ( $( $tt: tt) * ) => {
1469
+ $crate:: MaybeEnteredTraceSpan {
1470
+ #[ cfg( feature = "tracing" ) ]
1471
+ _entered_span: tracing:: info_span!( $( $tt) * ) . entered( )
1472
+ }
1473
+ } ;
1474
+ }
0 commit comments