Skip to content

Lint against iterator functions that panic when N is zero - #153563

Merged
rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
Urgau:lint-panics-when-n-is-zero
Jul 28, 2026
Merged

Lint against iterator functions that panic when N is zero #153563
rust-bors[bot] merged 4 commits into
rust-lang:mainfrom
Urgau:lint-panics-when-n-is-zero

Conversation

@Urgau

@Urgau Urgau commented Mar 8, 2026

Copy link
Copy Markdown
Member

View all comments

This PR extends the deny-by-default unconditional_panic lint, by linting on iterator functions that panics when N (chunks/windows size) is zero1.

Those methods are documented to panic if N is zero.

error: this operation will panic at runtime
  --> $DIR/const-n-is-zero.rs:11:13
   |
LL |     let _ = s.array_windows::<0>();
   |             ^^^^^^^^^^^^^^^^^^^^^^ const parameter `N` is zero
   |
   = note: `#[deny(unconditional_panic)]` on by default

cc @rust-lang/libs-api

Footnotes

  1. this is done by introducing a new internal attribute on the const parameter: #[rustc_panics_when_zero]

@rustbot

rustbot commented Mar 8, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_hir/src/attrs

cc @jdonszelmann, @JonathanBrouwer

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @vakaras

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann, @JonathanBrouwer

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a, @makai410

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Mar 8, 2026
@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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 8, 2026
@rustbot

rustbot commented Mar 8, 2026

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 69 candidates
  • Random selection from 16 candidates

@Urgau
Urgau force-pushed the lint-panics-when-n-is-zero branch from ff233d2 to 44380ea Compare March 8, 2026 12:25
@RalfJung

RalfJung commented Mar 8, 2026

Copy link
Copy Markdown
Member

This PR adds new kind of variant to the deny-by-default unconditional_panic lint, by linting on iterator functions that panics when N (chunks/windows size) is zero.

This seems to do much more than that. It adds a new language primitive, a new kind of built-in assertion. Could you motivate that?

Comment thread compiler/rustc_const_eval/src/const_eval/machine.rs Outdated
@Urgau

Urgau commented Mar 8, 2026

Copy link
Copy Markdown
Member Author

This seems to do much more than that. It adds a new language primitive, a new kind of built-in assertion. Could you motivate that?

The code is unfortunately tangled, it doesn't add a new language primitive per se, but report_assert_as_lint requires a AssertKind variant even if it's just for linting in one place.

I could probably modify that function so it doesn't take an AssertKind, but I wasn't sure about that.

@RalfJung

RalfJung commented Mar 8, 2026

Copy link
Copy Markdown
Member

Given that AssertKind is part of the MIR syntax, I feel very strongly that we shouldn't modify it for non-MIR/opsem purposes.

@Urgau
Urgau force-pushed the lint-panics-when-n-is-zero branch from 44380ea to 36de202 Compare March 8, 2026 13:23
@Urgau

Urgau commented Mar 8, 2026

Copy link
Copy Markdown
Member Author

Understandable, I've reworked the PR to avoid touching at AssertKind.

@JohnTitor JohnTitor left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I love the idea but it should better for someone more familiar with this topic other than me to review. @RalfJung Could you take over? Otherwise I'd reroll.

View changes since this review

@RalfJung

RalfJung commented Mar 9, 2026

Copy link
Copy Markdown
Member

I'm afraid not, I don't have capacity at the moment.

@rustbot reroll

@rustbot rustbot assigned fee1-dead and unassigned JohnTitor Mar 9, 2026
@rust-bors

This comment has been minimized.

@Urgau
Urgau force-pushed the lint-panics-when-n-is-zero branch from 36de202 to 0a8cacf Compare March 12, 2026 06:47
@rustbot

This comment has been minimized.

@fee1-dead fee1-dead left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the overall idea is good and I could see myself approving down the line, however I think we should see if we can have an attribute on the generic parameter itself instead of applying rustc_panics_when_n_is_zero to the enclosing function. If not, I'd still want something more verbose, such as #[rustc_panics_when_const_param_is_zero(N)]

I don't think this should be touching AssertLint. I feel like it might be simpler to just add a separate error for this rather than using existing mechanisms.

View changes since this review

@rustbot rustbot 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 Mar 29, 2026
@Urgau
Urgau force-pushed the lint-panics-when-n-is-zero branch from 0a8cacf to d033014 Compare March 29, 2026 19:04
@rustbot

This comment has been minimized.

@Urgau

Urgau commented Mar 29, 2026

Copy link
Copy Markdown
Member Author

I had to add the encoding of const-params in rmeta, but otherwise I've moved the attribute directly to the const generic.

I've also removed any interaction with AssertLint, but kept the reporting behind the unconditional_panic lint.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 29, 2026
@fee1-dead

Copy link
Copy Markdown
Member

overall looks good, would like someone else to sign off with me on this together though

