Skip to content

Overlapping From impls produce confusing errors in async code #69632

@DomantasJ

Description

@DomantasJ

Playground

struct MyError;

impl<T> From<T> for MyError {
    fn from(error: T) -> Self {
        MyError::from(error)
    }
}

async fn test() -> Result<(), MyError> {
    block().await?;
    Ok(())
}

async fn block() -> Result<(), MyError> {
    Ok(())
}

This produces a rather confusing error:

error[E0283]: type annotations needed
  --> src/lib.rs:10:18
   |
10 |     block().await?;
   |                  ^ cannot infer type for struct `MyError`
   |
   = note: cannot resolve `MyError: std::convert::From<MyError>`
   = note: required by `std::convert::From::from`

If you make functions non-async and remove the await then you get a proper error about overlapping From impls:

error[E0119]: conflicting implementations of trait `std::convert::From<MyError>` for type `MyError`:
 --> src/lib.rs:3:1
  |
3 | impl<T> From<T> for MyError {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: conflicting implementation in crate `core`:
          - impl<T> std::convert::From<T> for T;

Meta

Rust version 1.41.1 (current version on playground)
Same problem appears on beta and nightly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.T-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