Skip to content

internal: implement lifetime elision - #22927

Open
dfireBird wants to merge 3 commits into
rust-lang:masterfrom
dfireBird:lifetime_elision
Open

internal: implement lifetime elision#22927
dfireBird wants to merge 3 commits into
rust-lang:masterfrom
dfireBird:lifetime_elision

Conversation

@dfireBird

Copy link
Copy Markdown
Member

No description provided.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 27, 2026
@dfireBird

Copy link
Copy Markdown
Member Author

Not sure why this fails in CI and passes in my system?

@ShoyuVanilla

ShoyuVanilla commented Jul 27, 2026

Copy link
Copy Markdown
Member

Not sure why this fails in CI and passes in my system?

Use `env RUN_SLOW_TESTS=1 cargo test` to run the full suite.

You need to set an extra env var to run slow tests

@dfireBird

Copy link
Copy Markdown
Member Author

Oh I didn't know, we ran slow tests. My bad 😅

@Veykril Veykril left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only a partial review so far

View changes since this review

Comment thread crates/hir-def/src/expr_store/pretty.rs Outdated
Comment thread crates/hir-def/src/lib.rs Outdated
Comment thread crates/hir-def/src/resolver.rs Outdated
Comment thread crates/hir-ty/src/display.rs Outdated
fn bar(x: &u32) {}
"#,
expect!["ty: &'_ &'_ u32, name: x"],
expect!["ty: &'_ &'<erased> u32, name: x"],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably need to check our rendering now, we shouldn't show erased lifetimes to the user

@dfireBird dfireBird Jul 27, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could make the RegionKind::ReErased to display '_ for now, would that be fine?

Comment on lines +2675 to +2676
lc world &WorldSnapshot [type_could_unify+name+local]
ex world [type_could_unify]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, so we lose a lot of type equality now which I guess makes sense. Worth to keep in mind if we want t o have something like type-equal module lifetimes

Comment thread crates/ide/src/signature_help.rs
Comment thread crates/hir-def/src/expr_store/lower/generics.rs
@Veykril

Veykril commented Jul 27, 2026

Copy link
Copy Markdown
Member

Very excited for this, I've been wanting this for so long!

@ChayimFriedman2 ChayimFriedman2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread crates/ide/src/hover/tests.rs Outdated
Comment thread crates/hir-expand/src/name.rs Outdated
Comment thread crates/hir-def/src/lib.rs Outdated
self.lower_type_ref(
it,
impl_trait_lower_fn,
&mut Self::elided_lifetime_placeholder_allocator,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not look correct. We should first lower the arguments. If there is exactly one HRTB lifetime in them, any elided lifetime in the return type should refer to it. Otherwise, it's an error lifetime (plus a diagnostic). In fact, I'm pretty sure we should remove LifetimeRef::Placeholder.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use LifetimeRef::Placeholder for return types, so that I can resolve it correctly during hir-ty lowering. But I missed to copy from my old WIP of the lifetime elision.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see no reason to resolve it during hir-ty lowering and complicate the code with additional variant when we can resolve it during hir lowering instead.

&mut self,
node: ast::Type,
impl_trait_lower_fn: ImplTraitLowerFn<'_>,
lifetime_elision_fn: LifetimeElisionFn<'_>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not pleased with passing another callback everywhere. I would prefer an enum field saying what to do: an error, a 'static lifetime (for const and static types), a specific lifetime, or a new anonymous lifetime. In fact you can look at the old LifetimeElisionKind, which implements this (but only for diagnostics) and was copied from rustc.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would anyways need access to lifetime Arena in GenericParamCollector.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The callback works by pushing a '_ and using that Idx to create the LifetimeParamId.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So? Why can't this be done via an enum field?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea because of that i went with the callback setup for collect_impl_trait

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of having multiple callback factories, we can have that a state in ExprCollector and based on which a the callback (so a single callback factory) returns appropriate LifetimeRef?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can borrow just the parts you need, like you do for a callback.

@dfireBird dfireBird Aug 2, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it would work as well, only when lowering functions, because it would be borrowed mutably twice, once when I borrow it for the Arena and the other when it borrows for collect_impl_trait.

It might work in other places tho.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closures borrow only the fields they use since edition 2024. If they wouldn't the code using callbacks wouldn't compile either.

{
TypeBound::ForLifetime(binder, path)
} else {
TypeBound::Path(path, m)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we even need TypeBound::Path now? After all in rustc_type_ir every predicate has a binder. We could just always produce ForLifetime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants