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

Make <[T]>::array_* methods fail to compile on 0 len arrays #99471

Commits on May 20, 2023

  1. Make <[T]>::array_* methods fail to compile on 0 len arrays

    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
    AngelicosPhosphoros committed May 20, 2023
    Configuration menu
    Copy the full SHA
    7bb2c00 View commit details
    Browse the repository at this point in the history