-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwidening.go
More file actions
387 lines (354 loc) · 10.7 KB
/
widening.go
File metadata and controls
387 lines (354 loc) · 10.7 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
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
package parser
import (
"github.com/redneckbeard/thanos/types"
)
// typeWidening records a case where consumer usage implies a wider type
// than what the producer provides.
type typeWidening struct {
varName string // the variable whose type was widened
widerType types.Type // the wider type (after promotion)
sourceCall *MethodCall // the method call that produced the value
}
// propagateTypeWidenings runs after all analysis passes. It detects cases
// where consumer code (e.g., .nil? checks on array elements) has widened a
// variable's type beyond what the producing function returns, and propagates
// that wider type back to the producer.
//
// This implements backwards type propagation: thanos assumes the Ruby code
// is correct, so if consumer code checks .nil? on array elements, the
// producing function must be able to return nil elements.
func (r *Root) propagateTypeWidenings() {
var widenings []typeWidening
// Walk all module class methods
for _, mod := range r.TopLevelModules {
r.collectModuleWidenings(mod, &widenings)
}
// Walk all class methods
for _, cls := range r.Classes {
for _, m := range cls.MethodSet.Methods {
r.collectMethodWidenings(m, &widenings)
}
for _, m := range cls.ClassMethods {
r.collectMethodWidenings(m, &widenings)
}
}
// Walk global methods
for _, m := range r.MethodSetStack.Peek().Methods {
r.collectMethodWidenings(m, &widenings)
}
// Apply widenings
for _, w := range widenings {
r.applyWidening(w)
}
}
func (r *Root) collectModuleWidenings(mod *Module, widenings *[]typeWidening) {
for _, m := range mod.ClassMethods {
r.collectMethodWidenings(m, widenings)
}
for _, m := range mod.MethodSet.Methods {
r.collectMethodWidenings(m, widenings)
}
for _, sub := range mod.Modules {
r.collectModuleWidenings(sub, widenings)
}
for _, cls := range mod.Classes {
for _, m := range cls.MethodSet.Methods {
r.collectMethodWidenings(m, widenings)
}
for _, m := range cls.ClassMethods {
r.collectMethodWidenings(m, widenings)
}
}
}
// collectMethodWidenings walks a method body doing AST-level pattern matching
// to find cases where a variable's elements are nil-checked but the variable
// was assigned from a function returning non-Optional elements.
//
// This works even when gem method body analysis fails partway through,
// because it examines the AST directly rather than relying on scope types.
func (r *Root) collectMethodWidenings(m *Method, widenings *[]typeWidening) {
if m.Body == nil {
return
}
// Phase 1: Map variable names to the MethodCall that produced them
assignments := map[string]*MethodCall{}
for _, stmt := range m.Body.Statements {
collectAssignments(stmt, assignments)
}
// Phase 2: Find .nil? calls on bracket access of those variables
nilCheckedVars := map[string]bool{}
for _, stmt := range m.Body.Statements {
findNilCheckedArrayVars(stmt, nilCheckedVars)
}
// Phase 3: For each nil-checked variable that was assigned from a method call
// returning a non-Optional array, record the widening
for varName := range nilCheckedVars {
call, ok := assignments[varName]
if !ok {
continue
}
callRetType := call.Type()
if callRetType == nil {
continue
}
arr, isArray := callRetType.(types.Array)
if !isArray {
continue
}
if _, alreadyOpt := arr.Element.(types.Optional); alreadyOpt {
continue
}
widerType := types.NewArray(types.NewOptional(arr.Element))
*widenings = append(*widenings, typeWidening{
varName: varName,
widerType: widerType,
sourceCall: call,
})
}
}
// collectAssignments walks an AST node and records variable-to-MethodCall
// assignments (var = method_call(...)).
func collectAssignments(node Node, assignments map[string]*MethodCall) {
switch n := node.(type) {
case *AssignmentNode:
if len(n.Left) == 1 && len(n.Right) == 1 {
if ident, ok := n.Left[0].(*IdentNode); ok {
if call, ok := n.Right[0].(*MethodCall); ok {
assignments[ident.Val] = call
}
}
}
case *MethodCall:
if n.Block != nil && n.Block.Body != nil {
for _, stmt := range n.Block.Body.Statements {
collectAssignments(stmt, assignments)
}
}
case *WhileNode:
for _, stmt := range n.Body {
collectAssignments(stmt, assignments)
}
case *Condition:
for _, stmt := range n.True {
collectAssignments(stmt, assignments)
}
if n.False != nil {
collectAssignments(n.False, assignments)
}
}
}
// findNilCheckedArrayVars walks an AST node looking for the pattern
// var[idx].nil?() and records the variable names.
func findNilCheckedArrayVars(node Node, result map[string]bool) {
switch n := node.(type) {
case *MethodCall:
// Check for var[idx].nil?
if n.MethodName == "nil?" && n.Receiver != nil {
if ba, ok := n.Receiver.(*BracketAccessNode); ok {
if ident, ok := ba.Composite.(*IdentNode); ok {
result[ident.Val] = true
}
}
}
// Recurse into args
for _, arg := range n.Args {
findNilCheckedArrayVars(arg, result)
}
// Recurse into block
if n.Block != nil && n.Block.Body != nil {
for _, stmt := range n.Block.Body.Statements {
findNilCheckedArrayVars(stmt, result)
}
}
case *Condition:
findNilCheckedArrayVars(n.Condition, result)
for _, stmt := range n.True {
findNilCheckedArrayVars(stmt, result)
}
if n.False != nil {
findNilCheckedArrayVars(n.False, result)
}
case *WhileNode:
for _, stmt := range n.Body {
findNilCheckedArrayVars(stmt, result)
}
case *AssignmentNode:
for _, r := range n.Right {
findNilCheckedArrayVars(r, result)
}
case *ReturnNode:
for _, v := range n.Val {
findNilCheckedArrayVars(v, result)
}
case *NextNode:
if n.Val != nil {
findNilCheckedArrayVars(n.Val, result)
}
case *InfixExpressionNode:
findNilCheckedArrayVars(n.Left, result)
findNilCheckedArrayVars(n.Right, result)
}
}
// isWiderArrayType returns true if wider is an array with Optional elements
// where narrower has non-Optional elements of the same base type.
func isWiderArrayType(wider, narrower types.Type) bool {
wArr, wOk := wider.(types.Array)
nArr, nOk := narrower.(types.Array)
if !wOk || !nOk {
return false
}
wOpt, wIsOpt := wArr.Element.(types.Optional)
_, nIsOpt := nArr.Element.(types.Optional)
if !wIsOpt || nIsOpt {
return false
}
// The base types must match
return wOpt.Element == nArr.Element
}
// applyWidening propagates a type widening back to the producing function.
func (r *Root) applyWidening(w typeWidening) {
// Resolve the producing method from the call
method := r.resolveMethodFromCall(w.sourceCall)
if method == nil {
return
}
// Emit warning when user code widens gem/library code
if method.FromGem {
Note("note: widening return type of %s from %s to %s (consumer checks .nil? on elements)\n",
method.Name, method.Body.ReturnType, w.widerType)
}
// Update the method's return type
method.Body.ReturnType = w.widerType
// Find the variable in the method body that is returned and promote it.
r.promoteReturnedVariable(method, w.widerType)
}
// resolveMethodFromCall finds the Method node that a MethodCall invokes.
func (r *Root) resolveMethodFromCall(call *MethodCall) *Method {
if call.Receiver == nil {
// Bare function call — check global method set
if m, ok := r.MethodSetStack.Peek().Methods[call.MethodName]; ok {
return m
}
return nil
}
receiverType := call.Receiver.Type()
if receiverType == nil {
return nil
}
// Check classMethodSets for the receiver type (instance methods)
if ms, ok := classMethodSets[receiverType]; ok {
if m, ok := ms.Methods[call.MethodName]; ok {
return m
}
}
// For module/class method calls, the receiver is the class itself.
// Check ClassMethods on modules.
for _, mod := range r.TopLevelModules {
if m := r.findModuleClassMethod(mod, receiverType, call.MethodName); m != nil {
return m
}
}
for _, cls := range r.Classes {
for _, m := range cls.ClassMethods {
if m.Name == call.MethodName {
return m
}
}
}
return nil
}
// findModuleClassMethod recursively searches modules for a class method
// matching the given receiver type and method name.
func (r *Root) findModuleClassMethod(mod *Module, receiverType types.Type, methodName string) *Method {
if modType := mod.Type(); modType != nil {
if modType == receiverType {
for _, m := range mod.ClassMethods {
if m.Name == methodName {
return m
}
}
}
}
for _, sub := range mod.Modules {
if m := r.findModuleClassMethod(sub, receiverType, methodName); m != nil {
return m
}
}
for _, cls := range mod.Classes {
for _, m := range cls.ClassMethods {
if m.Name == methodName {
clsType := cls.Type()
if clsType == receiverType {
return m
}
}
}
}
return nil
}
// promoteReturnedVariable finds the variable that a method returns and
// promotes its type in the method's scope to match the widened return type.
func (r *Root) promoteReturnedVariable(method *Method, widerType types.Type) {
if method.Body == nil || len(method.Body.Statements) == 0 {
return
}
// The return value is either the last expression (implicit return)
// or an explicit ReturnNode. Extract the returned ident.
last := method.Body.Statements[len(method.Body.Statements)-1]
var ident *IdentNode
switch n := last.(type) {
case *IdentNode:
ident = n
case *ReturnNode:
if len(n.Val) == 1 {
if id, ok := n.Val[0].(*IdentNode); ok {
ident = id
}
}
}
if ident == nil {
return
}
// Promote the variable in the method's scope
if method.Scope != nil {
method.Scope.RefineVariableType(ident.Val, widerType)
}
// Also update the ident node's cached type
ident.SetType(widerType)
// Walk all bracket assignment statements to this variable and update
// the composite node's type so the compiler emits correct code.
for _, stmt := range method.Body.Statements {
r.promoteBracketAssignments(stmt, ident.Val, widerType)
}
}
// promoteBracketAssignments walks statements and updates the composite type
// on bracket assignments targeting the named variable.
func (r *Root) promoteBracketAssignments(node Node, varName string, widerType types.Type) {
switch n := node.(type) {
case *AssignmentNode:
if len(n.Left) == 1 {
if ba, ok := n.Left[0].(*BracketAssignmentNode); ok {
if ident, ok := ba.Composite.(*IdentNode); ok && ident.Val == varName {
ident.SetType(widerType)
}
}
}
case *WhileNode:
for _, stmt := range n.Body {
r.promoteBracketAssignments(stmt, varName, widerType)
}
case *Condition:
for _, stmt := range n.True {
r.promoteBracketAssignments(stmt, varName, widerType)
}
if n.False != nil {
r.promoteBracketAssignments(n.False, varName, widerType)
}
case *MethodCall:
if n.Block != nil && n.Block.Body != nil {
for _, stmt := range n.Block.Body.Statements {
r.promoteBracketAssignments(stmt, varName, widerType)
}
}
}
}