Class: RBS::VarianceCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/variance_calculator.rb

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder:) ⇒ VarianceCalculator

Returns a new instance of VarianceCalculator.



78
79
80
# File 'lib/rbs/variance_calculator.rb', line 78

def initialize(builder:)
  @builder = builder
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



76
77
78
# File 'lib/rbs/variance_calculator.rb', line 76

def builder
  @builder
end

Instance Method Details

#envObject



82
83
84
# File 'lib/rbs/variance_calculator.rb', line 82

def env
  builder.env
end

#function(type, result:, context:) ⇒ Object



169
170
171
172
173
174
# File 'lib/rbs/variance_calculator.rb', line 169

def function(type, result:, context:)
  type.each_param do |param|
    type(param.type, result: result, context: negate(context))
  end
  type(type.return_type, result: result, context: context)
end

#in_inherit(name:, args:, variables:) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rbs/variance_calculator.rb', line 98

def in_inherit(name:, args:, variables:)
  type = if name.class?
           Types::ClassInstance.new(name: name, args: args, location: nil)
         else
           Types::Interface.new(name: name, args: args, location: nil)
         end

  Result.new(variables: variables).tap do |result|
    type(type, result: result, context: :covariant)
  end
end

#in_method_type(method_type:, variables:) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rbs/variance_calculator.rb', line 86

def in_method_type(method_type:, variables:)
  result = Result.new(variables: variables)

  function(method_type.type, result: result, context: :covariant)

  if block = method_type.block
    function(block.type, result: result, context: :contravariant)
  end

  result
end

#in_type_alias(name:) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/rbs/variance_calculator.rb', line 110

def in_type_alias(name:)
  env.normalized_type_name!(name)

  decl = env.type_alias_decls[name].decl or raise
  variables = decl.type_params.each.map(&:name)

  Result.new(variables: variables).tap do |result|
    type(decl.type, result: result, context: :covariant)
  end
end

#negate(variance) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rbs/variance_calculator.rb', line 176

def negate(variance)
  case variance
  when :invariant
    :invariant
  when :covariant
    :contravariant
  when :contravariant
    :covariant
  else
    raise
  end
end

#type(type, result:, context:) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/rbs/variance_calculator.rb', line 121

def type(type, result:, context:)
  case type
  when Types::Variable
    if result.include?(type.name)
      case context
      when :covariant
        result.covariant(type.name)
      when :contravariant
        result.contravariant(type.name)
      when :invariant
        result.invariant(type.name)
      end
    end
  when Types::ClassInstance, Types::Interface, Types::Alias
    if type_name = env.normalize_type_name?(type.name)
      type_params = case type
                    when Types::ClassInstance
                      env.class_decls[type_name].type_params
                    when Types::Interface
                      env.interface_decls[type_name].decl.type_params
                    when Types::Alias
                      env.type_alias_decls[type_name].decl.type_params
                    end

      type.args.each.with_index do |ty, i|
        if var = type_params[i]
          case var.variance
          when :invariant
            type(ty, result: result, context: :invariant)
          when :covariant
            type(ty, result: result, context: context)
          when :contravariant
            type(ty, result: result, context: negate(context))
          end
        end
      end
    else
      raise NoTypeFoundError.new(type_name: type.name, location: type.location)
    end
  when Types::Proc
    function(type.type, result: result, context: context)
  else
    type.each_type do |ty|
      type(ty, result: result, context: context)
    end
  end
end