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

@@ -22102,6 +22102,40 @@
2210222102 return "(!sp_PolyArray_eq(" + lc + ", " + rc + "))"
2210322103 end
2210422104 end
22105+ # `<poly_array> == [nil, nil, ...]` shape (Array.new(N) ==
22106+ # [nil, nil, nil] etc.). The RHS array literal would
22107+ # otherwise lower to int_array of zeros and fall through to a
22108+ # raw pointer compare. Build a poly_array of nils inline to
22109+ # match the LHS shape and route through sp_PolyArray_eq. The
22110+ # global "[nil, ...] literal -> poly_array" lift is unsafe
22111+ # (it cascades into optcarrot's [nil] * N init pattern); the
22112+ # contextual lift here only fires at the == call site.
22113+ # Issue #619 puzzle 4.
22114+ if lt == "poly_array" && arg_id >= 0 && @nd_type[arg_id] == "ArrayNode"
22115+ rhs_elems_p4 = parse_id_list(@nd_elements[arg_id])
22116+ if rhs_elems_p4.length > 0
22117+ all_nil_p4 = 1
22118+ rhs_k_p4 = 0
22119+ while rhs_k_p4 < rhs_elems_p4.length
22120+ if @nd_type[rhs_elems_p4[rhs_k_p4]] != "NilNode"
22121+ all_nil_p4 = 0
22122+ end
22123+ rhs_k_p4 = rhs_k_p4 + 1
22124+ end
22125+ if all_nil_p4 == 1
22126+ @needs_rb_value = 1
22127+ @needs_gc = 1
22128+ rhs_pa_tmp = new_temp
22129+ emit(" sp_PolyArray *" + rhs_pa_tmp + " = sp_PolyArray_new();")
22130+ 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()); }")
22131+ if op == "=="
22132+ return "sp_PolyArray_eq(" + lc + ", " + rhs_pa_tmp + ")"
22133+ else
22134+ return "(!sp_PolyArray_eq(" + lc + ", " + rhs_pa_tmp + "))"
22135+ end
22136+ end
22137+ end
22138+ end
2210522139 # Hash equality arms. Same shape as the typed-array arms
2210622140 # above. Issue #555. Compares by sorted key set + per-key
2210722141 # value equality (each variant's _eq helper handles its