Lint against iterator functions that panic when N is zero #153563
Lint against iterator functions that panic when N is zero #153563Urgau wants to merge 4 commits into
N is zero #153563Conversation
|
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 Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt Some changes occurred in compiler/rustc_attr_parsing |
|
r? @JohnTitor rustbot has assigned @JohnTitor. Use Why was this reviewer chosen?The reviewer was selected based on:
|
ff233d2 to
44380ea
Compare
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 I could probably modify that function so it doesn't take an |
|
Given that |
44380ea to
36de202
Compare
|
Understandable, I've reworked the PR to avoid touching at |
There was a problem hiding this comment.
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.
|
I'm afraid not, I don't have capacity at the moment. @rustbot reroll |
This comment has been minimized.
This comment has been minimized.
36de202 to
0a8cacf
Compare
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
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.
0a8cacf to
d033014
Compare
This comment has been minimized.
This comment has been minimized.
|
I had to add the encoding of const-params in I've also removed any interaction with @rustbot ready |
|
overall looks good, would like someone else to sign off with me on this together though @rustbot reroll |
|
Before I review, I'm going to wait for the lang team to +1. |
This comment has been minimized.
This comment has been minimized.
e3bb247 to
c976f29
Compare
This comment has been minimized.
This comment has been minimized.
N is zero N is zero
|
The use of a runtime panic here (rather than a compile-time error like a const panic) was intentional to allow for cases like this: if N > 0 {
... array_chunks::<N>() ..
}If we used a compile-time error, people couldn't write code like that (without some kind of special "const if" that we don't yet have). Does the lint handle that case? It needs to. Please add this as a test case and make sure it does not produce a lint. |
c976f29 to
57cedae
Compare
|
I didn't know that, good to know. Fortunately, the lint as currently implemented does not lint on this case, due to only analyzing the generic MIR. I added a test case for it. |
This comment was marked as resolved.
This comment was marked as resolved.
57cedae to
c5f88da
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
c5f88da to
7b7975d
Compare
|
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. |
This comment was marked as outdated.
This comment was marked as outdated.
|
This seems like clearly worth linting, under the usual intent of "we should lint anything that will always panic if reached". Not this PR and not this FCP: I would love to see attributes like this move into the diagnostics namespace to let more things be self-serve for libraries to get lints. If we could no longer need lints like https://rust-lang.github.io/rust-clippy/stable/index.html?search=step_#iterator_step_by_zero because the definition of |
|
Team member @scottmcm has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
|
Makes sense to me. It's important that it not lint the @rfcbot reviewed |
View all comments
This PR adds new variant to the deny-by-default
unconditional_paniclint, by linting on iterator functions that panics whenN(chunks/windows size) is zero1.Those methods are documented to panic if
Nis zero.cc @rust-lang/libs-api
Footnotes
this is done by introducing a new internal attribute on the const parameter:
#[rustc_panics_when_zero]↩