diff --git a/lib/tapioca.rb b/lib/tapioca.rb index 2d9d82180..a1b3e87d8 100644 --- a/lib/tapioca.rb +++ b/lib/tapioca.rb @@ -43,8 +43,4 @@ class Error < StandardError; end }.freeze, T::Hash[String, String]) end -require "tapioca/runtime/reflection" -require "tapioca/runtime/trackers" -require "tapioca/dsl/compiler" -require "tapioca/runtime/dynamic_mixin_compiler" require "tapioca/version" diff --git a/lib/tapioca/internal.rb b/lib/tapioca/internal.rb index 33120bf6b..de264f3ec 100644 --- a/lib/tapioca/internal.rb +++ b/lib/tapioca/internal.rb @@ -2,6 +2,9 @@ # frozen_string_literal: true require "tapioca" +require "tapioca/runtime/reflection" +require "tapioca/runtime/trackers" +require "tapioca/runtime/dynamic_mixin_compiler" require "tapioca/runtime/loader" require "tapioca/sorbet_ext/generic_name_patch" require "tapioca/sorbet_ext/fixed_hash_patch" @@ -20,5 +23,6 @@ require "tapioca/gem/events" require "tapioca/gem/listeners" require "tapioca/gem/pipeline" -require "tapioca/static/requires_compiler" +require "tapioca/dsl/compiler" require "tapioca/dsl/pipeline" +require "tapioca/static/requires_compiler" diff --git a/lib/tapioca/sorbet_ext/generic_name_patch.rb b/lib/tapioca/sorbet_ext/generic_name_patch.rb index 338ba4307..22cbaa717 100644 --- a/lib/tapioca/sorbet_ext/generic_name_patch.rb +++ b/lib/tapioca/sorbet_ext/generic_name_patch.rb @@ -57,18 +57,6 @@ def type_template(variance = :invariant, fixed: nil, lower: T.untyped, upper: Ba module Types class Simple module GenericPatch - def valid?(obj) - # Since `Tapioca::TypeVariable` is a `Module`, it will be wrapped by a - # `Simple` type. We want to always make type variable types valid, so we - # need to explicitly check that `raw_type` is a `Tapioca::TypeVariable` - # and return `true` - if defined?(Tapioca::TypeVariableModule) && Tapioca::TypeVariableModule === @raw_type - return true - end - - obj.is_a?(@raw_type) - end - # This method intercepts calls to the `name` method for simple types, so that # it can ask the name to the type if the type is generic, since, by this point, # we've created a clone of that type with the `name` method returning the @@ -88,9 +76,34 @@ def name prepend GenericPatch end end + + module Utils + module CoercePatch + def coerce(val) + if val.is_a?(Tapioca::TypeVariableModule) + val.coerce_to_type_variable + else + super + end + end + end + + class << self + prepend(CoercePatch) + end + end end module Tapioca + class TypeVariable < ::T::Types::TypeVariable + def initialize(name, variance) + @name = name + super(variance) + end + + attr_reader :name + end + # This is subclassing from `Module` so that instances of this type will be modules. # The reason why we want that is because that means those instances will automatically # get bound to the constant names they are assigned to by Ruby. As a result, we don't @@ -153,6 +166,11 @@ def serialize serialized end + sig { returns(Tapioca::TypeVariable) } + def coerce_to_type_variable + TypeVariable.new(name, @variance) + end + private sig do