Commit f2ddceb9
f2ddceb91fdbMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~2 modified
Description
fix(analyze,codegen): propagate is_a? narrow into return-type unify
Proposal A from the post-#463 type-inference plan, discovered while introducing bm_jekyll_lite / bm_poly_cells / bm_micro_lisp. A function body like
def render_one(v) if v.is_a?(String) return v end if v.is_a?(Integer) return v.to_s end "?" end
inferred its return type as poly even though every return arm
yielded a string. collect_return_types' IfNode walker recursed
into the then-arm without pushing the is_a?(C) narrow, so
return v saw v as its poly param type, not as the narrowed
String. The unify then mixed string with poly and surfaced poly.
Analyze fix: collect_return_types' IfNode arm now mirrors scan_new_calls' narrow push/pop pair around the then-body walk (parse_is_a_predicate + push_type_narrow + pop_type_narrow).
Codegen mirror: compile_return_stmt routes the returned
expression through compile_expr_for_expected_type whenever the
function's declared return type is concrete but infer_type still
reports the expression as poly. That helper already emits the
matching .v. unbox (the same path called sites already use
when passing an is_a?-narrowed local into a concrete-typed param
slot), so return lv_v in a const char * function now lowers to
return lv_v.v.s cleanly.
Measurement on the three new benchmarks: bm_jekyll_lite: returns 0 / 15 (unchanged, already clean) bm_poly_cells: returns 1 / 9 (was 2; render_one now const char*) bm_micro_lisp: returns 5 / 20 (unchanged; remaining are genuine Lisp-value polyvariance)
Tests: 434 pass, bench 55/55 pass, bootstrap 4-way fixpoint OK.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- f2ddceb91fdb
- Parent
- 40a82ad99bce
spinel_analyze.rbmodifiedView file
return end if @nd_type[nid] == "IfNode" # Push the is_a?(C) narrow before walking the then-arm so a # `return v` inside contributes the narrowed type (string when # `v.is_a?(String)`, etc.) to the return unify instead of the # underlying poly param type. Mirrors scan_new_calls and # scan_cls_method_calls — without this push, infer_body_return # widens functions like `def render_one(v); if v.is_a?(String); # return v; end; ...; end` to poly even when every return arm # contributes a single concrete type. pred = @nd_predicate[nid] parsed = parse_is_a_predicate(pred) narrow_var = parsed[0] narrow_t = parsed[1] if narrow_var != "" push_type_narrow(narrow_var, narrow_t) end body = @nd_body[nid] if body >= 0 collect_return_types_nid(body, types) end if narrow_var != "" pop_type_narrow end sub = @nd_subsequent[nid] if sub >= 0 collect_return_types(sub, types)spinel_codegen.rbmodifiedView file
Loading diff…