Skip to content

Convert self to ClassInstance when #initialize method#2831

Merged
ksss merged 2 commits intoruby:masterfrom
ksss:dynamic-initialize
Jan 29, 2026
Merged

Convert self to ClassInstance when #initialize method#2831
ksss merged 2 commits intoruby:masterfrom
ksss:dynamic-initialize

Conversation

@ksss
Copy link
Collaborator

@ksss ksss commented Jan 28, 2026

In a class, the #initialize method is often copied as the type for the .new method.
In this case, if self is used in the type definition, they are copied as is.
As a result, a problem has been reported by @rhiroe where self is treated as singleton(Foo) even though it was written as self in the context of #initialize.

I aim to solve this by dynamically rewriting the type when it is copied from #initialize to .new.

Real problem

bn = OpenSSL::BN.new(1)
OpenSSL::BN.new(bn)
$ bundle exec steep check
# Type checking files:

.F

sample.rb:9:0: [error] Cannot find compatible overloading of method `new` of type `singleton(::OpenSSL::BN)`
│ Method types:
│   def new: (singleton(::OpenSSL::BN)) -> ::OpenSSL::BN
│          | (::Integer) -> ::OpenSSL::BN
│          | (::string) -> ::OpenSSL::BN
│          | (::string, (0 | 2 | 10 | 16)) -> ::OpenSSL::BN
│
│ Diagnostic ID: Ruby::UnresolvedOverloading
│
└ OpenSSL::BN.new(bn)
  ~~~~~~~~~~~~~~~~~~~

Detected 1 problem from 1 file

With Generics

# foo.rbs
class Foo[T]
  def initialize: () { (self, class, instance) -> void } -> void
end

Before

# foo.rb
Foo.new do |s, c, i|
  s #=> singleton(Foo)
  c #=> singleton(Foo)
  i #=> Foo[untyped]
end

After

# foo.rb
Foo.new do |s, c, i|
  s #=> Foo[untyped]         <- Fixed only here
  c #=> singleton(Foo)
  i #=> Foo[untyped]
end

@ksss ksss added this to the RBS 4.0 milestone Jan 29, 2026


def test_new
assert_send_type "(::OpenSSL::BN) -> OpenSSL::BN",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the changes

Failure: test_new(OpenSSLBNSingletonTest):
  Call trace does not match one of method definitions:
    #<struct RBS::Test::CallTrace method_name=:new, method_call=<RBS::Test::ArgumentsReturn:@arguments: [#<OpenSSL::BN:0x00000001279b8b38>], @exit_value: #<OpenSSL::BN:0x0000000127951e10>, @exit_type: :return>, block_calls=[], block_given=false>
    (self) -> ::OpenSSL::BN | (::Integer) -> ::OpenSSL::BN | (::string) -> ::OpenSSL::BN | (::string, 0 | 2 | 10 | 16) -> ::OpenSSL::BN.
  <false> is not true.
/Users/ksss/src/github.com/ksss/rbs/lib/rbs/unit_test/type_assertions.rb:187:in 'block in RBS::UnitTest::TypeAssertions#assert_send_type'
/Users/ksss/src/github.com/ksss/rbs/lib/rbs/unit_test/type_assertions.rb:168:in 'RBS::UnitTest::TypeAssertions#send_setup'
/Users/ksss/src/github.com/ksss/rbs/lib/rbs/unit_test/type_assertions.rb:172:in 'RBS::UnitTest::TypeAssertions#assert_send_type'
test/stdlib/OpenSSL_test.rb:237:in 'OpenSSLBNSingletonTest#test_new'
     234:   end
     235:
     236:   def test_new
  => 237:     assert_send_type "(::OpenSSL::BN) -> OpenSSL::BN",
     238:       OpenSSL::BN, :new, OpenSSL::BN.new(3)
     239:   end
     240: end

@ksss ksss changed the title Convert self with #initialize method Convert self to ClassInstance when #initialize method Jan 29, 2026
@ksss ksss merged commit f0134b2 into ruby:master Jan 29, 2026
33 of 36 checks passed
@ksss ksss deleted the dynamic-initialize branch January 29, 2026 06:36
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