Commit 650ced6a

650ced6a69beMerged

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

Description

fix(codegen): bare new in inherited cls method dispatches to subclass

A class method defined on a parent class containing bare new (no explicit self.), e.g., def self.create; new; end, resolved statically to the parent's constructor instead of the calling subclass. Article.create.x (where Article < Base, only Article defines x) emitted "cannot resolve call to 'x' on obj_Base" and fell through to 0; the variant def self.create(attrs); new(attrs); end produced a C-level type mismatch (sp_Base_new(lv_attrs) calling Base's 0-arg new with an extra pointer arg). Sister to issue #208 (cls method dispatch through parent chain); this is the same family but for the bare-new resolution inside the inherited body.

Three pieces:

  • propagate_inherited_class_methods (Pass 1.6 in collect_all): copy each parent cls method into every subclass's @cls_cmeth_* table. The synthetic copy reuses the parent's body AST id; emit_class_methods recompiles it under the subclass's @current_class_idx, so a bare new in the body resolves to sp_<Subclass>_new.

  • propagate_bare_new_to_subclass_initialize (in infer_all_returns, runs inside the iterative type-inference loop): walks each cls method body for bare new(args) and unifies the matching find_init_class(ci).initialize ptypes with arg types. Args that read a cls-method local resolve via pnames/ptypes (already widened by issue #207's call-site logic), other args fall through to infer_type with @current_class_idx pinned so @ivar refs in args resolve correctly. Without this the synthetic Article.create's new(attrs) would call sp_Article_new(mrb_int) with a sp_SymStrHash * arg.

  • emit_class_methods skip via body_has_bare_new_with_args: when a class has no resolvable initialize (no own one and no inherited one) and its cls method body contains bare new(args) with args, skip emission. The synthesized constructor would be 0-arg, so calling it with args would not C-compile. This shape only arises on parent classes that define def self.factory(attrs); new(attrs); ...; end to be inherited by subclasses with attrs-taking initializers; calling the parent's version directly is a runtime error in CRuby anyway.

The basic case (no-arg bare new) and variant case (bare new(attrs)) both verified end-to-end. Bootstrap clean (gen2.c == gen3.c). Sized at +337 in spinel_codegen.rb plus a new regression test exercising single-level, multi-level, and arity-bearing inheritance paths.

Closes #224 Refs #208

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

Details

Commit
650ced6a69be
Parent
a8c387d55a4b

spinel_codegen.rbmodifiedView file

@@ -4803,6 +4803,63 @@
48034803 # a fatal program error: bail with a clear message instead of letting
48044804 # the recursive helpers loop forever. Self-inheritance (`class A < A`)
48054805 # is detected as the trivial 1-step cycle.
4806+ # Issue #224: copy each inherited class method (def self.<m> on a
4807+ # parent class) into every subclass's @cls_cmeth_* tables so the
4808+ # subclass gets its own synthetic copy. Each copy reuses the
4809+ # parent's AST body id; emit_class_methods then re-compiles the
4810+ # body under the subclass's @current_class_idx, and a bare `new`
4811+ # inside the body resolves to the subclass's constructor.
4812+ #
4813+ # Run after class collection so all parents are populated, and
4814+ # before infer_all_returns / call-site widening so the synthetic
4815+ # entries participate in regular type inference.
4816+ def propagate_inherited_class_methods
4817+ ci = 0
4818+ while ci < @cls_names.length
4819+ seen = @cls_cmeth_names[ci].split(";")
4820+ cur_parent = @cls_parents[ci]
4821+ while cur_parent != ""
4822+ pi = find_class_idx(cur_parent)
4823+ if pi < 0
4824+ cur_parent = ""
4825+ else
4826+ p_cmnames = @cls_cmeth_names[pi].split(";")
4827+ p_cmreturns = @cls_cmeth_returns[pi].split(";")
4828+ p_cmbodies = @cls_cmeth_bodies[pi].split(";")
4829+ p_cmparams = @cls_cmeth_params[pi].split("|")
4830+ p_cmptypes = @cls_cmeth_ptypes[pi].split("|")
4831+ pj = 0
4832+ while pj < p_cmnames.length
4833+ mname = p_cmnames[pj]
4834+ if mname != "" && not_in(mname, seen) == 1
4835+ pret = "int"
4836+ if pj < p_cmreturns.length
4837+ pret = p_cmreturns[pj]
4838+ end
4839+ pbid = -1
4840+ if pj < p_cmbodies.length
4841+ pbid = p_cmbodies[pj].to_i
4842+ end
4843+ pparams = ""
4844+ if pj < p_cmparams.length
4845+ pparams = p_cmparams[pj]
4846+ end
4847+ pptypes = ""
4848+ if pj < p_cmptypes.length
4849+ pptypes = p_cmptypes[pj]
4850+ end
4851+ append_cls_cmeth(ci, mname, pparams, pptypes, pret, pbid)
4852+ seen.push(mname)
4853+ end
4854+ pj = pj + 1
4855+ end
4856+ cur_parent = @cls_parents[pi]
4857+ end
4858+ end
4859+ ci = ci + 1
4860+ end
4861+ end
4862+
48064863 def detect_circular_inheritance
48074864 i = 0
48084865 while i < @cls_names.length
@@ -4896,6 +4953,16 @@
48964953 # like CRuby. Issue #106.
48974954 detect_circular_inheritance
48984955
4956+ # Pass 1.6: copy inherited class methods (def self.<m>) into each
4957+ # subclass's @cls_cmeth_* table so the subclass gets its own
4958+ # synthetic copy of the parent's body. The copy compiles under
4959+ # the subclass's @current_class_idx, which means a bare `new`
4960+ # inside `def self.create; new; end` resolves to the subclass's
4961+ # constructor (issue #224). Without this, `new` statically binds
4962+ # to the lexical class — so `Article.create` returns a `Base`
4963+ # instance instead of an `Article`.
4964+ propagate_inherited_class_methods
4965+
48994966 # Pass 2: top-level methods, constants, define_method
49004967 stmts.each { |sid|
49014968 if @nd_type[sid] == "DefNode"
@@ -7743,6 +7810,155 @@
77437810 end
77447811 end
77457812
7813+ # Issue #224: scan_new_calls' `new` handler only widens an
7814+ # `initialize`'s ptypes when the call has an explicit class
7815+ # receiver (`Article.new(attrs)`). The bare form `new(attrs)`
7816+ # inside a class method body has no receiver, so the ptypes
7817+ # never propagate — leaving e.g. `sp_Article_new(mrb_int)`
7818+ # called with `sp_SymStrHash *` and a C-level type mismatch.
7819+ #
7820+ # propagate_inherited_class_methods has already given each
7821+ # subclass its own copy of the inherited cls method, with
7822+ # @cls_cmeth_ptypes widened from explicit call sites by issue
7823+ # #207's logic at scan_new_calls' tail. This pass walks each
7824+ # cls method body for bare `new(args)` and unifies the matching
7825+ # `find_init_class(ci).initialize` ptypes — args that read a
7826+ # cls-method local resolve via the cls method's pnames/ptypes
7827+ # (which carry the call-site type), other args fall through
7828+ # to infer_type with @current_class_idx pinned so @ivar refs
7829+ # in the args resolve against the right class.
7830+ def propagate_bare_new_to_subclass_initialize
7831+ ci = 0
7832+ while ci < @cls_names.length
7833+ cmnames = @cls_cmeth_names[ci].split(";")
7834+ cm_bodies = @cls_cmeth_bodies[ci].split(";")
7835+ cm_params = @cls_cmeth_params[ci].split("|")
7836+ cm_ptypes = @cls_cmeth_ptypes[ci].split("|")
7837+ j = 0
7838+ while j < cmnames.length
7839+ bid = -1
7840+ if j < cm_bodies.length
7841+ bs = cm_bodies[j]
7842+ if bs != ""
7843+ bid = bs.to_i
7844+ end
7845+ end
7846+ pnames = "".split(",")
7847+ if j < cm_params.length
7848+ pnames = cm_params[j].split(",")
7849+ end
7850+ ptypes = "".split(",")
7851+ if j < cm_ptypes.length
7852+ ptypes = cm_ptypes[j].split(",")
7853+ end
7854+ if bid > 0
7855+ saved_ci = @current_class_idx
7856+ @current_class_idx = ci
7857+ walk_bare_new_in_cmeth_body(bid, ci, pnames, ptypes)
7858+ @current_class_idx = saved_ci
7859+ end
7860+ j = j + 1
7861+ end
7862+ ci = ci + 1
7863+ end
7864+ end
7865+
7866+ def walk_bare_new_in_cmeth_body(nid, cls_ci, cm_pnames, cm_ptypes)
7867+ if nid < 0
7868+ return
7869+ end
7870+ if @nd_type[nid] == "CallNode" && @nd_name[nid] == "new" && @nd_receiver[nid] < 0
7871+ args_id = @nd_arguments[nid]
7872+ if args_id >= 0
7873+ arg_ids = get_args(args_id)
7874+ init_ci = find_init_class(cls_ci)
7875+ if init_ci >= 0
7876+ init_idx = cls_find_method_direct(init_ci, "initialize")
7877+ if init_idx >= 0
7878+ all_ptypes = @cls_meth_ptypes[init_ci].split("|")
7879+ if init_idx < all_ptypes.length
7880+ init_ptypes = all_ptypes[init_idx].split(",")
7881+ kk = 0
7882+ while kk < arg_ids.length
7883+ at = ""
7884+ if @nd_type[arg_ids[kk]] == "LocalVariableReadNode"
7885+ vname = @nd_name[arg_ids[kk]]
7886+ pi = 0
7887+ while pi < cm_pnames.length
7888+ if cm_pnames[pi] == vname && pi < cm_ptypes.length
7889+ at = cm_ptypes[pi]
7890+ end
7891+ pi = pi + 1
7892+ end
7893+ end
7894+ if at == ""
7895+ at = infer_type(arg_ids[kk])
7896+ end
7897+ if kk < init_ptypes.length && at != "" && at != "int"
7898+ init_ptypes[kk] = unify_call_types(init_ptypes[kk], at, arg_ids[kk])
7899+ end
7900+ kk = kk + 1
7901+ end
7902+ all_ptypes[init_idx] = init_ptypes.join(",")
7903+ @cls_meth_ptypes[init_ci] = all_ptypes.join("|")
7904+ end
7905+ end
7906+ end
7907+ end
7908+ end
7909+ # Mirror scan_new_calls' recursion shape so a `new(...)` at any
7910+ # depth in the body is reachable: stmts, body, conditionals,
7911+ # call args, expressions, etc.
7912+ if @nd_body[nid] >= 0
7913+ walk_bare_new_in_cmeth_body(@nd_body[nid], cls_ci, cm_pnames, cm_ptypes)
7914+ end
7915+ bn_stmts = parse_id_list(@nd_stmts[nid])
7916+ k = 0
7917+ while k < bn_stmts.length
7918+ walk_bare_new_in_cmeth_body(bn_stmts[k], cls_ci, cm_pnames, cm_ptypes)
7919+ k = k + 1
7920+ end
7921+ if @nd_receiver[nid] >= 0
7922+ walk_bare_new_in_cmeth_body(@nd_receiver[nid], cls_ci, cm_pnames, cm_ptypes)
7923+ end
7924+ if @nd_arguments[nid] >= 0
7925+ walk_bare_new_in_cmeth_body(@nd_arguments[nid], cls_ci, cm_pnames, cm_ptypes)
7926+ end
7927+ bn_args = parse_id_list(@nd_args[nid])
7928+ k = 0
7929+ while k < bn_args.length
7930+ walk_bare_new_in_cmeth_body(bn_args[k], cls_ci, cm_pnames, cm_ptypes)
7931+ k = k + 1
7932+ end
7933+ if @nd_expression[nid] >= 0
7934+ walk_bare_new_in_cmeth_body(@nd_expression[nid], cls_ci, cm_pnames, cm_ptypes)
7935+ end
7936+ if @nd_predicate[nid] >= 0
7937+ walk_bare_new_in_cmeth_body(@nd_predicate[nid], cls_ci, cm_pnames, cm_ptypes)
7938+ end
7939+ if @nd_subsequent[nid] >= 0
7940+ walk_bare_new_in_cmeth_body(@nd_subsequent[nid], cls_ci, cm_pnames, cm_ptypes)
7941+ end
7942+ if @nd_else_clause[nid] >= 0
7943+ walk_bare_new_in_cmeth_body(@nd_else_clause[nid], cls_ci, cm_pnames, cm_ptypes)
7944+ end
7945+ if @nd_left[nid] >= 0
7946+ walk_bare_new_in_cmeth_body(@nd_left[nid], cls_ci, cm_pnames, cm_ptypes)
7947+ end
7948+ if @nd_right[nid] >= 0
7949+ walk_bare_new_in_cmeth_body(@nd_right[nid], cls_ci, cm_pnames, cm_ptypes)
7950+ end
7951+ if @nd_block[nid] >= 0
7952+ walk_bare_new_in_cmeth_body(@nd_block[nid], cls_ci, cm_pnames, cm_ptypes)
7953+ end
7954+ bn_elems = parse_id_list(@nd_elements[nid])
7955+ k = 0
7956+ while k < bn_elems.length
7957+ walk_bare_new_in_cmeth_body(bn_elems[k], cls_ci, cm_pnames, cm_ptypes)
7958+ k = k + 1
7959+ end
7960+ end
7961+
77467962 def update_ivar_types_from_params
77477963 # Special case: synthetic struct constructors - ivars match params directly
77487964 i = 0
@@ -9031,6 +9247,14 @@
90319247 infer_cls_meth_param_from_body
90329248 # Pre-pass: scan for .new calls to infer constructor param types
90339249 infer_constructor_types
9250+ # Issue #224: bare `new(args)` in an inherited class method body
9251+ # widens the subclass's `initialize` ptypes via the cls method's
9252+ # already-widened ptypes. Runs after infer_constructor_types so
9253+ # the cls method ptypes (set by issue #207's logic in
9254+ # scan_new_calls) are current; runs inside the iterative loop
9255+ # so subsequent rounds pick up cls method widening from new
9256+ # call sites.
9257+ propagate_bare_new_to_subclass_initialize
90349258 # Update ivar types from constructor params
90359259 update_ivar_types_from_params
90369260 # Infer setter param types from ivar types
@@ -13354,13 +13578,124 @@
1335413578 if j < cm_ptypes.length
1335513579 ptypes = cm_ptypes[j].split(",")
1335613580 end
13357- emit_class_level_method(i, cmnames[j], pnames, ptypes, rt, bid)
13358- j = j + 1
13581+ # Issue #224: a class's cls method whose body has bare
13582+ # `new(args)` would emit `sp_<this_class>_new(args)`. If
13583+ # this class has no resolvable initialize (no own one and
13584+ # no inherited one), the synthesized constructor is 0-arg —
13585+ # so the call wouldn't C-compile. This shape only arises
13586+ # when a *parent* class defines `def self.factory(attrs);
13587+ # new(attrs); ...; end` to be inherited by subclasses with
13588+ # an attrs-taking initialize; calling the parent's version
13589+ # directly is a runtime error in CRuby anyway. Skip emit
13590+ # rather than producing a non-compiling body.
13591+ if bid > 0 && find_init_class(i) < 0 && body_has_bare_new_with_args(bid) == 1
13592+ j = j + 1
13593+ else
13594+ emit_class_level_method(i, cmnames[j], pnames, ptypes, rt, bid)
13595+ j = j + 1
13596+ end
1335913597 end
1336013598 i = i + 1
1336113599 end
1336213600 end
1336313601
13602+ # Issue #224: detect bare `new(args)` (recv < 0, args.length > 0)
13603+ # anywhere in a cls method body. Used by emit_class_methods to
13604+ # skip emitting a class's version of an inherited method when the
13605+ # class has no resolvable initialize — emitting would synthesize
13606+ # `sp_<class>_new(args)` against a 0-arg default constructor and
13607+ # the C compiler would reject the body. The synthetic copies on
13608+ # subclasses (with their own initialize) emit normally.
13609+ def body_has_bare_new_with_args(nid)
13610+ if nid < 0
13611+ return 0
13612+ end
13613+ if @nd_type[nid] == "CallNode" && @nd_name[nid] == "new" && @nd_receiver[nid] < 0
13614+ args_id = @nd_arguments[nid]
13615+ if args_id >= 0
13616+ arg_ids = get_args(args_id)
13617+ if arg_ids.length > 0
13618+ return 1
13619+ end
13620+ end
13621+ end
13622+ if @nd_body[nid] >= 0
13623+ if body_has_bare_new_with_args(@nd_body[nid]) == 1
13624+ return 1
13625+ end
13626+ end
13627+ bn_stmts = parse_id_list(@nd_stmts[nid])
13628+ k = 0
13629+ while k < bn_stmts.length
13630+ if body_has_bare_new_with_args(bn_stmts[k]) == 1
13631+ return 1
13632+ end
13633+ k = k + 1
13634+ end
13635+ if @nd_receiver[nid] >= 0
13636+ if body_has_bare_new_with_args(@nd_receiver[nid]) == 1
13637+ return 1
13638+ end
13639+ end
13640+ if @nd_arguments[nid] >= 0
13641+ if body_has_bare_new_with_args(@nd_arguments[nid]) == 1
13642+ return 1
13643+ end
13644+ end
13645+ bn_args = parse_id_list(@nd_args[nid])
13646+ k = 0
13647+ while k < bn_args.length
13648+ if body_has_bare_new_with_args(bn_args[k]) == 1
13649+ return 1
13650+ end
13651+ k = k + 1
13652+ end
13653+ if @nd_expression[nid] >= 0
13654+ if body_has_bare_new_with_args(@nd_expression[nid]) == 1
13655+ return 1
13656+ end
13657+ end
13658+ if @nd_predicate[nid] >= 0
13659+ if body_has_bare_new_with_args(@nd_predicate[nid]) == 1
13660+ return 1
13661+ end
13662+ end
13663+ if @nd_subsequent[nid] >= 0
13664+ if body_has_bare_new_with_args(@nd_subsequent[nid]) == 1
13665+ return 1
13666+ end
13667+ end
13668+ if @nd_else_clause[nid] >= 0
13669+ if body_has_bare_new_with_args(@nd_else_clause[nid]) == 1
13670+ return 1
13671+ end
13672+ end
13673+ if @nd_left[nid] >= 0
13674+ if body_has_bare_new_with_args(@nd_left[nid]) == 1
13675+ return 1
13676+ end
13677+ end
13678+ if @nd_right[nid] >= 0
13679+ if body_has_bare_new_with_args(@nd_right[nid]) == 1
13680+ return 1
13681+ end
13682+ end
13683+ if @nd_block[nid] >= 0
13684+ if body_has_bare_new_with_args(@nd_block[nid]) == 1
13685+ return 1
13686+ end
13687+ end
13688+ bn_elems = parse_id_list(@nd_elements[nid])
13689+ k = 0
13690+ while k < bn_elems.length
13691+ if body_has_bare_new_with_args(bn_elems[k]) == 1
13692+ return 1
13693+ end
13694+ k = k + 1
13695+ end
13696+ 0
13697+ end
13698+
1336413699 def emit_constructor(ci)
1336513700 saved_gc_scope = @in_gc_scope
1336613701 cname = @cls_names[ci]