Debian Codegraph @

internal/typeparams/free.go

raw symbols SCIP
1 // Copyright 2024 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package typeparams
6
7 import (
8 "go/types"
9 )
10
11 // Free is a memoization of the set of free type parameters within a
12 // type. It makes a sequence of calls to [Free.Has] for overlapping
13 // types more efficient. The zero value is ready for use.
14 //
15 // NOTE: Adapted from go/types/infer.go. If it is later exported, factor.
16 type Free struct {
17 seen map[types.Type]bool
18 }
19
20 // Has reports whether the specified type has a free type parameter.
21 func (w *Free) Has(typ types.Type) (res bool) {
22 // detect cycles
23 if x, ok := w.seen[typ]; ok {
24 return x
25 }
26 if w.seen == nil {
27 w.seen = make(map[types.Type]bool)
28 }
29 w.seen[typ] = false
30 defer func() {
31 w.seen[typ] = res
32 }()
33
34 switch t := typ.(type) {
35 case nil, *types.Basic: // TODO(gri) should nil be handled here?
36 break
37
38 case *types.Alias:
39 if t.TypeParams().Len() > t.TypeArgs().Len() {
40 return true // This is an uninstantiated Alias.
41 }
42 // The expansion of an alias can have free type parameters,
43 // whether or not the alias itself has type parameters:
44 //
45 // func _[K comparable]() {
46 // type Set = map[K]bool // free(Set) = {K}
47 // type MapTo[V] = map[K]V // free(Map[foo]) = {V}
48 // }
49 //
50 // So, we must Unalias.
51 return w.Has(types.Unalias(t))
52
53 case *types.Array:
54 return w.Has(t.Elem())
55
56 case *types.Slice:
57 return w.Has(t.Elem())
58
59 case *types.Struct:
60 for i, n := 0, t.NumFields(); i < n; i++ {
61 if w.Has(t.Field(i).Type()) {
62 return true
63 }
64 }
65
66 case *types.Pointer:
67 return w.Has(t.Elem())
68
69 case *types.Tuple:
70 n := t.Len()
71 for i := range n {
72 if w.Has(t.At(i).Type()) {
73 return true
74 }
75 }
76
77 case *types.Signature:
78 // t.tparams may not be nil if we are looking at a signature
79 // of a generic function type (or an interface method) that is
80 // part of the type we're testing. We don't care about these type
81 // parameters.
82 // Similarly, the receiver of a method may declare (rather than
83 // use) type parameters, we don't care about those either.
84 // Thus, we only need to look at the input and result parameters.
85 return w.Has(t.Params()) || w.Has(t.Results())
86
87 case *types.Interface:
88 for i, n := 0, t.NumMethods(); i < n; i++ {
89 if w.Has(t.Method(i).Type()) {
90 return true
91 }
92 }
93 terms, err := InterfaceTermSet(t)
94 if err != nil {
95 return false // ill typed
96 }
97 for _, term := range terms {
98 if w.Has(term.Type()) {
99 return true
100 }
101 }
102
103 case *types.Map:
104 return w.Has(t.Key()) || w.Has(t.Elem())
105
106 case *types.Chan:
107 return w.Has(t.Elem())
108
109 case *types.Named:
110 args := t.TypeArgs()
111 if params := t.TypeParams(); params.Len() > args.Len() {
112 return true // this is an uninstantiated named type.
113 }
114 for i, n := 0, args.Len(); i < n; i++ {
115 if w.Has(args.At(i)) {
116 return true
117 }
118 }
119 return w.Has(t.Underlying()) // recurse for types local to parameterized functions
120
121 case *types.TypeParam:
122 return true
123
124 default:
125 panic(t) // unreachable
126 }
127
128 return false
129 }