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

T.self_type does not work for union types (e.g. T::Boolean#tap returns TrueClass) #7890

Open
ipvalverde opened this issue May 16, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@ipvalverde
Copy link

Input

→ View on sorbet.run

# typed: true
extend T::Sig

my_boolean = rand(10).even?

boolean_tap_result = my_boolean.tap { puts "Do nothing" }
T.reveal_type(boolean_tap_result) # <== TrueClass!?


MyUnionType = T.type_alias { T.any(String, Integer) }

union_type_instance =
  if rand(10).even?
    "Hey there!"
  else
    1
  end

union_type_tap_result = union_type_instance.tap { puts "Do nothing" }
T.reveal_type(union_type_tap_result) # <== Integer!?

Observed output

editor.rb:7: Revealed type: TrueClass https://srb.help/7014
     7 |T.reveal_type(boolean_tap_result) # <== TrueClass!?
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  Got TrueClass originating from:
    editor.rb:6:
     6 |boolean_tap_result = my_boolean.tap { puts "Do nothing" }
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

editor.rb:20: Revealed type: Integer https://srb.help/7014
    20 |T.reveal_type(union_type_tap_result) # <== Integer!?
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  Got Integer originating from:
    editor.rb:19:
    19 |union_type_tap_result = union_type_instance.tap { puts "Do nothing" }
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Errors: 2

Expected behavior

The T.self_type should respect the union type. So instead of just retuning a (random?!) type from the union, it should return the whole union type definition.


@ipvalverde ipvalverde added bug Something isn't working unconfirmed This issue has not yet been confirmed by the Sorbet team labels May 16, 2024
@jez jez removed the unconfirmed This issue has not yet been confirmed by the Sorbet team label May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants