/
laser_method.rb
281 lines (255 loc) · 9.78 KB
/
laser_method.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
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
220
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
256
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
module Laser
module Analysis
# Laser representation of a method. This name is tweaked so it doesn't
# collide with ::Method.
class LaserMethod
extend ModuleExtensions
cattr_accessor_with_default :default_dispatched, true
attr_reader :name, :proc, :arglist, :owner
alias arguments arglist
attr_accessor :arity
def initialize(name, base_proc)
@name = name
@type_instantiations = {}
@proc = base_proc
@argument_annotations = nil
@dispatched = LaserMethod.default_dispatched
if base_proc # always true except some ugly test cases
@arglist = base_proc.arguments
@arity = Arity.for_arglist(@arglist)
else
@arglist = []
@arity = nil
end
yield self if block_given?
end
def owner=(mod)
@owner = mod
verify_override_safety
end
# Returns the method this method overrides: what gets called by `super`.
def super_method
owner.parent && owner.parent.instance_method(name)
end
################## Potentially Annotated Properties ######################
%w(special pure builtin predictable mutation annotated_return annotated_yield_usage
overloads annotated_raise_frequency raises).each do |attr|
define_method(attr) do
instance_variable_get("@#{attr}") || (self.proc ? self.proc.send(attr) : nil)
end
define_method("#{attr}=") do |val|
instance_variable_set("@#{attr}", val)
end
end
def overloads
@overloads || (self.proc ? self.proc.overloads : {})
end
def predictable
@predictable || (self.proc ? self.proc.predictable : true)
end
# Gets all annotations with the same name as an argument.
def argument_annotations
@argument_annotations ||=
if @proc
arguments.map do |arg|
@proc.annotations[arg.name] if @proc.annotations.has_key?(arg.name)
end.compact
else
[]
end
end
def yield_type
return annotated_yield_usage if annotated_yield_usage
return @yield_type if @yield_type
if builtin
:ignored
else
master_cfg.analyze(method: self)
@yield_type = master_cfg.yield_type
end
end
def yield_arity
return @yield_arity if @yield_arity
master_cfg.analyze(method: self)
@yield_arity = master_cfg.yield_arity
end
def raise_frequency_for_types(self_type, arg_types = [], block_type = nil)
block_type ||= Types::NILCLASS
unless arg_types_unify_with_annotations?(arg_types)
return Frequency::ALWAYS
end
return annotated_raise_frequency if annotated_raise_frequency
return Frequency::MAYBE if builtin || special
cfg_for_types(self_type, arg_types, block_type).raise_frequency
end
def raise_type_for_types(self_type, arg_types = [], block_type = nil)
block_type ||= Types::NILCLASS
Laser.debug_puts("Calculating raise type for #{owner.name}##{name} with types "+
"#{self_type.inspect} #{arg_types.inspect} #{block_type.inspect}")
unless arg_types_unify_with_annotations?(arg_types)
return ClassRegistry['TypeError'].as_type # needs parameterization later
end
if raises
Laser.debug_puts("Raise type is annotated: #{self.raises.inspect}")
return self.raises
end
if builtin || special
if annotated_raise_frequency == Frequency::NEVER
self.raises = Types::EMPTY
Laser.debug_puts("Raise type is annotated: #{self.raises.inspect}")
return self.raises
end
Laser.debug_puts("Builtin/special: Types::TOP")
return Types::TOP
end
cfg_for_types(self_type, arg_types, block_type).raise_type
end
def return_type_for_types(self_type, arg_types = [], block_type = nil)
block_type ||= Types::NILCLASS
return annotated_return if annotated_return
unless overloads.empty?
return overload_for_arg_types(arg_types)
end
if builtin || special
return Types::TOP
end
result = cfg_for_types(self_type, arg_types, block_type).return_type
check_return_type_against_expectations(result)
result
end
def valid_arity?(num_args)
@arity ? @arity.include?(num_args) : true
end
def overload_for_arg_types(arg_types)
overloads.each do |overload_args, proc_type|
# compatible arg count
if overload_args.size == arg_types.size
# all types unify?
if overload_args.zip(arg_types).all? { |spec, concrete| Types.subtype?(concrete, spec) }
return proc_type.subtypes[1]
end
end
end
raise TypeError.new("No overload found for #{self.inspect} with arg types #{arg_types.inspect}")
end
# Checks if the given argument types correctly unify with any
# user-requested restrictions.
def arg_types_unify_with_annotations?(arg_types)
if argument_annotations.any?
# Match actual arguments against formal arguments
formal_assignments = assign_formals(arg_types)
# Check annotations for formals against actual types
argument_annotations.each do |note_list|
note_list.each do |type_annotation|
expected = type_annotation.type
given = formal_assignments[type_annotation.name]
# no given -> optional arg that's left out.
return false if given && !Types.subtype?(given, expected)
end
end
end
true
end
def check_return_type_against_expectations(return_type)
if (expectation = Types::EXPECTATIONS[self.name]) &&
!Types.subtype?(return_type, expectation)
@proc.ast_node.add_error(ImproperOverrideTypeError.new(
"All methods named #{self.name} should return a subtype of #{expectation.inspect}",
@proc.ast_node))
elsif self.name.end_with?('?')
if !Types.subtype?(return_type, Types::BOOL_OR_NIL)
@proc.ast_node.add_error(ImproperOverrideTypeError.new(
"All methods whose name ends in ? should return a subtype of TrueClass | FalseClass | NilClass",
@proc.ast_node))
end
end
end
def verify_override_safety
overridden = super_method
return unless overridden
if OverrideSafetyInfo.warn_on_any_override?(overridden)
@proc.ast_node.add_error(DangerousOverrideError.new(
OverrideSafetyInfo.warning_message(overridden),
@proc.ast_node))
end
end
def dispatched?
@dispatched
end
def been_used!
@dispatched = true
end
def master_cfg
@master_cfg ||= @proc.ssa_cfg.tap do |cfg|
cfg.bind_self_type(Types::ClassType.new(owner.path, :covariant))
end
end
def cfg_for_types(self_type, arg_types = [], block_type = nil)
block_type ||= Types::NILCLASS
Laser.debug_puts("Calculating CFG(#{owner.name}##{name}, #{self_type.inspect}, #{arg_types.inspect}, #{block_type.inspect})")
@type_instantiations[[self_type, *arg_types, block_type]] ||= master_cfg.dup.tap do |cfg|
cfg.bind_self_type(self_type)
cfg.bind_formal_types(arg_types)
cfg.bind_block_type(block_type)
cfg.analyze(method: self)
end
end
def simulate_with_args(new_self, args, block, opts)
self_type = Utilities.type_for(new_self)
formal_types = args.map { |arg| Utilities.type_for(arg) }
block_type = Utilities.type_for(block)
cfg_for_types(self_type, formal_types, block_type).dup.simulate(
args, opts.merge(self: new_self,
method: self,
block: block,
start_block: @proc.start_block))
end
# Maps a sequence of objects (one per actual argument) to
# the corresponding formal argument.
def assign_formals(actual_objs)
args = arguments
result_array = []
num_required = args.count { |arg| arg.kind == :positional }
num_optional = actual_objs.size - num_required
current_arg = 0
current_actual = 0
rest = []
while num_required > 0 || num_optional > 0
next_arg = args[current_arg]
if next_arg.kind == :positional
result_array << [next_arg.name, actual_objs[current_actual]]
num_required -= 1
current_actual += 1
current_arg += 1
elsif next_arg.kind == :optional && num_optional > 0
result_array << [next_arg.name, actual_objs[current_actual]]
num_optional -= 1
current_actual += 1
current_arg += 1
elsif next_arg.kind == :optional && num_optional == 0
current_arg += 1
elsif next_arg.kind == :rest && num_optional > 0
rest << actual_objs[current_actual]
current_actual += 1
num_optional -= 1
elsif next_arg.kind == :rest
result_array << [next_arg.name, rest]
end
end
Hash[*result_array.flatten(1)]
end
def dup
result = LaserMethod.new(name, proc)
result.owner = self.owner
result.arity = self.arity
result
end
def refine_arity(new_arity)
return new_arity if @arity.nil?
new_begin = [new_arity.begin, @arity.begin].min
new_end = [new_arity.end, @arity.end].max
new_begin..new_end
end
end
end
end