Commit 92306b7f

92306b7fe57bMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~2 modified

Description

fix(analyze,codegen): RBS-arbitration + IVW unbox for case-by-sym dispatch

def []=(field, value) with RBS-declared union value: (Integer | String) and a case-by-symbol dispatch (when :id then @id = value) tripped the new RBS arbitration: spinel observed value as "poly" and flagged conflict with the declared Integer ivar slot, even though MRI semantics narrow the union per case-arm.

Two-part fix:

  • rbs_inferred_incompatible?: treat a "poly" observation against a concrete RBS-declared slot as a soft signal — trust RBS, the runtime narrowing the user wrote keeps it sound. Same shape as the #638 nullable-return fix.
  • compile_stmt's InstanceVariableWriteNode arm: when the slot is concrete (int / string / float / obj / hash / array) and the rhs is poly, extract via .v.i / .v.s / .v.f / (c_type *)(...).v.p so the C-level assignment type-checks.

Closes #639.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
92306b7fe57b
Parent
b7d8ed87b1a8

spinel_analyze.rbmodifiedView file

@@ -20396,6 +20396,17 @@
2039620396 if is_nullable_type(rbs_t) == 1 && inf_t == "nil"
2039720397 return false
2039820398 end
20399+ # Issue #639: case-by-symbol dispatch writing union-typed values
20400+ # into typed ivars (`when :id then @id = value` where `value` is
20401+ # `Integer | String`). spinel observes `value` as "poly" and
20402+ # would flag conflict with the declared `Integer`, but MRI
20403+ # semantics narrow the union per case-arm so the runtime type
20404+ # matches the ivar slot. Trust RBS — the matching codegen-side
20405+ # unbox at the IVW writes via `.v.i` / `.v.s` / `.v.f` so the
20406+ # C-level assignment type-checks.
20407+ if inf_t == "poly" && rbs_t != "poly" && is_nullable_type(rbs_t) == 0
20408+ return false
20409+ end
2039920410 # Container variant refinements where one is the other's poly form.
2040020411 if (rbs_t == "int_array" && inf_t == "poly_array") ||
2040120412 (rbs_t == "poly_array" && inf_t == "int_array")