Class: Steep::TypeInference::LogicTypeInterpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/type_inference/logic_type_interpreter.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

TRUE =
AST::Types::Literal.new(value: true)
FALSE =
AST::Types::Literal.new(value: false)
BOOL =
AST::Types::Boolean.instance
BOT =
AST::Types::Bot.instance
UNTYPED =
AST::Types::Any.instance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subtyping:, typing:, config:) ⇒ LogicTypeInterpreter

Returns a new instance of LogicTypeInterpreter.



24
25
26
27
28
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 24

def initialize(subtyping:, typing:, config:)
  @subtyping = subtyping
  @typing = typing
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



22
23
24
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 22

def config
  @config
end

#subtypingObject (readonly)

Returns the value of attribute subtyping.



20
21
22
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 20

def subtyping
  @subtyping
end

#typingObject (readonly)

Returns the value of attribute typing.



21
22
23
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 21

def typing
  @typing
end

Instance Method Details

#decompose_value(node) ⇒ Object



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 489

def decompose_value(node)
  case node.type
  when :lvar
    [node, Set[node.children[0]]]
  when :masgn
    _, rhs = node.children
    decompose_value(rhs)
  when :lvasgn
    var, rhs = node.children
    val, vars = decompose_value(rhs)
    [val, vars + [var]]
  when :begin
    decompose_value(node.children.last)
  when :and
    left, right = node.children
    _, left_vars = decompose_value(left)
    val, right_vars = decompose_value(right)
    [val, left_vars + right_vars]
  else
    [node, Set[]]
  end
end

#eval(env:, node:) ⇒ Object



56
57
58
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 56

def eval(env:, node:)
  evaluate_node(env: env, node: node)
end

#evaluate_assignment(assignment_node, env, rhs_type) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 221

def evaluate_assignment(assignment_node, env, rhs_type)
  case assignment_node.type
  when :lvasgn
    name, _ = assignment_node.children
    if TypeConstruction::SPECIAL_LVAR_NAMES.include?(name)
      env
    else
      env.refine_types(local_variable_types: { name => rhs_type })
    end
  when :masgn
    lhs, _ = assignment_node.children

    masgn = MultipleAssignment.new()
    assignments = masgn.expand(lhs, rhs_type, false)
    unless assignments
      rhs_type_converted = try_convert(rhs_type, :to_ary)
      rhs_type_converted ||= try_convert(rhs_type, :to_a)
      rhs_type_converted ||= AST::Types::Tuple.new(types: [rhs_type])
      assignments = masgn.expand(lhs, rhs_type_converted, false)
    end

    unless assignments
      raise "Multiple assignment rhs doesn't look correct: #{rhs_type.to_s} (#{assignment_node.location.expression&.source_line})"
    end

    assignments.each do |pair|
      node, type = pair
      env = evaluate_assignment(node, env, type)
    end

    env
  else
    env
  end
end

#evaluate_method_call(env:, type:, receiver:, arguments:) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 302

def evaluate_method_call(env:, type:, receiver:, arguments:)
  case type
  when AST::Types::Logic::ReceiverIsNil
    if receiver && arguments.size.zero?
      receiver_type = typing.type_of(node: receiver)
      unwrap = factory.unwrap_optional(receiver_type)
      truthy_receiver = AST::Builtin.nil_type
      falsy_receiver = unwrap || receiver_type

      truthy_env, falsy_env = refine_node_type(
        env: env,
        node: receiver,
        truthy_type: truthy_receiver,
        falsy_type: falsy_receiver
      )

      truthy_result = Result.new(type: TRUE, env: truthy_env, unreachable: false)
      truthy_result.unreachable! if no_subtyping?(sub_type: AST::Builtin.nil_type, super_type: receiver_type)

      falsy_result = Result.new(type: FALSE, env: falsy_env, unreachable: false)
      falsy_result.unreachable! unless unwrap

      [truthy_result, falsy_result]
    end

  when AST::Types::Logic::ReceiverIsArg
    if receiver && (arg = arguments[0])
      receiver_type = typing.type_of(node: receiver)
      arg_type = factory.deep_expand_alias(typing.type_of(node: arg))

      if arg_type.is_a?(AST::Types::Name::Singleton)
        truthy_type, falsy_type = type_case_select(receiver_type, arg_type.name)
        truthy_env, falsy_env = refine_node_type(
          env: env,
          node: receiver,
          truthy_type: truthy_type || factory.instance_type(arg_type.name),
          falsy_type: falsy_type || UNTYPED
        )

        truthy_result = Result.new(type: TRUE, env: truthy_env, unreachable: false)
        truthy_result.unreachable! unless truthy_type

        falsy_result = Result.new(type: FALSE, env: falsy_env, unreachable: false)
        falsy_result.unreachable! unless falsy_type

        [truthy_result, falsy_result]
      elsif (truthy_types, falsy_types = literal_var_type_case_select(arg, receiver_type, for_receiver: true))
        # Handle literal equality: receiver == literal_value
        truthy_env, falsy_env = refine_node_type(
          env: env,
          node: receiver,
          truthy_type: truthy_types.empty? ? BOT : AST::Types::Union.build(types: truthy_types),
          falsy_type: falsy_types.empty? ? BOT : AST::Types::Union.build(types: falsy_types)
        )

        truthy_result = Result.new(type: TRUE, env: truthy_env, unreachable: false)
        truthy_result.unreachable! if truthy_types.empty?

        falsy_result = Result.new(type: FALSE, env: falsy_env, unreachable: false)
        falsy_result.unreachable! if falsy_types.empty?

        [truthy_result, falsy_result]
      end
    end

  when AST::Types::Logic::ArgIsReceiver
    if receiver && (arg = arguments[0])
      receiver_type = factory.deep_expand_alias(typing.type_of(node: receiver))
      arg_type = typing.type_of(node: arg)

      if receiver_type.is_a?(AST::Types::Name::Singleton)
        truthy_type, falsy_type = type_case_select(arg_type, receiver_type.name)
        truthy_env, falsy_env = refine_node_type(
          env: env,
          node: arg,
          truthy_type: truthy_type || factory.instance_type(receiver_type.name),
          falsy_type: falsy_type || UNTYPED
        )

        truthy_result = Result.new(type: TRUE, env: truthy_env, unreachable: false)
        truthy_result.unreachable! unless truthy_type

        falsy_result = Result.new(type: FALSE, env: falsy_env, unreachable: false)
        falsy_result.unreachable! unless falsy_type

        [truthy_result, falsy_result]
      end
    end
  when AST::Types::Logic::ArgEqualsReceiver
    if receiver && (arg = arguments[0])
      arg_type = factory.expand_alias(typing.type_of(node: arg))
      if (truthy_types, falsy_types = literal_var_type_case_select(receiver, arg_type))
        truthy_env, falsy_env = refine_node_type(
          env: env,
          node: arg,
          truthy_type: truthy_types.empty? ? BOT : AST::Types::Union.build(types: truthy_types),
          falsy_type: falsy_types.empty? ? BOT : AST::Types::Union.build(types: falsy_types)
        )

        truthy_result = Result.new(type: TRUE, env: truthy_env, unreachable: false)
        truthy_result.unreachable! if truthy_types.empty?

        falsy_result = Result.new(type: FALSE, env: falsy_env, unreachable: false)
        falsy_result.unreachable! if falsy_types.empty?

        [truthy_result, falsy_result]
      end
    end

  when AST::Types::Logic::ArgIsAncestor
    if receiver && (arg = arguments[0])
      receiver_type = typing.type_of(node: receiver)
      arg_type = factory.deep_expand_alias(typing.type_of(node: arg))

      if arg_type.is_a?(AST::Types::Name::Singleton)
        truthy_type = arg_type
        falsy_type = receiver_type
        truthy_env, falsy_env = refine_node_type(
          env: env,
          node: receiver,
          truthy_type: truthy_type,
          falsy_type: falsy_type
        )

        truthy_result = Result.new(type: TRUE, env: truthy_env, unreachable: false)
        truthy_result.unreachable! unless truthy_type

        falsy_result = Result.new(type: FALSE, env: falsy_env, unreachable: false)
        falsy_result.unreachable! unless falsy_type

        [truthy_result, falsy_result]
      end
    end

  when AST::Types::Logic::Not
    if receiver
      truthy_result, falsy_result = evaluate_node(env: env, node: receiver)
      [
        falsy_result.update_type { TRUE },
        truthy_result.update_type { FALSE }
      ]
    end
  end
end

#evaluate_node(env:, node:, type: typing.type_of(node: node)) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 60

def evaluate_node(env:, node:, type: typing.type_of(node: node))
  if type.is_a?(AST::Types::Logic::Env)
    truthy_env = type.truthy
    falsy_env = type.falsy

    truthy_type, falsy_type = factory.partition_union(type.type)

    return [
      Result.new(env: truthy_env, type: truthy_type || TRUE, unreachable: !truthy_type),
      Result.new(env: falsy_env, type: falsy_type || FALSE, unreachable: !falsy_type)
    ]
  end

  if type.is_a?(AST::Types::Bot)
    return [
      Result.new(env: env, type: type, unreachable: true),
      Result.new(env: env, type: type, unreachable: true),
    ]
  end

  if type.is_a?(AST::Types::Var)
    type = config.upper_bound(type.name) || type
  end

  case node.type
  when :lvar
    name = node.children[0]
    truthy_type, falsy_type = factory.partition_union(type)

    truthy_result =
      if truthy_type
        Result.new(type: truthy_type, env: env.refine_types(local_variable_types: { name => truthy_type }), unreachable: false)
      else
        Result.new(type: type, env: env, unreachable: true)
      end

    falsy_result =
      if falsy_type
        Result.new(type: falsy_type, env: env.refine_types(local_variable_types: { name => falsy_type }), unreachable: false)
      else
        Result.new(type: type, env: env, unreachable: true)
      end

    return [truthy_result, falsy_result]

  when :lvasgn
    name, rhs = node.children
    if TypeConstruction::SPECIAL_LVAR_NAMES.include?(name)
      return [
        Result.new(type: type, env: env, unreachable: false),
        Result.new(type: type, env: env, unreachable: false)
      ]
    end

    truthy_result, falsy_result = evaluate_node(env: env, node: rhs)

    return [
      truthy_result.update_env { evaluate_assignment(node, truthy_result.env, truthy_result.type) },
      falsy_result.update_env { evaluate_assignment(node, falsy_result.env, falsy_result.type) }
    ]

  when :masgn
    _, rhs = node.children
    truthy_result, falsy_result = evaluate_node(env: env, node: rhs)

    return [
      truthy_result.update_env { evaluate_assignment(node, truthy_result.env, truthy_result.type) },
      falsy_result.update_env { evaluate_assignment(node, falsy_result.env, falsy_result.type) }
    ]

  when :begin
    last_node = node.children.last or raise
    return evaluate_node(env: env, node: last_node)

  when :csend
    if type.is_a?(AST::Types::Any)
      type = guess_type_from_method(node) || type
    end

    receiver, _, *arguments = node.children
    receiver_type = typing.type_of(node: receiver)

    truthy_receiver, falsy_receiver = evaluate_node(env: env, node: receiver)
    truthy_type, _ = factory.partition_union(type)

    truthy_result, falsy_result = evaluate_node(
      env: truthy_receiver.env,
      node: node.updated(:send),
      type: truthy_type || type
    )
    truthy_result.unreachable! if truthy_receiver.unreachable

    falsy_result = Result.new(
      env: env.join(falsy_receiver.env, falsy_result.env),
      unreachable: falsy_result.unreachable && falsy_receiver.unreachable,
      type: falsy_result.type
    )

    return [truthy_result, falsy_result]

  when :send
    if type.is_a?(AST::Types::Any)
      type = guess_type_from_method(node) || type
    end

    case type
    when AST::Types::Logic::Base
      receiver, _, *arguments = node.children
      if (truthy_result, falsy_result = evaluate_method_call(env: env, type: type, receiver: receiver, arguments: arguments))
        return [truthy_result, falsy_result]
      end
    else
      receiver, *_ = node.children
      receiver_type = typing.type_of(node: receiver) if receiver

      if env[receiver] && receiver_type.is_a?(AST::Types::Union)
        result = evaluate_union_method_call(node: node, type: type, env: env, receiver: receiver, receiver_type: receiver_type)
        if result
          truthy_result = result[0] unless result[0].unreachable
          falsy_result = result[1] unless result[1].unreachable
        end
      end

      truthy_result ||= Result.new(type: type, env: env, unreachable: false)
      falsy_result ||= Result.new(type: type, env: env, unreachable: false)

      truthy_type, falsy_type = factory.partition_union(type)

      if truthy_type
        truthy_result = truthy_result.update_type { truthy_type }
      else
        truthy_result = truthy_result.update_type { BOT }.unreachable!
      end

      if falsy_type
        falsy_result = falsy_result.update_type { falsy_type }
      else
        falsy_result = falsy_result.update_type { BOT }.unreachable!
      end

      if truthy_result.env[node] && falsy_result.env[node]
        if truthy_type
          truthy_result = Result.new(type: truthy_type, env: truthy_result.env.refine_types(pure_call_types: { node => truthy_type }), unreachable: false)
        end

        if falsy_type
          falsy_result = Result.new(type: falsy_type, env: falsy_result.env.refine_types(pure_call_types: { node => falsy_type }), unreachable: false)
        end
      end

      return [truthy_result, falsy_result]
    end
  end

  truthy_type, falsy_type = factory.partition_union(type)
  return [
    Result.new(type: truthy_type || BOT, env: env, unreachable: truthy_type.nil?),
    Result.new(type: falsy_type || BOT, env: env, unreachable: falsy_type.nil?)
  ]
