1

Add `REDUNDANT_LIFETIMES` lint to detect lifetimes which are semantically redund...

 4 weeks ago
source link: https://github.com/rust-lang/rust/pull/118391
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Conversation

There already is a UNUSED_LIFETIMES lint which is fired when we detect where clause bounds like where 'a: 'static, however, it doesn't use the full power of lexical region resolution to detect failures.

Right now UNUSED_LIFETIMES is an Allow lint, though presumably we could bump it to warn? I can (somewhat) easily implement a structured suggestion so this can be rustfix'd automatically, since we can just walk through the HIR body, replacing instances of the redundant lifetime.

Fixes #118376
r? lcnr

aliemjay, lcnr, Nadrieril, zoechi, and fmease reacted with heart emoji

compiler-errors

marked this pull request as ready for review

November 27, 2023 21:58

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

Nov 27, 2023

Member

Author

The only wrinkle is that this approach may be detrimental to performance...

@bors try @rust-timer queue

This comment has been minimized.

rustbot

added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label

Nov 27, 2023

Contributor

⌛ Trying commit b2c18d9 with merge 58aa0f0...

This comment has been minimized.

This comment has been minimized.

Contributor

💔 Test failed - checks-actions

bors

added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.

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

labels

Nov 27, 2023

Member

Author

@bors try

Contributor

⌛ Trying commit d528fad with merge 7a2c7d7...

Contributor

☀️ Try build successful - checks-actions
Build commit: 7a2c7d7 (7a2c7d785725785a467e2dc491986536eef554b5)

This comment has been minimized.

Collaborator

Finished benchmarking commit (7a2c7d7): comparison URL.

Overall result: ❌ regressions - ACTION NEEDED

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.5% [0.2%, 1.4%] 32
Regressions ❌
(secondary)
0.5% [0.0%, 0.8%] 16
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.5% [0.2%, 1.4%] 32

Max RSS (memory usage)

Results

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 675.422s -> 674.91s (-0.08%)
Artifact size: 313.36 MiB -> 313.46 MiB (0.03%)

compiler-errors reacted with confused emoji

rustbot

added perf-regression Performance regressions

and removed S-waiting-on-perf Status: Waiting on a perf run to be completed.

labels

Nov 28, 2023

Member

@aliemjay aliemjay

left a comment

few notes:

  • I think we should not lint if the "victim" lifetime is elided. The current impl lints in this case but it should not imo:
struct Ty<T: static>(T);
fn test(_: Ty<&str>) {}
  • There is no need to fork the inference context and do region resolution for each check, you can simply use outlives_env.free_region_map.sub_free_regions() to check for equality.

  • To avoid the perf impact, can you try to run the lint in enter_wf_checking_ctxt where we've already got the OutlivesEnvironment?

lcnr and compiler-errors reacted with thumbs up emoji

Member

Author

I will try adding this into WF if perf still comes back bad...

@bors try @rust-timer queue

This comment has been minimized.

rustbot

added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label

Nov 28, 2023

Contributor

⌛ Trying commit b09e25e with merge f5059fb...

Contributor

@rustbot labels -I-lang-nominated

We discussed this in the T-lang triage call today. The feeling was that we should do this, hence the proposed FCP merge above.

rustbot

removed the I-lang-nominated Indicates that an issue has been nominated for discussion during a lang team meeting. label

Dec 20, 2023

Contributor

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

Contributor

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

lcnr

removed the T-types Relevant to the types team, which will review and decide on the PR/issue. label

Feb 5, 2024

lcnr

added S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete.

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

labels

Mar 19, 2024

Contributor

Right now UNUSED_LIFETIMES is an Allow lint, though presumably we could bump it to warn? I can (somewhat) easily implement a structured suggestion so this can be rustfix'd automatically, since we can just walk through the HIR body, replacing instances of the redundant lifetime.

Is that about UNUSED_LIFETIMES or the new REDUNDANT_LIFETIMES lint?

anyways, blocked on FCP @rust-lang/lang. Nominating again to get this FCP finished

lcnr

added the I-lang-nominated Indicates that an issue has been nominated for discussion during a lang team meeting. label

Mar 19, 2024

rfcbot

added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised.

and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.

labels

Mar 20, 2024

🔔 This is now entering its final comment period, as per the review above. 🔔

Contributor

@rustbot labels -I-lang-nominated

We discussed this briefly in triage. Thanks to @lcnr for pinging us about this. This is now in FCP, so let's unnominate.

compiler-errors and lcnr reacted with heart emoji

rustbot

removed the I-lang-nominated Indicates that an issue has been nominated for discussion during a lang team meeting. label

Mar 20, 2024

Member

Author

Is that about UNUSED_LIFETIMES or the new REDUNDANT_LIFETIMES lint?

That comment is a bit out of date. It was referencing before this was split into REDUNDANT_LIFETIMES, though it's true for both lints.

}

for &victim in &lifetimes[(idx + 1)..] {

// We only care about lifetimes that are "real", i.e. that have a def-id.

"lifetimes that are present in the source code"?

Member

Author

'static is present in the source code, so "present in the source code" isn't necessarily correct.

let mut shadowed = FxHashSet::default();

for (idx, &candidate) in lifetimes.iter().enumerate() {

// Don't suggest removing a lifetime twice.

This comment should say "Don't suggest using a lifetime we already removed." and a new hashset contains check needs to be added for selecting the same victim twice.

I don't see any tests covering this hashset contains check.

Member

Author

A second check is not necessary due to the transitivity of being redundant (lifetime equality). If there's some other candidate down the road that also is equal to a given victim, then it must also be equal to the candidate lifetime here, and will be placed in the shadowed set.

Also, I don't agree that the reworded comment is necessarily clearer. We're really just suggesting to remove the lifetime twice.

I don't see any tests covering this hashset contains check.

There are no direct tests, but, for example, static_id_indirect below exercises this. Otherwise it would suggest that 'b is redundant twice.

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

Reviewers

riking

riking left review comments

cjgillot

cjgillot left review comments

aliemjay

aliemjay left review comments

lcnr

lcnr left review comments
Assignees

lcnr

Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

we should lint on named lifetimes forced to be equal to another named lifetime

14 participants

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK