ReviewOS

home-lang/home

Parser: 'try expr' statement (Zig error-propagation, 154 kernel sites)

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

Problem

The parser does not accept try expr as a statement (Zig-style error propagation). Today this fails with error[EP0001]: Expected '{':

fn foo(): MyError!u32 {
    try self.do_thing()                    // ← fails
    try buffer.write(data)
    return 0
}

The diagnostic column points at the next token after try, suggesting the parser treats try as a control-flow keyword that requires a brace block (like Zig's try expr is sometimes confused with).

Background

In Zig, try expr is shorthand for:

expr catch |err| return err;

It's the dominant idiom for propagating errors up the call stack — used everywhere in idiomatic Zig code, including the home-os kernel.

Scope

$ grep -rn "^[ ]*try " home-os/kernel/src --include='*.home' | wc -l
154
$ grep -rln "^[ ]*try " home-os/kernel/src --include='*.home' | wc -l
13 files

Affected kernel files (sample)

  • kernel/src/fs/extents.home:256 (try self.insert_extent(&path, ...))
  • kernel/src/security/dm_verity.home:292 (try self.data_device.read_block(...))
  • kernel/src/fs/largefile.home:374 (try self.free_block(...))
  • ...10 more files

Expected behavior

try EXPR parses as a unary expression / statement. Lowers to:

EXPR catch |err| return err;

The expression form is also valid: let x = try foo() should parse as let x = foo() catch |err| return err.

Implementation hint

In the statement parser, add Tok.Try as a prefix that consumes the next expression and wraps it in a Try/Catch-and-return AST node. Same in the expression parser for let x = try ... form.

The lowering needs the function's error-union return type to know what to return in the catch arm — typecheck-time concern, not parser. Parser just produces the AST.

Tests

Positive:

  • try foo() (statement)
  • let x = try foo() (expression)
  • try self.method(args) (chain)
  • Inside a function with explicit error-union return — should typecheck

Regression:

  • Existing try usages (if any) keep working

Acceptance

  1. The 13 listed kernel files clear home-debug ast past the try line.
  2. Existing test suite green.

Sign in to comment on this issue.