Skip to content

Oddity with lifetime elision and type aliases #140611

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
traviscross opened this issue May 3, 2025 · 4 comments
Open

Oddity with lifetime elision and type aliases #140611

traviscross opened this issue May 3, 2025 · 4 comments
Labels
A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. I-compiler-nominated Nominated for discussion during a compiler team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. needs-crater This change needs a crater run to check for possible breakage in the ecosystem. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@traviscross
Copy link
Contributor

traviscross commented May 3, 2025

In reviewing a test case for,

I was led to the following oddity:

pub struct W<'a>(&'a ());
pub type Alias<'a> = W<'a>;

impl<'a> Alias<'a> {
    fn f1<'x>(self: &W<'a>, x: &'x ()) -> &() { x } //~ `'_ == 'x`, what?
    fn f2<'x>(self: &Alias<'a>, x: &'x ()) -> &() { x } //~ `'_ == 'x`, what?
    fn f3<'x>(&self, _: &'x ()) -> &() { self.0 } //~ OK.
}

impl<'a> W<'a> {
    fn f4<'x>(self: &W<'a>, _: &'x ()) -> &() { self.0 } //~ OK.
    fn f5<'x>(self: &Alias<'a>, x: &'x ()) -> &() { x } //~ `'_ == 'x`, what?
    fn f6<'x>(&self, _: &'x ()) -> &() { self.0 } //~ OK.
}

Playground link

This was noticed long ago in #60944 (comment), which is presumably when the test case was added, but I can't immediately find later discussion.

The Reference doesn't document this behavior. (The FLS, as best I can tell on a skim, does not document lifetime elision at all.)

If there's a good reason for this as the correct behavior, we should probably write this down in the Reference. Otherwise, I'm going to propose we agree to try to do away with this somehow, if possible, maybe over an edition.

@rustbot labels +C-discussion +T-lang +I-lang-nominated +A-lifetimes

cc @rust-lang/lang @ehuss

@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-lifetimes Area: Lifetimes / regions C-discussion Category: Discussion or questions that doesn't represent real issues. I-lang-nominated Nominated for discussion during a lang team meeting. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels May 3, 2025
@traviscross traviscross removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 3, 2025
@Veykril
Copy link
Member

Veykril commented May 3, 2025

(The FLS, as best I can tell on a skim, does not document lifetime elision at all.)

It does, but it also does not capture this oddity I think: https://github.com/rust-lang/fls/blob/main/src/types-and-traits.rst#lifetime-elision

@traviscross
Copy link
Contributor Author

traviscross commented May 6, 2025

Interestingly, rust-analyzer, when configured to show inlay lifetime hints, gets f1-f3 and f5-f6 correct (i.e. matches the behavior of rustc) but gets f4 wrong.

That is, it annotates it like this:

impl<'a> Alias<'a> {
    fn f1<'x>(self: &W<'a>, x: &'x ()) -> &'x () { x } //~ `'_ == 'x`, what?
    fn f2<'x>(self: &Alias<'a>, x: &'x ()) -> &'x () { x } //~ `'_ == 'x`, what?
    fn f3<'0, 'x>(&self, _: &'x ()) -> &'0 () { self.0 } //~ OK.
}

impl<'a> W<'a> {
    fn f4<'x>(self: &W<'a>, _: &'x ()) -> &'x () { self.0 } //~ Wrong r-a annotation.
    fn f5<'x>(self: &Alias<'a>, x: &'x ()) -> &'x () { x } //~ `'_ == 'x`, what?
    fn f6<'0, 'x>(&self, _: &'x ()) -> &'0 () { self.0 } //~ OK.
}

@Veykril
Copy link
Member

Veykril commented May 7, 2025

Those inlay hints are entirely syntactically based and known to be wrong in some scenarios (r-a has not implemented semantic lifetime elision yet)

@joshtriplett
Copy link
Member

We discussed this in today's @rust-lang/lang meeting. This seems emphatically like a bug. We should try to fix it, and do a crater run to see if fixing it breaks anything.

@traviscross traviscross added C-bug Category: This is a bug. and removed C-discussion Category: Discussion or questions that doesn't represent real issues. labels May 7, 2025
@joshtriplett joshtriplett added the I-compiler-nominated Nominated for discussion during a compiler team meeting. label May 7, 2025
@traviscross traviscross added A-lifetimes Area: Lifetimes / regions T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. I-compiler-nominated Nominated for discussion during a compiler team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed A-lifetimes Area: Lifetimes / regions T-lang Relevant to the language team, which will review and decide on the PR/issue. C-bug Category: This is a bug. I-lang-nominated Nominated for discussion during a lang team meeting. I-compiler-nominated Nominated for discussion during a compiler team meeting. labels May 7, 2025
@joshtriplett joshtriplett added the needs-crater This change needs a crater run to check for possible breakage in the ecosystem. label May 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. I-compiler-nominated Nominated for discussion during a compiler team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. needs-crater This change needs a crater run to check for possible breakage in the ecosystem. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants