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

@@ -31524,7 +31524,7 @@
3152431524 # For non-int keys (string, sym, ...) the SP_TAG_INT branch
3152531525 # is unreachable at runtime and `(recv.v.i >> key)` is
3152631526 # invalid C — `>> const char *` won't compile.
31527- if mname == "[]" && arg_compiled.length >= 1 && (arg_types[0] == "int" || arg_types[0] == "poly" || arg_types[0] == "bigint")
31527+ if mname == "[]" && arg_compiled.length >= 1 && (arg_types[0] == "int" || arg_types[0] == "int?" || arg_types[0] == "poly" || arg_types[0] == "bigint")
3152831528 a0_int = arg_compiled[0]
3152931529 if arg_types[0] == "poly"
3153031530 a0_int = "(" + a0_int + ").v.i"
@@ -31545,8 +31545,8 @@
3154531545 if mname == "[]" && arg_compiled.length == 2 && is_poly_ret == 1
3154631546 a1_t_idx = arg_types.length > 0 ? arg_types[0] : ""
3154731547 a2_t_idx = arg_types.length > 1 ? arg_types[1] : ""
31548- if (a1_t_idx == "int" || a1_t_idx == "poly" || base_type(a1_t_idx) == "bigint") &&
31549- (a2_t_idx == "int" || a2_t_idx == "poly" || base_type(a2_t_idx) == "bigint")
31548+ if (a1_t_idx == "int" || a1_t_idx == "int?" || a1_t_idx == "poly" || base_type(a1_t_idx) == "bigint") &&
31549+ (a2_t_idx == "int" || a2_t_idx == "int?" || a2_t_idx == "poly" || base_type(a2_t_idx) == "bigint")
3155031550 a1s_idx = arg_compiled[0]
3155131551 if a1_t_idx == "poly"
3155231552 a1s_idx = "(" + a1s_idx + ").v.i"
@@ -31933,7 +31933,7 @@
3193331933 # runtime (a poly recv carrying a String key would be a hash,
3193431934 # not an array) and emitting them produces Wint-conversion
3193531935 # warnings — or hard errors under -Werror.
31936- a0_is_int = arg_compiled.length >= 1 && (arg_types.length == 0 || arg_types[0] == "int" || arg_types[0] == "poly" || arg_types[0] == "bigint")
31936+ 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")
3193731937 # Hash dispatch arms for `[]` on a poly recv whose runtime
3193831938 # storage is a *-Hash variant. The chained `outer[k1][k2]`
3193931939 # shape where outer is StrPolyHash lands here — the inner []