Commit 22863fac
22863fac2e07Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(analyze): propagate ctor-callsite arg types to ivar observations
Second half of the #575 fix. After the codegen-side change in the
prior commit, the emit loop consults the existing narrow-class-set
helper — but the helper kept returning empty for the canonical
shape because @ivar = w (where w is a constructor param) recorded
only the param's already-widened "poly" type. The concrete obj_X
classes that flow into w across each <C>.new(<obj_X>) callsite
were never reaching the ivar observation list.
Fixes:
-
Analyzer:
propagate_ctor_arg_types_to_ivar_observationswalks every<Class>.new(args)call in the program, indexes the args by class, then for each class with a recorded initialize walks<C>#initialize's body for@ivar = pnamewrites. Each matching arg's concrete obj_X type lands as an additional observation on @ivar. Runs once after the iterative fixpoint has converged so it sees the final ptypes / writer set. -
compiler_helpers:
poly_dispatch_narrow_class_setno longer bails when it encounters a bare "poly" observation. The writer-scan records "poly" for any union-widened write (canonically a method-param-to-ivar shape); when the new propagation also lays down the concrete obj_X siblings, the obj_X set is the sharp answer and the stale "poly" entry is uninformative noise rather than a true broadening. Truly scalar observations (int / string / float / ...) still bail so we don't unsoundly prune.
For the canonical #575 repro the dispatch table shrinks from 3
arms (Worker, Other, Unrelated) to 2 (Worker, Other), the result
temp's static type stays string (no widening to sp_RbVal), and the
downstream File.write(path, @worker.run(item)) compiles cleanly
because the value column is now const char *.
Closes #575. 586 tests pass / bootstrap fixpoint clean.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 22863fac2e07
- Parent
- 13f741ed400b
compiler_helpers.rbmodifiedView file
t = parts[k] if t == "" # blank slot -- uninformative, skip elsif t == "poly" # "poly" alone means the writer-scan saw a widened union value # (typically a method param that received obj_X across multiple # callsites and collapsed to poly). The companion ctor-arg # propagation pass adds the concrete obj_X entries alongside, # so when we ALSO see those, the obj_X set is sharper and we # should keep the narrow. Skip the bare "poly" here; if no # obj_X entries follow, `out` stays empty and the caller falls # back to the unrestricted dispatch. elsif is_obj_type(t) == 1 cname = t[4, t.length - 4] cidx = find_class_idx(cname) end end else # Any non-obj observation ("poly", "int", "string", etc.) # means the ivar can hold values whose dispatch is broader # than the obj-class set we can enumerate. Bail out so we # don't unsoundly prune. # Truly broad non-obj observation ("int", "string", "float" # etc.) — the ivar can hold a value whose dispatch isn't a # user-class method call at all. Bail to avoid unsound pruning. return "" end k = k + 1spinel_analyze.rbmodifiedView file
Loading diff…