Skip to content

Bad diagnostics when using async fn's and attributes #102823

Closed
@matheus-consoli

Description

@matheus-consoli

rustexplorer (rustexplorer is a playground alternative, I'm using it because play.rust-lang.org doesn't exposes tracing attributes)

This is probably related to some other issues, like #71968, but I decided to open it either way because the diagnostics differ from the async and sync version.

Simplifying my real case, given some struct:

#[derive(Debug)]
struct Foo;

I've tried implementing some instrumented associated async fn and hit check before finishing the code:

impl Foo {
    #[tracing::instrument()]
    async fn missing_implementation(&self) -> char {}

    #[tracing::instrument()]
    async fn missing_wrapper(&self) -> Option<char> {
        'a'
    }
}

which gave me weird diagnostics, especially the last one:

  --> src/main.rs:10:5
   |
10 |     #[tracing::instrument()]
   |     ^^^^^^^^^^^^^^^^^^^^^^^^ expected `char`, found `()`

error[E0308]: mismatched types
  --> src/main.rs:13:5
   |
13 |     #[tracing::instrument()]
   |     ^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `Option`, found `char`
   |
   = note: expected enum `Option<char>`
              found type `char`
help: try wrapping the expression in `Some`
   |
13 |     Some(#[tracing::instrument()])
   |     +++++                        +

but, to my surprise, testing with the sync version:

impl Foo {
    #[tracing::instrument()]
    fn missing_implementation(&self) -> char {}

    #[tracing::instrument()]
    fn missing_wrapper(&self) -> Option<char> {
        'a'
    }
}

outputs:

error[E0308]: mismatched types
  --> src/main.rs:19:51
   |
19 |     fn missing_implementation(&self) -> char {}
   |                                              ^^ expected `char`, found `()`

error[E0308]: mismatched types
  --> src/main.rs:23:9
   |
22 |     fn missing_wrapper(&self) -> Option<char> {
   |                                  ------------ expected `Option<char>` because of return type
23 |         'a'
   |         ^^^ expected enum `Option`, found `char`
   |
   = note: expected enum `Option<char>`
              found type `char`
help: try wrapping the expression in `Some`
   |
23 |         Some('a')
   |         +++++   +

I expect the async version outputs the same diagnostics as the sync version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions