ReviewOS

home-lang/home

Parser: error-union return type 'ErrorSet!T' / '!T' (Zig idiom)

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

Problem

The parser does not accept ErrorSet!T error-union types as function return types. Today this fails with error[EP0001]: Expected '{':

pub fn check_kernel_buffer(ptr: usize, size: usize, write: bool) UsercopyError!void {
    ...
}

Column points at !. The parser stops at UsercopyError, treats it as a complete type, then sees !void { and demands { (the function body) — the ! is mis-parsed.

Background

In Zig, ErrorSet!T denotes a type that is either an error from the set or a value of type T. The default error set form !T (no explicit error set) is also valid.

Affected kernel files (sample)

  • kernel/src/security/hardened_usercopy.home:159 (UsercopyError!void)
  • many others using Error!T patterns — full count requires another grep but easily 30+ sites kernel-wide

Expected behavior

In type position (function return, struct field, variable annotation, type alias), accept:

ErrorSet!T            // explicit error set
!T                    // anonymous error set
*ErrorSet!T           // optionally combined with prefix-position type forms

Lowering: typecheck-time, parser produces an ErrorUnionType AST node with error_set and payload slots.

Implementation hint

In parseTypeAnnotation (or wherever return types are parsed), after consuming an identifier that looks like a type name, peek for !:

  • If next is !, consume it, parse another type expression as the payload, return Type.ErrorUnion{ .error_set = first, .payload = second }.
  • Same for the anonymous form: if the type position starts with !, parse !T directly.

Tests

Positive:

  • fn foo(): MyError!void { ... }
  • fn foo(): MyError!u32 { ... }
  • fn foo(): !u32 { ... } (anonymous error set)
  • let x: MyError!u32 = bar();

Regression:

  • Existing types keep working
  • ! as boolean-not operator continues to work

Acceptance

  1. hardened_usercopy.home and other affected files clear home-debug ast past the !T return-type point.
  2. Existing test suite green.

Sign in to comment on this issue.