diff --git a/tracing-attributes/src/lib.rs b/tracing-attributes/src/lib.rs index 86e510197b..e3a706c21a 100644 --- a/tracing-attributes/src/lib.rs +++ b/tracing-attributes/src/lib.rs @@ -731,15 +731,16 @@ fn gen_block( }; return quote_spanned!(block.span()=> - if tracing::level_enabled!(#level) { - let __tracing_attr_span = #span; + let __tracing_attr_span = #span; + let __tracing_instrument_future = #mk_fut; + if !__tracing_attr_span.is_disabled() { tracing::Instrument::instrument( - #mk_fut, + __tracing_instrument_future, __tracing_attr_span ) .await } else { - #mk_fut.await + __tracing_instrument_future.await } ); } diff --git a/tracing-attributes/tests/async_fn.rs b/tracing-attributes/tests/async_fn.rs index 6b141771b9..6830cf6239 100644 --- a/tracing-attributes/tests/async_fn.rs +++ b/tracing-attributes/tests/async_fn.rs @@ -15,6 +15,21 @@ async fn test_async_fn(polls: usize) -> Result<(), ()> { future.await } +// Reproduces a compile error when returning an `impl Trait` from an +// instrumented async fn (see https://github.com/tokio-rs/tracing/issues/1615) +#[instrument] +async fn test_ret_impl_trait(n: i32) -> Result, ()> { + let n = n; + Ok((0..10).filter(move |x| *x < n)) +} + +// Reproduces a compile error when returning an `impl Trait` from an +// instrumented async fn (see https://github.com/tokio-rs/tracing/issues/1615) +#[instrument(err)] +async fn test_ret_impl_trait_err(n: i32) -> Result, &'static str> { + Ok((0..10).filter(move |x| *x < n)) +} + #[instrument] async fn test_async_fn_empty() {}