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

Lifetime error messages should have better spans #18759

Open
mdinger opened this issue Nov 8, 2014 · 4 comments
Open

Lifetime error messages should have better spans #18759

mdinger opened this issue Nov 8, 2014 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mdinger
Copy link
Contributor

mdinger commented Nov 8, 2014

This error has an arrow pointing where the lifetime issue is (though I don't know why it isn't more precise):

struct Mine<'a> { s: &'a str }

impl <'a>Iterator<&'a str> for Mine<'a> {
    fn next(&mut self) -> Option<&str> { Some("h") }
}

fn main() {}
<anon>:4:5: 4:53 error: method `next` has an incompatible type for trait: expected concrete lifetime, found bound lifetime parameter  [E0053]
<anon>:4     fn next(&mut self) -> Option<&str> { Some("h") }
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<anon>:4:40: 4:53 note: expected concrete lifetime is the lifetime 'a as defined on the block at 4:39
<anon>:4     fn next(&mut self) -> Option<&str> { Some("h") }
                                                ^~~~~~~~~~~~~
error: aborting due to previous error
playpen: application terminated with error code 101

This won't because next() is multiline:

struct Mine<'a> { s: &'a str }

impl <'a>Iterator<&'a str> for Mine<'a> {
    fn next(&mut self) -> Option<&str> {
        Some("h")
    }
}

fn main() {}
<anon>:4:5: 6:6 error: method `next` has an incompatible type for trait: expected concrete lifetime, found bound lifetime parameter  [E0053]
<anon>:4     fn next(&mut self) -> Option<&str> {
<anon>:5         Some("h")
<anon>:6     }
<anon>:4:40: 6:6 note: expected concrete lifetime is the lifetime 'a as defined on the block at 4:39
<anon>:4     fn next(&mut self) -> Option<&str> {
<anon>:5         Some("h")
<anon>:6     }
error: aborting due to previous error
playpen: application terminated with error code 101

Best would be:

<anon>:4:40: 4:53 note: expected concrete lifetime is the lifetime 'a as defined on the block at 4:39
<anon>:4     fn next(&mut self) -> Option<&str> { Some("h") }
                                           ^~~~~~~~~~~~~

Someone might easily put the lifetime on next(&'a self) instead of Option<&'a str> because it didn't point and they didn't understand the message.

@mdinger mdinger changed the title Lifetime error messages would be better with arrows Multiline function lifetime error messages would be better with arrows Nov 8, 2014
@steveklabnik steveklabnik added the A-diagnostics Area: Messages for errors, warnings, and lints label Jan 27, 2015
@steveklabnik
Copy link
Member

Triage: updated code:

struct Mine<'a> { s: &'a str }

impl <'a>Iterator for Mine<'a> {
    type Item = &'a str;
    fn next(&mut self) -> Option<&str> { Some("h") }
}

fn main() {}

same error, same problem.

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum changed the title Multiline function lifetime error messages would be better with arrows Lifetime error messages should have better spans May 31, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 22, 2017
@estebank
Copy link
Contributor

estebank commented Nov 15, 2017

Current output (updated 2018-02-11):

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in generic type due to conflicting requirements
 --> src/main.rs:5:5
  |
5 |     fn next(&mut self) -> Option<&str> { Some("h") }
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
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<&str> { Some("h") }
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = note: ...so that the method type is compatible with trait:
          expected fn(&mut Mine<'a>) -> std::option::Option<&str>
             found fn(&mut Mine<'a>) -> std::option::Option<&str>
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 3:1...
 --> src/main.rs:3:1
  |
3 | impl <'a>Iterator for Mine<'a> {
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = note: ...so that the types are compatible:
          expected std::iter::Iterator
             found std::iter::Iterator

This case should look more like the following:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in generic type due to conflicting requirements
 --> src/main.rs:5:5
  |
5 |     fn next(&mut self) -> Option<&str> { Some("h") }
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
note: the method `next` is defined in the trait as returning `Option<Self::Item>`, which is defined in the impl as `&'a str`...
 --> src/main.rs:4:5
  |
4 |     type Item = &'a str;
  |     ^^^^^^^^^^^^^^^^^^^^
note: ...but the implementation of `next` returns `Option<&str>`:
 --> src/main.rs:5:5
  |
5 |     fn next(&mut self) -> Option<&str> { Some("h") }
  |                           ^^^^^^^^^^^^
help: change the return type for `next` to match the trait:
 --> src/main.rs:5:5
  |
5 |     fn next(&mut self) -> Option<&'a str> { Some("h") }
  |                           ^^^^^^^^^^^^^^^

bors added a commit that referenced this issue Feb 3, 2018
Tweak presentation on lifetime trait mismatch

 - On trait/impl method discrepancy, add label pointing at trait signature.
 - Point only at method definition when referring to named lifetimes on lifetime mismatch.
 - When the sub and sup expectations are the same, tweak the output to avoid repeated spans.

Fix #30790, CC #18759.
@estebank estebank added the P-medium Medium priority label May 29, 2019
@estebank
Copy link
Contributor

Current output, few changes:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in generic type due to conflicting requirements
 --> src/main.rs:5:5
  |
5 |     fn next(&mut self) -> Option<&str> { Some("h") }
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
note: first, the lifetime cannot outlive the anonymous lifetime defined here...
 --> src/main.rs:5:13
  |
5 |     fn next(&mut self) -> Option<&str> { Some("h") }
  |             ^^^^^^^^^
note: ...so that the method type is compatible with trait
 --> src/main.rs:5:5
  |
5 |     fn next(&mut self) -> Option<&str> { Some("h") }
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = note: expected `fn(&mut Mine<'a>) -> Option<&str>`
             found `fn(&mut Mine<'a>) -> Option<&str>`
note: but, the lifetime must be valid for the lifetime `'a` as defined here...
 --> src/main.rs:3:7
  |
3 | impl <'a>Iterator for Mine<'a> {
  |       ^^
note: ...so that the types are compatible
 --> src/main.rs:5:5
  |
5 |     fn next(&mut self) -> Option<&str> { Some("h") }
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  = note: expected `<Mine<'a> as Iterator>`
             found `<Mine<'_> as Iterator>`

@Dylan-DPC Dylan-DPC added C-bug Category: This is a bug. and removed C-enhancement Category: An issue proposing an enhancement or a PR with one. labels May 23, 2023
@estebank
Copy link
Contributor

Current output:

error: `impl` item signature doesn't match `trait` item signature
 --> src/main.rs:5:5
  |
5 |     fn next(&mut self) -> Option<&str> { Some("h") }
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ found `fn(&'1 mut Mine<'a>) -> Option<&'1 str>`
 --> /rustc/42b1224e9eb37177f608d3f6a6f2be2ee13902e4/library/core/src/iter/traits/iterator.rs:112:5
  |
  = note: expected `fn(&'1 mut Mine<'a>) -> Option<&'a str>`
  |
  = note: expected signature `fn(&'1 mut Mine<'a>) -> Option<&'a str>`
             found signature `fn(&'1 mut Mine<'a>) -> Option<&'1 str>`
  = help: the lifetime requirements from the `impl` do not correspond to the requirements in the `trait`
  = help: verify the lifetime relationships in the `trait` and `impl` between the `self` argument, the other inputs and its output

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. P-medium Medium priority 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

5 participants