Commit 8a1f3ae7
8a1f3ae7a271Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #417 from mame/fix/poly-aref-cache-mismatch
fix(codegen): keep infer_type of <poly>[idx] in sync with the actual emit (sp_RbVal)
Details
- Commit
- 8a1f3ae7a271
- Parent
- 318a93a30188
- Merge parent
- 1f3ddcbe0c61
spinel_codegen.rbmodifiedView file
@@ -2082,6 +2082,19 @@
20822082
if nid < 020832083
return "void"20842084
end2085+
# `<poly>[idx]` — analyze may cache an optimistic concrete type2086+
# (e.g. "int" picked from one observed elem kind), but2087+
# compile_poly_method_call's actual emit widens to sp_RbVal2088+
# whenever its narrow_int check fails (e.g. block-param recv).2089+
# When the cache disagrees with the emit, downstream consumers2090+
# (compile_arg0_as_int for `>>` / `<<`, ...) skip the unbox and2091+
# produce `int >> sp_RbVal` in C — fails C compile. Compute the2092+
# answer that mirrors the actual emit, before honouring the2093+
# cache.2094+
pa = poly_aref_call_type(nid)2095+
if pa != ""2096+
return pa2097+
end20852098
# Cache lookup. analyze.rb's annotate_all_node_types fills20862099
# @nd_inferred_type for every reachable node OUTSIDE block bodies20872100
# (block-iteration scope is iterator-specific and dispatched at@@ -16063,6 +16076,27 @@
1606316076
# `[]` (IntArray + Method, the optcarrot `__fetch__` shape), the1606416077
# outer dispatch is guaranteed to land on a poly carrying an int1606516078
# tag, and we can read it as `mrb_int` directly. Returns 016079+
# Return the type that compile_poly_method_call would actually emit16080+
# for `<poly>[idx]`, or "" if `nid` isn't that shape. Used by16081+
# infer_type to keep its static answer in sync with the emit; the16082+
# cached annotation from analyze can lag the emit's narrow_int16083+
# decision (block-param recvs short-circuit narrowing), and16084+
# downstream consumers like compile_arg0_as_int rely on the16085+
# answer matching the C type of the temp the emit produces.16086+
def poly_aref_call_type(nid)16087+
if @nd_type[nid] != "CallNode" || @nd_name[nid] != "[]"16088+
return ""16089+
end16090+
recv_id = @nd_receiver[nid]16091+
if recv_id < 0 || infer_type(recv_id) != "poly"16092+
return ""16093+
end16094+
if poly_index_narrow_int(nid) == 116095+
return "int"16096+
end16097+
"poly"16098+
end16099+
1606616100
# whenever the chain doesn't fit the pattern, so the default1606716101
# poly-typed temp is preserved for everything else.1606816102
def poly_index_narrow_int(nid)test/poly_aref_shift_via_block_param.rbaddedView file
Loading diff…
test/poly_aref_shift_via_block_param.rb.expectedaddedView file
Loading diff…