Commit 0693c84d

0693c84d88d4Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Emit the return-type default for a bare return in a foreign-obj method

A bare return emitted return self whenever the method's inferred return type was any obj class (with self in scope). When inference types the return as a class DIFFERENT from self -- e.g. a side-effect method whose last expression is x.attr = Other.new -- return self returns self's type into the other class's pointer slot (incompatible-pointer-types). A recent return-type-inference change surfaced this: bm_ao_render's Plane#intersect is typed sp_Vec * (its tail is isect.pl = Vec.new(...)) but its bare return emitted return self (a Plane). A bare return is Ruby nil, so emit the type default (NULL) unless the return type is actually self's own class (the constructor / returns-self case).

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
0693c84d88d4
Parent
8e272bd092cb

spinel_codegen.rbmodifiedView file

@@ -40676,7 +40676,14 @@
4067640676 # functions don't have self; their bare returns fall through
4067740677 # to `c_return_default` (value-type classes get
4067840678 # `(sp_<C>){0}`, pointer-typed obj returns get NULL).
40679- if @current_method_has_self == 1
40679+ # Only return `self` when the method's return type is actually
40680+ # self's own class (the constructor-synthesis / returns-self case).
40681+ # When inference typed the return as a DIFFERENT obj class (e.g. a
40682+ # side-effect method whose last expression is `x.attr = Other.new`),
40683+ # `return self` would return self's type into the other class's slot
40684+ # (incompatible-pointer-types). A bare `return` is Ruby nil, so emit
40685+ # the type default (NULL) there.
40686+ if @current_method_has_self == 1 && @current_class_idx >= 0 && @current_method_return == "obj_" + @cls_names[@current_class_idx]
4068040687 emit(" return self;")
4068140688 else
4068240689 emit(" return " + c_return_default(@current_method_return) + ";")