Skip to content
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

Lifetime code style consistency in impls and other easy grabs #16141

Open
wants to merge 26 commits into
base: main
Choose a base branch
from

Conversation

BenjaminBrienen
Copy link
Contributor

@BenjaminBrienen BenjaminBrienen commented Oct 28, 2024

Objective

A certain level of consistency in how lifetimes are (or aren't) written.

Solution

Fix 'a -> 'w where appropriate.
Fix "'w should always be written" where appropriate.
Fix "'s should always be written" where appropriate.
Fix "named lifetimes should always be written" where appropriate.

Testing

Changes should be effectively "code style", not semantic.

@alice-i-cecile alice-i-cecile added D-Trivial Nice and easy! A great choice to get started with Bevy A-Build-System Related to build systems or continuous integration C-Code-Quality A section of code that is hard to understand or change S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels Oct 28, 2024
@BenjaminBrienen
Copy link
Contributor Author

Hold on, I made a mistake

@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels Oct 28, 2024
@BenjaminBrienen
Copy link
Contributor Author

image
I just want you to know that this is all coming from clippy

@BenjaminBrienen
Copy link
Contributor Author

@alice-i-cecile No longer 2 lines :(

@BenjaminBrienen
Copy link
Contributor Author

Ok, clippy isn't requiring anything more.

@BenjaminBrienen BenjaminBrienen added S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Oct 28, 2024
@Victoronz
Copy link
Contributor

All the 'a lifetimes that are hidden away are a nice cleanup, but this change also hides away a lot of named lifetimes, mostly 'w and 's.
There are cases when only one of the two is present, and now when hidden, is no longer discernible from just any elided 'a lifetime. In other places now only one of these two is hidden, which is no longer be discernible from <'w, 'a>, which also exists.

@alice-i-cecile
Copy link
Member

Yeah, I don't like these changes as a whole, and would prefer to allow this lint.

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

Rescinding my approval: we shouldn't do this systematically. The 'a lifetimes are fine, but as @Victoronz points out, the loss of named lifetimes is a net downgrade for readability.

@alice-i-cecile alice-i-cecile added X-Controversial There is active debate or serious implications around merging this PR S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Oct 28, 2024
@Victoronz
Copy link
Contributor

Victoronz commented Oct 28, 2024

Yeah, I don't like these changes as a whole, and would prefer to allow this lint.

I agree. My opinion (for impls) generally is:
Unnamed lifetime -> elide when possible
Named lifetime -> never elide

@alice-i-cecile alice-i-cecile added S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Oct 28, 2024
@BenjaminBrienen BenjaminBrienen self-assigned this Oct 29, 2024
@BenjaminBrienen BenjaminBrienen changed the title Remove unnecessary lifetimes from impl Ellide or specify lifetimes, fix new clippy lint Oct 31, 2024
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

Only a couple more fixes left! I'm in favor of these changes now, and I'm glad to see the lint allowed.

@alice-i-cecile alice-i-cecile added X-Contentious There are nontrivial implications that should be thought through and removed X-Controversial There is active debate or serious implications around merging this PR labels Nov 1, 2024
@Victoronz
Copy link
Contributor

Victoronz commented Nov 1, 2024

I would be careful about keeping the scope of this PR down. I see it now also specifies some named lifetimes, which is fine for impls, but there are also some function (and trait function?) definitions in there now.

Caution should be taken with other locations, because specifying lifetimes in function definitions (i.e. adding generic arguments) can be a breaking change. It also means no longer systematically following/allowing a lint, so that should be a separate decision/PR imo.

@BenjaminBrienen
Copy link
Contributor Author

I totally understand if we aren't interested in tackling "lifetime style consistency" right now. The problems I noticed are:

  • Many 'a should be 'w, so it's not obvious which changes should be reverted.
  • Existing inconsistencies in the way lifetimes are written that go against feedback I've received in the PR, which makes me want to fix those, too.
  • Other inconsistencies that are easy fixes, like Formatter<'_>.

@BenjaminBrienen BenjaminBrienen changed the title Ellide or specify lifetimes, fix new clippy lint Lifetime code style consistency in impl and common types Nov 1, 2024
@BenjaminBrienen BenjaminBrienen changed the title Lifetime code style consistency in impl and common types Lifetime code style consistency in impls and other easy grabs Nov 1, 2024
@chescock
Copy link
Contributor

chescock commented Nov 1, 2024

For what it's worth, I like the elisions described as "regressions" above, so I'd like to argue in favor of them briefly.

The advantage of impl Foo<'_> over impl<'w> Foo<'w> is that you know the lifetime can never be used elsewhere! (This is different from eliding lifetimes in a function signature, where the return type can still refer to one of them as '_.) Knowing that the impl can never depend on that lifetime can be a powerful tool for understanding it.

This is especially useful for SystemParam impls, since the 'w and 's on the type itself are never used, and would be shadowed by the 'w and 's lifetimes in the Item associated type and get_param method.

The other advantage is that the lint can enforce consistency. If the preferred style is to include 'w and 's, then PRs adding code that elides them will still slip in, and we'll wind up with a mix of styles. But if we can turn on a lint, then the CI will ensure that everything is written the same way! (An automatic check can also reduce the time spent bikeshedding on future PRs, although I've probably cost us more than that just by writing this. :) )

@BenjaminBrienen
Copy link
Contributor Author

BenjaminBrienen commented Nov 1, 2024

I also prefer to just follow the lints, for similar reasons. I'm just doing my best given the feedback since I think pretty much every member has been against that strategy. I'm trying to find a balance between "We don't want to have to write all the lifetimes" in #15916 (for example, writing Mut<'w, T> everywhere and "We don't want to ellide 'w" from here.

@chescock
Copy link
Contributor

chescock commented Nov 1, 2024

I'm just doing my best given the feedback since I think pretty much every member has been against that strategy.

Just in case it wasn't clear: I think you're doing a great job with this PR! My comment was for the folks who were against that strategy. There are definitely tradeoffs to be made here, but I wanted to highlight some of the benefits of your original proposal in case they hadn't been considered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Build-System Related to build systems or continuous integration C-Code-Quality A section of code that is hard to understand or change D-Trivial Nice and easy! A great choice to get started with Bevy S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged X-Contentious There are nontrivial implications that should be thought through
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants