Skip to content

"this can't be annotated with ?" on expressions not directly annotated with ? #144304

@purplesyringa

Description

@purplesyringa

Code

fn f() -> Result<(), i32> {
    Err("str").map_err(|e| e)?;
    Err("str").map_err(|e| e.to_string())?;
    Ok(())
}

Current output

error[E0277]: `?` couldn't convert the error to `i32`
 --> src/lib.rs:2:30
  |
1 | fn f() -> Result<(), i32> {
  |           --------------- expected `i32` because of this
2 |     Err("str").map_err(|e| e)?;
  |     ----------               ^ the trait `From<&str>` is not implemented for `i32`
  |     |
  |     this can't be annotated with `?` because it has type `Result<_, &str>`
  |
  = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
  = help: the following other types implement trait `From<T>`:
            `i32` implements `From<bool>`
            `i32` implements `From<i16>`
            `i32` implements `From<i8>`
            `i32` implements `From<u16>`
            `i32` implements `From<u8>`

error[E0277]: `?` couldn't convert the error to `i32`
 --> src/lib.rs:3:42
  |
1 | fn f() -> Result<(), i32> {
  |           --------------- expected `i32` because of this
2 |     Err("str").map_err(|e| e)?;
3 |     Err("str").map_err(|e| e.to_string())?;
  |     ---------- --------------------------^ the trait `From<String>` is not implemented for `i32`
  |     |          |
  |     |          this can't be annotated with `?` because it has type `Result<_, String>`
  |     this can't be annotated with `?` because it has type `Result<_, &str>`
  |
  = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
  = help: the following other types implement trait `From<T>`:
            `i32` implements `From<bool>`
            `i32` implements `From<i16>`
            `i32` implements `From<i8>`
            `i32` implements `From<u16>`
            `i32` implements `From<u8>`

Desired output

error[E0277]: `?` couldn't convert the error to `i32`
 --> src/lib.rs:2:30
  |
1 | fn f() -> Result<(), i32> {
  |           --------------- expected `i32` because of this
2 |     Err("str").map_err(|e| e)?;
  |     -------------------------^ the trait `From<&str>` is not implemented for `i32`
  |     |
  |     this can't be annotated with `?` because it has type `Result<_, &str>`
  |
  = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
  = help: the following other types implement trait `From<T>`:
            `i32` implements `From<bool>`
            `i32` implements `From<i16>`
            `i32` implements `From<i8>`
            `i32` implements `From<u16>`
            `i32` implements `From<u8>`

error[E0277]: `?` couldn't convert the error to `i32`
 --> src/lib.rs:3:42
  |
1 | fn f() -> Result<(), i32> {
  |           --------------- expected `i32` because of this
2 |     Err("str").map_err(|e| e)?;
3 |     Err("str").map_err(|e| e.to_string())?;
  |     -------------------------------------^ the trait `From<String>` is not implemented for `i32`
  |     |
  |     this can't be annotated with `?` because it has type `Result<_, &str>`
  |
  = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
  = help: the following other types implement trait `From<T>`:
            `i32` implements `From<bool>`
            `i32` implements `From<i16>`
            `i32` implements `From<i8>`
            `i32` implements `From<u16>`
            `i32` implements `From<u8>`

Rationale and extra context

"this can't be annotated with ?" shouldn't apply to subexpressions that caused the typeck failure, only to the expression directly annotated with ?. Either the span should be extended to the whole expression or the error description needs to be changed.

Other cases

Rust Version

rustc 1.90.0-nightly (a00149764 2025-07-14)
binary: rustc
commit-hash: a001497644bc229f1abcc5b2528733386591647f
commit-date: 2025-07-14
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.8

Anything else?

@rustbot label +D-papercut

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-papercutDiagnostics: An error or lint that needs small tweaks.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