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" so sp_<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

@@ -252,6 +252,14 @@
252252 @current_lexical_scope = ""
253253 @current_method_return = ""
254254 @current_method_block_param = ""
255+ # 1 when the wrapping C function being emitted has a `self`
256+ # binding (instance method, constructor synthesis). 0 for
257+ # class methods, module class methods, and top-level free
258+ # functions. Drives the bare-return-with-obj_<C>-return shape
259+ # in compile_return_stmt; without it a class method whose
260+ # inferred return is obj_<C> emits `return self;` and gcc
261+ # complains about an undeclared identifier.
262+ @current_method_has_self = 0
255263 @in_main = 0
256264 @in_loop = 0
257265 @hoisted_strlen_var = ""
@@ -5284,6 +5292,13 @@
52845292
52855293 def c_default_val(t)
52865294 if is_nullable_type(t) == 1
5295+ # Value-type classes (`obj_<C>?` where C is a value type) lower
5296+ # to a struct, not a pointer — `NULL` is an invalid initializer.
5297+ # Fall through to the value-type branch below by stripping `?`.
5298+ bt_n = base_type(t)
5299+ if is_value_type_obj(bt_n) == 1
5300+ return "{0}"
5301+ end
52875302 return "NULL"
52885303 end
52895304 # NOTE: nullable returns above, so rest handles base types only
@@ -5343,8 +5358,9 @@
53435358
53445359 def c_return_default(t)
53455360 # Like c_default_val but for return statements (compound literal for value types)
5346- if is_value_type_obj(t) == 1
5347- return "(" + c_type(t) + "){0}"
5361+ bt = base_type(t)
5362+ if is_value_type_obj(bt) == 1
5363+ return "(" + c_type(bt) + "){0}"
53485364 end
53495365 c_default_val(t)
53505366 end
@@ -17478,6 +17494,8 @@
1747817494 # Value-type constructors return a struct, leave at "" so the
1747917495 # bare-return path falls through to its existing 0 default.
1748017496 saved_method_return = @current_method_return
17497+ saved_has_self = @current_method_has_self
17498+ @current_method_has_self = 1
1748117499 if @cls_is_value_type[ci] == 1
1748217500 @current_method_return = ""
1748317501 else
@@ -17702,6 +17720,7 @@
1770217720 pop_scope
1770317721 @current_class_idx = -1
1770417722 @current_method_return = saved_method_return
17723+ @current_method_has_self = saved_has_self
1770517724 end
1770617725 else
1770717726 # No own initialize - call parent's if it exists
@@ -17755,6 +17774,8 @@
1775517774 # lowers to the void shape (`return;`) instead of leaking
1775617775 # whatever was set during the prior _new emission.
1775717776 saved_method_return = @current_method_return
17777+ saved_has_self2 = @current_method_has_self
17778+ @current_method_has_self = 1
1775817779 @current_method_return = "void"
1775917780 @current_class_idx = ci
1776017781 all_params = @cls_meth_params[ci].split("|")
@@ -17794,6 +17815,7 @@
1779417815 pop_scope
1779517816 @current_class_idx = -1
1779617817 @current_method_return = saved_method_return
17818+ @current_method_has_self = saved_has_self2
1779717819 end
1779817820 emit_raw("}")
1779917821 emit_raw("")
@@ -17807,6 +17829,7 @@
1780717829 @current_method_name = mname
1780817830 @current_method_return = rt
1780917831 @current_method_block_param = find_block_param_name(pnames, ptypes)
17832+ @current_method_has_self = 1
1781017833 @indent = 1
1781117834 @in_gc_scope = 0
1781217835
@@ -17900,6 +17923,7 @@
1790017923 @current_method_name = mname
1790117924 @current_method_return = rt
1790217925 @current_method_block_param = find_block_param_name(pnames, ptypes)
17926+ @current_method_has_self = 0
1790317927 @indent = 1
1790417928 @in_gc_scope = 0
1790517929
@@ -18016,6 +18040,7 @@
1801618040 mfullname = @meth_names[mi]
1801718041 @current_method_name = mfullname
1801818042 @current_method_return = @meth_return_types[mi]
18043+ @current_method_has_self = 0
1801918044 @indent = 1
1802018045 @in_main = 0
1802118046 @in_gc_scope = 0
@@ -30079,7 +30104,21 @@
3007930104 if @current_method_return == "void"
3008030105 emit(" return;")
3008130106 elsif is_obj_type(@current_method_return) == 1
30082- emit(" return self;")
30107+ # `self` is in C scope only when the wrapping function has a
30108+ # self binding — instance methods and constructor synthesis.
30109+ # Class methods, module class methods, and top-level free
30110+ # functions don't have self. Issue #314 follow-up: a class
30111+ # method whose return type was inferred as `obj_<C>` (because
30112+ # every explicit return path returned that type) lowered a
30113+ # bare `return` to `return self;` — undeclared identifier C
30114+ # error. Companion to b9d6303's bare-return-in-initialize fix.
30115+ # c_return_default handles value-type classes (returns
30116+ # `(sp_<C>){0}` for struct returns, NULL for pointer returns).
30117+ if @current_method_has_self == 1
30118+ emit(" return self;")
30119+ else
30120+ emit(" return " + c_return_default(@current_method_return) + ";")
30121+ end
3008330122 elsif is_nullable_pointer_type(@current_method_return) == 1
3008430123 emit(" return NULL;")
3008530124 else