fix: initialize the error-channel slot on every failable implicit success return (issue 0190)
A failable function that returned by IMPLICIT success (no explicit
`return`) left its error-tag slot uninitialized, so a caller's `catch` /
`or` (or `main`) read a garbage tag and reported a phantom unhandled
error — and for value-carrying failables the success value was dropped.
The "no error" sentinel was only written on the explicit-`return;` path.
Unified all function-body-return lowering so the failable-success slot
is always written:
- void `-> !` fall-through: `ensureTerminator` (control_flow.zig) now
emits `ret constInt(0)` for a pure-failable end-of-body.
- value-failable trailing-expression success: `lowerValueBody`
(stmt.zig) routes through `lowerFailableSuccessReturn`.
- generic + pack-fn instances: `monomorphizeFunction` (generic.zig) and
`monomorphizePackFn` (pack.zig) now DELEGATE their body-return to
`lowerValueBody` instead of hand-rolling a `coerce`+`ret` that drifted
(covers generic/pack value-failables).
Also fixes the missing-value diagnostic guard added here: it now counts
`.err`-level diagnostics (new `DiagnosticList.errorCount`) rather than the
total list length, so a warning/note emitted while lowering the body
(e.g. an ObjC selector arity warning) can no longer suppress a genuine
"body produces no value" error — which previously shipped an
uninitialized return at exit 0.
Regressions: examples/errors/1061 (void fall-through), 1062 (value-failable
trailing expr), 1063 (generic value-failable trailing expr).
This commit is contained in:
21
examples/errors/1061-errors-void-failable-fallthrough.sx
Normal file
21
examples/errors/1061-errors-void-failable-fallthrough.sx
Normal file
@@ -0,0 +1,21 @@
|
||||
// A pure-failable function (`-> !`) that succeeds by IMPLICIT fall-through —
|
||||
// no explicit `return;` — must initialize its error-channel slot to 0 ("no
|
||||
// error"), exactly like an explicit `return;` would. Otherwise the slot is
|
||||
// left undefined and a caller's `catch` (or `main`) reads a garbage tag and
|
||||
// reports a phantom unhandled error.
|
||||
//
|
||||
// This exercises:
|
||||
// - a `-> !` callee that falls off the end (no `return;`) — its `catch`
|
||||
// handler must NOT fire;
|
||||
// - a `main :: () -> !` that falls off the end — must exit 0.
|
||||
//
|
||||
// Regression (issue 0190).
|
||||
|
||||
#import "modules/std.sx";
|
||||
|
||||
noop :: () -> ! { } // success by fall-through, no `return;`
|
||||
|
||||
main :: () -> ! {
|
||||
noop() catch (e) { print("phantom: {}\n", e); }; // must NOT fire
|
||||
print("ok\n"); // main falls through → exit 0
|
||||
}
|
||||
43
examples/errors/1062-errors-value-failable-trailing-expr.sx
Normal file
43
examples/errors/1062-errors-value-failable-trailing-expr.sx
Normal file
@@ -0,0 +1,43 @@
|
||||
// Value-carrying failable functions (`-> T !E`) whose body ends in a trailing
|
||||
// success EXPRESSION (no explicit `return`) must set the success error slot to
|
||||
// 0 — the caller's `catch` must NOT fire and the success value must be intact.
|
||||
// Regression (issue 0190): `lowerValueBody` used to `coerceToType`+`ret` the
|
||||
// bare success value to the full failable tuple, leaving the error-tag slot
|
||||
// uninitialized → phantom catch on success (and dropped value for string /
|
||||
// multi-value returns).
|
||||
#import "modules/std.sx";
|
||||
|
||||
E :: error { Bad }
|
||||
|
||||
// Single-value trailing-expression success.
|
||||
val :: () -> i64 !E { 99 }
|
||||
|
||||
// String trailing-expression success.
|
||||
sval :: () -> string !E { "hi" }
|
||||
|
||||
// Multi-value (tuple) trailing-expression success.
|
||||
mval :: () -> Tuple(i64, i64) !E { .(1, 2) }
|
||||
|
||||
// A real error still propagates through the value-failable channel.
|
||||
fval :: (n: i64) -> i64 !E {
|
||||
if n < 0 { raise error.Bad; }
|
||||
n + 1
|
||||
}
|
||||
|
||||
main :: () -> i32 {
|
||||
x := val() catch (e) { print("PHANTOM val\n"); return 1; };
|
||||
print("x={}\n", x);
|
||||
|
||||
s := sval() catch (e) { print("PHANTOM sval\n"); return 1; };
|
||||
print("s={}\n", s);
|
||||
|
||||
t := mval() catch (e) { print("PHANTOM mval\n"); return 1; };
|
||||
print("t=({},{})\n", t.0, t.1);
|
||||
|
||||
ok := fval(10) catch (e) { print("PHANTOM fval-ok\n"); return 1; };
|
||||
print("ok={}\n", ok);
|
||||
|
||||
fval(-1) catch (e) { print("real error caught\n"); return 0; };
|
||||
print("UNEXPECTED no error\n");
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// Generic value-carrying failable functions (`($T) -> T !E`) whose body ends in
|
||||
// a trailing success EXPRESSION (no explicit `return`) must set the success
|
||||
// error slot to 0 — the caller's `catch` must NOT fire and the value must be
|
||||
// intact across instantiations (i64 / string / struct), and `or` must yield the
|
||||
// real value not the fallback.
|
||||
// Regression (issue 0190): `lowerGenericInstance` hand-rolled a body-return
|
||||
// (coerceToType+ret) that missed the value-failable success routing, leaving
|
||||
// the error-tag slot uninitialized → phantom catch on success / value
|
||||
// corruption for generic instantiations.
|
||||
#import "modules/std.sx";
|
||||
|
||||
E :: error { Bad }
|
||||
|
||||
Point :: struct { x: i64; y: i64; }
|
||||
|
||||
// Generic trailing-expression success — instantiated at i64 / string / struct.
|
||||
gen :: ($T: Type, v: T) -> T !E { v }
|
||||
|
||||
// Generic that RAISES — the caller's catch must still fire.
|
||||
gfail :: ($T: Type, v: T, bad: bool) -> T !E {
|
||||
if bad { raise error.Bad; }
|
||||
v
|
||||
}
|
||||
|
||||
main :: () -> i32 {
|
||||
a := gen(i64, 42) catch (e) { print("PHANTOM i64\n"); return 1; };
|
||||
print("i64: {}\n", a);
|
||||
|
||||
s := gen(string, "hello") catch (e) { print("PHANTOM string\n"); return 1; };
|
||||
print("string: {}\n", s);
|
||||
|
||||
p := gen(Point, Point.{ x = 3, y = 4 }) catch (e) { print("PHANTOM struct\n"); return 1; };
|
||||
print("struct: ({},{})\n", p.x, p.y);
|
||||
|
||||
// `or`-form on success must yield the real value, not the fallback.
|
||||
o := gen(i64, 7) or 999;
|
||||
print("or: {}\n", o);
|
||||
|
||||
// A generic that raises still propagates to the caller's catch.
|
||||
gfail(i64, 0, true) catch (e) {
|
||||
print("raise caught\n");
|
||||
return 0;
|
||||
};
|
||||
print("UNEXPECTED no error\n");
|
||||
return 1;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
ok
|
||||
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
x=99
|
||||
s=hi
|
||||
t=(1,2)
|
||||
ok=11
|
||||
real error caught
|
||||
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
i64: 42
|
||||
string: hello
|
||||
struct: (3,4)
|
||||
or: 7
|
||||
raise caught
|
||||
Reference in New Issue
Block a user