Skip to content

Instantly share code, notes, and snippets.

@ybiquitous
Last active June 25, 2020 01:11
Show Gist options
  • Save ybiquitous/378421613c7003d74d08afc367149d0d to your computer and use it in GitHub Desktop.
Save ybiquitous/378421613c7003d74d08afc367149d0d to your computer and use it in GitHub Desktop.
Steep failure for Enumerable#filter_map
class Foo
def run
(1...9).filter_map { |n| n * 2 if n.even? }
end
def run2
(1...9).filter(&:even?).map { |n| n * 2 }
end
end
pp Foo.new.run
#=> [4, 8, 12, 16]
pp Foo.new.run2
#=> [4, 8, 12, 16]
class Foo
def run: () -> Array[Integer]
def run2: () -> Array[Integer]
end
source "https://rubygems.org"
gem "steep", git: "https://github.com/soutaro/steep.git", ref: "24363d77095f9e6c5e4f14d3ddb549317ee1d43f"
target :lib do
signature "sig"
check "lib"
end
@ybiquitous
Copy link
Author

ybiquitous commented Jun 24, 2020

Reproduction:

  1. bundle install
  2. mkdir lib sig
  3. mv foo.rb lib/
  4. mv foo.rbs sig/
  5. bundle exec steep check
lib/foo.rb:2:2: MethodBodyTypeMismatch: method=run, expected=::Array[::Integer], actual=::Array[(::Integer & nil)] (def run)
  ::Array[(::Integer & nil)] <: ::Array[::Integer]
   ::Integer <: (::Integer & nil)
    ::Integer <: nil
==> ::Integer <: nil does not hold

@ybiquitous
Copy link
Author

ybiquitous commented Jun 24, 2020

module Enumerable[unchecked out Elem, out Return]: _Each[Elem, Return]
  def filter_map: [U] () { (Elem arg0) -> U? } -> ::Array[U]
                | () -> ::Enumerator[Elem, Return]
end

https://github.com/ruby/rbs/blob/503594a4901b37a9aabd30870d1824324f2de712/stdlib/builtin/enumerable.rbs#L377-L378

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment