Skip to content

Commit 9aea0e5

Browse files
Add trybuild test for instrumented async fn type mismatch
1 parent baae90b commit 9aea0e5

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

tracing-attributes/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ tokio-test = "0.4.2"
4545
tracing-core = { path = "../tracing-core", version = "0.2"}
4646
tracing-subscriber = { path = "../tracing-subscriber", version = "0.3", features = ["env-filter"] }
4747
async-trait = "0.1.56"
48+
trybuild = "1.0.64"
49+
rustversion = "1.0.9"
4850

4951
[badges]
5052
maintenance = { status = "experimental" }

tracing-attributes/tests/ui.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Only test on nightly, since UI tests are bound to change over time
2+
#[rustversion::nightly]
3+
#[test]
4+
fn async_instrument() {
5+
let t = trybuild::TestCases::new();
6+
t.compile_fail("tests/ui/async_instrument.rs");
7+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![allow(unreachable_code)]
2+
3+
#[tracing::instrument]
4+
async fn unit() {
5+
""
6+
}
7+
8+
#[tracing::instrument]
9+
async fn simple_mismatch() -> String {
10+
""
11+
}
12+
13+
// FIXME: this span is still pretty poor
14+
#[tracing::instrument]
15+
async fn opaque_unsatisfied() -> impl std::fmt::Display {
16+
("",)
17+
}
18+
19+
struct Wrapper<T>(T);
20+
21+
#[tracing::instrument]
22+
async fn mismatch_with_opaque() -> Wrapper<impl std::fmt::Display> {
23+
""
24+
}
25+
26+
fn main() {
27+
let _ = unit();
28+
let _ = simple_mismatch();
29+
let _ = opaque_unsatisfied();
30+
let _ = mismatch_with_opaque();
31+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
error[E0308]: mismatched types
2+
--> tests/ui/async_instrument.rs:5:5
3+
|
4+
5 | ""
5+
| ^^ expected `()`, found `&str`
6+
7+
error[E0308]: mismatched types
8+
--> tests/ui/async_instrument.rs:10:5
9+
|
10+
10 | ""
11+
| ^^- help: try using a conversion method: `.to_string()`
12+
| |
13+
| expected struct `String`, found `&str`
14+
|
15+
note: return type inferred to be `String` here
16+
--> tests/ui/async_instrument.rs:9:31
17+
|
18+
9 | async fn simple_mismatch() -> String {
19+
| ^^^^^^
20+
21+
error[E0277]: `(&str,)` doesn't implement `std::fmt::Display`
22+
--> tests/ui/async_instrument.rs:14:1
23+
|
24+
14 | #[tracing::instrument]
25+
| ^^^^^^^^^^^^^^^^^^^^^^ `(&str,)` cannot be formatted with the default formatter
26+
|
27+
= help: the trait `std::fmt::Display` is not implemented for `(&str,)`
28+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
29+
= note: this error originates in the attribute macro `tracing::instrument` (in Nightly builds, run with -Z macro-backtrace for more info)
30+
31+
error[E0277]: `(&str,)` doesn't implement `std::fmt::Display`
32+
--> tests/ui/async_instrument.rs:15:34
33+
|
34+
15 | async fn opaque_unsatisfied() -> impl std::fmt::Display {
35+
| ^^^^^^^^^^^^^^^^^^^^^^ `(&str,)` cannot be formatted with the default formatter
36+
|
37+
= help: the trait `std::fmt::Display` is not implemented for `(&str,)`
38+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
39+
40+
error[E0308]: mismatched types
41+
--> tests/ui/async_instrument.rs:23:5
42+
|
43+
23 | ""
44+
| ^^ expected struct `Wrapper`, found `&str`
45+
|
46+
= note: expected struct `Wrapper<_>`
47+
found reference `&'static str`
48+
note: return type inferred to be `Wrapper<_>` here
49+
--> tests/ui/async_instrument.rs:22:36
50+
|
51+
22 | async fn mismatch_with_opaque() -> Wrapper<impl std::fmt::Display> {
52+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
53+
help: try wrapping the expression in `Wrapper`
54+
|
55+
23 | Wrapper("")
56+
| ++++++++ +

0 commit comments

Comments
 (0)