Files
sx/examples/1055-errors-enum-value-failable-error-slot.sx
agra 82366a93df fix(ir): value-failable returning an enum zeroes the success error slot [0097]
A `-> (Enum, !E)` `return .variant` lowered the enum literal with
`target_type` set to the full failable tuple `(Enum, !E)` instead of the
success value type. The bare literal resolves its tag against `target_type`;
against a tuple it matched no variant (silent tag 0) and was stamped with the
tuple type, so `lowerFailableSuccessReturn` saw `val_ty == ret_ty` and took the
forwarding branch — returning the half-built `{value, undef}` aggregate and
never appending the `0` error slot. Every runtime read of the slot on the
success path (`cast(s64) e`, bare `if e`, `e == error.X`) saw garbage nonzero;
only the compile-time `if !e` proof masked it. The s32 case was already correct
because integer literals don't resolve variants against `target_type`.

Fix: in lowerReturn, narrow `target_type` to `failableSuccessType(ret_ty)` for
a value-carrying failable before lowering the returned expression. The enum
literal then resolves to its real ordinal and is typed as the value type, so
the success path correctly appends `0`. Forwarding (`return call()` / explicit
`(v, e)`) is unaffected — those still yield a value typed equal to the tuple.

Regression: examples/1055-errors-enum-value-failable-error-slot.sx reads the
error slot at runtime on the success path (cast, bare if, == error.X), checks a
non-zero ordinal (.blue=2, also corrupted to 0 pre-fix), and asserts the error
path still carries the right tag + error_tag_name. Fails pre-fix, passes after.
2026-06-05 22:10:14 +03:00

45 lines
2.0 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 int = {}\n", cast(s64) e3); // 1
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;
}