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
# ---- Scope stack for local variables ---- @scope_names = "".split(",", -1) @scope_types = "".split(",", -1) # Parallel to `@scope_names`, seeded at declare_var and NEVER mutated # by set_var_type: the variable's immutable declared type, i.e. the C # storage type the hoist actually emitted. `@scope_types` tracks the # current value's type (it flips to "nil"/"int"/etc. on each write), # so it is NOT a reliable proxy for the slot's C type. Use this when a # decision must hinge on the real declaration (e.g. is the slot a # plain `mrb_int` that needs a pointer round-trip cast on assignment). @scope_decl_types = "".split(",", -1) # Parallel to `@scope_names`: when a local was assigned directly # from an ivar read (`lv = @ivar`), record the ivar name here so # later sites that need ivar-side metadata (notably the @scope_names.push("---") @scope_types.push("---") @scope_ivar_alias.push("---") @scope_decl_types.push("---") 0 end @scope_names.pop @scope_types.pop @scope_ivar_alias.pop @scope_decl_types.pop return end @scope_names.pop @scope_types.pop @scope_ivar_alias.pop @scope_decl_types.pop end end @scope_names.push(name) @scope_types.push(vtype) @scope_ivar_alias.push("") @scope_decl_types.push(vtype) 0 end # The variable's immutable declared type (the C storage type the hoist # emitted), bypassing set_var_type's per-write mutations of # @scope_types. "" when the name isn't in scope. def find_var_decl_ctype_type(name) i = @scope_names.length - 1 while i >= 0 if @scope_names[i] == name return @scope_decl_types[i] end i = i - 1 end "" end # Emit a 1-arg `raise <arg>` -- handle three shapes: # raise ClassName -> sp_raise("ClassName") # raise <BuiltinExc>.new("msg") -> sp_raise_cls("ClsName", "msg") val = "(sp_" + cname_unbox + " *)(" + val + ").v.p" kept_typed_unbox = 1 end # The local's C slot is a plain `mrb_int` (analyze typed it int — # e.g. a yield-using method whose return type defaulted to int) # but the value codegen actually produced is a pointer (a string # yield body, an array, etc.). Round-trip the pointer through the # int slot; the read side already casts it back via the tracked # type set just below. Without the cast the assignment trips # -Wint-conversion under -Werror (bench / test builds). Gate on the # IMMUTABLE declared slot type, not `vt` (the tracked type flips to # "nil"/"int" on each write, and c_type("nil") is also "mrb_int" — # using it would misfire on a pointer slot that was momentarily nil, # e.g. `root = nil; root = insert(...)` in bm_rbtree). if c_type(find_var_decl_ctype_type(lname)) == "mrb_int" && type_is_pointer(rhs_t) == 1 val = "(mrb_int)(uintptr_t)(" + val + ")" end emit(" " + vref + " = " + val + ";") end if rhs_t != "nil" || is_nullable_type(vt) == 0test/yield_value_return.rbmodifiedView file
Loading diff…
test/yield_value_return.rb.expectedmodifiedView file
Loading diff…