end

#evaluate_union_method_call(node:, type:, env:, receiver:, receiver_type:) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 447

def evaluate_union_method_call(node:, type:, env:, receiver:, receiver_type:)
  call_type = typing.call_of(node: node) rescue nil
  return unless call_type.is_a?(Steep::TypeInference::MethodCall::Typed)

  truthy_types = [] #: Array[AST::Types::t]
  falsy_types = [] #: Array[AST::Types::t]

  receiver_type.types.each do |type|
    if shape = subtyping.builder.shape(type, config)
      method = shape.methods[call_type.method_name] or raise
      method_type = method.method_types.find do |method_type|
        call_type.method_decls.any? {|decl| factory.method_type(decl.method_type) == method_type }
      end
      if method_type
        return_type = method_type.type.return_type
        truthy, falsy = factory.partition_union(return_type)
        truthy_types << type if truthy
        falsy_types << type if falsy
        next
      end
    end

    truthy_types << type
    falsy_types << type
  end

  truthy_type = truthy_types.empty? ? BOT : AST::Types::Union.build(types: truthy_types)
  falsy_type = falsy_types.empty? ? BOT : AST::Types::Union.build(types: falsy_types)

  truthy_env, falsy_env = refine_node_type(
    env: env,
    node: receiver,
    truthy_type: truthy_type,
    falsy_type: falsy_type
  )

  return [
    Result.new(type: type, env: truthy_env, unreachable: truthy_type.nil?),
    Result.new(type: type, env: falsy_env, unreachable: falsy_type.nil?)
  ]
end

#factoryObject



30
31
32
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 30

def factory
  subtyping.factory
end

#guess_type_from_method(node) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 34

def guess_type_from_method(node)
  if node.type == :send
    method = node.children[1]
    case method
    when :is_a?, :kind_of?, :instance_of?
      AST::Types::Logic::ReceiverIsArg.instance
    when :nil?
      AST::Types::Logic::ReceiverIsNil.instance
    when :!
      AST::Types::Logic::Not.instance
    when :===
      AST::Types::Logic::ArgIsReceiver.instance
    end
  end
end

#literal_var_type_case_select(value_node, arg_type, for_receiver: false) ⇒ Object



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 512

def literal_var_type_case_select(value_node, arg_type, for_receiver: false)
  case arg_type
  when AST::Types::Union
    # @type var truthy_types: Array[AST::Types::t]
    truthy_types = []
    # @type var falsy_types: Array[AST::Types::t]
    falsy_types = []

    arg_type.types.each do |type|
      if (ts, fs = literal_var_type_case_select(value_node, type, for_receiver: for_receiver))
        truthy_types.push(*ts)
        falsy_types.push(*fs)
      else
        return
      end
    end

    [truthy_types, falsy_types]
  when AST::Types::Boolean
    [[arg_type], [arg_type]]
  when AST::Types::Top, AST::Types::Any
    [[arg_type], [arg_type]]
  else
    types = [arg_type]

    case value_node.type
    when :nil
      types.partition do |type|
        type.is_a?(AST::Types::Nil) || AST::Builtin::NilClass.instance_type?(type)
      end
    when :true
      types.partition do |type|
        AST::Builtin::TrueClass.instance_type?(type) ||
          (type.is_a?(AST::Types::Literal) && type.value == true)
      end
    when :false
      types.partition do |type|
        AST::Builtin::FalseClass.instance_type?(type) ||
          (type.is_a?(AST::Types::Literal) && type.value == false)
      end
    when :int, :str, :sym
      # @type var pairs: [Array[AST::Types::t], Array[AST::Types::t]]
      pairs = [[], []]

      types.each_with_object(pairs) do |type, pair|
        true_types, false_types = pair

        case
        when type.is_a?(AST::Types::Literal)
          if type.value == value_node.children[0]
            true_types << type
          else
            false_types << type
          end
        else
          # For === (for_receiver: false), non-literal types can match the literal in truthy branch
          # For == (for_receiver: true), non-literal types only go to falsy branch
          true_types << AST::Types::Literal.new(value: value_node.children[0]) unless for_receiver
          false_types << type
        end
      end
    end
  end
end

#no_subtyping?(sub_type:, super_type:) ⇒ Boolean

Returns:

  • (Boolean)


666
667
668
669
670
671
672
673
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 666

def no_subtyping?(sub_type:, super_type:)
  relation = Subtyping::Relation.new(sub_type: sub_type, super_type: super_type)
  result = subtyping.check(relation, constraints: Subtyping::Constraints.empty, self_type: AST::Types::Self.instance, instance_type: AST::Types::Instance.instance, class_type: AST::Types::Class.instance)

  if result.failure?
    result
  end
end

#refine_node_type(env:, node:, truthy_type:, falsy_type:) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 257

def refine_node_type(env:, node:, truthy_type:, falsy_type:)
  case node.type
  when :lvar
    name = node.children[0]

    if TypeConstruction::SPECIAL_LVAR_NAMES.include?(name)
      [env, env]
    else
      [
        env.refine_types(local_variable_types: { name => truthy_type }),
        env.refine_types(local_variable_types: { name => falsy_type })
      ]
    end

  when :lvasgn
    name, rhs = node.children

    truthy_env, falsy_env = refine_node_type(env: env, node: rhs, truthy_type: truthy_type, falsy_type: falsy_type)

    if TypeConstruction::SPECIAL_LVAR_NAMES.include?(name)
      [truthy_env, falsy_env]
    else
      [
        truthy_env.refine_types(local_variable_types: { name => truthy_type }),
        falsy_env.refine_types(local_variable_types: { name => falsy_type })
      ]
    end

  when :send
    if env[node]
      [
        env.refine_types(pure_call_types: { node => truthy_type }),
        env.refine_types(pure_call_types: { node => falsy_type })
      ]
    else
      [env, env]
    end
  when :begin
    last_node = node.children.last or raise
    refine_node_type(env: env, node: last_node, truthy_type: truthy_type, falsy_type: falsy_type)
  else
    [env, env]
  end
end

#subtyping?(sub_type:, super_type:) ⇒ Boolean

Returns:

  • (Boolean)


675
676
677
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 675

def subtyping?(sub_type:, super_type:)
  !no_subtyping?(sub_type: sub_type, super_type: super_type)
end

#try_convert(type, method) ⇒ Object



679
680
681
682
683
684
685
686
687
688
689
690
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 679

def try_convert(type, method)
  if shape = subtyping.builder.shape(type, config)
    if entry = shape.methods[method]
      method_type = entry.method_types.find do |method_type|
        method_type.type.params.nil? ||
          method_type.type.params.optional?
      end

      method_type.type.return_type if method_type
    end
  end
end

#type_case_select(type, klass) ⇒ Object



577
578
579
580
581
582
583
584
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 577

def type_case_select(type, klass)
  truth_types, false_types = type_case_select0(type, klass)

  [
    truth_types.empty? ? nil : AST::Types::Union.build(types: truth_types),
    false_types.empty? ? nil : AST::Types::Union.build(types: false_types)
  ]
end

#type_case_select0(type, klass) ⇒ Object



586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'lib/steep/type_inference/logic_type_interpreter.rb', line 586

def type_case_select0(type, klass)
  instance_type = factory.instance_type(klass)

  case type
  when AST::Types::Union
    truthy_types = [] # :Array[AST::Types::t]
    falsy_types = [] #: Array[AST::Types::t]

    type.types.each do |ty|
      truths, falses = type_case_select0(ty, klass)

      if truths.empty?
        falsy_types.push(ty)
      else
        truthy_types.push(*truths)
        falsy_types.push(*falses)
      end
    end

    [truthy_types, falsy_types]

  when AST::Types::Name::Alias
    ty = factory.expand_alias(type)
    if ty == type
      [[type], [type]]
    else
      type_case_select0(ty, klass)
    end

  when AST::Types::Any, AST::Types::Top, AST::Types::Var
    [
      [instance_type],
      [type]
    ]

  when AST::Types::Name::Interface
    [
      [instance_type],
      [type]
    ]

  else
    # There are four possible relations between `type` and `instance_type`
    #
    # ```ruby
    # case object      # object: T
    # when K           # K: singleton(K)
    # when ...
    # end
    # ````
    #
    # 1. T <: K && K <: T (T == K, T = Integer, K = Numeric)
    # 2. T <: K           (example: T = Integer, K = Numeric)
    # 3. K <: T           (example: T = Numeric, K = Integer)
    # 4. none of the above (example: T = String, K = Integer)

    if subtyping?(sub_type: type, super_type: instance_type)
      # 1 or 2. Satisfies the condition, no narrowing because `type` is already more specific than/equals to `instance_type`
      [
        [type],
        []
      ]
    else
      if subtyping?(sub_type: instance_type, super_type: type)
        # 3. Satisfied the condition, narrows to `instance_type`, but cannot remove it from *falsy* list
        [
          [instance_type],
          [type]
        ]
      else
        # 4
        [
          [],
          [type]
        ]
      end
    end
  end
end