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

Fix self union type checking #1467

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

marcotc
Copy link

@marcotc marcotc commented Jan 14, 2025

This fixes the case where a method can return either self or another value (e.g. -> (self | nil)).

Here's the concrete example: https://github.com/DataDog/dd-trace-rb/blob/5361e8e9a7092ca081ff0d5126daf8950bb2c792/lib/datadog/tracing/span_operation.rb#L202-L223

      def stop(stop_time = nil)
        return if stopped?

        now = Core::Utils::Time.now.utc

        start(stop_time || now) unless started?

        @end_time = stop_time || now
        @duration_end = stop_time.nil? ? duration_marker : nil

        events.after_stop.publish(self)

        self
      end

Currently this fails with:

Cannot allow method body have type `self` because declared as type `(self | nil)`
Diagnostic ID: Ruby::MethodBodyTypeMismatch

@ParadoxV5
Copy link
Contributor

Could be similar:

def example(x) = x
class Object
  def example: [X < top] (X) -> X?
end
# Type checking files:

.F

ruby.rb:1:4: [error] Cannot allow method body have type `X` because declared as type `(X | nil)`
│   X <: (X | nil)
│     top <: (X | nil)
│       top <: X
│
│ Diagnostic ID: Ruby::MethodBodyTypeMismatch
│
└ def example(x) = x
      ~~~~~~~

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.

2 participants