-
Notifications
You must be signed in to change notification settings - Fork 11
/
common.go
474 lines (420 loc) · 11.3 KB
/
common.go
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
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
446
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
package exhaustive
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"regexp"
"sort"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/ast/astutil"
)
// enumTypeAndMembers combines an enumType and its members set.
type enumTypeAndMembers struct {
typ enumType
members enumMembers
}
func fromNamed(pass *analysis.Pass, t *types.Named, typeparam bool) (result []enumTypeAndMembers, ok bool) {
if tpkg := t.Obj().Pkg(); tpkg == nil {
// go/types documentation says: nil for labels and
// objects in the Universe scope. This happens for the built-in
// error type for example.
return nil, false // not a valid enum type, so ok == false
}
et := enumType{t.Obj()}
if em, ok := importFact(pass, et); ok {
return []enumTypeAndMembers{{et, em}}, true
}
if typeparam {
// is it a named interface?
if intf, ok := t.Underlying().(*types.Interface); ok {
return fromInterface(pass, intf, typeparam)
}
}
return nil, false // not a valid enum type, so ok == false
}
func fromInterface(pass *analysis.Pass, intf *types.Interface, typeparam bool) (result []enumTypeAndMembers, ok bool) {
allOk := true
for i := 0; i < intf.NumEmbeddeds(); i++ {
r, ok := fromType(pass, intf.EmbeddedType(i), typeparam)
result = append(result, r...)
allOk = allOk && ok
}
return result, allOk
}
func fromUnion(pass *analysis.Pass, union *types.Union, typeparam bool) (result []enumTypeAndMembers, ok bool) {
allOk := true
// gather from each term in the union.
for i := 0; i < union.Len(); i++ {
r, ok := fromType(pass, union.Term(i).Type(), typeparam)
result = append(result, r...)
allOk = allOk && ok
}
return result, allOk
}
func fromTypeParam(pass *analysis.Pass, tp *types.TypeParam, typeparam bool) (result []enumTypeAndMembers, ok bool) {
// Does not appear to be explicitly documented, but based on Go language
// spec (see section Type constraints) and Go standard library source code,
// we can expect constraints to have underlying type *types.Interface
// Regardless it will be handled in fromType.
return fromType(pass, tp.Constraint().Underlying(), typeparam)
}
func fromType(pass *analysis.Pass, t types.Type, typeparam bool) (result []enumTypeAndMembers, ok bool) {
switch t := t.(type) {
case *types.Named:
return fromNamed(pass, t, typeparam)
case *types.Union:
return fromUnion(pass, t, typeparam)
case *types.TypeParam:
return fromTypeParam(pass, t, typeparam)
case *types.Interface:
if !typeparam {
return nil, true
}
// anonymous interface.
// e.g. func foo[T interface { M } | interface { N }](v T) {}
return fromInterface(pass, t, typeparam)
default:
// ignore these.
return nil, true
}
}
func composingEnumTypes(pass *analysis.Pass, t types.Type) (result []enumTypeAndMembers, ok bool) {
_, typeparam := t.(*types.TypeParam)
result, ok = fromType(pass, t, typeparam)
if typeparam {
var kind types.BasicKind
var kindSet bool
// sameBasicKind reports whether each type t that the function is called
// with has the same underlying basic kind.
sameBasicKind := func(t types.Type) (ok bool) {
basic, ok := t.Underlying().(*types.Basic)
if !ok {
return false
}
if kindSet && kind != basic.Kind() {
return false
}
kind = basic.Kind()
kindSet = true
return true
}
for _, rr := range result {
if !sameBasicKind(rr.typ.TypeName.Type()) {
ok = false
break
}
}
}
return result, ok
}
func denotesPackage(ident *ast.Ident, info *types.Info) bool {
obj := info.ObjectOf(ident)
if obj == nil {
return false
}
_, ok := obj.(*types.PkgName)
return ok
}
// exprConstVal returns the constantValue for an expression if the
// expression is a constant value and if the expression is considered
// valid to satisfy exhaustiveness as defined by this program.
// Otherwise it returns (_, false).
func exprConstVal(e ast.Expr, info *types.Info) (constantValue, bool) {
handleIdent := func(ident *ast.Ident) (constantValue, bool) {
obj := info.Uses[ident]
if obj == nil {
return "", false
}
if _, ok := obj.(*types.Const); !ok {
return "", false
}
// There are two scenarios.
// See related test cases in typealias/quux/quux.go.
//
// # Scenario 1
//
// Tag package and constant package are the same. This is
// simple; we just use fs.ModeDir's value.
// Example:
//
// var mode fs.FileMode
// switch mode {
// case fs.ModeDir:
// }
//
// # Scenario 2
//
// Tag package and constant package are different. In this
// scenario, too, we accept the case clause expr constant value,
// as is. If the Go type checker is okay with the name being
// listed in the case clause, we don't care much further.
//
// Example:
//
// var mode fs.FileMode
// switch mode {
// case os.ModeDir:
// }
//
// Or equivalently:
//
// // The type of mode is effectively fs.FileMode,
// // due to type alias.
// var mode os.FileMode
// switch mode {
// case os.ModeDir:
// }
return determineConstVal(ident, info), true
}
e = stripTypeConversions(astutil.Unparen(e), info)
switch e := e.(type) {
case *ast.Ident:
return handleIdent(e)
case *ast.SelectorExpr:
x := astutil.Unparen(e.X)
// Ensure we only see the form pkg.Const, and not e.g.
// structVal.f or structVal.inner.f.
//
// For this purpose, first we check that X, which is everything
// except the rightmost field selector *ast.Ident (the Sel
// field), is also an *ast.Ident.
xIdent, ok := x.(*ast.Ident)
if !ok {
return "", false
}
// Second, check that it's a package. It doesn't matter which
// package, just that it denotes some package.
if !denotesPackage(xIdent, info) {
return "", false
}
return handleIdent(e.Sel)
default:
// e.g. literal
// these aren't considered towards satisfying exhaustiveness.
return "", false
}
}
// stripTypeConversions removing type conversions from the expression.
func stripTypeConversions(e ast.Expr, info *types.Info) ast.Expr {
c, ok := e.(*ast.CallExpr)
if !ok {
return e
}
typ := info.TypeOf(c.Fun)
if typ == nil {
// can happen for built-ins.
return e
}
// do not allow function calls.
if _, ok := typ.Underlying().(*types.Signature); ok {
return e
}
// type conversions have exactly one arg.
if len(c.Args) != 1 {
return e
}
return stripTypeConversions(astutil.Unparen(c.Args[0]), info)
}
// member is a single member of an enum type.
type member struct {
pos token.Pos
typ enumType
name string
val constantValue
}
type checklist struct {
info map[enumType]enumMembers
checkl map[member]struct{}
ignoreConstantRe *regexp.Regexp
ignoreTypeRe *regexp.Regexp
}
func (c *checklist) ignoreConstant(pattern *regexp.Regexp) {
c.ignoreConstantRe = pattern
}
func (c *checklist) ignoreType(pattern *regexp.Regexp) {
c.ignoreTypeRe = pattern
}
func (*checklist) reMatch(re *regexp.Regexp, s string) bool {
if re == nil {
return false
}
return re.MatchString(s)
}
func (c *checklist) add(et enumType, em enumMembers, includeUnexported bool) {
addOne := func(name string) {
if isBlankIdentifier(name) {
// Blank identifier is often used to skip entries in iota
// lists. Also, it can't be referenced anywhere (e.g. can't
// be referenced in switch statement cases) It doesn't make
// sense to include it as required member to satisfy
// exhaustiveness.
return
}
if !ast.IsExported(name) && !includeUnexported {
return
}
if c.reMatch(c.ignoreConstantRe, fmt.Sprintf("%s.%s", et.Pkg().Path(), name)) {
return
}
if c.reMatch(c.ignoreTypeRe, fmt.Sprintf("%s.%s", et.Pkg().Path(), et.TypeName.Name())) {
return
}
mem := member{
em.NameToPos[name],
et,
name,
em.NameToValue[name],
}
if c.checkl == nil {
c.checkl = make(map[member]struct{})
}
c.checkl[mem] = struct{}{}
}
if c.info == nil {
c.info = make(map[enumType]enumMembers)
}
c.info[et] = em
for _, name := range em.Names {
addOne(name)
}
}
func (c *checklist) found(val constantValue) {
// delete all same-valued items.
for et, em := range c.info {
for _, name := range em.ValueToNames[val] {
delete(c.checkl, member{
em.NameToPos[name],
et,
name,
em.NameToValue[name],
})
}
}
}
func (c *checklist) remaining() map[member]struct{} {
return c.checkl
}
// group is a collection of same-valued members, possibly from
// different enum types.
type group []member
func groupify(items map[member]struct{}, types []enumType) []group {
// indices maps each element in the input slice to its index.
indices := func(vs []enumType) map[enumType]int {
ret := make(map[enumType]int, len(vs))
for i, v := range vs {
ret[v] = i
}
return ret
}
typesOrder := indices(types) // for quick lookup
astBefore := func(x, y member) bool {
if typesOrder[x.typ] < typesOrder[y.typ] {
return true
}
if typesOrder[x.typ] > typesOrder[y.typ] {
return false
}
return x.pos < y.pos
}
// byConstVal groups member names by constant value.
byConstVal := func(items map[member]struct{}) map[constantValue][]member {
ret := make(map[constantValue][]member)
for m := range items {
ret[m.val] = append(ret[m.val], m)
}
return ret
}
var groups []group
for _, ms := range byConstVal(items) {
groups = append(groups, group(ms))
}
// sort members within each group in AST order.
for i := range groups {
g := groups[i]
sort.Slice(g, func(i, j int) bool { return astBefore(g[i], g[j]) })
groups[i] = g
}
// sort groups themselves in AST order.
// the index [0] access is safe, because there will be at least one
// element per group.
sort.Slice(groups, func(i, j int) bool { return astBefore(groups[i][0], groups[j][0]) })
return groups
}
func diagnosticEnumType(enumType *types.TypeName) string {
return enumType.Pkg().Name() + "." + enumType.Name()
}
func diagnosticEnumTypes(types []enumType) string {
var buf strings.Builder
for i := range types {
buf.WriteString(diagnosticEnumType(types[i].TypeName))
if i != len(types)-1 {
buf.WriteByte('|')
}
}
return buf.String()
}
func diagnosticMember(m member) string {
return m.typ.Pkg().Name() + "." + m.name
}
func diagnosticGroups(gs []group) string {
out := make([]string, len(gs))
for i := range gs {
var buf strings.Builder
for j := range gs[i] {
buf.WriteString(diagnosticMember(gs[i][j]))
if j != len(gs[i])-1 {
buf.WriteByte('|')
}
}
out[i] = buf.String()
}
return strings.Join(out, ", ")
}
func toEnumTypes(es []enumTypeAndMembers) []enumType {
out := make([]enumType, len(es))
for i := range es {
out[i] = es[i].typ
}
return out
}
func dedupEnumTypes(types []enumType) []enumType {
m := make(map[enumType]struct{})
var ret []enumType
for _, t := range types {
_, ok := m[t]
if ok {
continue
}
m[t] = struct{}{}
ret = append(ret, t)
}
return ret
}
type boolCache struct {
m map[*ast.File]bool
compute func(*ast.File) bool
}
func (c *boolCache) get(file *ast.File) bool {
if _, ok := c.m[file]; !ok {
if c.m == nil {
c.m = make(map[*ast.File]bool)
}
c.m[file] = c.compute(file)
}
return c.m[file]
}
type commentCache struct {
m map[*ast.File]ast.CommentMap
compute func(*token.FileSet, *ast.File) ast.CommentMap
}
func (c *commentCache) get(fset *token.FileSet, file *ast.File) ast.CommentMap {
if _, ok := c.m[file]; !ok {
if c.m == nil {
c.m = make(map[*ast.File]ast.CommentMap)
}
c.m[file] = c.compute(fset, file)
}
return c.m[file]
}