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 < 0
20832083 return "void"
20842084 end
2085+ # `<poly>[idx]` — analyze may cache an optimistic concrete type
2086+ # (e.g. "int" picked from one observed elem kind), but
2087+ # compile_poly_method_call's actual emit widens to sp_RbVal
2088+ # whenever its narrow_int check fails (e.g. block-param recv).
2089+ # When the cache disagrees with the emit, downstream consumers
2090+ # (compile_arg0_as_int for `>>` / `<<`, ...) skip the unbox and
2091+ # produce `int >> sp_RbVal` in C — fails C compile. Compute the
2092+ # answer that mirrors the actual emit, before honouring the
2093+ # cache.
2094+ pa = poly_aref_call_type(nid)
2095+ if pa != ""
2096+ return pa
2097+ end
20852098 # Cache lookup. analyze.rb's annotate_all_node_types fills
20862099 # @nd_inferred_type for every reachable node OUTSIDE block bodies
20872100 # (block-iteration scope is iterator-specific and dispatched at
@@ -16063,6 +16076,27 @@
1606316076 # `[]` (IntArray + Method, the optcarrot `__fetch__` shape), the
1606416077 # outer dispatch is guaranteed to land on a poly carrying an int
1606516078 # tag, and we can read it as `mrb_int` directly. Returns 0
16079+ # Return the type that compile_poly_method_call would actually emit
16080+ # for `<poly>[idx]`, or "" if `nid` isn't that shape. Used by
16081+ # infer_type to keep its static answer in sync with the emit; the
16082+ # cached annotation from analyze can lag the emit's narrow_int
16083+ # decision (block-param recvs short-circuit narrowing), and
16084+ # downstream consumers like compile_arg0_as_int rely on the
16085+ # 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+ end
16090+ recv_id = @nd_receiver[nid]
16091+ if recv_id < 0 || infer_type(recv_id) != "poly"
16092+ return ""
16093+ end
16094+ if poly_index_narrow_int(nid) == 1
16095+ return "int"
16096+ end
16097+ "poly"
16098+ end
16099+
1606616100 # whenever the chain doesn't fit the pattern, so the default
1606716101 # poly-typed temp is preserved for everything else.
1606816102 def poly_index_narrow_int(nid)