ReviewOS

home-lang/home

Typechecker: int/float literal context propagation in assignment / if-expression / array-literal

#21
Closed glennmichael123 opened this 24 days ago · 0 comments
24 days ago

Context

2026-05-04-pm home-os audit triage identified a single typechecker rule gap as the root cause for ~26 typecheck-failing kernel modules across 4 distinct error patterns:

  • 17 × `Type mismatch (expected void, found [int]/[void]/[Struct])` on `const X: [N]T = [a, b, c, ...]` declarations
  • 7 × `if-expression branches have different types` (one branch is a bare literal, other is typed)
  • 1 × `Array elements must all have the same type` (literal mixed with typed expressions)
  • ~3 × misc cases like `*u64 = 0` (deref-assign), `let X: usize = 256` at module scope

Common root cause: integer/float literals are being typed in isolation rather than absorbing their contextual destination type.

Examples

```home // Pattern 1: array literal into typed const const NICE_TO_WEIGHT: [40]u32 = [88761, 71755, 56483, ...] // Currently fails: typechecker assigns [int] to literal, refuses to coerce to [40]u32

// Pattern 4: if-expr branches var clamped: u32 = if message_len > 1000 { 1000 } else { message_len } // Currently fails: 1000 typed as int, message_len as u32, branches diverge

// Pattern 9: mixed array literal var new_shape: [4]u64 = [1, input.shape[0], input.shape[1], input.shape[2]] // Currently fails: literal 1 typed as int, others as u64

// Pattern 3 sub-cases let RX_BUFFER_SIZE: usize = 256 // module-scope; usize not treated as numeric *last_ptr = 0 // *u64 deref-assign; 0 not coerced to u64 ```

Proposal

When typing an integer or float literal, the typechecker should look up the contextual destination type before defaulting to the literal's intrinsic type. Contexts that should propagate:

  1. Right-hand side of an assignment (incl. deref-assignment `*ptr = expr`) — type of LHS
  2. Initializer of a typed declaration (`var x: T = expr`, `const x: T = expr`, `let x: T = expr`) — declared type
  3. Branch of an if-expression with a sibling branch of known type — sibling type
  4. Element of a typed array literal (`[N]T` context) — element type T
  5. Argument in a function call — parameter type

If the literal fits the destination type (no overflow / no signedness loss for unsigned destinations / etc.), coerce. Otherwise emit the existing error.

Why this matters

Highest single-fix leverage in the post-audit triage — ~26 home-os kernel modules become typecheck-OK in one PR. Quoting the scout report: "the single biggest leverage point is int/float literal context propagation."

Reference

  • Triage report: scout agent on the 2026-05-04-pm audit (50 typecheck-failing files)
  • Audit TSV: `~/Desktop/home-os-kernel-audit-2026-05-04-pm.tsv`

Sign in to comment on this issue.