Commit 6d5985e0
6d5985e078d2Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): chained ivar write picks promoted constructor for empty literal
Issue #236 follow-up to #234: chained @a = @b = {} (or [])
where every chain participant has been promoted to a typed
container (e.g. str_str_hash via subsequent @h["k"] = "v"
writes) emitted the default sp_StrIntHash * temp from
compile_expr({}) and assigned it to both slots — incompatible
pointer type warning at every store.
The standard compile_stmt path for InstanceVariableWriteNode
already had per-slot empty-literal specialization (lines 23446+);
the chain helper bypassed it because it routes everything through
a single typed temp.
Reproduction:
class C
def initialize
@a = {}; @a["k"] = "v"
@b = {}; @b["k"] = "v"
end
def reset; @a = @b = {}; end
end
Pre-fix:
sp_StrIntHash * _t1 = sp_StrIntHash_new();
self->iv_a = _t1; /* warning: incompatible pointer */
self->iv_b = _t1;
Fix: detect empty hash/array literal RHS in
compile_chained_ivar_writes, take a consensus across the chain
slots' types, and emit the slot-typed constructor as the temp
when all participants agree on the same promoted type. Both the
type-correctness AND Ruby's "one shared object" semantic are
preserved (both ivars receive the same pointer; mutations through
either are observable through the other).
Disagreeing-type chains (rare; would require @a widened to
poly while @b stayed concrete) fall through to the existing
general-case path. The existing ivt_i == "poly" boxing branch
in that path handles the poly-slot side; the concrete-slot side
still warns, but that case was already broken pre-#234 and is
out of scope here.
Test: test/issue236_chain_empty_literal.rb covers the hash and
array shapes, verifies the shared-object semantic by writing
through one ivar and reading through the other, and adds a 3-chain
case.
290 pass, 0 fail. Bootstrap clean (gen2 == gen3).
Closes #236 Refs #234
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 6d5985e078d2
- Parent
- 40c6cbf3d163
spinel_codegen.rbmodifiedView file
end return end # Issue #236: empty `{}` / `[]` literal RHS — `compile_expr` # returns the default `sp_StrIntHash_new()` / `sp_IntArray_new()`, # but slots may have been promoted to a typed container by their # subsequent writes (e.g. `@h["k"] = "v"` lifts the slot to # str_str_hash). If every chain participant agrees on the same # promoted concrete container type, emit the slot-typed # constructor as the temp so each store is type-correct AND the # Ruby semantic of one shared object is preserved across the # chain. Disagreeing-type chains fall through to the general # case below; the resulting C will still warn about pointer # type mismatch but at least one slot will be correct. if is_empty_hash_literal(rhs_id) == 1 || is_empty_array_literal(rhs_id) == 1 consensus_t = "" consensus_ok = 1 pi = 0 while pi < ivars.length pt = "" if @current_class_idx >= 0 pt = cls_ivar_type(@current_class_idx, ivars[pi]) end if pi == 0 consensus_t = pt elsif pt != consensus_t consensus_ok = 0 end pi = pi + 1 end if consensus_ok == 1 && consensus_t != "" && consensus_t != "str_int_hash" && consensus_t != "int_array" && consensus_t != "poly" ctor236 = "" if is_empty_hash_literal(rhs_id) == 1 if consensus_t == "str_str_hash" @needs_str_str_hash = 1 ctor236 = "sp_StrStrHash_new()" elsif consensus_t == "int_str_hash" @needs_int_str_hash = 1 ctor236 = "sp_IntStrHash_new()" elsif consensus_t == "sym_int_hash" @needs_sym_int_hash = 1 ctor236 = "sp_SymIntHash_new()" elsif consensus_t == "sym_str_hash" @needs_sym_str_hash = 1 ctor236 = "sp_SymStrHash_new()" elsif consensus_t == "str_poly_hash" ctor236 = "sp_StrPolyHash_new()" elsif consensus_t == "sym_poly_hash" ctor236 = "sp_SymPolyHash_new()" end else ctor236 = empty_array_new_for_type(consensus_t) end if ctor236 != "" @needs_gc = 1 tmp236 = new_temp emit(" " + c_type(consensus_t) + " " + tmp236 + " = " + ctor236 + ";") ki236 = 0 while ki236 < ivars.length emit(" " + self_arrow + sanitize_ivar(ivars[ki236]) + " = " + tmp236 + ";") ki236 = ki236 + 1 end return end end end # General case: evaluate the rhs once into a typed temp, then # store into each slot. Side-effecting rhs (CallNode etc.) must # not be replicated.test/issue236_chain_empty_literal.rbaddedView file
Loading diff…
test/issue236_chain_empty_literal.rb.expectedaddedView file
Loading diff…