Skip to content

lifetime elision for return type can refer to an explicitly named region #121152

Open
@lcnr

Description

@lcnr

Originally posted by @danielhenrymantilla in #117967 (comment)

I would nonetheless like to take this opportunity to talk a bit about:

Aside: edge cases of lifetime elision rules which are confusing

There are two slightly related aspects which can make lifetime elision rules (w.r.t. the output elided lifetime) be rather confusing, or poorly readable.

  1. The gist of it is that output '_ can "connect"/match an input explicitly named lifetime "parameter".
  2. Worse, in certain cases this named lifetime "parameter" may not be generic at this layer (outer generic parameter), or may not be generic at all ('static hard-coded / fixed value)!

To illustrate, consider:

type Metadata = &'static str;

struct Foo<'lt>(&'lt str);

impl<'lt> Foo<'lt> {
    fn a(this: Self, metadata: &'static str) -> &str;
    fn b(this: Foo<'lt>, metadata: Metadata) -> &str;
}

What if I told you these two methods have different signatures

I personally find the capability of type aliases or Self aliases to affect semantics (here, those of lifetime elision rules) to be appaling.

It would thus be nice if certain efforts were made to try and go in the other directions, e.g., by linting whenever an output-elided lifetime ('_-explicitly, or implicitly) happens to match an explicitly named input lifetime param, and even more so when this input lifetime "param" is 'static or stems from an outer scope.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.D-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-langRelevant to the language team, which will review and decide on the PR/issue.T-typesRelevant to the types 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