Skip to content

Extend HIR to track the source and syntax of a lifetime #139945

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
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

shepmaster
Copy link
Member

An upcoming lint will want to be able to know if a lifetime is hidden (e.g. &u8, ContainsLifetime) or anonymous: (e.g. &'_ u8, ContainsLifetime<'_>). It will also want to know if the lifetime is related to a reference (&u8) or a path (ContainsLifetime).

r? @nnethercote

An upcoming lint will want to be able to know if a lifetime is
hidden (e.g. `&u8`, `ContainsLifetime`) or anonymous: (e.g. `&'_ u8`,
`ContainsLifetime<'_>`). It will also want to know if the lifetime is
related to a reference (`&u8`) or a path (`ContainsLifetime`).
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 17, 2025
Copy link
Contributor

@nnethercote nnethercote left a comment

Choose a reason for hiding this comment

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

Some questions and comments, but I definitely like the direction this is headed in. I'm not certain the LifetimeSource/LifetimeSyntax split is the optimal representation, but it's clearer than IsAnonInPath so I won't be too pedantic about it.


/// Details not yet needed. Feel free to give useful
/// categorization to these usages.
Other,
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this for? Can you give an example?

Copy link
Member Author

Choose a reason for hiding this comment

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

The comment is attempting to express this, but it's literally just every other usage that isn't one of the previous categories. Those usages are not relevant to the lint I'm writing, and I'm not familiar with what they are, so it didn't seem right to spend time to categorize them in ways that are not useful to me and potentially actively un-useful for the next person.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I'm fine with having Other, but can you give one or two examples in the comment? It's not obvious to me what other cases there are.

fn from(ident: Ident) -> Self {
let name = ident.name;

if name == kw::Empty {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can kw::Empty ever reach here? If so, where from? I'm curious because I've been eliminating kw::Empty uses aggressively lately.

Copy link
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 actually know; this was more of a "just in case" scenario. Do you want me to change this to a panic?

Copy link
Contributor

Choose a reason for hiding this comment

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

If it doesn't trip on any test, yes please.

}

#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable_Generic)]
pub enum LifetimeSyntax {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are all combinations of LifetimeSource/LifetimeSyntax possible?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, I don't believe so. For example, I don't think you can have LifetimeSyntax::Hidden with LifetimeSource::OutlivesBound or LifetimeSource::PreciseCapturing

Copy link
Contributor

Choose a reason for hiding this comment

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

That would be good to capture in a comment. Maybe on the definition of Lifetime, because that's where LifetimeSyntax and LifetimeSource are used together.

};
let span = self.tcx.sess.source_map().start_point(t.span).shrink_to_hi();
let region = Lifetime { ident: Ident::new(kw::UnderscoreLifetime, span), id };
(region, LifetimeSyntax::Hidden)
Copy link
Contributor

Choose a reason for hiding this comment

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

Here the name is '_ but the LifetimeSyntax is Hidden... why is that? It's the only call to lower_lifetime where the given ident doesn't match the ident in the lifetime.

Copy link
Member Author

Choose a reason for hiding this comment

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

It has to have a name — ident isn't an Option and you are removing these kinds of usages of kw::Empty, so UnderscoreLifetime would be the next most semantic choice. This corresponds to &Type — there is no original source code name for the lifetime, it is hidden.

It's the only call to lower_lifetime where the given ident doesn't match the ident in the lifetime.

That's true, but notably lower_lifetime_hidden_in_path and elided_dyn_bound do the same concept of pairing UnderscoreLifetime with Hidden.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok

@bors
Copy link
Collaborator

bors commented Apr 17, 2025

☔ The latest upstream changes (presumably #139938) made this pull request unmergeable. Please resolve the merge conflicts.

@nnethercote
Copy link
Contributor

Adding the panic on the empty case and a couple more comments should be enough for this to merge, thanks.

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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants