Do not reborrow the operand of PtrMetadata - #242
Merged
Conversation
`<[T]>::len` is lowered to `PtrMetadata`. From `-C opt-level=1` upward rustc applies it directly to the `&mut [T]` local rather than through a shared reborrow, so `ReborrowVisitor::visit_operand` rewrote the operand into a fresh `&mut` reborrow. `analyze_assignment` then discarded that reborrow and took its own shared borrow of the referent, leaving the reborrow's prophecy unresolved: the synthetic local is absent from the MIR `DropPoints` was computed over, so nothing ever equated it to its current value. Since borrowing a place replaces its value with the prophecy, `*v` was havoc'd for the rest of the function and `if i < v.len()` no longer discharged the bound on the following `v[i]`. `PtrMetadata` only reads the length out of its operand, so skip the reborrow and let `analyze_assignment` borrow the referent itself. Closes #240 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017rubDn1kuLtPnuRoLkkXTj
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The targeted fix is consistent with metadata handling and is covered by paired regression tests.
Pull request overview
Fixes optimization-dependent over-rejection when reading mutable slice metadata by preventing an unnecessary mutable reborrow.
Changes:
- Exempts
PtrMetadataoperands from reborrowing. - Adds paired positive and negative regression tests at optimization level 2.
File summaries
| File | Description |
|---|---|
src/analyze/basic_block/visitor/reborrow.rs |
Skips reborrowing for PtrMetadata. |
tests/ui/pass/slice_len_guard_mut.rs |
Verifies guarded mutable-slice indexing succeeds. |
tests/ui/fail/slice_len_guard_mut.rs |
Ensures an incorrect assertion remains rejected. |
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 0
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #240.
<[T]>::lenis lowered toPtrMetadata. From-C opt-level=1upward rustc applies it directly to the&mut [T]local (_len = PtrMetadata(copy _v)) rather than through the shared reborrow it inserts at-C opt-level=0, soReborrowVisitor::visit_operandrewrote the operand into a fresh&mutreborrow.analyze_assignmentthen discarded that reborrow and took its own shared borrow of the referent, leaving the reborrow's prophecy unresolved: the synthetic local is absent from the MIRDropPointswas computed over, so nothing ever equated it to its current value. Since borrowing a place replaces its value with the prophecy,*vwas havoc'd for the rest of the function andif i < v.len()no longer discharged the bound on the followingv[i].PtrMetadataonly reads the length out of its operand, so this skips the reborrow and letsanalyze_assignmentborrow the referent itself — which it already does.Testing
Verified against Z3 5.0.0 and the COAR image CI pins.
The behavior matrix in the issue is now green in every cell:
-C opt-level=0,1,2,3,s,z× the&mut [i32]reader, the&[i32]reader, the&mut [i32]writer anddouble_all.This is an over-rejection fix, not a vacuity one — the unsafe variants stay rejected, and a genuine write-then-read verifies:
if i < v.len() { let x = v[i]; assert!(x == 0); }if i < v.len() { v[i] = 7; assert!(v[i] == 8); }let x = v[i];if i < v.len() { v[i] = 7; assert!(v[i] == 7); }cargo testpasses (328 UI tests, plus unit and doc tests);cargo fmt --checkandcargo clippy --all-targetsare clean.Tests added
A
pass/failpair at-C opt-level=2, so the suite exercises thePtrMetadata(copy <&mut>)shape that CI never saw before:tests/ui/pass/slice_len_guard_mut.rs—if i < v.len() { v[i] = 7; assert!(v[i] == 7); }tests/ui/fail/slice_len_guard_mut.rs— the same with the asserted value broken to8Generated by Claude Code