Repository navigation
Slice patterns over a Vec, a slice or a borrowed array bind element views and lower; no more invalid MIR (§9.7, #1389) - #1578
Merged
ehartford merged 1 commit intoSep 24, 2026
Conversation
…iews and lower; no more invalid MIR (§9.7, #1389) `match v: [first, ..] => first` with `v: &Vec[i32]` passed Sema and then failed: "invalid MIR before codegen: use rvalue does not resolve to a concrete MIR type" (and "pattern kind 113 has no match-test lowering" once #1405 made the test path loud). `let [first, ..] = v else: return -1` failed the same way. A borrowed array (`match a: [x, y, z]` over `&[3]i32`) and a slice (`[]i32`) failed too. Root causes: - SemaCheck.check_pattern (NK_PAT_SLICE) took the element type only from a direct TY_ARRAY or TY_SLICE subject. A `&Vec[T]`, `Vec[T]` or `&[N]T` subject left every binding typed 0, silently, with no diagnostic. - MirLower.lower_pattern_match had a length test only for a direct array. MirLower.lower_pattern bound elements with `new_field_place(scrutinee, i)` on the unshaped subject (a reference, for `&Vec`) and defaulted the element type to i32 (`var sp_elem_ty = self.sema.ty_i32`), the invalid MIR in the report. Fix (D27: element access observes). An owned fixed-size array is still taken apart by value. Any other sequence is observed in place, and each named element binds as a view `&T`: - Sema: slice_pattern_seq_elem gives the element type of a borrowed array, a slice, or a Vec (owned or borrowed). Its bindings are `&T`. A subject that is not a sequence is an error ("a slice pattern requires an array, slice or Vec subject, found 'i32'"). exh_slice_always_matches sees through the borrow, so a borrowed array is decided by its length the same way. - MIR: a slice or Vec is tested at run time, `len == n` without a rest and `len >= n` with one. The length comes from RK_LEN on a copied slice view (RK_LEN does not see through a deref, see lower_sequence_place) or from VEC_LEN. lower_slice_pattern_views binds head elements by index and tail elements at len - k, through an RK_REF of the indexed element (slice), VEC_GET_REF (Vec), or an RK_REF of the array element (borrowed array, indices known). The i32 default is gone: a subject the binding path cannot shape is loud. - A named rest over a slice or Vec keeps its loud error ("slice rest binding for dynamic slices is not implemented yet"). §9.7 binds `rest` to the remaining count for fixed-size arrays and uses it as a slice in its match example, so which one a dynamic rest binds is not decided. Matrix (stage1 before -> after; every cell was invalid MIR or a missing test lowering before): &Vec[i32] match [first, ..] / _ -> 4 -1 &Vec[i32] let [first, ..] = v else -> 4 -1 Vec[i32] match [a, .., z] / [_] / _ -> "1 4", "one" &[3]i32 match [x, y, z]; let [p, q, r] -> 6, 1 2 3 &Vec[str] match [x, .., y] -> 3 ab cd (views, no moves) []i32 match [a, b] / [a, ..] / [] / _ -> 2 -2 9 &Vec[i32] match [] / [_] / _ -> empty one many let [first, .., last] = &[6, 0, 1] -> 6 1 The fixture runs with `leak count=0` under the debug allocator. The owned-array fixtures (behav_let_else_slice_rest_binding, err_let_else_slice_rest_unsupported) pass unchanged. `stage1 check src/main.w`: ok. Filed: a match that covers every length of a slice or Vec (`[]`/`[_]`/`[_, ..]`) still demands `_`, so the §9.7 example is rejected (#1533). The engine treats dynamic slices as open, by design since #1388. Fixtures: behav_1389_slice_pattern_sequences, err_1389_slice_pattern_non_sequence, err_1389_vec_rest_binding_unsupported.
Contributor
Author
|
Stack #1582 battery (seed-driven, new seed, cold c_import cache, with :drop-audit/:move-audit) on main 02301f8 + #1571 → … → #1581 (head dca1a8f; every PR head is a tested commit): build ok, :fixpoint ok, :drop-audit ok, :move-audit ok, :test ok (no reds), :test-green ok, :last-green ok. Merges cleanly into main. Merge the stack: |
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.
match v: [first, ..] => firstwithv: &Vec[i32]passed Sema and thenfailed: "invalid MIR before codegen: use rvalue does not resolve to a
concrete MIR type" (and "pattern kind 113 has no match-test lowering" once
#1405 made the test path loud).
let [first, ..] = v else: return -1failed the same way. A borrowed array (
match a: [x, y, z]over&[3]i32) and a slice ([]i32) failed too.Root causes:
direct TY_ARRAY or TY_SLICE subject. A
&Vec[T],Vec[T]or&[N]Tsubject left every binding typed 0, silently, with no diagnostic.
MirLower.lower_pattern bound elements with
new_field_place(scrutinee, i)on the unshaped subject (a reference, for&Vec) and defaulted theelement type to i32 (
var sp_elem_ty = self.sema.ty_i32), the invalidMIR in the report.
Fix (D27: element access observes). An owned fixed-size array is still
taken apart by value. Any other sequence is observed in place, and each
named element binds as a view
&T:a slice, or a Vec (owned or borrowed). Its bindings are
&T. A subjectthat is not a sequence is an error ("a slice pattern requires an array,
slice or Vec subject, found 'i32'"). exh_slice_always_matches sees
through the borrow, so a borrowed array is decided by its length the
same way.
len == nwithout a rest andlen >= nwith one. The length comes from RK_LEN on a copied slice view(RK_LEN does not see through a deref, see lower_sequence_place) or from
VEC_LEN. lower_slice_pattern_views binds head elements by index and tail
elements at len - k, through an RK_REF of the indexed element (slice),
VEC_GET_REF (Vec), or an RK_REF of the array element (borrowed array,
indices known). The i32 default is gone: a subject the binding path
cannot shape is loud.
binding for dynamic slices is not implemented yet"). §9.7 binds
restto the remaining count for fixed-size arrays and uses it as a slice in
its match example, so which one a dynamic rest binds is not decided.
Matrix (stage1 before -> after; every cell was invalid MIR or a missing
test lowering before):
&Vec[i32] match [first, ..] / _ -> 4 -1
&Vec[i32] let [first, ..] = v else -> 4 -1
Vec[i32] match [a, .., z] / [] / _ -> "1 4", "one"
&[3]i32 match [x, y, z]; let [p, q, r] -> 6, 1 2 3
&Vec[str] match [x, .., y] -> 3 ab cd (views, no moves)
[]i32 match [a, b] / [a, ..] / [] / _ -> 2 -2 9
&Vec[i32] match [] / [] / _ -> empty one many
let [first, .., last] = &[6, 0, 1] -> 6 1
The fixture runs with
leak count=0under the debug allocator. Theowned-array fixtures (behav_let_else_slice_rest_binding,
err_let_else_slice_rest_unsupported) pass unchanged.
stage1 check src/main.w: ok.Filed: a match that covers every length of a slice or Vec
(
[]/[_]/[_, ..]) still demands_, so the §9.7 example is rejected(#1533). The engine treats dynamic slices as open, by design since #1388.
Fixtures: behav_1389_slice_pattern_sequences,
err_1389_slice_pattern_non_sequence, err_1389_vec_rest_binding_unsupported.
Stack #1582, layer 8/11. Draft until the batch battery is green (D56).