@rustbot reroll

@Urgau

Urgau commented Jul 23, 2026

Copy link
Copy Markdown
Member Author

r? compiler

@rustbot rustbot assigned ShoyuVanilla and unassigned jackh726 Jul 23, 2026

@ShoyuVanilla ShoyuVanilla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The implementation looks good to me. Do we need another approval to merge this?

View changes since this review

@Urgau

Urgau commented Jul 27, 2026

Copy link
Copy Markdown
Member Author

@fee1-dead also approved it in this comment.

@bors r=fee1-dead,ShoyuVanilla

@rust-bors

rust-bors Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 93b8cf2 has been approved by fee1-dead,ShoyuVanilla

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

Reason for tree closure: spurious failures

@rust-bors rust-bors Bot 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 Jul 27, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jul 27, 2026
…r=fee1-dead,ShoyuVanilla

Lint against iterator functions that panic when `N` is zero

This PR extends the deny-by-default `unconditional_panic` lint, by linting on iterator functions that panics when `N` (chunks/windows size) is zero[^attr].

Those methods are [documented](https://doc.rust-lang.org/std/primitive.slice.html#panics-11) to panic if `N` is zero.

```
error: this operation will panic at runtime
  --> $DIR/const-n-is-zero.rs:11:13
   |
LL |     let _ = s.array_windows::<0>();
   |             ^^^^^^^^^^^^^^^^^^^^^^ const parameter `N` is zero
   |
   = note: `#[deny(unconditional_panic)]` on by default
```

cc @rust-lang/libs-api

[^attr]: this is done by introducing a new internal attribute on the const parameter: `#[rustc_panics_when_zero]`
@JonathanBrouwer

Copy link
Copy Markdown
Contributor

💔 I suspect this PR failed tests as part of a rollup
@bors r-

After fixing the problem, consider running a try job for the failed job before re-approving.

Link to failure: https://github.com/rust-lang/rust/actions/runs/30255157007/job/89942047433?pr=160002

@rust-bors rust-bors Bot 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 27, 2026
@rust-bors

rust-bors Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

This pull request was unapproved.

This PR was contained in a rollup (#160002), which was unapproved.

View changes since this unapproval

Comment thread compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs Outdated
@Urgau
Urgau force-pushed the lint-panics-when-n-is-zero branch from 93b8cf2 to 24e4698 Compare July 27, 2026 20:48
@rustbot

rustbot commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@ShoyuVanilla

Copy link
Copy Markdown
Member

@bors r=fee1-dead,ShoyuVanilla

@rust-bors

rust-bors Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 24e4698 has been approved by fee1-dead,ShoyuVanilla

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 100. This pull request will be tested once the tree is reopened.

Reason for tree closure: spurious failures

@rust-bors rust-bors Bot 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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 28, 2026
rust-bors Bot pushed a commit that referenced this pull request Jul 28, 2026
Rollup of 9 pull requests

Successful merges:

 - #153563 (Lint against iterator functions that panic when `N` is zero )
 - #159960 (Allow `UnsafeCell` content access without `get` in `invalid_reference_casting` lint)
 - #158893 (Clarify preconditions of raw size/align methods)
 - #159220 (Don't optimize across storage markers in SimplifyComparisonIntegral)
 - #159309 (Move tests batch 18)
 - #159450 (Add codegen test for enum clone)
 - #160017 (Make BorrowSet methods public again)
 - #160022 (Refactor rustc_hir re-exports)
 - #160041 (Correct tracking issue for `casefold` feature)
@rust-bors
rust-bors Bot merged commit f8ee207 into rust-lang:main Jul 28, 2026
13 checks passed
rust-timer added a commit that referenced this pull request Jul 28, 2026
Rollup merge of #153563 - Urgau:lint-panics-when-n-is-zero, r=fee1-dead,ShoyuVanilla

Lint against iterator functions that panic when `N` is zero

This PR extends the deny-by-default `unconditional_panic` lint, by linting on iterator functions that panics when `N` (chunks/windows size) is zero[^attr].

Those methods are [documented](https://doc.rust-lang.org/std/primitive.slice.html#panics-11) to panic if `N` is zero.

```
error: this operation will panic at runtime
  --> $DIR/const-n-is-zero.rs:11:13
   |
LL |     let _ = s.array_windows::<0>();
   |             ^^^^^^^^^^^^^^^^^^^^^^ const parameter `N` is zero
   |
   = note: `#[deny(unconditional_panic)]` on by default
```

cc @rust-lang/libs-api

[^attr]: this is done by introducing a new internal attribute on the const parameter: `#[rustc_panics_when_zero]`
@rustbot rustbot added this to the 1.99.0 milestone Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) 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. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team T-libs Relevant to the library team, which will review and decide on the PR/issue. to-announce Announce this issue on triage meeting

Projects

None yet

Development

Successfully merging this pull request may close these issues.