Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type narrowing for block_given? #1099

Open
HoneyryderChuck opened this issue Apr 12, 2024 · 1 comment
Open

type narrowing for block_given? #1099

HoneyryderChuck opened this issue Apr 12, 2024 · 1 comment
Labels
area/core Issues related to the type checker core enhancement

Comments

@HoneyryderChuck
Copy link

If I have the following:

def each: () { (Integer) -> void } -> void
              | () -> ::Enumerator[Integer, void]

then the following method presents a steep type error:

def each(&)
  return enum_for(__method__) unless block_given?

  @bag_of_ints.each(&)
  # Cannot pass a value of type `(^(::Integer) -> void | nil)` as a block-pass-argument of type `^(::Integer) -> void`
  # (^(::Integer) -> void | nil) <: ^(::Integer) -> void
  #   nil <: ^(::Integer) -> void(Ruby::BlockTypeMismatch)

because the block_given? call isn't narrowing the type.

The error goes away if I defined the function with (&blk) and do unless blk instead. However, rubocop will enforce nameless blocks in autocorrect for modern ruby, which exposes this error on ruby upgrades.

@soutaro
Copy link
Owner

soutaro commented Apr 15, 2024

Hmm, implementing type narrowing based on the block_given? call might be the solution...

@soutaro soutaro added enhancement area/core Issues related to the type checker core labels Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/core Issues related to the type checker core enhancement
Projects
None yet
Development

No branches or pull requests

2 participants