Skip to content

Add the %a{warning: ...} annotation in#2237

Open
sampersand wants to merge 1 commit into
soutaro:masterfrom
sampersand:swesterman/2026-06-09/add-warning-annotation-in
Open

Add the %a{warning: ...} annotation in#2237
sampersand wants to merge 1 commit into
soutaro:masterfrom
sampersand:swesterman/2026-06-09/add-warning-annotation-in

Conversation

@sampersand

@sampersand sampersand commented Jun 9, 2026

Copy link
Copy Markdown

This PR adds in support for the %a{warning: ...} annotation, which can be attached to constants, methods, or global variables.

Motivating Usage

This is the usage that motivated it (from ruby/rbs#2987), slightly simplified:

class Array[E]
  def min: () { (E a, E b) -> Comparable::_CompareToZero } -> E?
         | # ... other branches ...
end

This is the correct signature—the return value of the block cannot return nil. However, most <=> implementations do return nil, and having to do a kludge (eg array.min { |a,b| (a.foo <=> b.foo) || raise("never happens") }) is quite un-ergonomic, awkward, and can potentially break backwards-compat.

The solution is to add in a warning annotation:

class Array[E]
  def min: () { (E a, E b) -> Comparable::_CompareToZero } -> E?
         | %a{warning: returning nil from comparisons always raises} () { (E a, E b) -> Comparable::_CompareToZero? } -> E?
         | # ... other branches ...
end

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