Skip to content

Provide more context on ? where the error type can't be converted to the expected one #137238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
estebank opened this issue Feb 18, 2025 · 3 comments · Fixed by #137245
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. F-try_trait_v2 Tracking issue for RFC#3058 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

Code

struct E;

fn foo() -> Result<(), Box<dyn std::error::Error>> {
    Ok(bar()?)
}
fn bar() -> Result<(), E> {
    Err(E)
}
fn main() {}

Current output

error[E0277]: the trait bound `E: std::error::Error` is not satisfied
 --> bad.rs:4:13
  |
4 |     Ok(bar()?)
  |             ^ the trait `std::error::Error` is not implemented for `E`
  |
  = help: the trait `FromResidual<Result<Infallible, E>>` is implemented for `Result<T, F>`
  = note: required for `Box<dyn std::error::Error>` to implement `From<E>`

Desired output

error[E0277]: the trait bound `E: std::error::Error` is not satisfied
 --> bad.rs:4:13
  |
X | struct E;
  |        - doesn't implement trait `std::error::Error`
...
X | fn foo() -> Result<(), Box<dyn std::error::Error>> {
  |             -------------------------------------- requires that `Box<dyn std::error::Error>` implements `From<E>`
X |     Ok(bar()?)
  |             ^ the trait `From<E>` is not implemented for `Box<dyn std::error::Error>`
  |
  = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait

Rationale and extra context

As far as the (stable) end user is concerned, ? is a little more than a compiler intrinsic. We shouldn't mention FromResidual. We also should provide extra context on where the obligation comes from, and we should point at the return type of the enclosing function, and ideally also point at the type that couldn't be converted which is missing a From or Into impl.

Other cases

Rust Version

rustc 1.86.0-dev
binary: rustc
commit-hash: unknown
commit-date: unknown
host: x86_64-unknown-linux-gnu
release: 1.86.0-dev
LLVM version: 19.1.7

Anything else?

No response

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. F-try_trait_v2 Tracking issue for RFC#3058 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 18, 2025
@mcclure
Copy link

mcclure commented Feb 18, 2025

Note: I believe that this change was inspired by me complaining on Mastodon about an error message I did not understand. In context on my machine, the true meaning of the error message was that my type had met the requirements for std::trait::Error but I needed to add an empty impl std::error:Error for MyError {} for it to actually have the trait. The original error message was inadequate for me to understand I needed this. On the other hand, I believe this altered error message would have been enough for me to work it out (in conjunction with the documentation I had).

estebank added a commit to estebank/rust that referenced this issue Feb 18, 2025
When a `?` operation requires an `Into` conversion with additional bounds (like having a concrete error but wanting to convert to a trait object), we handle it speficically and provide the same kind of information we give other `?` related errors.

```
error[E0277]: `?` couldn't convert the error: `E: std::error::Error` is not satisfied
  --> $DIR/bad-question-mark-on-trait-object.rs:5:13
   |
LL | fn foo() -> Result<(), Box<dyn std::error::Error>> {
   |             -------------------------------------- required `E: std::error::Error` because of this
LL |     Ok(bar()?)
   |             ^ the trait `std::error::Error` is not implemented for `E`
   |
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = note: required for `Box<dyn std::error::Error>` to implement `From<E>`
```

Avoid talking about `FromResidual` when other more relevant information is being given, particularly from `rust_on_unimplemented`.

rust-lang#137238.
@estebank
Copy link
Contributor Author

estebank commented Feb 20, 2025

@mcclure is there anything you'd want to add to the following?

error[E0277]: `?` couldn't convert the error: `E: std::error::Error` is not satisfied
  --> $DIR/bad-question-mark-on-trait-object.rs:7:13
   |
LL | fn foo() -> Result<(), Box<dyn std::error::Error>> {
   |             -------------------------------------- required `E: std::error::Error` because of this
LL |     Ok(bar()?)
   |        -----^ the trait `std::error::Error` is not implemented for `E`
   |        |
   |        this has type `Result<_, E>`
   |
note: `E` needs to implement `std::error::Error`
  --> $DIR/bad-question-mark-on-trait-object.rs:1:1
   |
LL | struct E;
   | ^^^^^^^^
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = note: required for `Box<dyn std::error::Error>` to implement `From<E>`

Image

@bors bors closed this as completed in 890c4d2 Feb 22, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 22, 2025
Rollup merge of rust-lang#137245 - estebank:from-residual-note-2, r=oli-obk

Tweak E0277 when predicate comes indirectly from ?

When a `?` operation requires an `Into` conversion with additional bounds (like having a concrete error but wanting to convert to a trait object), we handle it speficically and provide the same kind of information we give other `?` related errors.

```
error[E0277]: `?` couldn't convert the error: `E: std::error::Error` is not satisfied
  --> $DIR/bad-question-mark-on-trait-object.rs:7:13
   |
LL | fn foo() -> Result<(), Box<dyn std::error::Error>> {
   |             -------------------------------------- required `E: std::error::Error` because of this
LL |     Ok(bar()?)
   |        -----^ the trait `std::error::Error` is not implemented for `E`
   |        |
   |        this has type `Result<_, E>`
   |
note: `E` needs to implement `std::error::Error`
  --> $DIR/bad-question-mark-on-trait-object.rs:1:1
   |
LL | struct E;
   | ^^^^^^^^
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = note: required for `Box<dyn std::error::Error>` to implement `From<E>`
```

Avoid talking about `FromResidual` when other more relevant information is being given, particularly from `rust_on_unimplemented`.

Fix rust-lang#137238.

-----

CC rust-lang#137232, which was a smaller step related to this.
@mcclure
Copy link

mcclure commented Feb 24, 2025

@estebank , that seems fine to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. D-verbose Diagnostics: Too much output caused by a single piece of incorrect code. F-try_trait_v2 Tracking issue for RFC#3058 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants