Implement slice construction from array literals and Vec::as_slice#158
Draft
coord-e wants to merge 3 commits into
Draft
Implement slice construction from array literals and Vec::as_slice#158coord-e wants to merge 3 commits into
coord-e wants to merge 3 commits into
Conversation
Handle `Rvalue::Aggregate(Array, ...)` by building a Seq<T> model (Box<Array<Int,T>>, Box<Int>) whose array component is the store-fold of all literal elements and whose length is the static count N. Also handle `mir::Const::Ty` (type-level constants, e.g. array length N) by extracting the scalar integer and delegating to `const_value_ty`. Map `[T; N]` to `model::Seq<T::Ty>` in `std.rs`, matching `[T]` and `Vec<T>`. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013oVufAKCMzsLRnQ8F58PdP
6797bf5 to
42ed8eb
Compare
Support the MIR operations needed to turn `[T; N]` into `&[T]` and index it: - `Rvalue::CopyForDeref`: pass-through to the place type (coerce-for-deref). - `Rvalue::Cast(PointerCoercion::Unsize)`: identity coercion since `[T; N]` and `[T]` share the same `Seq<T>` model. Fix a bug in `remove_bounds_check_setup` where the slice receiver local was incorrectly NOP'd when `PtrMetadata` was applied directly to it. The receiver must remain live for the reconstructed `Index::index` call. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013oVufAKCMzsLRnQ8F58PdP
21264d7 to
e77e5ab
Compare
Specify the postcondition `*result == *vec` for the three ways a `Vec<T>` can be coerced to `&[T]`: `as_slice`, `Deref::deref`, and `AsRef::as_ref`. This lets the refinement checker treat all three as preserving the Seq model. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013oVufAKCMzsLRnQ8F58PdP
e77e5ab to
86e1be8
Compare
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.
Builds on the
codex/slices-basicwork (merged into this branch) to add support for constructing&[T]slices from array literals ([T; N]) and fromVec<T>viaas_slice, then indexing through them with full refinement-type tracking.Changes
src/analyze/basic_block.rsAggregateKind::Array: Build aSeq<T>model ((Box<Array<Int,T>>, Box<Int>)) from array literal elements by foldingstorecalls to pin each element at its index, then construct the length asBox<N>.Rvalue::CopyForDeref: Delegate toenv.place_type(same asUse).Rvalue::Len: Extract length by projecting field 1 of the Seq tuple and dereffing the box.PointerCoercion::Unsize: Identity pass-through for&[T; N] → &[T](both share the same Seq model).mir::Const::Ty: Handle type-level constants (const generics) by extracting their scalar value.src/refine/env.rsPath::Indexvariant: Projects an element out of a Seq-modeled slice. Gets the innerArray<Int, T>, introduces an existential for the element value, and constrains it witharr.select(idx).src/analyze/reconstruct_slice_indexing.rsremove_bounds_check_setupwas incorrectly adding the slice receiver local tolowered_localswhenPtrMetadatawas applied directly to the slice reference (rather than an intermediate raw-pointer temporary). This caused the receiver's assignment to be NOP'd in-block, making it appear live-in to the entry block. The resulting synthetic unit parameter carried a refinement referencingRefinedTypeVar::Value, which panicked whenvalue_varwasNone(empty tuple → singleton sort). Fix: skipraw_place.localwhen it equals the receiver local.std.rs[T; N]model:impl<T: Model, const N: usize> Model for [T; N]mapping toSeq<T::Ty>.Vec::as_sliceextern spec:#[ensures(*result == *vec)]— copies the Seq refinement from the Vec to the returned slice.Tests
10 new UI tests (5 pass / 5 fail):
pass/array_literal_1.rs[1i32,2,3], coerce to&[i32], asserts[0]==1pass/array_literal_2.rspass/array_literal_3.rs&mut [i32], write through slice, verify updated valuepass/array_literal_4.rs[&mut i32; 2], dereference through slicepass/vec_as_slice.rsas_slice, assert len and element valuesfail/array_literal_1.rsfail/array_literal_2.rsfail/array_literal_3.rsfail/array_literal_4.rs&mut→ Unsatfail/vec_as_slice.rsGenerated by Claude Code