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

Rewrite never_loop as a strict reachability pass #11447

Merged
merged 2 commits into from Sep 2, 2023

Conversation

digama0
Copy link
Contributor

@digama0 digama0 commented Sep 2, 2023

Fixes #11004. This is a "more principled" rewrite of never_loop which does a proper reachability analysis of continue 'main_loop.

  • Rather than using the IgnoreUntilEnd(label) which had an unclear (and ultimately incorrect) relationship to reachability of labels and divergence, local label reachability is now being tracked once per label in the local_labels list (previously ignore_ids)
  • AlwaysBreak and Otherwise have been renamed to Diverging and Normal respectively
  • The lint now strictly only evaluates subexpressions when the entry point is reachable. This is important because we do not want unreachable subexpressions to return MayContinueMainLoop - this was the source of one of the bugs.

The constant check from #11005 was removed, because it is no longer necessary to avoid the FP and it is necessarily quite approximate (it only checks for a literal true in the if statement, which would already be caught by another lint). Since we are using a sound underapproximation now, any if statement will simply be treated as if both branches are reachable, no matter the condition. This ensures no FPs at the cost of some FNs (which IIUC is the desired error direction). But we could put that check back in (or something like it) if desired.

changelog: [never_loop]: Handles more edge cases involving labeled breaks

@rustbot
Copy link
Collaborator

rustbot commented Sep 2, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Centri3 (or someone else) soon.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 2, 2023
bors added a commit that referenced this pull request Sep 2, 2023
`never_loop` catches `loop { panic!() }`

* Depends on: #11447

This is an outgrowth of #11447 which I felt would best be done as a separate PR because it yields significant new results.

This uses typecheck results to determine divergence, meaning we can now detect cases like `loop { std::process::abort() }` or `loop { panic!() }`. A downside is that `loop { unimplemented!() }` is also being linted, which is arguably a false positive. I'm not really sure how to check this from HIR though, and it seems best to leave this epicycle for a later PR.

changelog: [`never_loop`]: Now lints on `loop { panic!() }` and similar constructs
@bors bors merged commit 39b316d into rust-lang:master Sep 2, 2023
4 checks passed
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.

Clippy incorrectly says that an infinite loop will never loop.
4 participants