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

Compiler should not omit associated type in compile error message #65230

Closed
rhysd opened this issue Oct 9, 2019 · 2 comments · Fixed by #89633
Closed

Compiler should not omit associated type in compile error message #65230

rhysd opened this issue Oct 9, 2019 · 2 comments · Fixed by #89633
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. 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.

Comments

@rhysd
Copy link
Contributor

rhysd commented Oct 9, 2019

Environment

  • Rust: Nightly 2019-10-07

Repro

  1. Open https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=933123bb14852662962ab5bf5f2073c8
  2. Click run
struct X<'a>(&'a mut i32);

impl<'a> Iterator for X<'a> {
    type Item = &'a i32;
    fn next(&mut self) -> Option<Self::Item> {
        Some(self.0)
    }
}

fn main() {}

Expected behavior

Compiler should raise an error with error message which explains why lifetime parameters are conflicting clearly

Actual behavior

It shows following error:

   Compiling playground v0.0.1 (/playground)
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
 --> src/main.rs:6:9
  |
6 |         Some(self.0)
  |         ^^^^^^^^^^^^
  |
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 5:5...
 --> src/main.rs:5:5
  |
5 | /     fn next(&mut self) -> Option<Self::Item> {
6 | |         Some(self.0)
7 | |     }
  | |_____^
note: ...so that reference does not outlive borrowed content
 --> src/main.rs:6:14
  |
6 |         Some(self.0)
  |              ^^^^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 3:6...
 --> src/main.rs:3:6
  |
3 | impl<'a> Iterator for X<'a> {
  |      ^^
  = note: ...so that the types are compatible:
          expected std::iter::Iterator
             found std::iter::Iterator

error: aborting due to previous error

error: could not compile `playground`.

To learn more, run the command again with --verbose.

Confusing part is as follows:

  = note: ...so that the types are compatible:
          expected std::iter::Iterator
             found std::iter::Iterator

Compiler says 'expected' and 'found' are incompatible but they look the same. This is caused because associated type Item is omitted from error output of 'expected' and 'found'. Actually lifetime parameter in the associated type between 'found' and 'expected' are different but compiler hides them.

@csmoe csmoe added the A-diagnostics Area: Messages for errors, warnings, and lints label Oct 9, 2019
@jonas-schievink jonas-schievink added C-bug Category: This is a bug. 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. labels Oct 9, 2019
@estebank
Copy link
Contributor

Associated items are predicates that are not carried around in the type, so by the time reach this we're not ferrying around the assoc types anymore. This will be hard to fix but very worthwhile.

@rhysd
Copy link
Contributor Author

rhysd commented Feb 18, 2020

Thank you for the comment. I did not know the implementation under the hood.

It might be better to change the expected ... found ... error format in this case for avoiding confusion.
For example,

  = note: ...so that the associated types in std::iter::Iterator are compatible

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-bug Category: This is a bug. 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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants