Commit a99d24e9

a99d24e95c24Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +1 added · ~2 modified

Description

Implement Struct#dig

Struct#dig fell through to an unresolved nil and emitted a C type error. Implement it as a variadic dig: the first key is member access (by symbol/string name or integer position, same resolution as Struct#[]); any further keys dig recursively into the member's value via emit_dig_step's cls_id-aware traversal (so a member holding a hash or array digs correctly). The multi-key result is poly; a single-key result keeps the member's concrete type.

Also gate the array- and hash-method dig handlers on their receiver types: both ran unconditionally and intercepted dig on an object receiver before the object dispatch (emitting nil), which is why Struct#dig never reached the new arm.

This completes the Time/Struct/Object method-gap bundle (J1, J2, J5, J6 were already resolved; J3 added Struct#to_h with a block).

Fixes #1222

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
a99d24e95c24
Parent
c20c6a7e0d0b

spinel_analyze.rbmodifiedView file

@@ -5312,6 +5312,42 @@
53125312 return ""
53135313 end
53145314 rt = infer_type(recv)
5315+ # Struct#dig(key, ...): the first key is member access, typed as
5316+ # that member's ivar type. With further keys the result digs deeper
5317+ # and is dynamic (poly). Mirrors the Struct#[] arm + the codegen
5318+ # emit_dig_step chain.
5319+ sci_dg = struct_recv_ci(recv)
5320+ if sci_dg >= 0
5321+ args_id_dg = @nd_arguments[nid]
5322+ if args_id_dg >= 0
5323+ a_dg = get_args(args_id_dg)
5324+ if a_dg.length >= 1
5325+ readers_dg = @cls_attr_readers[sci_dg].split(";", -1)
5326+ fld_dg = ""
5327+ if @nd_type[a_dg[0]] == "SymbolNode" || @nd_type[a_dg[0]] == "StringNode"
5328+ fld_dg = @nd_content[a_dg[0]]
5329+ elsif @nd_type[a_dg[0]] == "IntegerNode"
5330+ idx_dg = @nd_value[a_dg[0]].to_i
5331+ if idx_dg < 0
5332+ idx_dg = idx_dg + readers_dg.length
5333+ end
5334+ if idx_dg >= 0 && idx_dg < readers_dg.length
5335+ fld_dg = readers_dg[idx_dg]
5336+ end
5337+ end
5338+ if fld_dg != ""
5339+ ft_dg = cls_ivar_type(sci_dg, "@" + fld_dg)
5340+ if ft_dg != ""
5341+ if a_dg.length == 1
5342+ return ft_dg
5343+ end
5344+ @needs_rb_value = 1
5345+ return "poly"
5346+ end
5347+ end
5348+ end
5349+ end
5350+ end
53155351 # poly_array recv with multi-arg dig: result is sp_RbVal
53165352 # because the inner step walks a per-element cls_id
53175353 # dispatch. Issue #555 case 07.