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
if @nd_receiver[nid] >= 0 collect_param_methods(@nd_receiver[nid], pname, acc) end # Issue #314 (A): an IfNode's predicate / else-branch and a # CaseNode's predicate / when-conditions used to be invisible to # this walk. `def update(p); if p.title.nil?; ...; else self.title # = p.title; end; end` had `p.title` only inside the predicate + # else, so collect returned []; body-side type inference then # left `p` at "int" and the int-class fallback (or its companion # in infer_recv_method_type) silently picked an arbitrary user # class. if @nd_predicate[nid] >= 0 collect_param_methods(@nd_predicate[nid], pname, acc) end if @nd_subsequent[nid] >= 0 collect_param_methods(@nd_subsequent[nid], pname, acc) end if @nd_else_clause[nid] >= 0 collect_param_methods(@nd_else_clause[nid], pname, acc) end if @nd_collection[nid] >= 0 collect_param_methods(@nd_collection[nid], pname, acc) end if @nd_block[nid] >= 0 collect_param_methods(@nd_block[nid], pname, acc) end elems = parse_id_list(@nd_elements[nid]) k = 0 while k < elems.length collect_param_methods(elems[k], pname, acc) k = k + 1 end parts = parse_id_list(@nd_parts[nid]) k = 0 while k < parts.length collect_param_methods(parts[k], pname, acc) k = k + 1 end conds = parse_id_list(@nd_conditions[nid]) k = 0 while k < conds.length collect_param_methods(conds[k], pname, acc) k = k + 1 end end # Issue #58: collect every element type seen in `pname.push(elem)` 0 end # Issue #314 (A): true when `mname` is defined on 2+ user classes # (as a method, attr_reader, or attr_writer for `=`-suffixed # `mname`). Used to gate compile_int_class_fallback_expr — when the # name is ambiguous across user classes, the "pick first match" # walk silently grabs an arbitrary class, and an unpinned-mrb_int # receiver routed through it is almost certainly wrong. Refuse the # cast and let the unresolved-call placeholder fire instead. def method_defined_on_multiple_user_classes(mname) is_writer = 0 base_mname = mname if mname.length > 1 && mname[mname.length - 1] == "=" is_writer = 1 base_mname = mname[0, mname.length - 1] end count = 0 ci = 0 while ci < @cls_names.length found = 0 mns = @cls_meth_names[ci].split(";") jj = 0 while jj < mns.length if mns[jj] == mname found = 1 end jj = jj + 1 end if found == 0 readers = @cls_attr_readers[ci].split(";") rk = 0 while rk < readers.length if readers[rk] == base_mname found = 1 end rk = rk + 1 end end if found == 0 && is_writer == 1 writers = @cls_attr_writers[ci].split(";") wk = 0 while wk < writers.length if writers[wk] == base_mname found = 1 end wk = wk + 1 end end if found == 1 count = count + 1 if count >= 2 return 1 end end ci = ci + 1 end 0 end # Whitelist of methods defined on built-in primitive types that # commonly collide with user-class method names. Mirrors (and # delegates the surface to) the predicatetest/param_inference_if_predicate.rbaddedView file
Loading diff…
test/param_inference_if_predicate.rb.expectedaddedView file
Loading diff…