Skip to content

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

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

Open
lcnr opened this issue Feb 15, 2024 · 2 comments
Open

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

lcnr opened this issue Feb 15, 2024 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@lcnr
Copy link
Contributor

lcnr commented Feb 15, 2024

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.

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 15, 2024
@jieyouxu jieyouxu added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 15, 2024
@jieyouxu jieyouxu added T-lang Relevant to the language team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue. labels May 31, 2024
@traviscross
Copy link
Contributor

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 A-lifetimes Area: Lifetimes / regions A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants