Problem
if (cond) a else b is supported as a top-level expression / statement, but NOT in argument position or as a struct-field initializer. Today this fails with error[EP0001]: Expected '{' after if condition:
serial.write_string(if (audit_cap_checks == 1) \"ON\" else \"OFF\")const snr_clamped = if (snr < 0.0) 0.0 else snr;return ExtentHeader{
.magic = EXTENT_MAGIC,
.depth = if (is_leaf) 0 else 1, // ← fails here
.generation = 0,
};The parser appears to commit to "this is an if-statement" when it sees if (, and then demands a brace block — instead of recognizing the expression context.
Affected kernel files
- kernel/src/fs/extents.home:49 (
.depth = if (is_leaf) 0 else 1in struct field initializer) - kernel/src/security/capabilities.home:410 (
f(if (x == 1) "A" else "B")in argument position) - kernel/src/iot/audio_processing.home:336 (
const x = if (c) a else b;in let initializer)
Expected behavior
if (cond) a else b (with mandatory else) should parse as an expression in any expression position:
- function-call argument:
f(if (c) a else b) - struct-field initializer:
.{ .x = if (c) 1 else 2 } - let/const/var initializer:
const x = if (c) a else b; - return:
return if (c) a else b; - nested in arithmetic:
x + (if (c) 1 else 0)
The arms must agree on type (or join via the existing common-supertype logic — same as switch-as-expression / ternary).
Note on else requirement
If the user writes if (c) a (no else) in expression position, that's a type error (the implicit "false" branch produces void) — emit a clean diagnostic. Statement-form if (c) stmt (no else) continues to work as today.
Implementation hint
The expression parser likely needs an if-expression entry point separate from the statement-form. Look at how switch-as-expression (recently shipped in #45) wires through primary() between if and match — apply the same pattern. The two arms should parse as expressions (not blocks) and join via the existing match/switch arm-type-join logic.
Tests
- Positive: each of the 5 contexts above
- Regression: existing if-statement (with or without else, with brace bodies) keeps working
- Negative:
let x = if (c) a;(no else) yields a clean type-error diagnostic at typecheck time
Acceptance
- The 3 listed kernel files clear
home-debug astpast the if-expression point. - Existing test suite green.