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
end if bid >= 0 @current_class_idx = ci # Issue #337: pin the synthesized sp_<C>_new function's return # type so a bare `return` inside the initialize body lowers to # `return self;` (the partially-initialized instance) rather # than the default `return 0;` (which silently NULLs the # pointer-returning constructor for the heap-allocated shape). # Value-type constructors return a struct, leave at "" so the # bare-return path falls through to its existing 0 default. saved_method_return = @current_method_return if @cls_is_value_type[ci] == 1 @current_method_return = "" else @current_method_return = "obj_" + cname end all_params = @cls_meth_params[ci].split("|") all_ptypes = @cls_meth_ptypes[ci].split("|") pnames = "".split(",") } pop_scope @current_class_idx = -1 @current_method_return = saved_method_return end else # No own initialize - call parent's if it exists bid = bodies[init_idx].to_i end if bid >= 0 # Issue #337: this wrapper has C return type `void`. Pin # @current_method_return so a bare `return` inside the body # lowers to the void shape (`return;`) instead of leaking # whatever was set during the prior _new emission. saved_method_return = @current_method_return @current_method_return = "void" @current_class_idx = ci all_params = @cls_meth_params[ci].split("|") all_ptypes = @cls_meth_ptypes[ci].split("|") } pop_scope @current_class_idx = -1 @current_method_return = saved_method_return end emit_raw("}") emit_raw("") return end end # bare return — use NULL for nullable pointer types, 0 otherwise # bare return — emit shape depends on the enclosing function's # C return type: # void → return; # obj_<C> → return self; (constructor synthesis; # returns the partially- # initialized instance) # nullable pointer → return NULL; # anything else → return 0; # Issue #337: `return if x.nil?` inside a `def initialize` was # emitted as `return 0;` regardless of the wrapping function. In # the synthesized `sp_<C>_new` (sp_<C> *-returning) it silently # NULLed the instance on the early-return branch; in the void # `sp_<C>_initialize` form it was a `void function should not # return a value` C error. if @in_gc_scope == 1 emit(" SP_GC_RESTORE();") end if is_nullable_pointer_type(@current_method_return) == 1 if @current_method_return == "void" emit(" return;") elsif is_obj_type(@current_method_return) == 1 emit(" return self;") elsif is_nullable_pointer_type(@current_method_return) == 1 emit(" return NULL;") else emit(" return 0;")test/bare_return_in_initialize.rbaddedView file
Loading diff…
test/bare_return_in_initialize.rb.expectedaddedView file
Loading diff…