Commit e084a30b
e084a30b5123Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Dispatch int? indices in poly [] like int indices
emit_poly_builtin_dispatch / compile_poly_method_call gated every poly []
arm -- the SP_TAG_INT bit-extract, the SP_TAG_OBJ per-array-type reads, and
the 2-arg substring arm -- on the index argument being int / poly / bigint.
An int? index, already produced by the shipped int_int / str_int / sym_int
hash variants on any h[k] lookup, matched none of them, so the whole
dispatch collapsed to its sp_box_nil() default: an empty
if (recv.tag == SP_TAG_OBJ) {} shell that silently returns nil.
int? shares mrb_int storage with int (the nil lives in-band as SP_INT_NIL), so an int? index is a valid integer index and needs no unboxing. Add int? to the three index-type guards.
The repro (test/poly_idx_int_nullable.rb) needs no array changes: a poly
receiver from a heterogeneous literal indexed by an int? h[k] returned nil
instead of the element (want 30). Surfaced while wiring int_array #[] -> int?,
but the bug predates it.
Refs #1180
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- e084a30b5123
- Parent
- bf972ef7da5b
spinel_codegen.rbmodifiedView file
# For non-int keys (string, sym, ...) the SP_TAG_INT branch # is unreachable at runtime and `(recv.v.i >> key)` is # invalid C — `>> const char *` won't compile. if mname == "[]" && arg_compiled.length >= 1 && (arg_types[0] == "int" || arg_types[0] == "poly" || arg_types[0] == "bigint") if mname == "[]" && arg_compiled.length >= 1 && (arg_types[0] == "int" || arg_types[0] == "int?" || arg_types[0] == "poly" || arg_types[0] == "bigint") a0_int = arg_compiled[0] if arg_types[0] == "poly" a0_int = "(" + a0_int + ").v.i" if mname == "[]" && arg_compiled.length == 2 && is_poly_ret == 1 a1_t_idx = arg_types.length > 0 ? arg_types[0] : "" a2_t_idx = arg_types.length > 1 ? arg_types[1] : "" if (a1_t_idx == "int" || a1_t_idx == "poly" || base_type(a1_t_idx) == "bigint") && (a2_t_idx == "int" || a2_t_idx == "poly" || base_type(a2_t_idx) == "bigint") if (a1_t_idx == "int" || a1_t_idx == "int?" || a1_t_idx == "poly" || base_type(a1_t_idx) == "bigint") && (a2_t_idx == "int" || a2_t_idx == "int?" || a2_t_idx == "poly" || base_type(a2_t_idx) == "bigint") a1s_idx = arg_compiled[0] if a1_t_idx == "poly" a1s_idx = "(" + a1s_idx + ").v.i" # runtime (a poly recv carrying a String key would be a hash, # not an array) and emitting them produces Wint-conversion # warnings — or hard errors under -Werror. a0_is_int = arg_compiled.length >= 1 && (arg_types.length == 0 || arg_types[0] == "int" || arg_types[0] == "poly" || arg_types[0] == "bigint") a0_is_int = arg_compiled.length >= 1 && (arg_types.length == 0 || arg_types[0] == "int" || arg_types[0] == "int?" || arg_types[0] == "poly" || arg_types[0] == "bigint") # Hash dispatch arms for `[]` on a poly recv whose runtime # storage is a *-Hash variant. The chained `outer[k1][k2]` # shape where outer is StrPolyHash lands here — the inner []test/poly_idx_int_nullable.rbaddedView file
Loading diff…
test/poly_idx_int_nullable.rb.expectedaddedView file
Loading diff…