Skip to content

Commit 6e59a13

Browse files
authored
attributes: fix tracing::instrument regression around shadowing (#3311)
The change to allow the `dead_code` lint to work on the `#[instrument]` attribute (#3108) introduced a shadowing problem which caused compilation failure in certain cases, as reported in #3306. The body of the original function was not given its own scope and `use` statements made there could leak out into the code inserted by the `#[instrument]`. This change fixes this problem by explicitly re-introducing the parenthesis to give the original function body its own scope. Closes #3306
1 parent e4df761 commit 6e59a13

File tree

9 files changed

+56
-21
lines changed

9 files changed

+56
-21
lines changed

tracing-attributes/src/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub(crate) fn gen_function<'a, B: ToTokens + 'a>(
8787
let block = quote! {
8888
{
8989
#fake_return_edge
90-
#block
90+
{ #block }
9191
}
9292
};
9393

tracing-attributes/tests/ui.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
// Only test on nightly, since UI tests are bound to change over time
1+
// Only test on stable, since UI tests are bound to change over time
2+
23
#[rustversion::stable]
34
#[test]
4-
fn async_instrument() {
5+
fn pass() {
56
let t = trybuild::TestCases::new();
6-
t.compile_fail("tests/ui/async_instrument.rs");
7+
t.pass("tests/ui/pass/*.rs");
78
}
89

910
#[rustversion::stable]
1011
#[test]
11-
fn const_instrument() {
12+
fn compile_fail() {
1213
let t = trybuild::TestCases::new();
13-
t.compile_fail("tests/ui/const_instrument.rs");
14+
t.compile_fail("tests/ui/fail/*.rs");
1415
}

tracing-attributes/tests/ui/async_instrument.stderr renamed to tracing-attributes/tests/ui/fail/async_instrument.stderr

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
error[E0308]: mismatched types
2-
--> tests/ui/async_instrument.rs:5:5
2+
--> tests/ui/fail/async_instrument.rs:5:5
33
|
44
5 | ""
55
| ^^ expected `()`, found `&str`
66
|
77
note: return type inferred to be `()` here
8-
--> tests/ui/async_instrument.rs:4:10
8+
--> tests/ui/fail/async_instrument.rs:4:10
99
|
1010
4 | async fn unit() {
1111
| ^^^^
1212

1313
error[E0308]: mismatched types
14-
--> tests/ui/async_instrument.rs:10:5
14+
--> tests/ui/fail/async_instrument.rs:10:5
1515
|
1616
10 | ""
1717
| ^^- help: try using a conversion method: `.to_string()`
1818
| |
1919
| expected `String`, found `&str`
2020
|
2121
note: return type inferred to be `String` here
22-
--> tests/ui/async_instrument.rs:9:31
22+
--> tests/ui/fail/async_instrument.rs:9:31
2323
|
2424
9 | async fn simple_mismatch() -> String {
2525
| ^^^^^^
2626

2727
error[E0277]: `(&str,)` doesn't implement `std::fmt::Display`
28-
--> tests/ui/async_instrument.rs:14:57
28+
--> tests/ui/fail/async_instrument.rs:14:57
2929
|
3030
14 | async fn opaque_unsatisfied() -> impl std::fmt::Display {
3131
| _________________________________________________________-
@@ -40,7 +40,7 @@ error[E0277]: `(&str,)` doesn't implement `std::fmt::Display`
4040
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
4141

4242
error[E0277]: `(&str,)` doesn't implement `std::fmt::Display`
43-
--> tests/ui/async_instrument.rs:14:34
43+
--> tests/ui/fail/async_instrument.rs:14:34
4444
|
4545
14 | async fn opaque_unsatisfied() -> impl std::fmt::Display {
4646
| ^^^^^^^^^^^^^^^^^^^^^^ `(&str,)` cannot be formatted with the default formatter
@@ -49,15 +49,15 @@ error[E0277]: `(&str,)` doesn't implement `std::fmt::Display`
4949
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
5050

5151
error[E0308]: mismatched types
52-
--> tests/ui/async_instrument.rs:22:5
52+
--> tests/ui/fail/async_instrument.rs:22:5
5353
|
5454
22 | ""
5555
| ^^ expected `Wrapper<_>`, found `&str`
5656
|
5757
= note: expected struct `Wrapper<_>`
5858
found reference `&'static str`
5959
note: return type inferred to be `Wrapper<_>` here
60-
--> tests/ui/async_instrument.rs:21:36
60+
--> tests/ui/fail/async_instrument.rs:21:36
6161
|
6262
21 | async fn mismatch_with_opaque() -> Wrapper<impl std::fmt::Display> {
6363
| ^^^^^^^
@@ -67,33 +67,33 @@ help: try wrapping the expression in `Wrapper`
6767
| ++++++++ +
6868

6969
error[E0308]: mismatched types
70-
--> tests/ui/async_instrument.rs:28:16
70+
--> tests/ui/fail/async_instrument.rs:28:16
7171
|
7272
28 | return "";
7373
| ^^ expected `()`, found `&str`
7474
|
7575
note: return type inferred to be `()` here
76-
--> tests/ui/async_instrument.rs:26:10
76+
--> tests/ui/fail/async_instrument.rs:26:10
7777
|
7878
26 | async fn early_return_unit() {
7979
| ^^^^^^^^^^^^^^^^^
8080

8181
error[E0308]: mismatched types
82-
--> tests/ui/async_instrument.rs:35:16
82+
--> tests/ui/fail/async_instrument.rs:35:16
8383
|
8484
35 | return "";
8585
| ^^- help: try using a conversion method: `.to_string()`
8686
| |
8787
| expected `String`, found `&str`
8888
|
8989
note: return type inferred to be `String` here
90-
--> tests/ui/async_instrument.rs:33:28
90+
--> tests/ui/fail/async_instrument.rs:33:28
9191
|
9292
33 | async fn early_return() -> String {
9393
| ^^^^^^
9494

9595
error[E0308]: mismatched types
96-
--> tests/ui/async_instrument.rs:40:1
96+
--> tests/ui/fail/async_instrument.rs:40:1
9797
|
9898
40 | #[tracing::instrument]
9999
| ^^^^^^^^^^^^^^^^^^^^^^ expected `i32`, found `()`

tracing-attributes/tests/ui/const_instrument.stderr renamed to tracing-attributes/tests/ui/fail/const_instrument.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: macros that expand to items must be delimited with braces or followed by a semicolon
2-
--> tests/ui/const_instrument.rs:3:1
2+
--> tests/ui/fail/const_instrument.rs:3:1
33
|
44
3 | #[tracing::instrument]
55
| ^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: this error originates in the attribute macro `tracing::instrument` (in Nightly builds, run with -Z macro-backtrace for more info)
88

99
error: the `#[instrument]` attribute may not be used with `const fn`s
10-
--> tests/ui/const_instrument.rs:3:1
10+
--> tests/ui/fail/const_instrument.rs:3:1
1111
|
1212
3 | #[tracing::instrument]
1313
| ^^^^^^^^^^^^^^^^^^^^^^
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![deny(dead_code)]
2+
3+
#[tracing::instrument]
4+
fn never_used() {}
5+
6+
fn main() {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: function `never_used` is never used
2+
--> tests/ui/fail/unused_instrumented_fn.rs:4:4
3+
|
4+
4 | fn never_used() {}
5+
| ^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> tests/ui/fail/unused_instrumented_fn.rs:1:9
9+
|
10+
1 | #![deny(dead_code)]
11+
| ^^^^^^^^^
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//! This program is a regression test for [#3306], where shadowing
2+
//! caused compilation failure in certain cases due to the original
3+
//! function body not getting its own scope.
4+
//!
5+
//! [#3306]: https://github.com/tokio-rs/tracing/issues/3306
6+
type Foo = ();
7+
enum Bar {
8+
Foo,
9+
}
10+
11+
#[tracing::instrument]
12+
fn this_is_fine() -> Foo {
13+
// glob import imports Bar::Foo, shadowing Foo
14+
use Bar::*;
15+
}
16+
17+
fn main() {}

0 commit comments

Comments
 (0)