Commit 033037f8

033037f844a0Merged

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

Description

Round-trip a pointer value through an int-declared local slot

When analyze types a local as int (e.g. a yield-using method whose return type defaults to int) but codegen actually produces a pointer value (a string yield body, an array, ...), the assignment lv = <ptr> to the mrb_int slot trips -Wint-conversion under -Werror. The read side already casts the slot back to the value's tracked type, so cast the pointer in on the way through the int slot.

Gate the cast on the local's IMMUTABLE declared type, tracked in a new @scope_decl_types stack seeded at declare_var and never touched by set_var_type. The mutable @scope_types flips to "nil"/"int" on each write and c_type("nil") is also "mrb_int", so keying on it would misfire on a genuine pointer slot that was momentarily nil (root = nil; root = insert(...), e.g. bm_rbtree).

Refs #1362

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

Details

Commit
033037f844a0
Parent
e01a9f022012

spinel_codegen.rbmodifiedView file

@@ -376,6 +376,14 @@
376376 # ---- Scope stack for local variables ----
377377 @scope_names = "".split(",", -1)
378378 @scope_types = "".split(",", -1)
379+ # Parallel to `@scope_names`, seeded at declare_var and NEVER mutated
380+ # by set_var_type: the variable's immutable declared type, i.e. the C
381+ # storage type the hoist actually emitted. `@scope_types` tracks the
382+ # current value's type (it flips to "nil"/"int"/etc. on each write),
383+ # so it is NOT a reliable proxy for the slot's C type. Use this when a
384+ # decision must hinge on the real declaration (e.g. is the slot a
385+ # plain `mrb_int` that needs a pointer round-trip cast on assignment).
386+ @scope_decl_types = "".split(",", -1)
379387 # Parallel to `@scope_names`: when a local was assigned directly
380388 # from an ivar read (`lv = @ivar`), record the ivar name here so
381389 # later sites that need ivar-side metadata (notably the
@@ -1579,6 +1587,7 @@
15791587 @scope_names.push("---")
15801588 @scope_types.push("---")
15811589 @scope_ivar_alias.push("---")
1590+ @scope_decl_types.push("---")
15821591 0
15831592 end
15841593
@@ -1589,11 +1598,13 @@
15891598 @scope_names.pop
15901599 @scope_types.pop
15911600 @scope_ivar_alias.pop
1601+ @scope_decl_types.pop
15921602 return
15931603 end
15941604 @scope_names.pop
15951605 @scope_types.pop
15961606 @scope_ivar_alias.pop
1607+ @scope_decl_types.pop
15971608 end
15981609 end
15991610
@@ -1601,9 +1612,24 @@
16011612 @scope_names.push(name)
16021613 @scope_types.push(vtype)
16031614 @scope_ivar_alias.push("")
1615+ @scope_decl_types.push(vtype)
16041616 0
16051617 end
16061618
1619+ # The variable's immutable declared type (the C storage type the hoist
1620+ # emitted), bypassing set_var_type's per-write mutations of
1621+ # @scope_types. "" when the name isn't in scope.
1622+ def find_var_decl_ctype_type(name)
1623+ i = @scope_names.length - 1
1624+ while i >= 0
1625+ if @scope_names[i] == name
1626+ return @scope_decl_types[i]
1627+ end
1628+ i = i - 1
1629+ end
1630+ ""
1631+ end
1632+
16071633 # Emit a 1-arg `raise <arg>` -- handle three shapes:
16081634 # raise ClassName -> sp_raise("ClassName")
16091635 # raise <BuiltinExc>.new("msg") -> sp_raise_cls("ClsName", "msg")
@@ -37534,6 +37560,20 @@
3753437560 val = "(sp_" + cname_unbox + " *)(" + val + ").v.p"
3753537561 kept_typed_unbox = 1
3753637562 end
37563+ # The local's C slot is a plain `mrb_int` (analyze typed it int —
37564+ # e.g. a yield-using method whose return type defaulted to int)
37565+ # but the value codegen actually produced is a pointer (a string
37566+ # yield body, an array, etc.). Round-trip the pointer through the
37567+ # int slot; the read side already casts it back via the tracked
37568+ # type set just below. Without the cast the assignment trips
37569+ # -Wint-conversion under -Werror (bench / test builds). Gate on the
37570+ # IMMUTABLE declared slot type, not `vt` (the tracked type flips to
37571+ # "nil"/"int" on each write, and c_type("nil") is also "mrb_int" —
37572+ # using it would misfire on a pointer slot that was momentarily nil,
37573+ # e.g. `root = nil; root = insert(...)` in bm_rbtree).
37574+ if c_type(find_var_decl_ctype_type(lname)) == "mrb_int" && type_is_pointer(rhs_t) == 1
37575+ val = "(mrb_int)(uintptr_t)(" + val + ")"
37576+ end
3753737577 emit(" " + vref + " = " + val + ";")
3753837578 end
3753937579 if rhs_t != "nil" || is_nullable_type(vt) == 0