lib+test: unify errors to Hare named-void tagged-union

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.
This commit is contained in:
2026-05-13 20:19:03 +09:00
parent 388ab8a707
commit 2243849855
4 changed files with 71 additions and 45 deletions

View File

@@ -40,8 +40,9 @@ export fn readbyte(b: *buf) (i32 | void) = {
// Distinct alias so `(str | linerr)` has two variant types the // Distinct alias so `(str | linerr)` has two variant types the
// tagged-union machinery can keep apart at the tag level. The error // tagged-union machinery can keep apart at the tag level. The error
// variant carries a short description; callers compare with errors.equal // variant carries a short description; callers inspect by length.
// or just inspect by length. // Placeholder until bufio graduates to io.stream + named-void tags
// (eof / closed / underread).
type linerr = str; type linerr = str;
// readline — Hare-style fallible line read (name transliterated from // readline — Hare-style fallible line read (name transliterated from

View File

@@ -1,30 +1,25 @@
// errors — error type (a string) and a few sentinels. Plan 9 model: // errors — domain-agnostic error types. Mirrors ref/hare/errors/.
// the empty string means OK, a non-empty string is the message. //
// 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"; // The user does not have permission to use this resource.
def underread: error = "short read"; export type noaccess = !void;
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";
export fn isnil(e: error) bool = { // An entry was requested which does not exist.
return e.len == 0; export type noentry = !void;
};
// equal — compare an error against a sentinel (or any other error). // An attempt was made to create a resource which already exists.
// Pure byte equality. (Was `errors.is` before `is` became a keyword export type exists = !void;
// for tagged-union type-tests; rename matches bytes.equal / strings.equal.)
export fn equal(e: error, want: error) bool = { // The requested operation is not supported.
if (e.len != want.len) { return false; }; export type unsupported = !void;
let i: i32 = 0;
for (i < e.len) {
if (e[i] != want[i]) { return false; };
i += 1;
};
return true;
};

View File

@@ -5,14 +5,23 @@
// value of read/write/close — Hare-shaped tagged unions instead of // value of read/write/close — Hare-shaped tagged unions instead of
// errno-style integer sentinels. // errno-style integer sentinels.
// eof — read past the end of the stream. NAMED-void so it's a // eof — read past the end of the stream. Hare uses the `done`
// distinct variant tag from `void` (which would be "no result yet"). // singleton for EOF; ww doesn't have `done` yet so we ship a
// named-void variant tag.
export type eof = void; export type eof = void;
// closed — operation attempted on a stream that has already been // 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; 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 { export type stream = struct {
ctx: *void, ctx: *void,
read: fn(s: *stream, buf: []u8) (i32 | eof | closed), read: fn(s: *stream, buf: []u8) (i32 | eof | closed),

View File

@@ -1252,33 +1252,54 @@ static const struct row rows[] = {
" };\n" " };\n"
" return 0;\n" " return 0;\n"
"};", 7 }, "};", 7 },
/* errors.equal — sentinel comparison through a (T | error) union. /* errors named-void tags — dispatch through a (T | tag | tag)
* Sets up two errors, dispatches each, and confirms the matching * union, one variant per error condition. Replaces the old
* sentinel detection. */ * errors.equal sentinel-string comparison. */
{ "use errors;\n" { "use errors;\n"
"fn parse(n: i64) (i64 | errors.error) = {\n" "fn parse(n: i64) (i64 | errors.invalid | errors.noentry) = {\n"
" if (n < 0) { return errors.eof; };\n" " if (n < 0) { let e: errors.invalid; return e; };\n"
" if (n == 0) { return errors.underread; };\n" " if (n == 0) { let e: errors.noentry; return e; };\n"
" return n;\n" " return n;\n"
"};\n" "};\n"
"fn main() i32 = {\n" "fn main() i32 = {\n"
" let r1: (i64 | errors.error) = parse(-1);\n" " let r1: (i64 | errors.invalid | errors.noentry) = parse(-1);\n"
" let r2: (i64 | errors.error) = parse(0);\n" " let r2: (i64 | errors.invalid | errors.noentry) = parse(0);\n"
" let acc: i32 = 0;\n" " let acc: i32 = 0;\n"
" match (r1) {\n" " match (r1) {\n"
" case let v: i64 => acc += -100;\n" " case let v: i64 => acc += -100;\n"
" case let e: errors.error =>\n" " case let e: errors.invalid => acc += 1;\n"
" if (errors.equal(e, errors.eof)) { acc += 1; }\n" " case let e: errors.noentry => acc += -100;\n"
" else { acc += -100; };\n"
" };\n" " };\n"
" match (r2) {\n" " match (r2) {\n"
" case let v: i64 => acc += -100;\n" " case let v: i64 => acc += -100;\n"
" case let e: errors.error =>\n" " case let e: errors.invalid => acc += -100;\n"
" if (errors.equal(e, errors.underread)) { acc += 10; }\n" " case let e: errors.noentry => acc += 10;\n"
" else { acc += -100; };\n"
" };\n" " };\n"
" return acc;\n" " return acc;\n"
"};", 11 }, "};", 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 /* bufio.readline: drain successive '\\n'-terminated lines from a
* pre-filled buffer, then a trailing fragment that returns the * pre-filled buffer, then a trailing fragment that returns the
* `linerr` variant carrying \"no newline\" (10 chars). */ * `linerr` variant carrying \"no newline\" (10 chars). */