Commit 9b7a2ad0

9b7a2ad0721aMerged

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

Description

fix(codegen): #286 inherited param widen, #287 poly hash literal

#287: a method returning { key: @ivar, ... } froze the inferred return type at the first scan, before #247 widened the ivar to poly via a sibling-writer disagreement. The hash literal then emitted as sp_StrIntHash * and tried to insert the now-poly ivar value at C compile. infer_hash_val_type's all_same branch missed the first_vt == "poly" case — every value already inferred poly should give a *_poly_hash, same storage the mixed-types else branch picks. Add the case for both sym-keyed and str-keyed shapes.

#286: a parent class's instance method param wasn't widening from subclass call sites. scan_new_calls's bare-call branch only checked find_method_idx (top-level methods); a bar(arg) inside a subclass that resolves through inheritance to the parent's bar left the parent's param frozen at its initial type. Add a parallel walk: when @current_class_idx is set and the bare call resolves on the current class (or via find_method_owner on an ancestor), unify the call args into that class's @cls_meth_ptypes — same shape the recv >= 0 branch already uses for explicit-receiver calls.

The widening exposed a sibling-issue on the emit side: a bare call to an inherited method emitted sp_<Parent>_<m>(self, ...) without casting self from sp_<Subclass> * to sp_<Parent> *. Under -Werror that's -Wincompatible-pointer-types. Add the same (sp_<owner> *)self cast that the recv >= 0 / instance _eval branches above already use.

Fixes #286 #287

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

Details

Commit
9b7a2ad0721a
Parent
40403f434501

spinel_codegen.rbmodifiedView file

@@ -2206,6 +2206,17 @@
22062206 if all_sym_keys == 1 && (first_vt == "int" || first_vt == "bool" || first_vt == "nil")
22072207 return "sym_int_hash"
22082208 end
2209+ # Issue #287: every value already inferred as poly (the slot
2210+ # was widened upstream — typically an ivar that #247's
2211+ # finalize_ivar_heterogeneity widened on a sibling-writer
2212+ # disagreement). Same poly-hash storage as the mixed-types
2213+ # `else` branch — every value carries its own tag.
2214+ if first_vt == "poly"
2215+ if all_sym_keys == 1
2216+ return "sym_poly_hash"
2217+ end
2218+ return "str_poly_hash"
2219+ end
22092220 else
22102221 # Mixed value types: use a *_poly_hash so each slot carries its
22112222 # own tag (sp_RbVal) rather than coercing everything to one type.
@@ -8802,6 +8813,48 @@
88028813 @meth_param_types[mi] = ptypes.join(",")
88038814 end
88048815 end
8816+ # Issue #286: bare call inside a class method body that
8817+ # resolves to an inherited instance method. find_method_idx
8818+ # above only finds *top-level* methods; an `assert_not_nil(x)`
8819+ # inside `T2 < T`'s body needs to widen T's `assert_not_nil`
8820+ # ptypes from this call's args. Mirror the obj.method() walk
8821+ # in the recv >= 0 branch below — find_method_owner finds the
8822+ # ancestor that actually defines the method, then we update
8823+ # *that* class's @cls_meth_ptypes.
8824+ if @current_class_idx >= 0
8825+ cls_ci_286 = @current_class_idx
8826+ cls_owner_286 = cls_ci_286
8827+ midx_286 = cls_find_method_direct(cls_ci_286, mname)
8828+ if midx_286 < 0
8829+ owner_286 = find_method_owner(cls_ci_286, mname)
8830+ if owner_286 != "" && owner_286 != @cls_names[cls_ci_286]
8831+ cls_owner_286 = find_class_idx(owner_286)
8832+ if cls_owner_286 >= 0
8833+ midx_286 = cls_find_method_direct(cls_owner_286, mname)
8834+ end
8835+ end
8836+ end
8837+ if midx_286 >= 0
8838+ args_id_286 = @nd_arguments[nid]
8839+ if args_id_286 >= 0
8840+ arg_ids_286 = get_args(args_id_286)
8841+ all_ptypes_286 = @cls_meth_ptypes[cls_owner_286].split("|")
8842+ if midx_286 < all_ptypes_286.length
8843+ ptypes_286 = all_ptypes_286[midx_286].split(",")
8844+ kk_286 = 0
8845+ while kk_286 < arg_ids_286.length
8846+ at_286 = infer_type(arg_ids_286[kk_286])
8847+ if kk_286 < ptypes_286.length
8848+ ptypes_286[kk_286] = unify_call_types(ptypes_286[kk_286], at_286, arg_ids_286[kk_286])
8849+ end
8850+ kk_286 = kk_286 + 1
8851+ end
8852+ all_ptypes_286[midx_286] = ptypes_286.join(",")
8853+ @cls_meth_ptypes[cls_owner_286] = all_ptypes_286.join("|")
8854+ end
8855+ end
8856+ end
8857+ end
88058858 end
88068859 if @nd_name[nid] == "new"
88078860 recv = @nd_receiver[nid]
@@ -19470,7 +19523,17 @@
1947019523 bp = "0"
1947119524 end
1947219525 end
19473- return "sp_" + owner + "_" + sanitize_name(mname) + "(" + self_expr + build_call_tail(ca, bp) + ")"
19526+ # Issue #286: when the call resolves through inheritance to an
19527+ # ancestor's method, the C function takes `sp_<owner> *self`.
19528+ # Our `self` here is `sp_<current> *`. Cast so the `-Werror`
19529+ # build doesn't trip on -Wincompatible-pointer-types. The same
19530+ # cast pattern is already used by the recv >= 0 / instance_eval
19531+ # branches a few lines above.
19532+ recv_arg = self_expr
19533+ if owner != "" && owner != @cls_names[@current_class_idx]
19534+ recv_arg = "(sp_" + owner + " *)" + self_expr
19535+ end
19536+ return "sp_" + owner + "_" + sanitize_name(mname) + "(" + recv_arg + build_call_tail(ca, bp) + ")"
1947419537 end
1947519538 # Check attr_readers (bare method call like `x` meaning self.x)
1947619539 readers = @cls_attr_readers[@current_class_idx].split(";")