Commit 76229ef5
76229ef590fdMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(analyze,codegen): safe navigation &. returns nil on nil receiver
nil&.foo previously emitted warning: cannot resolve call to 'foo' on nil (emitting 0) and the expression collapsed to int 0 -- both
contrary to Ruby's &. semantics (returns nil when receiver is nil,
otherwise dispatches normally).
Two coordinated changes:
-
analyze (infer_type): when CallNode has
@nd_callop == "&.":- statically-nil receiver → return "nil"
- dynamic nullable receiver → call's method-dispatch result widened to nullable (T?)
- non-nullable receiver → falls through to regular dispatch Implementation temporarily clears nd_callop for the nested infer_type call to read the base method-dispatch return type without re-entering the &. arm.
-
codegen (compile_call_expr): same dispatch top of function:
- statically-nil receiver → emit
c_default_val(<inferred_T?>)-- no warning, no method emit - dynamic nullable receiver → emit a stmt-expression
({ T tmp = recv; tmp_is_nil ? nil_val : tmp.method(...); })reusing compile_call_expr after clearing the &. operator and pushing a non-nullable type narrow for the recv LV - non-nullable receiver → falls through (&. equivalent to .)
- statically-nil receiver → emit
Test: test/safe_navigation_returns_nil.rb covers literal nil, nullable string, hash-from-method-with-nil-return, and the non-nil pass-through. 446 -> 447 tests; optcarrot 59662 holds.
Fixes #701
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 76229ef590fd
- Parent
- bd40506bcceb
spinel_analyze.rbmodifiedView file
# the cache once with correct context, infer_type stays # uncached during emission. t = @nd_type[nid] # Safe navigation `recv&.method`: result is nil when recv is nil. # - Statically nil recv → result is "nil" # - Otherwise → method's normal return type, widened to nullable # The cache lookup above already returns the persisted result for # subsequent calls; we only compute this path at first visit. if t == "CallNode" && @nd_callop[nid] == "&." sn_recv = @nd_receiver[nid] if sn_recv >= 0 sn_recv_t = infer_type(sn_recv) if @nd_type[sn_recv] == "NilNode" || sn_recv_t == "nil" return "nil" end # For dynamic nullable recv, compute the regular method return # type by temporarily clearing the call_operator field so the # nested infer_type call sees a plain `.` dispatch. Then widen # to nullable if the base type supports it. @nd_callop[nid] = "" base_ret_sn = infer_type(nid) @nd_callop[nid] = "&." if base_ret_sn == "" || base_ret_sn == "void" || base_ret_sn == "nil" return base_ret_sn end if is_nullable_type(base_ret_sn) == 1 return base_ret_sn end if is_nullable_pointer_type(base_ret_sn) == 1 || base_ret_sn == "int" return base_ret_sn + "?" end return base_ret_sn end end if t == "SuperNode" || t == "ForwardingSuperNode" # `super` returns whatever the parent's same-named method # returns. Walk to the parent's `find_method_owner`-resolvedspinel_codegen.rbmodifiedView file
Loading diff…