-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Make <[T]>::array_*
methods fail to compile on 0 len arrays
#99471
Make <[T]>::array_*
methods fail to compile on 0 len arrays
#99471
Conversation
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
There are also few other methods, would change them too. |
while it results in a better user experience than runtime panic, this solution has the issue that it only panics during monomorphization, so This also means that So this doesn't allow us to stabilize these methods as is and add a @rustbot label +T-libs-api -T-libs |
r? rust-lang/libs |
1561b70
to
26e0a2d
Compare
Ooops, really |
Added this check to more methods. |
Also relevant: #99682 |
Hi! Do I need to do something with this PR? |
☔ The latest upstream changes (presumably #100759) made this pull request unmergeable. Please resolve the merge conflicts. |
Might just do |
As other reviewers already mentioned, asserts in inline const blocks work now and should be used. @rustbot author |
1c393d3
to
40a75ef
Compare
@joshtriplett @the8472 I switched to inline const blocks as requested. |
@rustbot label -S-waiting-on-author +S-waiting-on-review |
@scottmcm Old docs for the methods mentioned that in future any instantiations with Also, we can always remove checks later if we wish because it would not be breaking changes. |
Where bound would stop you writing |
You can then have a separate implementation for N = 0. Assuming the trait solver can then figure out that for each N there exists exactly one implementation. |
The full-fat solution to working with generic instantiation errors is some sort of static if (C++ calls it "constexpr if"), e.g. static if N != 0 {
foo.as_chunks::<N>() // ...
} else {
// ...
} where each arm of the static if is only instantiated if the controlling condition (which is constant evaluated) says to take that arm. With arbitrary requirements and conditions, we probably can't prevent instantiation errors (post-mono errors), but we could probably utilize pattern restricted types to effect here (i.e. instead of |
☔ The latest upstream changes (presumably #111453) made this pull request unmergeable. Please resolve the merge conflicts. |
Methods updated: * `array_windows` * `array_chunks` * `array_chunks_mut` * `as_chunks` * `as_chunks_mut` * `as_chunks_unchecked` * `as_rchunks` * `as_rchunks_mut` * `as_chunks_unchecked_mut` I implemented this using compile time assertions. Example compilation error: ``` > rustc +stage1 .\improper_array_windows.rs --crate-type=rlib error[E0080]: evaluation of `core::slice::<impl [u32]>::array_windows::<0>::{constant#0}` failed --> J:\rust_lang\library\core\src\slice\mod.rs:1381:13 | 1381 | assert!(N != 0, "window size must be non-zero"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'window size must be non-zero', J:\rust_lang\library\core\src\slice\mod.rs:1381:13 | = note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) note: the above error was encountered while instantiating `fn core::slice::<impl [u32]>::array_windows::<0>` --> .\improper_array_windows.rs:4:14 | 4 | for _ in s.array_windows::<0>(){ | ^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to previous error ``` I also added doctests and adjusted documentation. Relates: rust-lang#74985 rust-lang#75027
40a75ef
to
7bb2c00
Compare
I would like to merge this PR already because it just gather merge conflicts as it stays. So I like to address all concerns in a single place:
TLDR: We cannot broke any valid code by adding this, we can always remove this later and it would prevent breaking more code if we keep this behaviour. |
I don't feel comfortable unilaterally approving this. cc @rust-lang/libs-api |
Let's FCP this perhaps? @rfcbot fcp merge |
Team member @BurntSushi has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns:
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! See this document for info about what commands tagged team members can give me. |
@rfcbot concern monomorphization errors
I don't agree generally. I don't think we should be litigating whether we want to start introducing more monomorphization errors in this PR. It should probably be done as a separate decision, albeit I'm not sure by what process and by what team. |
I agree with #99471 (comment) and I would strongly prefer not to accept this PR. The following is fine and correct code; we do not need to make it fail to compile. fn f<const N: usize>() {
if N == 0 {
...
} else {
foo.as_chunks::<N>()...
}
}
fn main() {
f::<0>();
} |
@BurntSushi proposal cancelled. |
I'd like |
Maybe allow such compile errors for unstable functions/traits/structs until we implement bounds on constant generics? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks anyway for the PR! This was worth exploring.
Methods updated:
array_windows
array_chunks
array_chunks_mut
as_chunks
as_chunks_mut
as_chunks_unchecked
as_rchunks
as_rchunks_mut
as_chunks_unchecked_mut
I implemented this using compile time assertions.
Example compilation error:
I also added doctests and adjusted documentation.
Relates:
#74985
#75027