Skip to content

Report precondition violation for <usize as SliceIndex>::get_unchecked in const-eval - #161616

Open
bardiharborow wants to merge 1 commit into
rust-lang:mainfrom
bardiharborow:get-unchecked-index-precondition-const-eval
Open

Report precondition violation for <usize as SliceIndex>::get_unchecked in const-eval#161616
bardiharborow wants to merge 1 commit into
rust-lang:mainfrom
bardiharborow:get-unchecked-index-precondition-const-eval

Conversation

@bardiharborow

@bardiharborow bardiharborow commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

The precondition check was gated on check_language_ub, which is disabled in const-eval and Miri, on the grounds that the assume below it is language UB that the interpreter will catch anyway. It does catch it, but only as "assume called with false", which says nothing about what the caller did wrong.

This PR instead gates the check on check_library_ub, matching get_unchecked_mut, so that the interpreter reports the violated precondition.

Fixes #161611

@rustbot label +A-const-eval +A-diagnostics +A-slice +T-libs +requires-nightly

@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 Aug 23, 2026
@rustbot

rustbot commented Aug 23, 2026

Copy link
Copy Markdown
Collaborator

r? @wesleywiser

rustbot has assigned @wesleywiser.
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 75 candidates
  • Random selection from 19 candidates

@rustbot rustbot added A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-diagnostics Area: Messages for errors, warnings, and lints A-slice Area: `[T]` requires-nightly This issue requires a nightly compiler in some way. When possible, use a F-* label instead. labels Aug 23, 2026
@bardiharborow bardiharborow changed the title Report precondition violations for <usize as SliceIndex>::get_unchecked in const-eval Report precondition violation for <usize as SliceIndex>::get_unchecked in const-eval Aug 23, 2026

@teor2345 teor2345 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.

Thanks, looks like a straightforward fix!

View changes since this review

Comment thread library/core/src/slice/index.rs Outdated
@rustbot

rustbot commented Sep 5, 2026

Copy link
Copy Markdown
Collaborator

Any special-casing of Miri in the standard library requires review.

cc @rust-lang/miri

Comment thread library/core/src/slice/index.rs Outdated
@RalfJung

RalfJung commented Sep 5, 2026

Copy link
Copy Markdown
Member

@bors squash

@rust-bors

This comment has been minimized.

…ed` in const-eval

* Add a test for the diagnostics on `<usize as SliceIndex>::get_unchecked` in const-eval
* Report a precondition violation for `<usize as SliceIndex>::get_unchecked` in const-eval

The precondition check was gated on `check_language_ub`, which is disabled in const-eval and Miri, on the grounds that the `assume` below it is language UB that the interpreter will catch anyway. It does catch it, but only as "`assume` called with `false`", which says nothing about what the caller did wrong.

This commit instead gates the check on `check_library_ub`, matching `get_unchecked_mut`, so that the interpreter reports the violated precondition.
* fixup! Report a precondition violation for `<usize as SliceIndex>::get_unchecked` in const-eval
* tweak comment

Co-authored-by: Ralf Jung <post@ralfj.de>
@rust-bors

rust-bors Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

🔨 4 commits were squashed into 3974528.

@rust-bors
rust-bors Bot force-pushed the get-unchecked-index-precondition-const-eval branch from 2a40480 to 3974528 Compare September 5, 2026 07:33
@RalfJung

RalfJung commented Sep 5, 2026

Copy link
Copy Markdown
Member

Makes sense, thanks :)
@bors r+ rollup

@rust-bors

rust-bors Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 3974528 has been approved by RalfJung

It is now in the queue for this repository.

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

Reason for tree closure: Linux x64 EC2 instances are not starting

@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 Sep 5, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 5, 2026
…precondition-const-eval, r=RalfJung

Report precondition violation for `<usize as SliceIndex>::get_unchecked` in const-eval

The precondition check was gated on `check_language_ub`, which is disabled in const-eval and Miri, on the grounds that the `assume` below it is language UB that the interpreter will catch anyway. It does catch it, but only as "`assume` called with `false`", which says nothing about what the caller did wrong.

This PR instead gates the check on `check_library_ub`, matching `get_unchecked_mut`, so that the interpreter reports the violated precondition.

Fixes rust-lang#161611
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-diagnostics Area: Messages for errors, warnings, and lints A-slice Area: `[T]` requires-nightly This issue requires a nightly compiler in some way. When possible, use a F-* label instead. 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-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

6 participants