fix(lower): infer no-annotation return type with params in scope (issue 0059)
A function with no explicit return type (arrow `=> expr`, or a block whose `return <v>` drives the type) has its return type inferred from the body — but the body references the function's own params. resolveReturnType ran that inference before the params were pushed into self.scope (they're bound later, at body lowering), so inferExprType couldn't resolve them and yielded .unresolved, which reached LLVM emission and panicked. It only worked when a same-named binding lingered in scope from earlier lowering (e.g. inside the big smoke file). Bind the function's plain annotated value params into a temporary scope during return-type inference. Resolve their types via resolveTypeWithBindings rather than resolveParamType — the latter does variadic/pack bookkeeping that must run exactly once, at body lowering; calling it here too corrupted the format/index path. Variadic/pack/comptime/unannotated params are skipped (no by-name return dependency; their types come from substitution). Regression: examples/0308-closures-arrow-inferred-return.sx (arrow + block inferred-return, top-level + local). Resolves issue 0059. Suite: 293 passed.
This commit is contained in:
@@ -11617,13 +11617,38 @@ pub const Lowering = struct {
|
||||
if (fd.return_type) |rt| {
|
||||
return self.resolveTypeWithBindings(rt);
|
||||
}
|
||||
// Arrow functions without explicit return type: infer from body expression
|
||||
// No explicit annotation — the type is inferred from the body, which
|
||||
// references the function's own parameters (`(x: s32) => x * 2`). Those
|
||||
// params aren't pushed into `self.scope` until body lowering, so bind
|
||||
// them into a temporary scope here; otherwise `inferExprType` can't
|
||||
// resolve `x`, the inference yields `.unresolved`, and that reaches LLVM
|
||||
// emission as `func.ret` (issue 0059). Whether it slipped through used to
|
||||
// depend on a same-named binding lingering from earlier lowering.
|
||||
var tmp_scope = Scope.init(self.alloc, self.scope);
|
||||
defer tmp_scope.deinit();
|
||||
const saved_scope = self.scope;
|
||||
self.scope = &tmp_scope;
|
||||
defer self.scope = saved_scope;
|
||||
for (fd.params, 0..) |p, i| {
|
||||
// Bind only plain annotated value params — that's all the body's
|
||||
// return type can depend on by name. Skip variadic / pack / comptime
|
||||
// params (their concrete types come from per-call substitution) and
|
||||
// unannotated ones (no context here). Resolve the type directly via
|
||||
// resolveTypeWithBindings rather than resolveParamType: the latter
|
||||
// does variadic/pack bookkeeping that must run exactly once, at body
|
||||
// lowering — calling it here too corrupts that state.
|
||||
if (p.is_variadic or p.is_pack or p.is_comptime) continue;
|
||||
if (p.type_expr.data == .inferred_type) continue;
|
||||
const pty = self.resolveTypeWithBindings(p.type_expr);
|
||||
tmp_scope.put(p.name, .{ .ref = Ref.fromIndex(@intCast(i)), .ty = pty, .is_alloca = false });
|
||||
}
|
||||
// Arrow functions without explicit return type: infer from body expression.
|
||||
if (fd.is_arrow) {
|
||||
return self.inferExprType(fd.body);
|
||||
}
|
||||
// No annotation, not arrow: an explicit `return <value>` statement
|
||||
// wins. Otherwise default to void — the body's tail expression is
|
||||
// a side-effect statement, not an implicit return.
|
||||
// Not arrow: an explicit `return <value>` statement wins. Otherwise
|
||||
// default to void — the body's tail expression is a side-effect
|
||||
// statement, not an implicit return.
|
||||
if (self.findReturnValueType(fd.body)) |ty| return ty;
|
||||
return .void;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user