Commit e34a705a
e34a705af2d2Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): poly_array == [nil, nil, ...] literal compares element-wise
Array.new(3) == [nil, nil, nil] should fold to true: both sides
are arrays of three nils. With Array.new(n) -> poly_array of nils
(a79d368), the LHS is poly_array but the RHS array literal still
lowers to int_array of zeros, and the compile_eq fallback emits a
raw (lc == rc) pointer compare -- always false at runtime even
for matching values.
Lifting [nil, ...] literals globally to poly_array breaks
optcarrot's [nil] * N slot init pattern (the resulting
poly_array slot cascades into typed-int reads on the slot's
elements failing the C compile). The fix instead lifts only at
the == call site: when the LHS static type is poly_array and
the RHS is an ArrayNode whose elements are all NilNode, emit an
inline poly_array of box_nil() pushes for the RHS and dispatch
through sp_PolyArray_eq.
Refs #619 puzzle 4.
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- e34a705af2d2
- Parent
- a626da320c2f
spinel_codegen.rbmodifiedView file
return "(!sp_PolyArray_eq(" + lc + ", " + rc + "))" end end # `<poly_array> == [nil, nil, ...]` shape (Array.new(N) == # [nil, nil, nil] etc.). The RHS array literal would # otherwise lower to int_array of zeros and fall through to a # raw pointer compare. Build a poly_array of nils inline to # match the LHS shape and route through sp_PolyArray_eq. The # global "[nil, ...] literal -> poly_array" lift is unsafe # (it cascades into optcarrot's [nil] * N init pattern); the # contextual lift here only fires at the == call site. # Issue #619 puzzle 4. if lt == "poly_array" && arg_id >= 0 && @nd_type[arg_id] == "ArrayNode" rhs_elems_p4 = parse_id_list(@nd_elements[arg_id]) if rhs_elems_p4.length > 0 all_nil_p4 = 1 rhs_k_p4 = 0 while rhs_k_p4 < rhs_elems_p4.length if @nd_type[rhs_elems_p4[rhs_k_p4]] != "NilNode" all_nil_p4 = 0 end rhs_k_p4 = rhs_k_p4 + 1 end if all_nil_p4 == 1 @needs_rb_value = 1 @needs_gc = 1 rhs_pa_tmp = new_temp emit(" sp_PolyArray *" + rhs_pa_tmp + " = sp_PolyArray_new();") emit(" { mrb_int _n_pa = " + rhs_elems_p4.length.to_s + "; for (mrb_int _i_pa = 0; _i_pa < _n_pa; _i_pa++) sp_PolyArray_push(" + rhs_pa_tmp + ", sp_box_nil()); }") if op == "==" return "sp_PolyArray_eq(" + lc + ", " + rhs_pa_tmp + ")" else return "(!sp_PolyArray_eq(" + lc + ", " + rhs_pa_tmp + "))" end end end end # Hash equality arms. Same shape as the typed-array arms # above. Issue #555. Compares by sorted key set + per-key # value equality (each variant's _eq helper handles itstest/poly_array_eq_all_nil_literal.rbaddedView file
Loading diff…
test/poly_array_eq_all_nil_literal.rb.expectedaddedView file
Loading diff…