diff --git a/tracing-attributes/Cargo.toml b/tracing-attributes/Cargo.toml index b93ddec1ed..16d0e903e9 100644 --- a/tracing-attributes/Cargo.toml +++ b/tracing-attributes/Cargo.toml @@ -45,6 +45,8 @@ tokio-test = "0.4.2" tracing-core = { path = "../tracing-core", version = "0.2"} tracing-subscriber = { path = "../tracing-subscriber", version = "0.3", features = ["env-filter"] } async-trait = "0.1.56" +trybuild = "1.0.64" +rustversion = "1.0.9" [badges] maintenance = { status = "experimental" } diff --git a/tracing-attributes/tests/ui.rs b/tracing-attributes/tests/ui.rs new file mode 100644 index 0000000000..72858033f0 --- /dev/null +++ b/tracing-attributes/tests/ui.rs @@ -0,0 +1,7 @@ +// Only test on nightly, since UI tests are bound to change over time +#[rustversion::nightly] +#[test] +fn async_instrument() { + let t = trybuild::TestCases::new(); + t.compile_fail("tests/ui/async_instrument.rs"); +} diff --git a/tracing-attributes/tests/ui/async_instrument.rs b/tracing-attributes/tests/ui/async_instrument.rs new file mode 100644 index 0000000000..66581c66b7 --- /dev/null +++ b/tracing-attributes/tests/ui/async_instrument.rs @@ -0,0 +1,31 @@ +#![allow(unreachable_code)] + +#[tracing::instrument] +async fn unit() { + "" +} + +#[tracing::instrument] +async fn simple_mismatch() -> String { + "" +} + +// FIXME: this span is still pretty poor +#[tracing::instrument] +async fn opaque_unsatisfied() -> impl std::fmt::Display { + ("",) +} + +struct Wrapper(T); + +#[tracing::instrument] +async fn mismatch_with_opaque() -> Wrapper { + "" +} + +fn main() { + let _ = unit(); + let _ = simple_mismatch(); + let _ = opaque_unsatisfied(); + let _ = mismatch_with_opaque(); +} diff --git a/tracing-attributes/tests/ui/async_instrument.stderr b/tracing-attributes/tests/ui/async_instrument.stderr new file mode 100644 index 0000000000..967a3174ea --- /dev/null +++ b/tracing-attributes/tests/ui/async_instrument.stderr @@ -0,0 +1,56 @@ +error[E0308]: mismatched types + --> tests/ui/async_instrument.rs:5:5 + | +5 | "" + | ^^ expected `()`, found `&str` + +error[E0308]: mismatched types + --> tests/ui/async_instrument.rs:10:5 + | +10 | "" + | ^^- help: try using a conversion method: `.to_string()` + | | + | expected struct `String`, found `&str` + | +note: return type inferred to be `String` here + --> tests/ui/async_instrument.rs:9:31 + | +9 | async fn simple_mismatch() -> String { + | ^^^^^^ + +error[E0277]: `(&str,)` doesn't implement `std::fmt::Display` + --> tests/ui/async_instrument.rs:14:1 + | +14 | #[tracing::instrument] + | ^^^^^^^^^^^^^^^^^^^^^^ `(&str,)` cannot be formatted with the default formatter + | + = help: the trait `std::fmt::Display` is not implemented for `(&str,)` + = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead + = note: this error originates in the attribute macro `tracing::instrument` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: `(&str,)` doesn't implement `std::fmt::Display` + --> tests/ui/async_instrument.rs:15:34 + | +15 | async fn opaque_unsatisfied() -> impl std::fmt::Display { + | ^^^^^^^^^^^^^^^^^^^^^^ `(&str,)` cannot be formatted with the default formatter + | + = help: the trait `std::fmt::Display` is not implemented for `(&str,)` + = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead + +error[E0308]: mismatched types + --> tests/ui/async_instrument.rs:23:5 + | +23 | "" + | ^^ expected struct `Wrapper`, found `&str` + | + = note: expected struct `Wrapper<_>` + found reference `&'static str` +note: return type inferred to be `Wrapper<_>` here + --> tests/ui/async_instrument.rs:22:36 + | +22 | async fn mismatch_with_opaque() -> Wrapper { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: try wrapping the expression in `Wrapper` + | +23 | Wrapper("") + | ++++++++ +