Skip to content

Lifetime elision on methods can easily create obscure errors. #17822

Closed
@eddyb

Description

@eddyb

While working on adding lifetimes to rustc::middle::ty, which have since spread throughout the compiler, I've found lifetime elision to create issues more times than it was helping.
Consider this:

struct Foo<'a> {...}
impl<'a> Foo<'a> {
    fn bar(&self) -> Bar {...}
    // ^ needs to be changed to:
    fn bar(&self) -> Bar<'a> {...}
}

For more complicated signatures, elision would not apply and I would often get a (regionck) error within the method, which was great, but where it did apply, it considered the signature to be this:

    fn bar<'b>(&'b self) -> Bar<'b>

Many times, the method itself would typeck, but callers would get the wrong constraints during regionck.
That leaves you with an error in a caller to bar, and because we're lacking blame assignment (is it even feasible?), you usually only know the function in which such an lifetime-elided method is being called.

This is suboptimal and even just a command line option (or attribute) to disable lifetime elision would've helped.
@nikomatsakis suggested disallowing lifetime elision inside an impl generic over lifetimes, or at least making it more careful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions