check array literals against a target Array(T, N) annotation #77

Merged
jacksonopp merged 1 commit from us67/check-array-comptime-size-against-annotation into main 2026-09-30 02:25:14 +00:00
Owner

Closes #67.

Threads an expected: Option<Ty<'db>> hint through check_let → check_expr → check_array_literal, so a bare array literal ([1, 2, 3]) can be checked against a sized Array(T, N) target instead of always inferring List(T).

  • check_let passes its annotation's resolved type down as expected.
  • The bare-literal arm of check_array_literal matches expected against Instantiated { name: Array, args: [Type(_), Value(Literal::Int(_))], .. } and, when it matches, checks element count and element type against it (ArraySizeMismatch/ArrayTypeMismatch), reusing the same instantiation path as the already-written [N]T[...] sized-literal form.
  • Any other expected shape falls back to the existing List(T) inference unchanged.
  • Removed the unused expected param from check_type_expr — it resolves a TypeExpr's syntax into a Ty and never had an ambiguity to resolve, unlike check_expr.

New tests: sized-literal-vs-matching/mismatched annotation, bare-literal-vs-matching/mismatched annotation.

#67's remaining bullet (arbitrary comptime-evaluated sizes, e.g. Array(Int, computeSize())) is out of scope here — literals-only was already the ceiling set by #66/#71's AST/checker types. Split out to #76.

Closes #67. Threads an `expected: Option<Ty<'db>>` hint through `check_let` → `check_expr` → `check_array_literal`, so a bare array literal (`[1, 2, 3]`) can be checked against a sized `Array(T, N)` target instead of always inferring `List(T)`. - `check_let` passes its annotation's resolved type down as `expected`. - The bare-literal arm of `check_array_literal` matches `expected` against `Instantiated { name: Array, args: [Type(_), Value(Literal::Int(_))], .. }` and, when it matches, checks element count and element type against it (`ArraySizeMismatch`/`ArrayTypeMismatch`), reusing the same instantiation path as the already-written `[N]T[...]` sized-literal form. - Any other `expected` shape falls back to the existing `List(T)` inference unchanged. - Removed the unused `expected` param from `check_type_expr` — it resolves a `TypeExpr`'s syntax into a `Ty` and never had an ambiguity to resolve, unlike `check_expr`. New tests: sized-literal-vs-matching/mismatched annotation, bare-literal-vs-matching/mismatched annotation. #67's remaining bullet (arbitrary comptime-evaluated sizes, e.g. `Array(Int, computeSize())`) is out of scope here — literals-only was already the ceiling set by #66/#71's AST/checker types. Split out to #76.
check bare/sized array literals against a target Array(T, N) annotation
All checks were successful
CI / check (pull_request) Successful in 3m43s
ec253b98c7
jacksonopp deleted branch us67/check-array-comptime-size-against-annotation 2026-09-30 02:25:14 +00:00
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!77
No description provided.