Skip to content
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

Suggest missing Debug Impl when unwrapping #85851

Closed
jamesmunns opened this issue May 31, 2021 · 3 comments · Fixed by #86943
Closed

Suggest missing Debug Impl when unwrapping #85851

jamesmunns opened this issue May 31, 2021 · 3 comments · Fixed by #86943
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jamesmunns
Copy link
Member

jamesmunns commented May 31, 2021

Hey all, here's another common pitfall our new Rust learners stumble into during our training. The diagnostics you get on unwrap when the E: Debug bound isn't met do tell you that it isn't possible, BUT it doesn't tell you how to fix this. I think in most cases, suggesting #[derive(Debug)] is a better option, though this might give false positives if the user doesn't control the Error type (I don't know if diagnostics can "see" whether a type is local or not, but maybe you can!)

Given the following code: Playground Link

enum MyError {
    Oops,
    No,
    DebugImpl,
}

fn main() {
    let x: Result<(), MyError> = Ok(());
    let y = x.unwrap();
}

The current output is:

error[E0599]: the method `unwrap` exists for enum `Result<(), MyError>`, but its trait bounds were not satisfied
 --> src/main.rs:9:15
  |
1 | enum MyError {
  | ------------ doesn't satisfy `MyError: Debug`
...
9 |     let y = x.unwrap();
  |               ^^^^^^ method cannot be called on `Result<(), MyError>` due to unsatisfied trait bounds
  |
  = note: the following trait bounds were not satisfied:
          `MyError: Debug`

error: aborting due to previous error

I would hope for something that tells users more directly "When you use unwrap(), the Err variant must implement the Debug trait in case the unwrap fails and we need to display a panic message".

error[E0599]: the method `unwrap` exists for enum `Result<(), MyError>`, but its trait bounds were not satisfied
 --> src/main.rs:9:15
  |
1 | enum MyError {
  | ------------ doesn't satisfy `MyError: Debug`
...
9 |     let y = x.unwrap();
  |               ^^^^^^ method cannot be called on `Result<(), MyError>` due to unsatisfied trait bounds
  |
  = note: the following trait bounds were not satisfied:
          `MyError: Debug`
  = help: try implementing the `Debug` trait on `MyError`. When you use `unwrap()`, the `Err(MyError)`
          variant must implement the `Debug` trait in order to display a panic message if the unwrap fails

  | #[derive(Debug)]
  | ^^^^^^^^^^^^^^^^ add a derive Debug here
1 | enum MyError {
  | 

error: aborting due to previous error

Applies to Rust 1.51.1, and likely all other older versions of Rust.

@jamesmunns jamesmunns added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 31, 2021
@jyn514
Copy link
Member

jyn514 commented Jun 1, 2021

This would be nice to suggest for all traits with derives in the standard library, and maybe with derives from third-party crates too if the crate is in your dependency tree (like thiserror and Error, or From and derive_more).

@inquisitivecrystal
Copy link
Contributor

inquisitivecrystal commented Jun 5, 2021

@rustbot label C-enhancement D-newcomer-roadblock

@rustbot rustbot added C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Jun 5, 2021
@ptrojahn
Copy link
Contributor

@rustbot claim

bors added a commit to rust-lang-ci/rust that referenced this issue Sep 8, 2021
Suggest deriving traits if possible

This only applies to builtin derives as I don't think there is a
clean way to get the available derives in typeck.

Closes rust-lang#85851
@bors bors closed this as completed in 50e5f90 Sep 8, 2021
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 C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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.

5 participants