check array literals (bare, typed, sized) #68

Merged
jacksonopp merged 2 commits from us56/check-array-list-literals into main 2026-09-17 19:18:56 +00:00
Owner

Closes #56.

Type-checks Expr::ArrayLiteral's three fields:

  • element_type when present is the source of truth — every element must match it (ArrayTypeMismatch otherwise).
  • Otherwise every element must agree with the first element's inferred type (bare [1, 2, 3]).
  • size when present must type-check to Int (InvalidArraySize otherwise) and must match the literal's actual element count (ArraySizeMismatch otherwise) — checked independently from the element-type check, so a literal that's wrong on both counts reports both.

As the issue's own "Dependency" note flags: there's no TyKind variant yet for a generic container instantiation, so the result type today is just the element type itself (Ty::int, etc.), not a real List(T)/sized-array Ty. That's deferred along with #6's TypeExpr::Generic resolution question, not solved here.

Also includes a mechanical, unrelated-but-prerequisite refactor: every check_* function's parameters were reordered to a consistent (db, scope, diagnostics, ...construct-specific) shape (previously diagnostics was last with no particular reason) — done first so the new check_array_literal/check_array_size/array_members_contains_mismatch functions could follow the same convention from the start.

Test plan

  • cargo test --workspace (201 + 229 + 27 passing; 10 new tests for check_array_literal covering bare/typed/sized, matching/mismatched elements, empty arrays, wrong size, wrong size + wrong type together, and non-Int size)
  • cargo clippy --workspace --all-targets --all-features
Closes #56. Type-checks `Expr::ArrayLiteral`'s three fields: - `element_type` when present is the source of truth — every element must match it (`ArrayTypeMismatch` otherwise). - Otherwise every element must agree with the first element's inferred type (bare `[1, 2, 3]`). - `size` when present must type-check to `Int` (`InvalidArraySize` otherwise) and must match the literal's actual element count (`ArraySizeMismatch` otherwise) — checked independently from the element-type check, so a literal that's wrong on both counts reports both. As the issue's own "Dependency" note flags: there's no `TyKind` variant yet for a generic container instantiation, so the result type today is just the element type itself (`Ty::int`, etc.), not a real `List(T)`/sized-array `Ty`. That's deferred along with #6's `TypeExpr::Generic` resolution question, not solved here. Also includes a mechanical, unrelated-but-prerequisite refactor: every `check_*` function's parameters were reordered to a consistent `(db, scope, diagnostics, ...construct-specific)` shape (previously `diagnostics` was last with no particular reason) — done first so the new `check_array_literal`/`check_array_size`/`array_members_contains_mismatch` functions could follow the same convention from the start. ## Test plan - [x] `cargo test --workspace` (201 + 229 + 27 passing; 10 new tests for `check_array_literal` covering bare/typed/sized, matching/mismatched elements, empty arrays, wrong size, wrong size + wrong type together, and non-`Int` size) - [x] `cargo clippy --workspace --all-targets --all-features`
check array/list literals
All checks were successful
CI / check (pull_request) Successful in 3m13s
936c2e2c5b
clean up stray comment
All checks were successful
CI / check (pull_request) Successful in 3m14s
f6837a9dc1
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
jacksonopp/twill!68
No description provided.