Skip to content

<usize as SliceIndex<[T]>>::get_unchecked reports "assume called with false" in const-eval instead of the out-of-bounds precondition message #161611

Description

@bardiharborow

Code

#![feature(const_index, const_trait_impl)]

const ARRAY: [u32; 5] = [1, 2, 3, 4, 5];
const OUT_OF_BOUNDS: &u32 = unsafe { ARRAY.get_unchecked(9) };

Current output

error[E0080]: `assume` called with `false`
 --> src/lib.rs:4:38
  |
4 | const OUT_OF_BOUNDS: &u32 = unsafe { ARRAY.get_unchecked(9) };
  |                                      ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `OUT_OF_BOUNDS` failed here

For more information about this error, try `rustc --explain E0080`.
error: could not compile `playground` (lib) due to 1 previous error

Desired output

error[E0080]: evaluation panicked: unsafe precondition(s) violated: slice::get_unchecked requires that the index is within the slice
              
              This indicates a bug in the program. This Undefined Behavior check is optional, and cannot be relied on for safety.
 --> src/lib.rs:4:38
  |
4 | const OUT_OF_BOUNDS: &u32 = unsafe { ARRAY.get_unchecked(9) };
  |                                      ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `OUT_OF_BOUNDS` failed here

For more information about this error, try `rustc --explain E0080`.
error: could not compile `playground` (lib) due to 1 previous error

Rationale and extra context

No response

Other cases

#![feature(const_index, const_trait_impl)]

const MUT: () = unsafe {
    let mut array: [u32; 5] = [1, 2, 3, 4, 5];
    let _ = array.get_unchecked_mut(9);
};

correctly errors with:

error[E0080]: evaluation panicked: unsafe precondition(s) violated: slice::get_unchecked_mut requires that the index is within the slice
              
              This indicates a bug in the program. This Undefined Behavior check is optional, and cannot be relied on for safety.
 --> src/lib.rs:5:13
  |
5 |     let _ = array.get_unchecked_mut(9);
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `MUT` failed here

For more information about this error, try `rustc --explain E0080`.
error: could not compile `playground` (lib) due to 1 previous error

Rust Version

Nightly version: 1.100.0-nightly (2026-08-22 c54751567b19c4ceb08b)

Anything else?

Ref #143775

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-diagnosticsArea: Messages for errors, warnings, and lintsA-sliceArea: `[T]`D-confusingDiagnostics: Confusing error or lint that should be reworked.T-libsRelevant to the library team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions