Commit 45b41226

45b412267afbMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): bare return in poly-returning method emits sp_box_nil()

A bare return (no value) in a method whose declared return type is sp_RbVal (poly) emitted literal return 0;, which gcc rejected as incompatible types when returning type 'int' but 'sp_RbVal' was expected. The implicit end-of-function fallthrough already emitted return sp_box_nil(); correctly — only the explicit bare-return statement was missed.

Add the poly arm to compile_return_stmt's bare-return shape table, alongside the existing void / obj_ / nullable-pointer arms. Sister to the bare-return-in-initialize (#337) and bare-return-in-class-method (#314 follow-up) fixes.

Surfaced via Roundhouse's framework runtime which uses bare return as a nil-guard idiom in heterogeneous-return methods.

Fixes #383

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
45b412267afb
Parent
ea2b009adb16

spinel_codegen.rbmodifiedView file

@@ -32395,18 +32395,19 @@
3239532395 # bare return — emit shape depends on the enclosing function's
3239632396 # C return type:
3239732397 # void → return;
32398- # obj_<C> → return self; (constructor synthesis;
32399- # returns the partially-
32400- # initialized instance)
32398+ # poly → return sp_box_nil(); (boxed nil)
32399+ # obj_<C> → return self; (constructor
32400+ # synthesis path
32401+ # with self in
32402+ # scope)
3240132403 # nullable pointer → return NULL;
3240232404 # anything else → return 0;
3240332405 # The shape per @current_method_return matters because a
32404- # `return if x.nil?` inside a `def initialize` reaches this
32405- # bare-return path: in the synthesized `sp_<C>_new` (sp_<C>
32406- # *-returning) emitting `return 0;` would silently NULL the
32407- # instance on the early-return branch, and in the void
32408- # `sp_<C>_initialize` form it would be a `void function
32409- # should not return a value` C error.
32406+ # `return if x.nil?` inside an `sp_RbVal`-returning method
32407+ # would otherwise emit `return 0;` (int) into a poly slot,
32408+ # and the same bare-return inside a `def initialize` reaches
32409+ # the constructor-synthesis branch where `return 0;` would
32410+ # silently NULL the instance.
3241032411 emit_setjmp_depth_unwind
3241132412 emit_ensure_replays
3241232413 if @in_gc_scope == 1
@@ -32414,17 +32415,16 @@
3241432415 end
3241532416 if @current_method_return == "void"
3241632417 emit(" return;")
32418+ elsif @current_method_return == "poly"
32419+ @needs_rb_value = 1
32420+ emit(" return sp_box_nil();")
3241732421 elsif is_obj_type(@current_method_return) == 1
3241832422 # `self` is in C scope only when the wrapping function has a
3241932423 # self binding — instance methods and constructor synthesis.
3242032424 # Class methods, module class methods, and top-level free
32421- # functions don't have self. Issue #314 follow-up: a class
32422- # method whose return type was inferred as `obj_<C>` (because
32423- # every explicit return path returned that type) lowered a
32424- # bare `return` to `return self;` — undeclared identifier C
32425- # error. Companion to b9d6303's bare-return-in-initialize fix.
32426- # c_return_default handles value-type classes (returns
32427- # `(sp_<C>){0}` for struct returns, NULL for pointer returns).
32425+ # functions don't have self; their bare returns fall through
32426+ # to `c_return_default` (value-type classes get
32427+ # `(sp_<C>){0}`, pointer-typed obj returns get NULL).
3242832428 if @current_method_has_self == 1
3242932429 emit(" return self;")
3243032430 else