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

Error when concrete type doesn't satisfy impl trait is poorly spanned #80583

Closed
SNCPlay42 opened this issue Jan 1, 2021 · 1 comment · Fixed by #97818
Closed

Error when concrete type doesn't satisfy impl trait is poorly spanned #80583

SNCPlay42 opened this issue Jan 1, 2021 · 1 comment · Fixed by #97818
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@SNCPlay42
Copy link
Contributor

SNCPlay42 commented Jan 1, 2021

trait Trait {}

fn foo() -> impl Trait {
    0u8
}

Currently emits

error[E0277]: the trait bound `u8: Trait` is not satisfied
 --> src/lib.rs:3:13
  |
3 | fn foo() -> impl Trait {
  |             ^^^^^^^^^^ the trait `Trait` is not implemented for `u8`

It should probably point to the return value as the span of the actual error, with the current span appearing in a note, e.g.

error[E0277]: the trait bound `u8: Trait` is not satisfied
 --> src/lib.rs:4:4
  |
3 | fn foo() -> impl Trait {
  |             ---------- the return value of this function must implement `Trait`
4 |     0u8
  |     ^^^  the trait `Trait` is not implemented for `u8`

@rustbot label T-compiler A-impl-trait A-diagnostics D-terse

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 1, 2021
@SNCPlay42
Copy link
Contributor Author

The problem is, the error comes from the obligation registered on the inference var created when instantiating the opaque type, which happens very early in type-checking, so when creating the obligation we don't yet have a good span for the actual return value(s) of the function (it's easy to find the function's body's tail, but what if it only returns from return statements?)

JohnTitor added a commit to JohnTitor/rust that referenced this issue Jun 22, 2022
… r=oli-obk

Point at return expression for RPIT-related error

Certainly this needs some diagnostic refining, but I wanted to show that it was possible first and foremost. Not sure if this is the right approach. Open to feedback.

Fixes rust-lang#80583
@bors bors closed this as completed in dfc6d7a Jun 22, 2022
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 A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. 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