ReviewOS

home-lang/home

Parser: switch-as-expression in initializer position (6 kernel files blocked)

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

Problem

The parser does not accept switch (or match) as an expression — i.e. on the right-hand side of a let/const/var binding or as a struct-field initializer. Today the kernel pattern below fails with Expected '}' after struct fields:

const sev_str = switch (self.severity) {
    .INFO => "[INFO] ",
    .WARNING => "[WARN] ",
    .ERROR => "[ERROR] ",
    .CRITICAL => "[CRIT] ",
    .FATAL => "[FATAL] ",
};

The parser appears to start parsing the right-hand side, sees the leading switch (...), and then mis-interprets the arm body as a struct-literal — the => is treated as a malformed field separator, so it bails when it can't find } "after struct fields".

Affected kernel files

All in home-lang/home-os@main:

  • kernel/src/fs/fsck.home:174
  • kernel/src/fs/sparse.home:91
  • kernel/src/fs/largefile.home:156
  • kernel/src/security/dm_verity.home:164
  • kernel/src/security/keyring.home:189
  • kernel/src/security/module_signing.home:94

(Bucket "Expected '}' after struct fields", 6/66 of remaining kernel parse failures per 2026-05-07 audit.)

Expected behavior

switch should be valid in expression position. Each arm body is an expression; the whole construct evaluates to the chosen arm's value. The parser should accept:

  • let x = switch (y) { .A => 1, .B => 2 };
  • const cfg = .{ .timeout = switch (mode) { .fast => 10, .slow => 100 } };
  • return switch (state) { .ok => 0, .err => 1 };

Statement-position switch should continue to work unchanged.

Implementation hint

Most likely the fix is in parsePrimaryExpression (or whatever entry point handles RHS expressions in parseLetDecl/parseConstDecl/parseVarDecl/struct-literal field values): add a Switch/Match token branch that parses the full arm list and returns a SwitchExpr AST node. The typechecker should already join arm types via the existing common-supertype logic (the same mechanism if-as-expression uses).

Tests to add

  • tests/diagnostics/cases/switch_expression_in_let.home (positive)
  • tests/diagnostics/cases/switch_expression_in_struct_field.home (positive)
  • tests/diagnostics/cases/switch_expression_arm_type_mismatch.home (negative)

Acceptance

  1. The 6 listed kernel files clear home-debug ast and home-debug check.
  2. New diagnostic snapshots committed.
  3. Existing 1106/1106 test suite still green.

Sign in to comment on this issue.