Skip to content

RBS signatures can't be defined on method_added or singleton_method_added #862

@amomchilov

Description

@amomchilov

Describe the bug

Sorbet runtime doesn't support you using sig on method_added and singleton_method_added.

It is not a static error (Sorbet.run), but it is an error at runtime:

https://github.com/sorbet/sorbet/blob/234fef89444438a2e152d5658c8588b1d80db229/gems/sorbet-runtime/lib/types/private/methods/_methods.rb#L221-L226

Putting a sig on method_added is not supported (sorbet-runtime uses this method internally to perform sig validation logic)

RBS doesn't have to have this limitation, since it doesn't need this runtime support.

To Reproduce
Spoom version: '1.7.11'

Steps to reproduce the behavior:

  1. Add an RBS signature to an implementation of method_added or singleton_method_added, e.g.

    class Demo
      class << self
        extend T::Sig
        
        # @override
        #: (Symbol) -> void
        def method_added(m)
          super
        end
    
        # @override
        #: (Symbol) -> void
        def singleton_method_added(m)
          super
        end
      end
    end
  2. Translate the file with Spoom::Sorbet::Translate.rbs_comments_to_sorbet_sigs (like Tapioca does)

  3. Execute the translated file

Expected behavior

No crash.

Workaround

Mark the signatures with @without_runtime

class Demo
  class << self
    extend T::Sig
    
    # @override
+   # @without_runtime
    #: (Symbol) -> void
    def method_added(m)
      super
    end

    # @override
+   # @without_runtime
    #: (Symbol) -> void
    def singleton_method_added(m)
      super
    end
  end
end

Potential solution

  1. Add a special case to skip translation for every method_added and singleton_method_added
  2. Similarly (but more regular), treat each such method as if it were tagged @without_runtime

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions