Skip to content

Add safe navigation operator (&.) support#65

Merged
dak2 merged 1 commit intomainfrom
add-safe-navigation-operator-support
Mar 16, 2026
Merged

Add safe navigation operator (&.) support#65
dak2 merged 1 commit intomainfrom
add-safe-navigation-operator-support

Conversation

@dak2
Copy link
Owner

@dak2 dak2 commented Mar 15, 2026

Summary

  • Add safe_navigation: bool flag through the call pipeline (NeedsChildKind::MethodCallMethodCallContextMethodCallBox) to track &. operator usage
  • Suppress false "undefined method for NilClass" errors when receiver is nil and &. is used
  • Add nil to return type only when receiver types actually include nil, preserving type precision for non-nilable receivers (e.g. str&.upcaseString, not String | nil)

Test plan

  • Rust unit tests: MethodCallBox with safe_navigation flag (nil receiver, nilable receiver, non-nil receiver, normal call comparison)
  • Rust integration tests: test_safe_navigation_basic, test_safe_navigation_undefined_method, test_safe_navigation_nil_receiver, test_safe_navigation_chain
  • All existing tests pass with safe_navigation: false added to call sites

🤖 Generated with Claude Code

Implement Ruby's safe navigation operator semantics in the type checker:

- Add `safe_navigation` flag to NeedsChildKind::MethodCall, MethodCallContext,
  and MethodCallBox, detected via ruby-prism's `is_safe_navigation()`
- Skip nil receiver in process_recv_type() to suppress false "undefined method
  for NilClass" errors when using `&.`
- Add nil to return type in run() only when receiver types actually include nil,
  preserving type precision for non-nilable receivers (e.g. `str&.upcase` stays
  `String`, not `String | nil`)

This eliminates false positives on `&.` calls, which are pervasive in Rails and
modern Ruby codebases.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@dak2 dak2 force-pushed the add-safe-navigation-operator-support branch from 84649bf to 1e02eae Compare March 15, 2026 15:13
@dak2 dak2 merged commit 53b39e9 into main Mar 16, 2026
4 checks passed
@dak2 dak2 deleted the add-safe-navigation-operator-support branch March 16, 2026 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant