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

@@ -5207,7 +5207,11 @@
52075207 if @nd_type[recv] == "ConstantReadNode" && @nd_name[recv] == "ENV"
52085208 return "string"
52095209 end
5210- rt = infer_type(recv)
5210+ # Strip nullable suffix: `Hash[K,V]?` return narrowed inside
5211+ # `if r; r[k]; end` should dispatch on the underlying variant,
5212+ # not fall through to the int default. The C-storage stays the
5213+ # same; the narrow just confirms non-NULL receiver.
5214+ rt = base_type(infer_type(recv))
52115215 if rt == "string"
52125216 return "string"
52135217 end