Skip to content

Commit

Permalink
Merge pull request #839 from Shopify/uk-fix-generic-type-variable-arg
Browse files Browse the repository at this point in the history
Fix generic type variable argument serialization
  • Loading branch information
paracycle committed Mar 1, 2022
2 parents ee2143b + 4e64825 commit 473258e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/tapioca/runtime/generic_type_registry.rb
Expand Up @@ -111,8 +111,10 @@ def create_generic_type(constant, name)
constant.clone
end

# Let's set the `name` method to return the proper generic name
generic_type.define_singleton_method(:name) { name }
# Let's set the `name` and `to_s` methods to return the proper generic name
name_proc = -> { name }
generic_type.define_singleton_method(:name, name_proc)
generic_type.define_singleton_method(:to_s, name_proc)

# We need to define a `<=` method on the cloned constant, so that Sorbet
# can do covariance/contravariance checks on the type variables.
Expand Down
38 changes: 38 additions & 0 deletions spec/tapioca/gem/pipeline_spec.rb
Expand Up @@ -2819,6 +2819,44 @@ class Foo
assert_equal(output, compile)
end

it "can compile generics in type variable arguments" do
add_ruby_file("service.rb", <<~RUBY)
class Result
extend T::Generic
OkType = type_member
ErrType = type_member
end
class Service
extend T::Generic
InputType = type_member(upper: Result[Integer, String])
ReturnType = type_member(fixed: Result[Integer, String])
TempType = type_member(lower: Result[Integer, String])
end
RUBY

output = template(<<~RBI)
class Result
extend T::Generic
OkType = type_member
ErrType = type_member
end
class Service
extend T::Generic
InputType = type_member(upper: Result[::Integer, ::String])
ReturnType = type_member(fixed: Result[::Integer, ::String])
TempType = type_member(lower: Result[::Integer, ::String])
end
RBI

assert_equal(output, compile)
end

it "can compile typed struct generics" do
add_ruby_file("tstruct_generic.rb", <<~RUBY)
class Foo < T::Struct
Expand Down

0 comments on commit 473258e

Please sign in to comment.