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!

Activity

added 8 commits that reference this issue on Apr 18, 2019

Rollup merge of rust-lang#60064 - estebank:issue-59980, r=varkor

c6732f0

Rollup merge of rust-lang#60064 - estebank:issue-59980, r=varkor

f77b9da

Rollup merge of rust-lang#60064 - estebank:issue-59980, r=varkor

73307ed

Rollup merge of rust-lang#60064 - estebank:issue-59980, r=varkor

75da4a8

Rollup merge of rust-lang#60064 - estebank:issue-59980, r=varkor

cc6b2e1

Rollup merge of rust-lang#60064 - estebank:issue-59980, r=varkor

3f329f6

Rollup merge of rust-lang#60064 - estebank:issue-59980, r=varkor

fc33206

Rollup merge of rust-lang#60064 - estebank:issue-59980, r=varkor

37f835c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @estebank@jonas-schievink@thecodewarrior

      Issue actions

        E277 could have a more helpful error message when it occurs on a try expression · Issue #59980 · rust-lang/rust