Skip to content

Lifetime error messages should have better spans #18759

Open
@mdinger

Description

@mdinger

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions