Skip to content

Type Kernel#<=> as returning Integer? - #3210

Draft
apiology wants to merge 1 commit into
ruby:masterfrom
apiology:widen-kernel-cmp-return
Draft

apiology wants to merge 1 commit into
ruby:masterfrom
apiology:widen-kernel-cmp-return

Conversation

@apiology

@apiology apiology commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Claude: This PR was written by Claude Code on behalf of @apiology.

Problem: Steep rejects an ordinary <=> whose class's RBS does not redeclare it.

class Version
  attr_reader :major, :minor
  def <=>(other) = [major, minor] <=> [other.major, other.minor]
end
lib/version.rb:9:6: [error] Cannot allow method body have type `(::Integer | nil)` because declared as type `(0 | nil)`
│ Diagnostic ID: Ruby::MethodBodyTypeMismatch

Any class implementing <=> without redeclaring it inherits Kernel#<=>'s 0?, which only Object's default satisfies, so delegating to Array#<=> or Integer#<=> (typed Integer?) is an error.

Solution: Type Kernel#<=> as Integer?, the type those comparisons have and what it was before #1945, which still covers the default's 0 or nil.

Kernel#<=> was typed 0?, the exact result of Object's default
implementation. A class that implements <=> in Ruby without
redeclaring it in RBS inherits that type, so Steep rejects any body
that delegates to Array#<=> or Integer#<=> (typed Integer?) with
MethodBodyTypeMismatch. Integer? is the type those comparisons have,
still covers the default's 0 or nil, and is what Kernel#<=> was typed
before it was narrowed.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhUSLUimEYJyCZv8NdQGKb
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