Attempt-1 narrowed lowerReturn's target to failableSuccessType(ret_ty) for
every value-carrying failable. That fixed the bare-enum success slot but
introduced two defects (attempt-2 review):
F1 — explicit full failable tuple `return (.v, error.X)` panicked. With the
target narrowed to the value type, the trailing error element no longer
resolved against the error set, leaving an `.unresolved` tuple field that
tripped "unresolved type reached LLVM emission" in backend/llvm/types.zig.
F2 — a `-> (Enum, !E)` body with a comptime parameter is inlined
(lowerComptimeCall), so its success `return .red` took the inline-return path,
which the first cut skipped: it stored `{value, undef}` (error slot undef) into
the inline slot, so the success error slot read garbage at runtime.
Fix: choose the return-expr target via failableReturnTarget(ret_ty, value_node)
— a BARE value resolves against failableSuccessType (real enum ordinal), while
an EXPLICIT full failable tuple literal (arity == full-tuple field count) keeps
the full-tuple target and is forwarded as-is. This applies on the inline path
too, and the inline value-failable return now routes through
lowerFailableSuccessReturn (whose emitTupleRet stores `{value, 0}` into the
inline slot + branches), so the success error slot is 0 there as well.
Regression: examples/1056-errors-enum-value-failable-tuple-and-comptime.sx —
F1 explicit-tuple error return + bare-value success in one fn (no panic, slot 0
on success, tag 1 on error); F2 comptime-param enum value-failable read at
runtime on the success path (cast, bare if, == error.X) + error path. Reads the
slot at runtime so an undef is caught, not masked by the `if !e` proof.
examples/1055 + the original 0097 repro still pass. Gate: zig build 0,
zig build test 0, run_examples.sh 453 ok / 0 failed / 0 timed out.
67 lines
3.5 KiB
Plaintext
67 lines
3.5 KiB
Plaintext
// Enum-valued value-carrying failables, two paths the bare-success fix (issue
|
|
// 0097) did NOT originally cover. Regression (issue 0097, attempt-2 review F1+F2).
|
|
//
|
|
// F1 — EXPLICIT full failable tuple return `return (.v, error.X)`. The bug was
|
|
// the inverse of 0097: narrowing the return target to the value type for ALL
|
|
// value-failables broke the explicit-tuple form (the trailing error element no
|
|
// longer resolved against the error set → `.unresolved` field → LLVM-emit
|
|
// panic). The target must stay the FULL failable tuple for an explicit tuple
|
|
// literal of full arity, and narrow to the value type only for a BARE value.
|
|
// This pins both branches in one fn: bare-value success + explicit-tuple error.
|
|
//
|
|
// F2 — COMPTIME-PARAM ($n) value-failable. The body is INLINED at the call
|
|
// site (lowerComptimeCall), so the success return takes the inline-return path,
|
|
// which the original fix skipped — it stored `{value, undef}` (error slot
|
|
// undefined) on success. Read the error slot at RUNTIME on the success path
|
|
// (cast, bare `if`, `== error.X`) so an undef slot is caught, not masked by the
|
|
// `if !e` proof.
|
|
|
|
#import "modules/std.sx";
|
|
|
|
Color :: enum { red; green; blue; }
|
|
E :: error { Nope }
|
|
|
|
// F1: bare-value success path AND explicit-tuple error path in one function.
|
|
classify :: (s: string) -> (Color, !E) {
|
|
if s == "ok" { return .blue; } // bare value → {2, 0}
|
|
return (.red, error.Nope); // explicit full tuple → {0, 1}
|
|
}
|
|
|
|
// F2: comptime parameter forces inline lowering of the body.
|
|
ct_pick :: ($n: s32, s: string) -> (Color, !E) {
|
|
if s == "red" { return .red; } // bare value, inline path → {0, 0}
|
|
if s == "blue" { return .blue; } // bare value, inline path → {2, 0}
|
|
raise error.Nope; // inline error path → {undef, 1}
|
|
}
|
|
|
|
main :: () -> s32 {
|
|
// ── F1 success (bare value, explicit-tuple error fn): error slot 0 ──
|
|
c, e := classify("ok");
|
|
print("F1 ok: err int = {}\n", cast(s64) e); // 0
|
|
if e { print("F1 ok bare-if: ERROR (WRONG)\n"); } else { print("F1 ok bare-if: ok\n"); }
|
|
if !e { print("F1 ok guard: c = {}\n", cast(s64) c); } // 2 (blue)
|
|
|
|
// ── F1 error (explicit tuple): right tag flows, no panic ──
|
|
c2, e2 := classify("bad");
|
|
print("F1 bad: err int = {}\n", cast(s64) e2); // 1
|
|
if e2 == error.Nope { print("F1 bad: is Nope (ok)\n"); } else { print("F1 bad: not Nope (WRONG)\n"); }
|
|
print("F1 bad: tag name = {}\n", error_tag_name(e2)); // Nope
|
|
|
|
// ── F2 success (comptime-param, inline path): error slot 0 at runtime ──
|
|
c3, e3 := ct_pick(7, "red");
|
|
print("F2 red: err int = {}\n", cast(s64) e3); // 0
|
|
if e3 { print("F2 red bare-if: ERROR (WRONG)\n"); } else { print("F2 red bare-if: ok\n"); }
|
|
if e3 == error.Nope { print("F2 red == Nope (WRONG)\n"); } else { print("F2 red != Nope (ok)\n"); }
|
|
if !e3 { print("F2 red guard: c = {}\n", cast(s64) c3); } // 0
|
|
|
|
c4, e4 := ct_pick(7, "blue");
|
|
if !e4 { print("F2 blue: err int = {}, c = {}\n", cast(s64) e4, cast(s64) c4); } // 0, 2
|
|
|
|
// ── F2 error (comptime-param, inline error path): right tag ──
|
|
c5, e5 := ct_pick(7, "x");
|
|
print("F2 err: err int = {}\n", cast(s64) e5); // 1
|
|
if e5 == error.Nope { print("F2 err: is Nope (ok)\n"); } else { print("F2 err: not Nope (WRONG)\n"); }
|
|
|
|
return 0;
|
|
}
|