From 2243849855e6b5e61b6a367cdbb16d02b5f7d540 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Wed, 13 May 2026 20:19:03 +0900 Subject: [PATCH] lib+test: unify errors to Hare named-void tagged-union MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Five tags from ref/hare/errors/common.ha — invalid, noaccess, noentry, exists, unsupported — all !void. lib/io keeps eof/closed as plain void (no Hare analogue for ww's singleton-style done) and adds underread. Drops errors.equal/isnil and the old str-sentinel surface. --- lib/bufio/bufio.ww | 5 +++-- lib/errors/errors.ww | 47 +++++++++++++++++++----------------------- lib/io/io.ww | 15 +++++++++++--- test/wcc/700_e2e.c | 49 +++++++++++++++++++++++++++++++------------- 4 files changed, 71 insertions(+), 45 deletions(-) diff --git a/lib/bufio/bufio.ww b/lib/bufio/bufio.ww index 14b2b65f..d3aa8953 100644 --- a/lib/bufio/bufio.ww +++ b/lib/bufio/bufio.ww @@ -40,8 +40,9 @@ export fn readbyte(b: *buf) (i32 | void) = { // Distinct alias so `(str | linerr)` has two variant types the // tagged-union machinery can keep apart at the tag level. The error -// variant carries a short description; callers compare with errors.equal -// or just inspect by length. +// variant carries a short description; callers inspect by length. +// Placeholder until bufio graduates to io.stream + named-void tags +// (eof / closed / underread). type linerr = str; // readline — Hare-style fallible line read (name transliterated from diff --git a/lib/errors/errors.ww b/lib/errors/errors.ww index 9c42a576..03427258 100644 --- a/lib/errors/errors.ww +++ b/lib/errors/errors.ww @@ -1,30 +1,25 @@ -// errors — error type (a string) and a few sentinels. Plan 9 model: -// the empty string means OK, a non-empty string is the message. +// errors — domain-agnostic error types. Mirrors ref/hare/errors/. +// +// Named-void tagged-union variants, so `(T | errors.invalid | ...)` +// composes with every other module's error surface at the tag level. +// Per-domain errors (io.eof, io.closed, io.underread, strconv.overflow, +// ...) live in their own modules; this module ships only the generic +// conditions Hare's errors:: exports. +// +// Subset shipped today; add more from Hare's errors/common.ha as callers +// need them. -type error = str; +// A function was called with an invalid combination of arguments. +export type invalid = !void; -def eof: error = "eof"; -def underread: error = "short read"; -def underwrite: error = "short write"; -def closed: error = "closed"; -def invalid: error = "invalid argument"; -def noaccess: error = "permission denied"; -def noentry: error = "not found"; -def exists: error = "already exists"; +// The user does not have permission to use this resource. +export type noaccess = !void; -export fn isnil(e: error) bool = { - return e.len == 0; -}; +// An entry was requested which does not exist. +export type noentry = !void; -// equal — compare an error against a sentinel (or any other error). -// Pure byte equality. (Was `errors.is` before `is` became a keyword -// for tagged-union type-tests; rename matches bytes.equal / strings.equal.) -export fn equal(e: error, want: error) bool = { - if (e.len != want.len) { return false; }; - let i: i32 = 0; - for (i < e.len) { - if (e[i] != want[i]) { return false; }; - i += 1; - }; - return true; -}; +// An attempt was made to create a resource which already exists. +export type exists = !void; + +// The requested operation is not supported. +export type unsupported = !void; diff --git a/lib/io/io.ww b/lib/io/io.ww index 62ca039e..47ec6767 100644 --- a/lib/io/io.ww +++ b/lib/io/io.ww @@ -5,14 +5,23 @@ // value of read/write/close — Hare-shaped tagged unions instead of // errno-style integer sentinels. -// eof — read past the end of the stream. NAMED-void so it's a -// distinct variant tag from `void` (which would be "no result yet"). +// eof — read past the end of the stream. Hare uses the `done` +// singleton for EOF; ww doesn't have `done` yet so we ship a +// named-void variant tag. export type eof = void; // closed — operation attempted on a stream that has already been -// closed. NAMED-void; same shape, different tag. +// closed. ww-specific: Hare's io collapses this into the wider +// errors union, but our stream vtable has no handle-ownership +// semantics, so a distinct tag is honest. Named void. export type closed = void; +// underread — an I/O handle hit eof partway through a fixed-size +// read. Payload is the byte count actually delivered. Mirrors +// Hare's `io::underread = !size`; ww uses i32 because the +// underlying buffer-length type is i32 today. +export type underread = !i32; + export type stream = struct { ctx: *void, read: fn(s: *stream, buf: []u8) (i32 | eof | closed), diff --git a/test/wcc/700_e2e.c b/test/wcc/700_e2e.c index b5035afb..455696f6 100644 --- a/test/wcc/700_e2e.c +++ b/test/wcc/700_e2e.c @@ -1252,33 +1252,54 @@ static const struct row rows[] = { " };\n" " return 0;\n" "};", 7 }, - /* errors.equal — sentinel comparison through a (T | error) union. - * Sets up two errors, dispatches each, and confirms the matching - * sentinel detection. */ + /* errors named-void tags — dispatch through a (T | tag | tag) + * union, one variant per error condition. Replaces the old + * errors.equal sentinel-string comparison. */ { "use errors;\n" - "fn parse(n: i64) (i64 | errors.error) = {\n" - " if (n < 0) { return errors.eof; };\n" - " if (n == 0) { return errors.underread; };\n" + "fn parse(n: i64) (i64 | errors.invalid | errors.noentry) = {\n" + " if (n < 0) { let e: errors.invalid; return e; };\n" + " if (n == 0) { let e: errors.noentry; return e; };\n" " return n;\n" "};\n" "fn main() i32 = {\n" - " let r1: (i64 | errors.error) = parse(-1);\n" - " let r2: (i64 | errors.error) = parse(0);\n" + " let r1: (i64 | errors.invalid | errors.noentry) = parse(-1);\n" + " let r2: (i64 | errors.invalid | errors.noentry) = parse(0);\n" " let acc: i32 = 0;\n" " match (r1) {\n" " case let v: i64 => acc += -100;\n" - " case let e: errors.error =>\n" - " if (errors.equal(e, errors.eof)) { acc += 1; }\n" - " else { acc += -100; };\n" + " case let e: errors.invalid => acc += 1;\n" + " case let e: errors.noentry => acc += -100;\n" " };\n" " match (r2) {\n" " case let v: i64 => acc += -100;\n" - " case let e: errors.error =>\n" - " if (errors.equal(e, errors.underread)) { acc += 10; }\n" - " else { acc += -100; };\n" + " case let e: errors.invalid => acc += -100;\n" + " case let e: errors.noentry => acc += 10;\n" " };\n" " return acc;\n" "};", 11 }, + /* errors remaining named-void tags — pin the rest of the surface + * (noaccess / exists / unsupported) so each is callable as both a + * return variant and a match arm. One row per tag would bloat the + * table; fold them into one (T | A | B | C) dispatch. */ + { "use errors;\n" + "fn classify(n: i32) (i32 | errors.noaccess | errors.exists | errors.unsupported) = {\n" + " if (n == 1) { let e: errors.noaccess; return e; };\n" + " if (n == 2) { let e: errors.exists; return e; };\n" + " if (n == 3) { let e: errors.unsupported; return e; };\n" + " return n;\n" + "};\n" + "fn dispatch(r: (i32 | errors.noaccess | errors.exists | errors.unsupported)) i32 = {\n" + " match (r) {\n" + " case let v: i32 => return v;\n" + " case let e: errors.noaccess => return 10;\n" + " case let e: errors.exists => return 20;\n" + " case let e: errors.unsupported => return 30;\n" + " };\n" + " return -1;\n" + "};\n" + "fn main() i32 = {\n" + " return dispatch(classify(1)) + dispatch(classify(2)) + dispatch(classify(3));\n" + "};", 60 }, /* bufio.readline: drain successive '\\n'-terminated lines from a * pre-filled buffer, then a trailing fragment that returns the * `linerr` variant carrying \"no newline\" (10 chars). */