Commit 1a954abe

1a954abe2238Merged

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

Description

fix(codegen): #314 (A) walk if-predicate / else / case-when in collect_param_methods

infer_cls_meth_param_from_body derives a param's static type from the methods called on it inside the body — when the body says p.title; p.body and one user class has both, that class becomes the param's type. Without this signal a param without call-site widening defaults to "int", and any p.method(...) access then falls through to compile_int_class_fallback_expr — which silently picks the first user class that happens to define the method and emits ((sp_<that-class> *)lv_p)->iv_<method> regardless of the actual class the call site passes.

The walker that drives the body inference (collect_param_methods) covered @nd_body / @nd_stmts / @nd_expression / @nd_args / @nd_receiver but NOT @nd_predicate, @nd_subsequent, @nd_else_clause, @nd_collection, @nd_block, @nd_elements, @nd_parts, or @nd_conditions. Bodies like:

def update(p) if p.title.nil? nil else self.title = p.title end end

had every p.<method> use buried inside an if-predicate or else-branch — the walker visited the IfNode and recursed only into @nd_body, returned called=[], and the param-from-body inference skipped the method entirely. Same shape for case p; when Foo ... end where the predicate / when-conditions hold the only references to p.

Surfaced via Roundhouse's Article#update(p) body, which had exactly this if/predicate-elsewhere shape. Without the fix the emitted C cast lv_p to ArticleRow for .title access AND to ActionResponse for .body access — same value used as two incompatible struct pointers in a single function body, structurally invalid C.

Companion helper method_defined_on_multiple_user_classes is introduced (gated unused right now) for a future tightening of the int-class fallback's first-match-wins behavior; the body-walker fix alone closes the Roundhouse blocker.

Roundhouse archive: residual errors on make spinel-test-build drop from 2 to 0; all four test binaries (article_test, comment_test, articles_controller_test, comments_controller_test) build cleanly with this commit. Combined with the (B) fix in da8fca7, the spinel-side blockers Sam Ruby flagged in #314 are fully resolved.

Refs #314

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

Details

Commit
1a954abe2238
Parent
c9958f2712aa

spinel_codegen.rbmodifiedView file

@@ -10358,6 +10358,47 @@
1035810358 if @nd_receiver[nid] >= 0
1035910359 collect_param_methods(@nd_receiver[nid], pname, acc)
1036010360 end
10361+ # Issue #314 (A): an IfNode's predicate / else-branch and a
10362+ # CaseNode's predicate / when-conditions used to be invisible to
10363+ # this walk. `def update(p); if p.title.nil?; ...; else self.title
10364+ # = p.title; end; end` had `p.title` only inside the predicate +
10365+ # else, so collect returned []; body-side type inference then
10366+ # left `p` at "int" and the int-class fallback (or its companion
10367+ # in infer_recv_method_type) silently picked an arbitrary user
10368+ # class.
10369+ if @nd_predicate[nid] >= 0
10370+ collect_param_methods(@nd_predicate[nid], pname, acc)
10371+ end
10372+ if @nd_subsequent[nid] >= 0
10373+ collect_param_methods(@nd_subsequent[nid], pname, acc)
10374+ end
10375+ if @nd_else_clause[nid] >= 0
10376+ collect_param_methods(@nd_else_clause[nid], pname, acc)
10377+ end
10378+ if @nd_collection[nid] >= 0
10379+ collect_param_methods(@nd_collection[nid], pname, acc)
10380+ end
10381+ if @nd_block[nid] >= 0
10382+ collect_param_methods(@nd_block[nid], pname, acc)
10383+ end
10384+ elems = parse_id_list(@nd_elements[nid])
10385+ k = 0
10386+ while k < elems.length
10387+ collect_param_methods(elems[k], pname, acc)
10388+ k = k + 1
10389+ end
10390+ parts = parse_id_list(@nd_parts[nid])
10391+ k = 0
10392+ while k < parts.length
10393+ collect_param_methods(parts[k], pname, acc)
10394+ k = k + 1
10395+ end
10396+ conds = parse_id_list(@nd_conditions[nid])
10397+ k = 0
10398+ while k < conds.length
10399+ collect_param_methods(conds[k], pname, acc)
10400+ k = k + 1
10401+ end
1036110402 end
1036210403
1036310404 # Issue #58: collect every element type seen in `pname.push(elem)`
@@ -10750,6 +10791,63 @@
1075010791 0
1075110792 end
1075210793
10794+ # Issue #314 (A): true when `mname` is defined on 2+ user classes
10795+ # (as a method, attr_reader, or attr_writer for `=`-suffixed
10796+ # `mname`). Used to gate compile_int_class_fallback_expr — when the
10797+ # name is ambiguous across user classes, the "pick first match"
10798+ # walk silently grabs an arbitrary class, and an unpinned-mrb_int
10799+ # receiver routed through it is almost certainly wrong. Refuse the
10800+ # cast and let the unresolved-call placeholder fire instead.
10801+ def method_defined_on_multiple_user_classes(mname)
10802+ is_writer = 0
10803+ base_mname = mname
10804+ if mname.length > 1 && mname[mname.length - 1] == "="
10805+ is_writer = 1
10806+ base_mname = mname[0, mname.length - 1]
10807+ end
10808+ count = 0
10809+ ci = 0
10810+ while ci < @cls_names.length
10811+ found = 0
10812+ mns = @cls_meth_names[ci].split(";")
10813+ jj = 0
10814+ while jj < mns.length
10815+ if mns[jj] == mname
10816+ found = 1
10817+ end
10818+ jj = jj + 1
10819+ end
10820+ if found == 0
10821+ readers = @cls_attr_readers[ci].split(";")
10822+ rk = 0
10823+ while rk < readers.length
10824+ if readers[rk] == base_mname
10825+ found = 1
10826+ end
10827+ rk = rk + 1
10828+ end
10829+ end
10830+ if found == 0 && is_writer == 1
10831+ writers = @cls_attr_writers[ci].split(";")
10832+ wk = 0
10833+ while wk < writers.length
10834+ if writers[wk] == base_mname
10835+ found = 1
10836+ end
10837+ wk = wk + 1
10838+ end
10839+ end
10840+ if found == 1
10841+ count = count + 1
10842+ if count >= 2
10843+ return 1
10844+ end
10845+ end
10846+ ci = ci + 1
10847+ end
10848+ 0
10849+ end
10850+
1075310851 # Whitelist of methods defined on built-in primitive types that
1075410852 # commonly collide with user-class method names. Mirrors (and
1075510853 # delegates the surface to) the predicate