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

Misleading error message for code with associated types and lifetimes #74736

Closed
smmalis37 opened this issue Jul 25, 2020 · 1 comment
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. 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

@smmalis37
Copy link
Contributor

The following code

struct S {}

trait T<'a>{
    type A;
}

impl T<'_> for S {
    type A = i32;
}

fn foo<'a>(x: impl Fn(<S as T<'_>>::A) -> <S as T<'a>>::A) {
    x(2i32);
}

fn main() {
    foo(|x| x);
}

currently produces the following error:

error[E0308]: mismatched types
  --> src/main.rs:12:7
   |
12 |     x(2i32);
   |       ^^^^ expected associated type, found `i32`
   |
   = note: expected associated type `<S as T<'_>>::A`
                         found type `i32`

This is confusing because at a glance it would seem like the error is nonsensical. The associated type is i32, so how could they mismatch? I believe the real problem is that because there are no input references, no lifetime elision is taking place, meaning there is no '_ in scope for that function. The error message should probably be changed to something that actually mentions lifetimes (if I'm correct in my understanding of the real issue).

@smmalis37 smmalis37 added the C-bug Category: This is a bug. label Jul 25, 2020
@JohnTitor JohnTitor added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. labels Jul 25, 2020
@JohnTitor JohnTitor added D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. A-lifetimes Area: Lifetimes / regions labels Oct 23, 2020
@smmalis37
Copy link
Contributor Author

Closing in favor of #76956

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-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. 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

No branches or pull requests

2 participants