Class: Steep::TypeInference::MultipleAssignment

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

Constant Summary collapse

Assignments =
_ = Struct.new(:rhs_type, :optional, :leading_assignments, :trailing_assignments, :splat_assignment, keyword_init: true) do
  # @implements Assignments

  def each(&block)
    if block
      leading_assignments.each(&block)
      if sp = splat_assignment
        yield sp
      end
      trailing_assignments.each(&block)
    else
      enum_for :each
    end
  end
end

Instance Method Summary collapse

Instance Method Details

#expand(mlhs, rhs_type, optional) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/steep/type_inference/multiple_assignment.rb', line 20

def expand(mlhs, rhs_type, optional)
  lhss = mlhs.children

  case rhs_type
  when AST::Types::Tuple
    expand_tuple(lhss.dup, rhs_type, rhs_type.types.dup, optional)
  when AST::Types::Name::Instance
    if AST::Builtin::Array.instance_type?(rhs_type)
      expand_array(lhss.dup, rhs_type, optional)
    end
  when AST::Types::Any
    expand_any(lhss, rhs_type, AST::Builtin.any_type, optional)
  end
end

#expand_any(nodes, rhs_type, element_type, optional) ⇒ Object



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
# File 'lib/steep/type_inference/multiple_assignment.rb', line 142

def expand_any(nodes, rhs_type, element_type, optional)
  # @type var leading_assignments: Array[node_type_pair]
  leading_assignments = []
  # @type var trailing_assignments: Array[node_type_pair]
  trailing_assignments = []
  # @type var splat_assignment: node_type_pair?
  splat_assignment = nil

  array = leading_assignments

  nodes.each do |node|
    case node.type
    when :splat
      splat_assignment = [node, AST::Builtin::Array.instance_type(element_type)]
      array = trailing_assignments
    else
      array << [node, element_type]
    end
  end

  Assignments.new(
    rhs_type: rhs_type,
    optional: optional,
    leading_assignments: leading_assignments,
    trailing_assignments: trailing_assignments,
    splat_assignment: splat_assignment
  )
end

#expand_array(lhss, rhs_type, optional) ⇒ Object



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
# File 'lib/steep/type_inference/multiple_assignment.rb', line 87

def expand_array(lhss, rhs_type, optional)
  element_type = rhs_type.args[0] or raise

  # @type var leading_assignments: Array[node_type_pair]
  leading_assignments = []
  # @type var trailing_assignments: Array[node_type_pair]
  trailing_assignments = []
  # @type var splat_assignment: node_type_pair?
  splat_assignment = nil

  while !lhss.empty?
    first = lhss.first or raise

    case
    when first.type == :splat
      break
    else
      leading_assignments << [first, AST::Builtin.optional(element_type)]
      lhss.shift
    end
  end

  while !lhss.empty?
    last = lhss.last or raise

    case
    when last.type == :splat
      break
    else
      trailing_assignments << [last, AST::Builtin.optional(element_type)]
      lhss.pop
    end
  end

  case lhss.size
  when 0
    # nop
  when 1
    splat_assignment = [
      lhss.first || raise,
      AST::Builtin::Array.instance_type(element_type)
    ]
  else
    raise
  end

  Assignments.new(
    rhs_type: rhs_type,
    optional: optional,
    leading_assignments: leading_assignments,
    trailing_assignments: trailing_assignments,
    splat_assignment: splat_assignment
  )
end

#expand_tuple(lhss, rhs_type, tuples, optional) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
# File 'lib/steep/type_inference/multiple_assignment.rb', line 35

def expand_tuple(lhss, rhs_type, tuples, optional)
  # @type var leading_assignments: Array[node_type_pair]
  leading_assignments = []
  # @type var trailing_assignments: Array[node_type_pair]
  trailing_assignments = []
  # @type var splat_assignment: node_type_pair?
  splat_assignment = nil

  while !lhss.empty?
    first = lhss.first or raise

    case
    when first.type == :splat
      break
    else
      leading_assignments << [first, tuples.first || AST::Builtin.nil_type]
      lhss.shift
      tuples.shift
    end
  end

  while !lhss.empty?
    last = lhss.last or raise

    case
    when last.type == :splat
      break
    else
      trailing_assignments << [last, tuples.last || AST::Builtin.nil_type]
      lhss.pop
      tuples.pop
    end
  end

  case lhss.size
  when 0
    # nop
  when 1
    splat_assignment = [lhss.first || raise, AST::Types::Tuple.new(types: tuples)]
  else
    raise
  end

  Assignments.new(
    rhs_type: rhs_type,
    optional: optional,
    leading_assignments: leading_assignments,
    trailing_assignments: trailing_assignments,
    splat_assignment: splat_assignment
  )
end

#hint_for_mlhs(mlhs, env) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/steep/type_inference/multiple_assignment.rb', line 171

def hint_for_mlhs(mlhs, env)
  case mlhs.type
  when :mlhs
    types = mlhs.children.map do |node|
      hint_for_mlhs(node, env) or return
    end
    AST::Types::Tuple.new(types: types)
  when :lvasgn, :ivasgn, :gvasgn
    name = mlhs.children[0]
    
    unless TypeConstruction::SPECIAL_LVAR_NAMES.include?(name)
      env[name] || AST::Builtin.any_type
    else
      AST::Builtin.any_type
    end
  when :splat
    return
  else
    return
  end
end