Sorbet: Kernel methods not avaiable for mixin modules

Created on 27 Jun 2019  Â·  4Comments  Â·  Source: sorbet/sorbet

Input

→ View on sorbet.run

# typed: true
module Foo
  def bar
    raise 'foo'
  end
end

class Fooer
  include Foo
end

Fooer.new.bar # Raises an error when run

Observed output

editor.rb:4: Method raise does not exist on Foo https://srb.help/7003
     4 |    raise 'foo'
            ^^^^^^^^^^^
  Did you mean to `include Kernel` in this module?
    https://github.com/sorbet/sorbet/tree/90c20f728c528aa4a1a3dc1a137e4c9eae95716d/rbi/core/kernel.rbi#L870: Did you mean: Kernel#raise?
     870 |  def raise(arg0=T.unsafe(nil), arg1=T.unsafe(nil), arg2=T.unsafe(nil)); end
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Errors: 1

In real execution this code is perfectly valid, the call to the mixed-in method will raise an error.

Expected behavior

No error when running typecheck

Depending on the underlying system, this may be the same root cause as https://github.com/sorbet/sorbet/issues/397

bug

All 4 comments

You should just need to include Kernel in the definition of module Foo to resolve this error during typechecking. For more information, see including kernel

@elliottt Thanks for linking to that trouble shooting doc, my searching focused on others having the issue with raise not Kernel.

@bmalinconico The reason why it is this way is because this would fail at runtime (note the subclassing from BasicObject here):

# typed: true
module Foo
  def bar
    raise 'foo'
  end
end

class Baz < BasicObject
  include ::Foo
end

Baz.new.bar 

Basically, Sorbet cannot guarantee that the module would always be included into something that inherits from Object, so it tries to be safe.

Because this is expected, documented behavior, I'm closing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

viraptor picture viraptor  Â·  4Comments

pariser picture pariser  Â·  3Comments

smt116 picture smt116  Â·  4Comments

elliottt picture elliottt  Â·  3Comments

julius-stripe picture julius-stripe  Â·  3Comments