Commit e438d961

e438d961b51cMerged

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

Description

fix(codegen): bare return in initialize emits per wrapping function's C return type

A bare return (Ruby's "exit early" with no value) inside an initialize body lowered to return 0; regardless of the wrapping function's emitted C return type. Two bad outcomes depending on the emit shape:

  • sp_<C>_new(...) (sp_ *-returning, the heap-allocated constructor synthesis): return 0; silently NULLed the instance on the early-return branch — semantically wrong, no C diagnostic to flag it.

  • sp_<C>_initialize(...) (void-returning, used for super chains): return 0; is a strict-mode "void function should not return a value" error.

Pin @current_method_return per emission shape so the bare-return path picks the right C statement:

  • _new synthesis: obj_<C> → return self; (the partially- initialized instance, matching the issue's expected-behavior table).
  • _initialize wrapper: "void" → return;.
  • Existing nullable-pointer / int defaults preserved.

Surfaced via Roundhouse's HashWithIndifferentAccess#initialize (return if other.nil?) and used throughout the framework runtime.

Fixes #337

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

Details

Commit
e438d961b51c
Parent
ab64a279d511

spinel_codegen.rbmodifiedView file

@@ -17042,6 +17042,19 @@
1704217042 end
1704317043 if bid >= 0
1704417044 @current_class_idx = ci
17045+ # Issue #337: pin the synthesized sp_<C>_new function's return
17046+ # type so a bare `return` inside the initialize body lowers to
17047+ # `return self;` (the partially-initialized instance) rather
17048+ # than the default `return 0;` (which silently NULLs the
17049+ # pointer-returning constructor for the heap-allocated shape).
17050+ # Value-type constructors return a struct, leave at "" so the
17051+ # bare-return path falls through to its existing 0 default.
17052+ saved_method_return = @current_method_return
17053+ if @cls_is_value_type[ci] == 1
17054+ @current_method_return = ""
17055+ else
17056+ @current_method_return = "obj_" + cname
17057+ end
1704517058 all_params = @cls_meth_params[ci].split("|")
1704617059 all_ptypes = @cls_meth_ptypes[ci].split("|")
1704717060 pnames = "".split(",")
@@ -17248,6 +17261,7 @@
1724817261 }
1724917262 pop_scope
1725017263 @current_class_idx = -1
17264+ @current_method_return = saved_method_return
1725117265 end
1725217266 else
1725317267 # No own initialize - call parent's if it exists
@@ -17296,6 +17310,12 @@
1729617310 bid = bodies[init_idx].to_i
1729717311 end
1729817312 if bid >= 0
17313+ # Issue #337: this wrapper has C return type `void`. Pin
17314+ # @current_method_return so a bare `return` inside the body
17315+ # lowers to the void shape (`return;`) instead of leaking
17316+ # whatever was set during the prior _new emission.
17317+ saved_method_return = @current_method_return
17318+ @current_method_return = "void"
1729917319 @current_class_idx = ci
1730017320 all_params = @cls_meth_params[ci].split("|")
1730117321 all_ptypes = @cls_meth_ptypes[ci].split("|")
@@ -17333,6 +17353,7 @@
1733317353 }
1733417354 pop_scope
1733517355 @current_class_idx = -1
17356+ @current_method_return = saved_method_return
1733617357 end
1733717358 emit_raw("}")
1733817359 emit_raw("")
@@ -29445,11 +29466,28 @@
2944529466 return
2944629467 end
2944729468 end
29448- # bare return — use NULL for nullable pointer types, 0 otherwise
29469+ # bare return — emit shape depends on the enclosing function's
29470+ # C return type:
29471+ # void → return;
29472+ # obj_<C> → return self; (constructor synthesis;
29473+ # returns the partially-
29474+ # initialized instance)
29475+ # nullable pointer → return NULL;
29476+ # anything else → return 0;
29477+ # Issue #337: `return if x.nil?` inside a `def initialize` was
29478+ # emitted as `return 0;` regardless of the wrapping function. In
29479+ # the synthesized `sp_<C>_new` (sp_<C> *-returning) it silently
29480+ # NULLed the instance on the early-return branch; in the void
29481+ # `sp_<C>_initialize` form it was a `void function should not
29482+ # return a value` C error.
2944929483 if @in_gc_scope == 1
2945029484 emit(" SP_GC_RESTORE();")
2945129485 end
29452- if is_nullable_pointer_type(@current_method_return) == 1
29486+ if @current_method_return == "void"
29487+ emit(" return;")
29488+ elsif is_obj_type(@current_method_return) == 1
29489+ emit(" return self;")
29490+ elsif is_nullable_pointer_type(@current_method_return) == 1
2945329491 emit(" return NULL;")
2945429492 else
2945529493 emit(" return 0;")