Files
sx/examples/1055-errors-enum-value-failable-error-slot.sx
agra 330c3aeef7 std: full namespace tail — fs/process/socket/json/cli/hash/test
With 0115's own-wins globals landed, the remaining tail modules join
std.sx: every '#import "modules/std.sx"' now carries mem/xml/log/fs/
process/socket/json/cli/hash/test as namespaces (trace stays a direct
import).

Enablers in the same change:
- emit: dead-global elimination — a plain-data global no instruction
  references is not emitted, so tail modules' data (hash's 64-entry K
  table, OS/ARCH/POINTER_SIZE) stays out of binaries that don't use it.
  Comptime-backed globals keep their #run evaluation. 37 pinned IR
  snapshots regenerated (dead globals dropped + string renumbering from
  the larger module).
- 1055/1056 stop pinning the global error-tag ordinal (it shifts with
  program composition); they assert nonzero + tag identity + name.
- specs/readme/CLAUDE.md tail docs updated.
2026-06-11 10:49:39 +03:00

45 lines
2.1 KiB
Plaintext

// Enum-valued value-carrying failable: the SUCCESS path must zero the trailing
// error slot. Regression (issue 0097). A `-> (Enum, !E)` `return .variant`
// resolves the enum literal against the function's VALUE type (the enum), not
// the failable tuple — otherwise the literal mis-resolves (tag 0) and is stamped
// with the tuple type, which the success-return lowering mistakes for a forwarded
// full tuple and leaves the error slot UNDEFINED (read back as garbage nonzero).
//
// This pins the slot at RUNTIME on the success path (cast(s64) e, bare `if e`,
// and `e == error.X`) — not only via the `if !e` proof that the compiler can
// fold away. It also exercises a non-zero ordinal (`.blue` = 2) so a value slot
// that collapses to 0 is caught, and asserts the error PATH still carries the
// right tag and `error_tag_name`.
#import "modules/std.sx";
Color :: enum { red; green; blue; }
E :: error { Nope }
pick :: (s: string) -> (Color, !E) {
if s == "red" { return .red; }
if s == "blue" { return .blue; } // non-zero ordinal (2)
raise error.Nope;
}
main :: () -> s32 {
// ── success path: error slot MUST read 0 at runtime ──
c, e := pick("red");
print("success err int = {}\n", cast(s64) e); // 0
if e { print("bare-if e: ERROR (WRONG)\n"); } else { print("bare-if e: ok\n"); }
if e == error.Nope { print("e == Nope (WRONG)\n"); } else { print("e != Nope (ok)\n"); }
if !e { print("guard !e: c = {}\n", cast(s64) c); } // 0 (red)
// ── non-zero ordinal: value slot must carry the real ordinal ──
c2, e2 := pick("blue");
if !e2 { print("blue: err int = {}, c = {}\n", cast(s64) e2, cast(s64) c2); } // 0, 2
// ── error path: the right tag flows through ──
c3, e3 := pick("xxx");
print("error err nonzero = {}\n", cast(s64) e3 != 0); // true (ordinal is program-global, not pinned)
if e3 == error.Nope { print("error: is Nope (ok)\n"); } else { print("error: not Nope (WRONG)\n"); }
print("error tag name = {}\n", error_tag_name(e3)); // Nope
return 0;
}