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

@@ -21984,6 +21984,69 @@
2198421984 end
2198521985 return
2198621986 end
21987+ # Issue #236: empty `{}` / `[]` literal RHS — `compile_expr`
21988+ # returns the default `sp_StrIntHash_new()` / `sp_IntArray_new()`,
21989+ # but slots may have been promoted to a typed container by their
21990+ # subsequent writes (e.g. `@h["k"] = "v"` lifts the slot to
21991+ # str_str_hash). If every chain participant agrees on the same
21992+ # promoted concrete container type, emit the slot-typed
21993+ # constructor as the temp so each store is type-correct AND the
21994+ # Ruby semantic of one shared object is preserved across the
21995+ # chain. Disagreeing-type chains fall through to the general
21996+ # case below; the resulting C will still warn about pointer
21997+ # type mismatch but at least one slot will be correct.
21998+ if is_empty_hash_literal(rhs_id) == 1 || is_empty_array_literal(rhs_id) == 1
21999+ consensus_t = ""
22000+ consensus_ok = 1
22001+ pi = 0
22002+ while pi < ivars.length
22003+ pt = ""
22004+ if @current_class_idx >= 0
22005+ pt = cls_ivar_type(@current_class_idx, ivars[pi])
22006+ end
22007+ if pi == 0
22008+ consensus_t = pt
22009+ elsif pt != consensus_t
22010+ consensus_ok = 0
22011+ end
22012+ pi = pi + 1
22013+ end
22014+ if consensus_ok == 1 && consensus_t != "" && consensus_t != "str_int_hash" && consensus_t != "int_array" && consensus_t != "poly"
22015+ ctor236 = ""
22016+ if is_empty_hash_literal(rhs_id) == 1
22017+ if consensus_t == "str_str_hash"
22018+ @needs_str_str_hash = 1
22019+ ctor236 = "sp_StrStrHash_new()"
22020+ elsif consensus_t == "int_str_hash"
22021+ @needs_int_str_hash = 1
22022+ ctor236 = "sp_IntStrHash_new()"
22023+ elsif consensus_t == "sym_int_hash"
22024+ @needs_sym_int_hash = 1
22025+ ctor236 = "sp_SymIntHash_new()"
22026+ elsif consensus_t == "sym_str_hash"
22027+ @needs_sym_str_hash = 1
22028+ ctor236 = "sp_SymStrHash_new()"
22029+ elsif consensus_t == "str_poly_hash"
22030+ ctor236 = "sp_StrPolyHash_new()"
22031+ elsif consensus_t == "sym_poly_hash"
22032+ ctor236 = "sp_SymPolyHash_new()"
22033+ end
22034+ else
22035+ ctor236 = empty_array_new_for_type(consensus_t)
22036+ end
22037+ if ctor236 != ""
22038+ @needs_gc = 1
22039+ tmp236 = new_temp
22040+ emit(" " + c_type(consensus_t) + " " + tmp236 + " = " + ctor236 + ";")
22041+ ki236 = 0
22042+ while ki236 < ivars.length
22043+ emit(" " + self_arrow + sanitize_ivar(ivars[ki236]) + " = " + tmp236 + ";")
22044+ ki236 = ki236 + 1
22045+ end
22046+ return
22047+ end
22048+ end
22049+ end
2198722050 # General case: evaluate the rhs once into a typed temp, then
2198822051 # store into each slot. Side-effecting rhs (CallNode etc.) must
2198922052 # not be replicated.