Skip to content

E277 could have a more helpful error message when it occurs on a try expression #59980

Closed
@thecodewarrior

Description

@thecodewarrior

Currently E277's message is confusing and seemingly ambiguous when it occurs on a try (?) expression (this may not be limited to E277, it's just the one I noticed). For example, in this code:

fn the_outer_method() -> Result<(), ()> {
    the_inner_method()?;
    Ok(())
}

fn the_inner_method() -> Result<(), bool> {
    Err(true)
}
error[E0277]: the trait bound `(): std::convert::From<bool>` is not satisfied
 --> src/main.rs:6:5
  |
6 |     the_inner_method()?;
  |     ^^^^^^^^^^^^^^^^^^^ the trait `std::convert::From<bool>` is not implemented for `()`
  |
  = note: required by `std::convert::From::from`

The problem is that the_inner_method's error type is bool while the_outer_method's error type is (). In this case the error isn't too confusing, but if the try is in a chained expression (foo()?.bar()) and you're a noob it isn't clear why it wants std::convert::From<bool> implemented on ().

It doesn't necessarily even require a whole fix suggestion, all it would take is making the error only highlight the ?, as the issue is actually occuring when unwrapping the result.

error[E0277]: the trait bound `(): std::convert::From<bool>` is not satisfied
 --> src/main.rs:6:5
  |
6 |     the_inner_method()?;
  |                       ^ the trait `std::convert::From<bool>` is not implemented for `()`
  |
  = note: required by `std::convert::From::from`

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions