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_observations walks 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 = pname writes. 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_set no 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

@@ -288,6 +288,15 @@
288288 t = parts[k]
289289 if t == ""
290290 # blank slot -- uninformative, skip
291+ elsif t == "poly"
292+ # "poly" alone means the writer-scan saw a widened union value
293+ # (typically a method param that received obj_X across multiple
294+ # callsites and collapsed to poly). The companion ctor-arg
295+ # propagation pass adds the concrete obj_X entries alongside,
296+ # so when we ALSO see those, the obj_X set is sharper and we
297+ # should keep the narrow. Skip the bare "poly" here; if no
298+ # obj_X entries follow, `out` stays empty and the caller falls
299+ # back to the unrestricted dispatch.
291300 elsif is_obj_type(t) == 1
292301 cname = t[4, t.length - 4]
293302 cidx = find_class_idx(cname)
@@ -312,10 +321,9 @@
312321 end
313322 end
314323 else
315- # Any non-obj observation ("poly", "int", "string", etc.)
316- # means the ivar can hold values whose dispatch is broader
317- # than the obj-class set we can enumerate. Bail out so we
318- # don't unsoundly prune.
324+ # Truly broad non-obj observation ("int", "string", "float"
325+ # etc.) — the ivar can hold a value whose dispatch isn't a
326+ # user-class method call at all. Bail to avoid unsound pruning.
319327 return ""
320328 end
321329 k = k + 1