Debian Codegraph @

internal/typeparams/normalize.go

raw symbols SCIP
1 // Copyright 2021 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 typeparamsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/
6
7 import (
8 "errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/"
9 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
10 "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/"
11 "osscip-go gomod github.com/golang/go/src go1.25.0 os/"
12 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
13 )
14
15 //go:generate go run copytermlist.go
16
17 const debugscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/debug. = false
18
19 var ErrEmptyTypeSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/ErrEmptyTypeSet. = errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Newscip-go gomod github.com/golang/go/src go1.25.0 errors/New().("empty type set")
20
21 // StructuralTerms returns a slice of terms representing the normalized
22 // structural type restrictions of a type parameter, if any.
23 //
24 // Structural type restrictions of a type parameter are created via
25 // non-interface types embedded in its constraint interface (directly, or via a
26 // chain of interface embeddings). For example, in the declaration
27 //
28 // type T[P interface{~int; m()}] int
29 //
30 // the structural restriction of the type parameter P is ~int.
31 //
32 // With interface embedding and unions, the specification of structural type
33 // restrictions may be arbitrarily complex. For example, consider the
34 // following:
35 //
36 // type A interface{ ~string|~[]byte }
37 //
38 // type B interface{ int|string }
39 //
40 // type C interface { ~string|~int }
41 //
42 // type T[P interface{ A|B; C }] int
43 //
44 // In this example, the structural type restriction of P is ~string|int: A|B
45 // expands to ~string|~[]byte|int|string, which reduces to ~string|~[]byte|int,
46 // which when intersected with C (~string|~int) yields ~string|int.
47 //
48 // StructuralTerms computes these expansions and reductions, producing a
49 // "normalized" form of the embeddings. A structural restriction is normalized
50 // if it is a single union containing no interface terms, and is minimal in the
51 // sense that removing any term changes the set of types satisfying the
52 // constraint. It is left as a proof for the reader that, modulo sorting, there
53 // is exactly one such normalized form.
54 //
55 // Because the minimal representation always takes this form, StructuralTerms
56 // returns a slice of tilde terms corresponding to the terms of the union in
57 // the normalized structural restriction. An error is returned if the
58 // constraint interface is invalid, exceeds complexity bounds, or has an empty
59 // type set. In the latter case, StructuralTerms returns ErrEmptyTypeSet.
60 //
61 // StructuralTerms makes no guarantees about the order of terms, except that it
62 // is deterministic.
63 func StructuralTermsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/StructuralTerms().(tparam *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.TypeParamscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeParam#) ([]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Termscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Term#, error) {
64 constraint := tparam.Constraintscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeParam#Constraint().()
65 if constraint == nil {
66 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%s has nil constraint", tparam)
67 }
68 iface, _ := constraint.Underlyingscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#Underlying().().(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Interfacescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Interface#)
69 if iface == nil {
70 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("constraint is %T, not *types.Interface", constraint.Underlyingscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#Underlying().())
71 }
72 return InterfaceTermSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/InterfaceTermSet().(iface)
73 }
74
75 // InterfaceTermSet computes the normalized terms for a constraint interface,
76 // returning an error if the term set cannot be computed or is empty. In the
77 // latter case, the error will be ErrEmptyTypeSet.
78 //
79 // See the documentation of StructuralTerms for more information on
80 // normalization.
81 func InterfaceTermSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/InterfaceTermSet().(iface *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Interfacescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Interface#) ([]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Termscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Term#, error) {
82 return computeTermSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/computeTermSet().(iface)
83 }
84
85 // UnionTermSet computes the normalized terms for a union, returning an error
86 // if the term set cannot be computed or is empty. In the latter case, the
87 // error will be ErrEmptyTypeSet.
88 //
89 // See the documentation of StructuralTerms for more information on
90 // normalization.
91 func UnionTermSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/UnionTermSet().(union *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Unionscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Union#) ([]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Termscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Term#, error) {
92 return computeTermSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/computeTermSet().(union)
93 }
94
95 func computeTermSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/computeTermSet().(typ typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#) ([]*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Termscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Term#, error) {
96 tset, err := computeTermSetInternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/computeTermSetInternal().(typ, make(map[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#]*termSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#), 0)
97 if err != nil {
98 return nil, err
99 }
100 if tset.termsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#terms..isEmptyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termlist#isEmpty().() {
101 return nil, ErrEmptyTypeSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/ErrEmptyTypeSet.
102 }
103 if tset.termsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#terms..isAllscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termlist#isAll().() {
104 return nil, nil
105 }
106 var terms []*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Termscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Term#
107 for _, term := range tset.termsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#terms. {
108 terms = append(terms, typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewTermscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewTerm().(term.tildescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/term#tilde., term.typscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/term#typ.))
109 }
110 return terms, nil
111 }
112
113 // A termSet holds the normalized set of terms for a given type.
114 //
115 // The name termSet is intentionally distinct from 'type set': a type set is
116 // all types that implement a type (and includes method restrictions), whereas
117 // a term set just represents the structural restrictions on a type.
118 type termSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet# struct {
119 completescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#complete. bool
120 termsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#terms. termlistscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termlist#
121 }
122
123 func indentfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/indentf().(depth int, format string, args ...any) {
124 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr., stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Repeatscip-go gomod github.com/golang/go/src go1.25.0 strings/Repeat().(".", depth)+format+"\n", args...)
125 }
126
127 func computeTermSetInternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/computeTermSetInternal().(t typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#, seen map[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#]*termSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#, depth int) (res *termSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#, err error) {
128 if t == nil {
129 panic("nil type")
130 }
131
132 if debugscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/debug. {
133 indentfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/indentf().(depth, "%s", t.Stringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#String().())
134 defer func() {
135 if err != nil {
136 indentfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/indentf().(depth, "=> %s", err)
137 } else {
138 indentfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/indentf().(depth, "=> %s", res.termsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#terms..Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termlist#String().())
139 }
140 }()
141 }
142
143 const maxTermCount = 100
144 if tset, ok := seen[t]; ok {
145 if !tset.completescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#complete. {
146 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("cycle detected in the declaration of %s", t)
147 }
148 return tset, nil
149 }
150
151 // Mark the current type as seen to avoid infinite recursion.
152 tset := new(termSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#)
153 defer func() {
154 tset.completescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#complete. = true
155 }()
156 seen[t] = tset
157
158 switch u := t.Underlyingscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#Underlying().().(type) {
159 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Interfacescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Interface#:
160 // The term set of an interface is the intersection of the term sets of its
161 // embedded types.
162 tset.termsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#terms. = allTermlistscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/allTermlist.
163 for embedded := range u.EmbeddedTypesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Interface#EmbeddedTypes().() {
164 if _, ok := embedded.Underlyingscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#Underlying().().(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.TypeParamscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeParam#); ok {
165 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("invalid embedded type %T", embedded)
166 }
167 tset2, err := computeTermSetInternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/computeTermSetInternal().(embedded, seen, depth+1)
168 if err != nil {
169 return nil, err
170 }
171 tset.termsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#terms. = tset.termsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#terms..intersectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termlist#intersect().(tset2.termsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#terms.)
172 }
173 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Unionscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Union#:
174 // The term set of a union is the union of term sets of its terms.
175 tset.termsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#terms. = nil
176 for t := range u.Termsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Union#Terms().() {
177 var terms termlistscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termlist#
178 switch t.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Term#Type().().Underlyingscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#Underlying().().(type) {
179 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Interfacescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Interface#:
180 tset2, err := computeTermSetInternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/computeTermSetInternal().(t.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Term#Type().(), seen, depth+1)
181 if err != nil {
182 return nil, err
183 }
184 terms = tset2.termsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#terms.
185 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.TypeParamscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeParam#, *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Unionscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Union#:
186 // A stand-alone type parameter or union is not permitted as union
187 // term.
188 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("invalid union term %T", t)
189 default:
190 if t.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Term#Type().() == typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Invalidscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Invalid.] {
191 continue
192 }
193 terms = termlistscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termlist#{{t.Tildescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Term#Tilde().(), t.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Term#Type().()}}
194 }
195 tset.termsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#terms. = tset.termsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#terms..unionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termlist#union().(terms)
196 if len(tset.termsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#terms.) > maxTermCount {
197 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("exceeded max term count %d", maxTermCount)
198 }
199 }
200 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.TypeParamscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeParam#:
201 panic("unreachable")
202 default:
203 // For all other types, the term set is just a single non-tilde term
204 // holding the type itself.
205 if u != typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Invalidscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Invalid.] {
206 tset.termsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termSet#terms. = termlistscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/termlist#{{false, t}}
207 }
208 }
209 return tset, nil
210 }
211
212 // under is a facade for the go/types internal function of the same name. It is
213 // used by typeterm.go.
214 func underscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/under().(t typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#) typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type# {
215 return t.Underlyingscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#Underlying().()
216 }