Skip to content

Commit e1e3431

Browse files
authored
attributes: fix #[instrument(err)] with impl Trait return types (#1236)
This backports PR #1233 to v0.1.x. This isn't *just* a simple cherry-pick because the new tests in that branch use the v0.2.x module names, so that had to be fixed. Otherwise, though, it's the same change, and I'll go ahead and merge it when CI passes, since it was approved on `master`. ## Motivation Currently, using `#[instrument(err)]` on a function returning a `Result` with an `impl Trait` in it results in a compiler error. This is because we generate a type annotation on the closure in the function body that contains the user function's actual body, and `impl Trait` isn't allowed on types in local bindings, only on function parameters and return types. ## Solution This branch fixes the issue by simply removing the return type annotation from the closure. I've also added tests that break on master for functions returning `impl Trait`, both with and without the `err` argument. Fixes #1227 Signed-off-by: Eliza Weisman <[email protected]>
1 parent 4609f22 commit e1e3431

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

tracing-attributes/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,8 @@ fn gen_body(
654654
quote_spanned!(block.span()=>
655655
let __tracing_attr_span = #span;
656656
let __tracing_attr_guard = __tracing_attr_span.enter();
657-
match move || #return_type #block () {
657+
#[allow(clippy::redundant_closure_call)]
658+
match (move || #block)() {
658659
#[allow(clippy::unit_arg)]
659660
Ok(x) => Ok(x),
660661
Err(e) => {

tracing-attributes/tests/err.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,34 @@ fn test_mut_async() {
116116
});
117117
handle.assert_finished();
118118
}
119+
120+
#[test]
121+
fn impl_trait_return_type() {
122+
// Reproduces https://github.com/tokio-rs/tracing/issues/1227
123+
124+
#[instrument(err)]
125+
fn returns_impl_trait(x: usize) -> Result<impl Iterator<Item = usize>, String> {
126+
Ok(0..x)
127+
}
128+
129+
let span = span::mock().named("returns_impl_trait");
130+
131+
let (subscriber, handle) = subscriber::mock()
132+
.new_span(
133+
span.clone()
134+
.with_field(field::mock("x").with_value(&format_args!("10")).only()),
135+
)
136+
.enter(span.clone())
137+
.exit(span.clone())
138+
.drop_span(span)
139+
.done()
140+
.run_with_handle();
141+
142+
with_default(subscriber, || {
143+
for _ in returns_impl_trait(10).unwrap() {
144+
// nop
145+
}
146+
});
147+
148+
handle.assert_finished();
149+
}

tracing-attributes/tests/instrument.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,32 @@ fn methods() {
200200

201201
handle.assert_finished();
202202
}
203+
204+
#[test]
205+
fn impl_trait_return_type() {
206+
#[instrument]
207+
fn returns_impl_trait(x: usize) -> impl Iterator<Item = usize> {
208+
0..x
209+
}
210+
211+
let span = span::mock().named("returns_impl_trait");
212+
213+
let (subscriber, handle) = subscriber::mock()
214+
.new_span(
215+
span.clone()
216+
.with_field(field::mock("x").with_value(&format_args!("10")).only()),
217+
)
218+
.enter(span.clone())
219+
.exit(span.clone())
220+
.drop_span(span)
221+
.done()
222+
.run_with_handle();
223+
224+
with_default(subscriber, || {
225+
for _ in returns_impl_trait(10) {
226+
// nop
227+
}
228+
});
229+
230+
handle.assert_finished();
231+
}

0 commit comments

Comments
 (0)