Commit eaad8a4e

eaad8a4edb93Merged

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

Description

fix(codegen): #207 factory pattern — implicit new + cls method param widening

The Rails-idiomatic ActiveRecord-style factory shape

class T attr_accessor :title def self.from_raw(params) instance = new instance.title = params.fetch(:title, "") instance end end

hits four composing gaps in spinel's class-method handling. Fix each:

  1. Bare new inside a def self.<m> body fell through to the "cannot resolve call to 'new' on (no receiver)" warning because compile_call_expr short-circuits to compile_no_recv_call_expr for recv < 0 before reaching the constructor branch. Add a new handler at the top of compile_no_recv_call_expr that delegates to compile_constructor_expr; teach compile_constructor_expr / infer_constructor_type to fall back on current_class_method_owning_class when the receiver is empty.

  2. infer_all_returns' class-method loop didn't push params or scan_locals before infer_body_return — so the local instance = new reference resolved with no scope, and the factory's return type degenerated to mrb_int. Mirror the instance-method side's scope setup for cls methods, including pinning @current_method_name to "cls" so implicit new resolves while inferring the return.

  3. infer_ivar_types_from_writers walked instance-method bodies but skipped class-method bodies, so a instance.title = poly_value call inside def self.from_raw couldn't widen the iv_title slot. Add a parallel cls_cmeth_bodies loop with scan_writer_calls.

  4. scan_new_calls had a receiver-method unify path for obj.method(args) calls (PR #179) but no equivalent for class-constant recvs (T.from_raw(p)), so cls method params never widened from external call sites. Add a ConstantReadNode-recv branch that walks @cls_cmeth_ptypes the same way. Also iterate class methods in infer_class_body_call_types so calls inside cls method bodies (e.g. params.fetch(...) widening fetch's default param) propagate.

current_class_method_owning_class accepts both <Class>_cls_<m> (scan-side) and bare <m> (emit-side) format because the two sides set @current_method_name differently.

The original full-recursion repro in #207 still hits a deeper gap (is_a?(Hash) type narrowing for symbolize_keys' recursive call) that's out of scope here. The pragmatic factory shape (the canonical real-blog from_raw) is fixed.

Refs #207

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

Details

Commit
eaad8a4edb93
Parent
e341e5e20470

spinel_codegen.rbmodifiedView file

@@ -3187,6 +3187,15 @@
31873187
31883188 def infer_constructor_type(nid, mname, recv)
31893189 if mname == "new"
3190+ # Issue #207: implicit recv-less `new` in a class method body
3191+ # resolves to obj_<CurrentClass> so subsequent attr_writer
3192+ # calls and ivar widening see the right type.
3193+ if recv < 0
3194+ implicit = current_class_method_owning_class
3195+ if implicit != ""
3196+ return "obj_" + implicit
3197+ end
3198+ end
31903199 if recv >= 0
31913200 rn = constructor_class_name(recv)
31923201 if rn != ""
@@ -7232,6 +7241,45 @@
72327241 end
72337242 end
72347243 end
7244+ # Issue #207: `<Class>.cls_method(args)` — widen class
7245+ # method parameter types from call-site argument types.
7246+ # Same shape as the receiver-method unify above but
7247+ # operating on @cls_cmeth_ptypes for class-constant recvs.
7248+ if @nd_type[nid] == "CallNode" && @nd_receiver[nid] >= 0
7249+ crecv = @nd_receiver[nid]
7250+ if @nd_type[crecv] == "ConstantReadNode" || @nd_type[crecv] == "ConstantPathNode"
7251+ rcname = constructor_class_name(crecv)
7252+ if rcname != ""
7253+ cci = find_class_idx(rcname)
7254+ if cci >= 0
7255+ cmname = @nd_name[nid]
7256+ cmnames = @cls_cmeth_names[cci].split(";")
7257+ cm_ptypes_all = @cls_cmeth_ptypes[cci].split("|")
7258+ cmidx = 0
7259+ while cmidx < cmnames.length
7260+ if cmnames[cmidx] == cmname
7261+ args_id = @nd_arguments[nid]
7262+ if args_id >= 0 && cmidx < cm_ptypes_all.length
7263+ arg_ids = get_args(args_id)
7264+ cmptypes = cm_ptypes_all[cmidx].split(",")
7265+ kk = 0
7266+ while kk < arg_ids.length
7267+ at = infer_type(arg_ids[kk])
7268+ if kk < cmptypes.length
7269+ cmptypes[kk] = unify_call_types(cmptypes[kk], at, arg_ids[kk])
7270+ end
7271+ kk = kk + 1
7272+ end
7273+ cm_ptypes_all[cmidx] = cmptypes.join(",")
7274+ @cls_cmeth_ptypes[cci] = cm_ptypes_all.join("|")
7275+ end
7276+ end
7277+ cmidx = cmidx + 1
7278+ end
7279+ end
7280+ end
7281+ end
7282+ end
72357283 end
72367284 # Recurse into children
72377285 if @nd_body[nid] >= 0
@@ -7972,7 +8020,7 @@
79728020 pop_scope
79738021 i = i + 1
79748022 end
7975- # Scan class method bodies
8023+ # Scan class instance method bodies
79768024 ci = 0
79778025 while ci < @cls_names.length
79788026 @current_class_idx = ci
@@ -8015,6 +8063,56 @@
80158063 end
80168064 bj = bj + 1
80178065 end
8066+ # Issue #207: also scan class method bodies (def self.<m>) so
8067+ # an attr_writer call on a freshly-`new`'d instance inside a
8068+ # `def self.from_raw(...)` factory widens the ivar's type.
8069+ cm_bodies = @cls_cmeth_bodies[ci].split(";")
8070+ cm_names = @cls_cmeth_names[ci].split(";")
8071+ cm_params = @cls_cmeth_params[ci].split("|")
8072+ cm_ptypes = @cls_cmeth_ptypes[ci].split("|")
8073+ saved_meth = @current_method_name
8074+ cbj = 0
8075+ while cbj < cm_bodies.length
8076+ cbid = cm_bodies[cbj].to_i
8077+ if cbid >= 0
8078+ # Pin @current_method_name to the "<Class>_cls_<m>" form
8079+ # used by current_class_method_owning_class so implicit
8080+ # bare `new` inside the body resolves to obj_<Class>.
8081+ if cbj < cm_names.length
8082+ @current_method_name = @cls_names[ci] + "_cls_" + cm_names[cbj]
8083+ end
8084+ push_scope
8085+ cpnames = "".split(",")
8086+ cptypes = "".split(",")
8087+ if cbj < cm_params.length
8088+ cpnames = cm_params[cbj].split(",")
8089+ end
8090+ if cbj < cm_ptypes.length
8091+ cptypes = cm_ptypes[cbj].split(",")
8092+ end
8093+ cpk = 0
8094+ while cpk < cpnames.length
8095+ cpt = "int"
8096+ if cpk < cptypes.length
8097+ cpt = cptypes[cpk]
8098+ end
8099+ declare_var(cpnames[cpk], cpt)
8100+ cpk = cpk + 1
8101+ end
8102+ cml = "".split(",")
8103+ cmt = "".split(",")
8104+ scan_locals(cbid, cml, cmt, cpnames)
8105+ clk = 0
8106+ while clk < cml.length
8107+ declare_var(cml[clk], cmt[clk])
8108+ clk = clk + 1
8109+ end
8110+ scan_writer_calls(cbid)
8111+ pop_scope
8112+ end
8113+ cbj = cbj + 1
8114+ end
8115+ @current_method_name = saved_meth
80188116 ci = ci + 1
80198117 end
80208118 @current_class_idx = -1
@@ -8729,10 +8827,16 @@
87298827 j = j + 1
87308828 end
87318829
8732- # Class methods
8830+ # Class methods. Issue #207: mirror the param/local scope
8831+ # setup the instance-method side does so a `def self.<m>`
8832+ # body's return type sees its locals (instance = new etc.)
8833+ # and bare `new` resolves via current_class_method_owning_class.
87338834 cmnames = @cls_cmeth_names[i].split(";")
87348835 cm_bodies = @cls_cmeth_bodies[i].split(";")
87358836 cm_returns = @cls_cmeth_returns[i].split(";")
8837+ cm_params = @cls_cmeth_params[i].split("|")
8838+ cm_ptypes = @cls_cmeth_ptypes[i].split("|")
8839+ saved_meth = @current_method_name
87368840 j = 0
87378841 while j < cmnames.length
87388842 push_scope
@@ -8740,6 +8844,34 @@
87408844 if j < cm_bodies.length
87418845 bid = cm_bodies[j].to_i
87428846 end
8847+ @current_method_name = @cls_names[i] + "_cls_" + cmnames[j]
8848+ cpnames = "".split(",")
8849+ cptypes = "".split(",")
8850+ if j < cm_params.length
8851+ cpnames = cm_params[j].split(",")
8852+ end
8853+ if j < cm_ptypes.length
8854+ cptypes = cm_ptypes[j].split(",")
8855+ end
8856+ cpk = 0
8857+ while cpk < cpnames.length
8858+ cpt = "int"
8859+ if cpk < cptypes.length
8860+ cpt = cptypes[cpk]
8861+ end
8862+ declare_var(cpnames[cpk], cpt)
8863+ cpk = cpk + 1
8864+ end
8865+ if bid >= 0
8866+ ml = "".split(",")
8867+ mt = "".split(",")
8868+ scan_locals(bid, ml, mt, cpnames)
8869+ lk = 0
8870+ while lk < ml.length
8871+ declare_var(ml[lk], mt[lk])
8872+ lk = lk + 1
8873+ end
8874+ end
87438875 rt = infer_body_return(bid)
87448876 if j < cm_returns.length
87458877 cm_returns[j] = rt
@@ -8747,6 +8879,7 @@
87478879 pop_scope
87488880 j = j + 1
87498881 end
8882+ @current_method_name = saved_meth
87508883 @cls_cmeth_returns[i] = cm_returns.join(";")
87518884 @current_class_idx = -1
87528885 i = i + 1
@@ -10323,6 +10456,59 @@
1032310456 end
1032410457 mi = mi + 1
1032510458 end
10459+ # Issue #207: also iterate this class's class methods
10460+ # (def self.<m>) so a `params.fetch(:k, "")` call inside a
10461+ # `def self.from_raw` widens P.fetch's default param via the
10462+ # receiver-method unify path inside scan_new_calls.
10463+ cm_names = @cls_cmeth_names[ci].split(";")
10464+ cm_bodies = @cls_cmeth_bodies[ci].split(";")
10465+ cm_params = @cls_cmeth_params[ci].split("|")
10466+ cm_ptypes = @cls_cmeth_ptypes[ci].split("|")
10467+ saved_meth_cb = @current_method_name
10468+ cmi = 0
10469+ while cmi < cm_names.length
10470+ cbid = -1
10471+ if cmi < cm_bodies.length
10472+ cbid = cm_bodies[cmi].to_i
10473+ end
10474+ if cbid >= 0
10475+ @current_class_idx = ci
10476+ @current_method_name = @cls_names[ci] + "_cls_" + cm_names[cmi]
10477+ push_scope
10478+ cpnames = "".split(",")
10479+ cptypes = "".split(",")
10480+ if cmi < cm_params.length
10481+ cpnames = cm_params[cmi].split(",")
10482+ end
10483+ if cmi < cm_ptypes.length
10484+ cptypes = cm_ptypes[cmi].split(",")
10485+ end
10486+ cpk = 0
10487+ while cpk < cpnames.length
10488+ cpt = "int"
10489+ if cpk < cptypes.length
10490+ cpt = cptypes[cpk]
10491+ end
10492+ if cpnames[cpk] != ""
10493+ declare_var(cpnames[cpk], cpt)
10494+ end
10495+ cpk = cpk + 1
10496+ end
10497+ cml = "".split(",")
10498+ cmt = "".split(",")
10499+ scan_locals_first_type(cbid, cml, cmt, cpnames)
10500+ clk = 0
10501+ while clk < cml.length
10502+ declare_var(cml[clk], cmt[clk])
10503+ clk = clk + 1
10504+ end
10505+ scan_new_calls(cbid)
10506+ pop_scope
10507+ @current_class_idx = -1
10508+ end
10509+ cmi = cmi + 1
10510+ end
10511+ @current_method_name = saved_meth_cb
1032610512 ci = ci + 1
1032710513 end
1032810514 pass = pass + 1
@@ -16297,6 +16483,17 @@
1629716483 end
1629816484
1629916485 def compile_no_recv_call_expr(nid, mname)
16486+ # Issue #207: bare `new` inside a `def self.<m>` body resolves
16487+ # to <CurrentClass>.new. Dispatched here (rather than relying on
16488+ # the later `mname == "new"` branch in compile_call_expr) because
16489+ # compile_call_expr short-circuits to this function for recv < 0
16490+ # before reaching that branch.
16491+ if mname == "new"
16492+ r = compile_constructor_expr(nid, -1)
16493+ if r != ""
16494+ return r
16495+ end
16496+ end
1630016497 # catch as expression
1630116498 if mname == "catch"
1630216499 if @nd_block[nid] >= 0
@@ -17296,8 +17493,48 @@
1729617493 ""
1729717494 end
1729817495
17496+ # Issue #207: implicit `new` (recv-less) inside a `def self.<m>`
17497+ # body resolves to <CurrentClass>.new. Without this the recv < 0
17498+ # branch fell through to "0" with the warning, leaving a factory
17499+ # like `def self.from_raw(p); instance = new; instance.x = ...; end`
17500+ # broken. Mirrors how implicit `self` inside instance methods
17501+ # already routes recv-less calls to the enclosing class.
17502+ def implicit_new_class_name(recv)
17503+ if recv >= 0
17504+ return ""
17505+ end
17506+ return current_class_method_owning_class
17507+ end
17508+
17509+ def current_class_method_owning_class
17510+ if @current_method_name == ""
17511+ return ""
17512+ end
17513+ # Two formats land here: scan_writer_calls pins
17514+ # "<Class>_cls_<m>" so we can split on the marker; emit-side
17515+ # sets just "<m>" and relies on @current_class_idx for context.
17516+ cls_idx = @current_method_name.index("_cls_")
17517+ if cls_idx != nil && cls_idx >= 0
17518+ return @current_method_name[0, cls_idx]
17519+ end
17520+ if @current_class_idx >= 0 && @current_class_idx < @cls_names.length
17521+ cm = @cls_cmeth_names[@current_class_idx].split(";")
17522+ k = 0
17523+ while k < cm.length
17524+ if cm[k] == @current_method_name
17525+ return @cls_names[@current_class_idx]
17526+ end
17527+ k = k + 1
17528+ end
17529+ end
17530+ ""
17531+ end
17532+
1729917533 def compile_constructor_expr(nid, recv)
1730017534 cname = constructor_class_name(recv)
17535+ if cname == ""
17536+ cname = implicit_new_class_name(recv)
17537+ end
1730117538 if cname != ""
1730217539 if cname == "Proc"
1730317540 if @nd_block[nid] >= 0