Skip to content

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 into
fix-1366-tuple-rest-patternsfrom
fix-1389-slice-pattern-vec
Sep 24, 2026
Merged

ehartford merged 1 commit into
fix-1366-tuple-rest-patternsfrom
fix-1389-slice-pattern-vec

Conversation

@ehartford

@ehartford ehartford commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

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.

Stack #1582, layer 8/11. Draft until the batch battery is green (D56).

…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.
@ehartford
ehartford added this pull request to stack #1582 September 23, 2026 21:37
@ehartford ehartford changed the title fix 1389 slice pattern vec Slice patterns over a Vec, a slice or a borrowed array bind element views and lower; no more invalid MIR (§9.7, #1389) Sep 23, 2026
@ehartford

Copy link
Copy Markdown
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: gh stack merge 1581 --squash.

@ehartford
ehartford marked this pull request as ready for review September 23, 2026 22:04
@ehartford
ehartford merged commit 6b05e27 into main Sep 24, 2026
@ehartford
ehartford deleted the fix-1389-slice-pattern-vec branch September 24, 2026 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant