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:

  1. 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.
  2. 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 .)

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

@@ -1895,6 +1895,37 @@
18951895 # the cache once with correct context, infer_type stays
18961896 # uncached during emission.
18971897 t = @nd_type[nid]
1898+ # Safe navigation `recv&.method`: result is nil when recv is nil.
1899+ # - Statically nil recv → result is "nil"
1900+ # - Otherwise → method's normal return type, widened to nullable
1901+ # The cache lookup above already returns the persisted result for
1902+ # subsequent calls; we only compute this path at first visit.
1903+ if t == "CallNode" && @nd_callop[nid] == "&."
1904+ sn_recv = @nd_receiver[nid]
1905+ if sn_recv >= 0
1906+ sn_recv_t = infer_type(sn_recv)
1907+ if @nd_type[sn_recv] == "NilNode" || sn_recv_t == "nil"
1908+ return "nil"
1909+ end
1910+ # For dynamic nullable recv, compute the regular method return
1911+ # type by temporarily clearing the call_operator field so the
1912+ # nested infer_type call sees a plain `.` dispatch. Then widen
1913+ # to nullable if the base type supports it.
1914+ @nd_callop[nid] = ""
1915+ base_ret_sn = infer_type(nid)
1916+ @nd_callop[nid] = "&."
1917+ if base_ret_sn == "" || base_ret_sn == "void" || base_ret_sn == "nil"
1918+ return base_ret_sn
1919+ end
1920+ if is_nullable_type(base_ret_sn) == 1
1921+ return base_ret_sn
1922+ end
1923+ if is_nullable_pointer_type(base_ret_sn) == 1 || base_ret_sn == "int"
1924+ return base_ret_sn + "?"
1925+ end
1926+ return base_ret_sn
1927+ end
1928+ end
18981929 if t == "SuperNode" || t == "ForwardingSuperNode"
18991930 # `super` returns whatever the parent's same-named method
19001931 # returns. Walk to the parent's `find_method_owner`-resolved