Commit bd40506b
bd40506bccebMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(analyze): hash [] arm strips nullable suffix on receiver type
def f(...); return nil if cond; h = {}; h[k] = v; h; end returns
str_str_hash? (or str_int_hash?, sym_str_hash? etc.). At the
call site:
r = f(arg) if r puts r[k] end
r["k"] was inferred as int (the unconstrained default) because
the [] arm's variant table compared against bare str_str_hash
etc. and the nullable receiver type str_str_hash? matched none
of them. The result of the bracket read then dispatched through
the int puts path and printed the pointer address as a decimal
long long.
Strip the nullable suffix via base_type before dispatch -- the C storage is the same and the surrounding nil-check / narrow already ensures we're not reading through a NULL receiver.
Repro from roundhouse archive (filed as shape D of #700):
Router.match_pattern("/users/:id", "/users/42") returns
Hash[String, String]?; outer puts r["id"] produced
105898246583433 (pointer cast to int) instead of 42.
Test: test/nullable_hash_recv_bracket.rb.
Refs #700
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- bd40506bcceb
- Parent
- 0b1f8d7d93c4
spinel_analyze.rbmodifiedView file
if @nd_type[recv] == "ConstantReadNode" && @nd_name[recv] == "ENV" return "string" end rt = infer_type(recv) # Strip nullable suffix: `Hash[K,V]?` return narrowed inside # `if r; r[k]; end` should dispatch on the underlying variant, # not fall through to the int default. The C-storage stays the # same; the narrow just confirms non-NULL receiver. rt = base_type(infer_type(recv)) if rt == "string" return "string" endtest/nullable_hash_recv_bracket.expectedaddedView file
Loading diff…
test/nullable_hash_recv_bracket.rbaddedView file
Loading diff…