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

Add REDUNDANT_LIFETIMES lint to detect lifetimes which are semantically redundant #118391

Merged
merged 8 commits into from Apr 10, 2024

Conversation

compiler-errors
Copy link
Member

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

@compiler-errors compiler-errors marked this pull request as ready for review November 27, 2023 21:58
@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 Nov 27, 2023
@compiler-errors
Copy link
Member Author

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

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 27, 2023
@bors
Copy link
Contributor

bors commented Nov 27, 2023

⌛ Trying commit b2c18d9 with merge 58aa0f0...

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 27, 2023
Extend `UNUSED_LIFETIMES` lint to detect lifetimes which are semantically redundant

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 rust-lang#118376
r? lcnr
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Nov 27, 2023

💔 Test failed - checks-actions

@bors 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
@compiler-errors
Copy link
Member Author

@bors try

@bors
Copy link
Contributor

bors commented Nov 27, 2023

⌛ Trying commit d528fad with merge 7a2c7d7...

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 27, 2023
Extend `UNUSED_LIFETIMES` lint to detect lifetimes which are semantically redundant

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 rust-lang#118376
r? lcnr
@bors
Copy link
Contributor

bors commented Nov 28, 2023

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

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
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

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.9% [3.9%, 3.9%] 1
Improvements ✅
(primary)
-2.3% [-2.3%, -2.3%] 1
Improvements ✅
(secondary)
-3.0% [-4.1%, -2.0%] 2
All ❌✅ (primary) -2.3% [-2.3%, -2.3%] 1

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%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 28, 2023
Copy link
Member

@aliemjay aliemjay left a comment

Choose a reason for hiding this comment

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

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?

@compiler-errors
Copy link
Member Author

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

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 28, 2023
@bors
Copy link
Contributor

bors commented Nov 28, 2023

⌛ Trying commit b09e25e with merge f5059fb...

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 28, 2023
Extend `UNUSED_LIFETIMES` lint to detect lifetimes which are semantically redundant

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 rust-lang#118376
r? lcnr
@compiler-errors compiler-errors force-pushed the lifetimes-eq branch 2 times, most recently from b222147 to d5cdea7 Compare March 27, 2024 20:15
@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Mar 30, 2024
@rfcbot
Copy link

rfcbot commented Mar 30, 2024

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@rfcbot rfcbot added the to-announce Announce this issue on triage meeting label Mar 30, 2024
@compiler-errors compiler-errors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. labels Apr 2, 2024
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Apr 4, 2024
Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

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

r=me after nits

compiler/rustc_hir_analysis/src/check/wfcheck.rs Outdated Show resolved Hide resolved
compiler/rustc_lint_defs/src/builtin.rs Outdated Show resolved Hide resolved
compiler/rustc_lint_defs/src/builtin.rs Outdated Show resolved Hide resolved
@matthiaskrgr
Copy link
Member

How different is this implementation from the preexisting clippy::needless_lifetimes lint?
https://rust-lang.github.io/rust-clippy/master/index.html#/needless_lifetimes

@lcnr
Copy link
Contributor

lcnr commented Apr 6, 2024

this is about named lifetimes which are equal to other named lifetimes, e.g. in the followiuinig example 'b and 'a are actually equal due to implied bounds

struct Foo<'a>(&'a ());
impl<'a> Foo<'a> {
    fn use<'b: 'a>(&'b self, s: &'b str) {}
}

@lcnr
Copy link
Contributor

lcnr commented Apr 10, 2024

thank you for implementing this ❤️ I would like to move both lifetime lints to WARN by default in a separate PR soon 🤔

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Apr 10, 2024

📌 Commit da2b714 has been approved by lcnr

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 10, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 10, 2024
…llaumeGomez

Rollup of 7 pull requests

Successful merges:

 - rust-lang#118391 (Add `REDUNDANT_LIFETIMES` lint to detect lifetimes which are semantically redundant)
 - rust-lang#123534 (Windows: set main thread name without re-encoding)
 - rust-lang#123659 (Add support to intrinsics fallback body)
 - rust-lang#123689 (Add const generics support for pattern types)
 - rust-lang#123701 (Only assert for child/parent projection compatibility AFTER checking that theyre coming from the same place)
 - rust-lang#123702 (Further cleanup cfgs in the UI test suite)
 - rust-lang#123706 (rustdoc: reduce per-page HTML overhead)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit fa696a3 into rust-lang:master Apr 10, 2024
11 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Apr 10, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 10, 2024
Rollup merge of rust-lang#118391 - compiler-errors:lifetimes-eq, r=lcnr

Add `REDUNDANT_LIFETIMES` lint to detect lifetimes which are semantically redundant

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 rust-lang#118376
r? lcnr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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