ReviewOS

home-lang/home

Parser: 'if let Some(x) = expr { ... }' Rust pattern (85 sites in 8 kernel files)

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

Problem

The parser does not accept the Rust-style if let PATTERN = EXPR { body } form. Today the kernel pattern below fails with error[EP0001]: Expected ')' after binding name:

if let Some(ref mut sensor) = THERMAL.sensors[i] {
    sensor.update_temp(t)
}

if let Some(parent_id) = parent {
    update_parent(parent_id)
}

Background

Rust's if let is a syntactic shortcut for matching one specific pattern of an enum variant and binding its payload, with an optional else for the no-match case. Functionally equivalent to:

match EXPR {
    PATTERN => body,
    _ => {} // or `else_body`
}

The kernel was written in a Rust-influenced style and uses this pattern heavily for Option<T> unwrapping.

Scope

$ grep -rn "if let Some" home-os/kernel/src --include='*.home' | wc -l
85
$ grep -rln "if let Some" home-os/kernel/src --include='*.home' | wc -l
8 files

Affected kernel files

  • kernel/src/power/thermal_x86.home:382
  • kernel/src/power/battery.home:357
  • kernel/src/power/device_pm.home:266
  • kernel/src/drivers/bluetooth/hci.home:466
  • kernel/src/arch/arm64/rpi/devicetree.home:289
  • kernel/src/power/cpufreq_x86.home (multiple sites)
  • kernel/src/arch/arm64/rpi/emmc.home (multiple sites)
  • 1 more

Expected behavior

Parse if let PATTERN = EXPR { body } [else { else_body }] as syntactic sugar for a match expression on EXPR with one explicit arm and a wildcard fallback:

  • if let Some(x) = expr { body }
  • if let Some(ref mut x) = expr { body } (Rust's ref mut borrow modifier — acceptable if treated as binding declaration; actual mutability semantics can be future work)
  • if let .Variant(payload) = expr { body } (using Home's existing dot-prefixed enum-variant pattern syntax)
  • if let Some(x) = expr { body } else { fallback }

Implementation hint

In statement-context dispatch, after seeing if, peek for let. If next is let, dispatch to a new parseIfLetStatement that:

  1. Consumes let
  2. Parses a pattern using the existing parsePattern machinery (which already handles Some(x), .Variant(x), etc. for match arms)
  3. Expects =
  4. Parses the scrutinee expression
  5. Expects {, parses body block
  6. Optional else { ... }
  7. Lowers to match EXPR { PATTERN => body, _ => else_body or {} } AST node — reuses existing match-arm machinery so codegen needs no changes.

Note: ref mut in the binding can be parsed and ignored for now (or attached as a binding modifier flag) — full borrow semantics are downstream.

Tests

Positive:

  • if let Some(x) = foo() { use(x) }
  • if let Some(x) = foo() { use(x) } else { handle_none() }
  • if let .Ok(v) = result { use(v) }
  • Nested: if let Some(a) = x { if let Some(b) = y { use(a, b) } }

Regression:

  • if cond { ... } keeps working
  • if let pattern = expr does NOT match if let_var: T = expr (variable named "let" — there shouldn't be one, but be careful with the disambiguation)

Acceptance

  1. The 5+ listed kernel files clear home-debug ast past the if let line.
  2. Existing test suite green.

Sign in to comment on this issue.