Commit 4fc5db6f

4fc5db6f5a25Merged

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

Description

fix(analyze,codegen): default-typed param + inlined-yield self/local cascade

Five cooperating fixes peeling back the C-compile error layers in Sam Ruby's roundhouse controller-test pipeline (issue #572's "test binary doesn't build" surface, distinct from the kwarg-bundle fix in 4019767).

  1. Analyzer infer_cls_meth_param_from_body + infer_param_type_from_body: when widening a param's type because the body invokes methods on it, re-infer the param's default expression FIRST (with the method's owning class scope set) and trust an obj_<C> answer over the "first class that has every called method" walk. The walk's first-match rule silently picked ActionController_Base (defined earlier in the file) when assert_response(expected, response = @__response)'s default was actually ActionResponse, so callers passing self->iv___response (sp_ActionResponse) tripped -Wincompatible-pointer-types on the third arg.

  2. Analyzer widen_param_types_from_body_writes: set up the method's scope (push_scope + per-param declare_var) around the per-param scan so the body-write widening's is_reliable_literal_for_widen LV-read arm (below) sees sibling-param types. Without this, an inter-param assignment opts = content_or_opts couldn't see the rhs's poly type.

  3. Analyzer is_reliable_literal_for_widen: accept LocalVariableReadNode when the local has a known non-nil non-int type. With (2), opts = content_or_opts between two params correctly widens opts to poly, matching the C-side write lv_opts = lv_content_or_opts.

  4. Codegen compile_yield_method_call_stmt: when inlining the yield-using method's body, wrap @self_override in a cast to the callee's class pointer. The enclosing function's self is the subclass type; calls inside the inlined body to inherited methods that go through self_expr previously emitted bare self, mismatching the callee's param type. Also declare each inlined param/local under its ORIGINAL name (in addition to the renamed _yN form) so infer_type calls during emit -- which look up by original name -- see the correct type and don't fall through to the int default.

  5. Codegen compile_stmt_with_block LocalVariableWriteNode arm: mirror the main compile_stmt LV-write boxing so a c = nil against a poly-typed slot lowers to lv_c = sp_box_nil(), not lv_c = 0 (which gcc rejects). Also switch the inlined default-arg initializer to c_default_val(pt) so the slot's C type and initializer agree (sp_RbVal x = 0 is invalid C).

After the cascade: roundhouse articles_controller_test.rb and comments_controller_test.rb both compile and run. They surface runtime test failures from unrelated logic (e.g. STATUS_SYMBOLS[:success].include?(actual) not matching), but the original C-compile blocker -- self / param / boxing type mismatches -- is cleared.

make test: 564/564 pass (one new fixture). make optcarrot: checksum 59662 stable. Stage-2 bootstrap fixpoint clean.

Refs #572

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

Details

Commit
4fc5db6f5a25
Parent
4c62268e2452

spinel_analyze.rbmodifiedView file

@@ -12143,17 +12143,49 @@
1214312143 called = "".split(",")
1214412144 collect_param_methods(bid, pnames[pk], called)
1214512145 if called.length > 0 && called_methods_only_on_container_builtins(called) == 0
12146- ci2 = 0
12147- best = -1
12148- while ci2 < @cls_names.length
12149- if best < 0 && class_has_all_methods(ci2, called) == 1
12150- best = ci2
12146+ # Check the param's default expression first: if it's an
12147+ # ivar/local/call that NOW (after ivars are known) infers
12148+ # to a concrete `obj_<C>`, trust that over the
12149+ # "first class that has all called methods" heuristic --
12150+ # which silently picks the wrong sibling class when two
12151+ # classes define the same surface (e.g. `response.status`
12152+ # exists on both ActionController_Base and ActionResponse).
12153+ default_obj_t = ""
12154+ defs_str_cm = ""
12155+ if j < @cls_meth_defaults[oci].split("|").length
12156+ defs_str_cm = @cls_meth_defaults[oci].split("|")[j]
12157+ end
12158+ defs_arr_cm = defs_str_cm.split(",")
12159+ if pk < defs_arr_cm.length
12160+ def_id_cm = defs_arr_cm[pk].to_i
12161+ if def_id_cm > 0
12162+ saved_ci_pd = @current_class_idx
12163+ @current_class_idx = oci
12164+ push_scope
12165+ dt_inferred = infer_type(def_id_cm)
12166+ pop_scope
12167+ @current_class_idx = saved_ci_pd
12168+ if is_obj_type(dt_inferred) == 1
12169+ default_obj_t = dt_inferred
12170+ end
1215112171 end
12152- ci2 = ci2 + 1
1215312172 end
12154- if best >= 0
12155- ptypes[pk] = "obj_" + @cls_names[best]
12173+ if default_obj_t != ""
12174+ ptypes[pk] = default_obj_t
1215612175 cls_meth_ptypes_put(oci, j, ptypes)
12176+ else
12177+ ci2 = 0
12178+ best = -1
12179+ while ci2 < @cls_names.length
12180+ if best < 0 && class_has_all_methods(ci2, called) == 1
12181+ best = ci2
12182+ end
12183+ ci2 = ci2 + 1
12184+ end
12185+ if best >= 0
12186+ ptypes[pk] = "obj_" + @cls_names[best]
12187+ cls_meth_ptypes_put(oci, j, ptypes)
12188+ end
1215712189 end
1215812190 end
1215912191 end
@@ -12181,17 +12213,43 @@
1218112213 called = "".split(",")
1218212214 collect_param_methods(bid, pnames[pk], called)
1218312215 if called.length > 0 && called_methods_only_on_container_builtins(called) == 0
12184- ci2 = 0
12185- best = -1
12186- while ci2 < @cls_names.length
12187- if best < 0 && class_has_all_methods(ci2, called) == 1
12188- best = ci2
12216+ # Same precedence rule as the cls_meth side above: if the
12217+ # param's default expression infers to a concrete obj_<C>
12218+ # under the current scope, prefer that over the
12219+ # "first class with all called methods" walk.
12220+ default_obj_t = ""
12221+ defs_str_mp = ""
12222+ if mi < @meth_has_defaults.length
12223+ defs_str_mp = @meth_has_defaults[mi]
12224+ end
12225+ defs_arr_mp = defs_str_mp.split(",")
12226+ if pk < defs_arr_mp.length
12227+ def_id_mp = defs_arr_mp[pk].to_i
12228+ if def_id_mp > 0
12229+ push_scope
12230+ dt_inferred_mp = infer_type(def_id_mp)
12231+ pop_scope
12232+ if is_obj_type(dt_inferred_mp) == 1
12233+ default_obj_t = dt_inferred_mp
12234+ end
1218912235 end
12190- ci2 = ci2 + 1
1219112236 end
12192- if best >= 0
12193- ptypes[pk] = "obj_" + @cls_names[best]
12237+ if default_obj_t != ""
12238+ ptypes[pk] = default_obj_t
1219412239 @meth_param_types[mi] = ptypes.join(",")
12240+ else
12241+ ci2 = 0
12242+ best = -1
12243+ while ci2 < @cls_names.length
12244+ if best < 0 && class_has_all_methods(ci2, called) == 1
12245+ best = ci2
12246+ end
12247+ ci2 = ci2 + 1
12248+ end
12249+ if best >= 0
12250+ ptypes[pk] = "obj_" + @cls_names[best]
12251+ @meth_param_types[mi] = ptypes.join(",")
12252+ end
1219512253 end
1219612254 end
1219712255 end
@@ -13591,6 +13649,21 @@
1359113649 pnames = @meth_param_names[mi].split(",")
1359213650 ptypes = @meth_param_types[mi].split(",")
1359313651 ptypes_changed = 0
13652+ # Set up the method's scope so scan_param_body_write_unify's
13653+ # LV-read arm (used by is_reliable_literal_for_widen) can look
13654+ # up sibling params via find_var_type. Without this, an
13655+ # `opts = content_or_opts` assignment between two params
13656+ # never widened `opts` because the rhs's type was unknown.
13657+ push_scope
13658+ dpi = 0
13659+ while dpi < pnames.length
13660+ pt_dp = "int"
13661+ if dpi < ptypes.length
13662+ pt_dp = ptypes[dpi]
13663+ end
13664+ declare_var(pnames[dpi], pt_dp)
13665+ dpi = dpi + 1
13666+ end
1359413667 pk = 0
1359513668 while pk < pnames.length
1359613669 if pk < ptypes.length
@@ -13602,6 +13675,7 @@
1360213675 end
1360313676 pk = pk + 1
1360413677 end
13678+ pop_scope
1360513679 if ptypes_changed == 1
1360613680 @meth_param_types[mi] = ptypes.join(",")
1360713681 end
@@ -13625,6 +13699,18 @@
1362513699 pnames_j = all_params[mj].split(",")
1362613700 ptypes_j = all_ptypes[mj].split(",")
1362713701 inner_changed = 0
13702+ # Same scope setup as the top-level branch so the LV-read
13703+ # widening picks up sibling-param types.
13704+ push_scope
13705+ dpcj = 0
13706+ while dpcj < pnames_j.length
13707+ pt_cj = "int"
13708+ if dpcj < ptypes_j.length
13709+ pt_cj = ptypes_j[dpcj]
13710+ end
13711+ declare_var(pnames_j[dpcj], pt_cj)
13712+ dpcj = dpcj + 1
13713+ end
1362813714 pk = 0
1362913715 while pk < pnames_j.length
1363013716 if pk < ptypes_j.length
@@ -13652,6 +13738,7 @@
1365213738 end
1365313739 pk = pk + 1
1365413740 end
13741+ pop_scope
1365513742 if inner_changed == 1
1365613743 all_ptypes[mj] = ptypes_j.join(",")
1365713744 cls_changed = 1
@@ -13950,6 +14037,20 @@
1395014037 if nt == "StringNode" || nt == "IntegerNode" || nt == "FloatNode" || nt == "SymbolNode" || nt == "NilNode" || nt == "TrueNode" || nt == "FalseNode"
1395114038 return 1
1395214039 end
14040+ # Reading another local that's already typed (typically a
14041+ # sibling param) is reliable enough -- the analyzer's scope
14042+ # holds the declared type for that name. Without this,
14043+ # `opts = content_or_opts` inside a method body (where both
14044+ # are params, one nil-defaulted int, the other poly-defaulted)
14045+ # left `opts` typed nil/int even though the body write
14046+ # delivered poly, and the C emit type-mismatched the
14047+ # assignment.
14048+ if nt == "LocalVariableReadNode"
14049+ vt = find_var_type(@nd_name[nid])
14050+ if vt != "" && vt != "int" && vt != "nil"
14051+ return 1
14052+ end
14053+ end
1395314054 0
1395414055 end
1395514056