Commit bfe58c28

bfe58c281865Merged

Ryan Seys <[email protected]> · 6 months ago · 5 files · +4 added · ~1 modified

Description

fix(codegen): inspect tuple:int,int / tuple:int,float for p

Methods that return Ruby Arrays of fixed arity — Integer#divmod, Array#minmax, etc. — internally lower to a tuple:int,int (or tuple:int,float) struct rather than building a heap-allocated sp_PtrArray. That's efficient for destructuring (q, r = …) and arithmetic, but compile_inspect_for had no entry for these tuple types, so p 100.divmod(7) and p arr.minmax fell through to the puts-style printf("%lld", val) default and printed the struct pointer instead of [14, 2] / [1, 9].

Adds two inspect branches that emit the Ruby array literal form via sp_sprintf:

tuple:int,int → "[%lld, %lld]" tuple:int,float → "[%lld, %s]" (second slot via sp_float_inspect)

The float-second variant covers Float#divmod, which returns [int, float] per CRuby (e.g. 5.5.divmod(2) → [2, 1.5]).

Bootstrap fixed-point holds. Tests added: integer_divmod, array_minmax, array_minmax_2 — each just p <expr> against the matching CRuby reference output.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
bfe58c281865
Parent
8ff9aa915a30

spinel_codegen.rbmodifiedView file

@@ -31180,6 +31180,20 @@
3118031180 @needs_int_array = 1
3118131181 return "sp_SymArrayPtrArray_inspect(" + val + ")"
3118231182 end
31183+ # Tuples produced by methods that return Ruby Arrays of fixed
31184+ # arity (Integer#divmod, Array#minmax, etc.). Without an inspect
31185+ # path here, `p arr.minmax` would fall through to printf("%lld",...)
31186+ # and emit the struct pointer. Format as Ruby's "[a, b]" so the
31187+ # output round-trips with CRuby.
31188+ if at == "tuple:int,int"
31189+ return "sp_sprintf(\"[%lld, %lld]\", (long long)" + val + "->_0, (long long)" + val + "->_1)"
31190+ end
31191+ if at == "tuple:int,float"
31192+ return "sp_sprintf(\"[%lld, %s]\", (long long)" + val + "->_0, sp_float_inspect(" + val + "->_1))"
31193+ end
31194+ if at == "tuple:float,float"
31195+ return "sp_sprintf(\"[%s, %s]\", sp_float_inspect(" + val + "->_0), sp_float_inspect(" + val + "->_1))"
31196+ end
3118331197 ""
3118431198 end
3118531199