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 boolean and result (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 end
23682368 return "int"
23692369 end
2370+ if t == "InstanceVariableWriteNode"
2371+ # `@x = expr` used as an expression value lowers to the assigned
2372+ # slot's value (after boxing into the slot), exactly like the
2373+ # LocalVariableWriteNode arm above. Reporting the ivar slot type
2374+ # lets a method whose body's last expression is `@x = ...` infer
2375+ # its return type from the slot -- a poly-boxed @id makes
2376+ # `def bump = (@id = @id + 1)` return sp_RbVal, not mrb_int -- and
2377+ # makes `(@x = expr) and rest` type @x from expr rather than from
2378+ # the boolean `and` result. Falls back to the rhs type when the
2379+ # 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_w
2384+ end
2385+ if @current_class_idx >= 0
2386+ ivt_w = cls_ivar_type(@current_class_idx, iname_w)
2387+ if ivt_w != ""
2388+ return ivt_w
2389+ end
2390+ end
2391+ tit_w = toplevel_ivar_type(iname_w)
2392+ if tit_w != ""
2393+ return tit_w
2394+ end
2395+ return infer_type(@nd_expression[nid])
2396+ end
23702397 if t == "IndexOrWriteNode" || t == "IndexAndWriteNode" || t == "IndexOperatorWriteNode"
23712398 # `recv[k] ||= v` (etc.) as an expression value. The result type
23722399 # is the recv's element type — same shape as Hash#[] / Array#[]