Commit ab16f035
ab16f03537b8Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Type an ivar-write expression by the ivar slot, not the int default
infer_type had no InstanceVariableWriteNode arm, so @x = expr used as
an expression value fell through to the default int. Two failures
followed:
def bump = (@id = @id + 1): the method's return type inferred as mrb_int (from the+ 1), but a poly-boxed @id makes the assignment value sp_RbVal, so the function returned sp_RbVal from an mrb_int slot.@debug = o[:debug] and rest(parsed(@debug = o[:debug]) and rest): @debug's slot was typed from the booleanandresult (mrb_int) while the emitted write assigned o[:debug] (sp_RbVal) -> incompatible-type initialization.
Add the arm, mirroring the LocalVariableWriteNode arm: report the ivar slot type (class-method static slot / class ivar / toplevel ivar), falling back to the rhs expression type when the slot isn't known yet. This aligns the inferred expression value with the slot-typed value codegen actually emits.
Closes #1353 Closes #1364
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- ab16f03537b8
- Parent
- d531879d898a
spinel_analyze.rbmodifiedView file
@@ -2367,6 +2367,33 @@
23672367
end23682368
return "int"23692369
end2370+
if t == "InstanceVariableWriteNode"2371+
# `@x = expr` used as an expression value lowers to the assigned2372+
# slot's value (after boxing into the slot), exactly like the2373+
# LocalVariableWriteNode arm above. Reporting the ivar slot type2374+
# lets a method whose body's last expression is `@x = ...` infer2375+
# its return type from the slot -- a poly-boxed @id makes2376+
# `def bump = (@id = @id + 1)` return sp_RbVal, not mrb_int -- and2377+
# makes `(@x = expr) and rest` type @x from expr rather than from2378+
# the boolean `and` result. Falls back to the rhs type when the2379+
# slot isn't known yet (early fixpoint / unregistered toplevel).2380+
iname_w = @nd_name[nid]2381+
cls_ct_w = class_def_ivar_const_type(iname_w)2382+
if cls_ct_w != ""2383+
return cls_ct_w2384+
end2385+
if @current_class_idx >= 02386+
ivt_w = cls_ivar_type(@current_class_idx, iname_w)2387+
if ivt_w != ""2388+
return ivt_w2389+
end2390+
end2391+
tit_w = toplevel_ivar_type(iname_w)2392+
if tit_w != ""2393+
return tit_w2394+
end2395+
return infer_type(@nd_expression[nid])2396+
end23702397
if t == "IndexOrWriteNode" || t == "IndexAndWriteNode" || t == "IndexOperatorWriteNode"23712398
# `recv[k] ||= v` (etc.) as an expression value. The result type23722399
# is the recv's element type — same shape as Hash#[] / Array#[]test/ivar_assign_and_expr.rbaddedView file
Loading diff…
test/ivar_assign_and_expr.rb.expectedaddedView file
Loading diff…