Commit 5fef0b63
5fef0b63ea90Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): bare return in class method emits per-self-presence
Companion to b9d6303 (#337) which fixed the bare-return-in-initialize emit shape for the constructor synthesis path.
A class method (def self.foo or module class method via
module_function) whose return type was inferred as obj_<C> —
because every explicit return path produced a value of that class —
lowered a bare return to return self; in the obj_ branch of
compile_return_stmt. But class methods, module class methods, and
top-level free functions have no self C parameter, so gcc
complained 'self' undeclared (first use in this function).
Fix: thread a @current_method_has_self flag through every emit
path that opens a function:
- emit_instance_method: has_self = 1 (sp_ *self param)
- emit_class_level_method: has_self = 0 (no self param)
- emit_toplevel_method: has_self = 0 (also covers module
class methods stored
in @meth_* under
<Mod>_cls_<m>) - constructor synthesis sp__new (heap path): has_self = 1
- sp__initialize (super-chain wrapper): has_self = 1
The bare-return-with-obj_-return path now emits return self; only
when has_self == 1; otherwise it falls through to return c_return_default(<rt>); — which knows to emit (sp_<C>){0} for
value-type objects and NULL for pointer-typed objects.
Two supporting fixes for c_return_default / c_default_val to make the value-type fallback work cleanly:
- c_return_default: base_type the input first so
obj_<C>?(nullable) reaches the value-type check (was returning "NULL" via the c_default_val nullable arm, invalid for struct returns). - c_default_val: when the nullable type strips to a value-type obj,
return
{0}(struct-init compound literal) instead of "NULL" sosp_<C> lv_x = c_default_val(...)is a valid initializer.
Surfaced via Roundhouse's InMemoryAdapter.update — the early
return if row.nil? lowered to return self; inside a class method
with no self binding. matz/spinel#314 follow-up.
Refs #314
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 5fef0b63ea90
- Parent
- 4de7032cd104
spinel_codegen.rbmodifiedView file
@current_lexical_scope = "" @current_method_return = "" @current_method_block_param = "" # 1 when the wrapping C function being emitted has a `self` # binding (instance method, constructor synthesis). 0 for # class methods, module class methods, and top-level free # functions. Drives the bare-return-with-obj_<C>-return shape # in compile_return_stmt; without it a class method whose # inferred return is obj_<C> emits `return self;` and gcc # complains about an undeclared identifier. @current_method_has_self = 0 @in_main = 0 @in_loop = 0 @hoisted_strlen_var = "" def c_default_val(t) if is_nullable_type(t) == 1 # Value-type classes (`obj_<C>?` where C is a value type) lower # to a struct, not a pointer — `NULL` is an invalid initializer. # Fall through to the value-type branch below by stripping `?`. bt_n = base_type(t) if is_value_type_obj(bt_n) == 1 return "{0}" end return "NULL" end # NOTE: nullable returns above, so rest handles base types only def c_return_default(t) # Like c_default_val but for return statements (compound literal for value types) if is_value_type_obj(t) == 1 return "(" + c_type(t) + "){0}" bt = base_type(t) if is_value_type_obj(bt) == 1 return "(" + c_type(bt) + "){0}" end c_default_val(t) end # 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 saved_has_self = @current_method_has_self @current_method_has_self = 1 if @cls_is_value_type[ci] == 1 @current_method_return = "" else pop_scope @current_class_idx = -1 @current_method_return = saved_method_return @current_method_has_self = saved_has_self end else # No own initialize - call parent's if it exists # lowers to the void shape (`return;`) instead of leaking # whatever was set during the prior _new emission. saved_method_return = @current_method_return saved_has_self2 = @current_method_has_self @current_method_has_self = 1 @current_method_return = "void" @current_class_idx = ci all_params = @cls_meth_params[ci].split("|") pop_scope @current_class_idx = -1 @current_method_return = saved_method_return @current_method_has_self = saved_has_self2 end emit_raw("}") emit_raw("") @current_method_name = mname @current_method_return = rt @current_method_block_param = find_block_param_name(pnames, ptypes) @current_method_has_self = 1 @indent = 1 @in_gc_scope = 0 @current_method_name = mname @current_method_return = rt @current_method_block_param = find_block_param_name(pnames, ptypes) @current_method_has_self = 0 @indent = 1 @in_gc_scope = 0 mfullname = @meth_names[mi] @current_method_name = mfullname @current_method_return = @meth_return_types[mi] @current_method_has_self = 0 @indent = 1 @in_main = 0 @in_gc_scope = 0 if @current_method_return == "void" emit(" return;") elsif is_obj_type(@current_method_return) == 1 emit(" return self;") # `self` is in C scope only when the wrapping function has a # self binding — instance methods and constructor synthesis. # Class methods, module class methods, and top-level free # functions don't have self. Issue #314 follow-up: a class # method whose return type was inferred as `obj_<C>` (because # every explicit return path returned that type) lowered a # bare `return` to `return self;` — undeclared identifier C # error. Companion to b9d6303's bare-return-in-initialize fix. # c_return_default handles value-type classes (returns # `(sp_<C>){0}` for struct returns, NULL for pointer returns). if @current_method_has_self == 1 emit(" return self;") else emit(" return " + c_return_default(@current_method_return) + ";") end elsif is_nullable_pointer_type(@current_method_return) == 1 emit(" return NULL;") elsetest/bare_return_in_cls_method.rbaddedView file
Loading diff…
test/bare_return_in_cls_method.rb.expectedaddedView file
Loading diff…