Source file
src/go/types/typeset.go
1
2
3
4
5
6
7
8 package types
9
10 import (
11 "go/token"
12 . "internal/types/errors"
13 "sort"
14 "strings"
15 )
16
17
18
19
20
21
22
23
24
25
26
27
28
29 type _TypeSet struct {
30 methods []*Func
31 terms termlist
32 comparable bool
33 }
34
35
36 func (s *_TypeSet) IsEmpty() bool { return s.terms.isEmpty() }
37
38
39 func (s *_TypeSet) IsAll() bool { return s.IsMethodSet() && len(s.methods) == 0 }
40
41
42 func (s *_TypeSet) IsMethodSet() bool { return !s.comparable && s.terms.isAll() }
43
44
45 func (s *_TypeSet) IsComparable(seen map[Type]bool) bool {
46 if s.terms.isAll() {
47 return s.comparable
48 }
49 return s.is(func(t *term) bool {
50 return t != nil && comparable(t.typ, false, seen, nil)
51 })
52 }
53
54
55 func (s *_TypeSet) NumMethods() int { return len(s.methods) }
56
57
58
59 func (s *_TypeSet) Method(i int) *Func { return s.methods[i] }
60
61
62 func (s *_TypeSet) LookupMethod(pkg *Package, name string, foldCase bool) (int, *Func) {
63 return methodIndex(s.methods, pkg, name, foldCase)
64 }
65
66 func (s *_TypeSet) String() string {
67 switch {
68 case s.IsEmpty():
69 return "∅"
70 case s.IsAll():
71 return "𝓤"
72 }
73
74 hasMethods := len(s.methods) > 0
75 hasTerms := s.hasTerms()
76
77 var buf strings.Builder
78 buf.WriteByte('{')
79 if s.comparable {
80 buf.WriteString("comparable")
81 if hasMethods || hasTerms {
82 buf.WriteString("; ")
83 }
84 }
85 for i, m := range s.methods {
86 if i > 0 {
87 buf.WriteString("; ")
88 }
89 buf.WriteString(m.String())
90 }
91 if hasMethods && hasTerms {
92 buf.WriteString("; ")
93 }
94 if hasTerms {
95 buf.WriteString(s.terms.String())
96 }
97 buf.WriteString("}")
98 return buf.String()
99 }
100
101
102
103
104
105 func (s *_TypeSet) hasTerms() bool { return !s.terms.isEmpty() && !s.terms.isAll() }
106
107
108 func (s1 *_TypeSet) subsetOf(s2 *_TypeSet) bool { return s1.terms.subsetOf(s2.terms) }
109
110
111
112
113
114
115 func (s *_TypeSet) is(f func(*term) bool) bool {
116 if !s.hasTerms() {
117 return f(nil)
118 }
119 for _, t := range s.terms {
120 assert(t.typ != nil)
121 if !f(t) {
122 return false
123 }
124 }
125 return true
126 }
127
128
129
130
131 func (s *_TypeSet) underIs(f func(Type) bool) bool {
132 if !s.hasTerms() {
133 return f(nil)
134 }
135 for _, t := range s.terms {
136 assert(t.typ != nil)
137
138 u := Unalias(t.typ)
139 if !t.tilde {
140 u = under(u)
141 }
142 if debug {
143 assert(Identical(u, under(u)))
144 }
145 if !f(u) {
146 return false
147 }
148 }
149 return true
150 }
151
152
153 var topTypeSet = _TypeSet{terms: allTermlist}
154
155
156 func computeInterfaceTypeSet(check *Checker, pos token.Pos, ityp *Interface) *_TypeSet {
157 if ityp.tset != nil {
158 return ityp.tset
159 }
160
161
162
163
164
165
166
167
168
169
170
171 if !ityp.complete {
172 return &topTypeSet
173 }
174
175 if check != nil && check.conf._Trace {
176
177
178
179 if !pos.IsValid() && len(ityp.methods) > 0 {
180 pos = ityp.methods[0].pos
181 }
182
183 check.trace(pos, "-- type set for %s", ityp)
184 check.indent++
185 defer func() {
186 check.indent--
187 check.trace(pos, "=> %s ", ityp.typeSet())
188 }()
189 }
190
191
192
193
194
195
196 ityp.tset = &_TypeSet{terms: allTermlist}
197
198 var unionSets map[*Union]*_TypeSet
199 if check != nil {
200 if check.unionTypeSets == nil {
201 check.unionTypeSets = make(map[*Union]*_TypeSet)
202 }
203 unionSets = check.unionTypeSets
204 } else {
205 unionSets = make(map[*Union]*_TypeSet)
206 }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 var seen objset
222 var allMethods []*Func
223 mpos := make(map[*Func]token.Pos)
224 addMethod := func(pos token.Pos, m *Func, explicit bool) {
225 switch other := seen.insert(m); {
226 case other == nil:
227 allMethods = append(allMethods, m)
228 mpos[m] = pos
229 case explicit:
230 if check != nil {
231 err := check.newError(DuplicateDecl)
232 err.addf(atPos(pos), "duplicate method %s", m.name)
233 err.addf(atPos(mpos[other.(*Func)]), "other declaration of method %s", m.name)
234 err.report()
235 }
236 default:
237
238
239
240
241
242 if check != nil {
243 check.later(func() {
244 if pos.IsValid() && !check.allowVersion(atPos(pos), go1_14) || !Identical(m.typ, other.Type()) {
245 err := check.newError(DuplicateDecl)
246 err.addf(atPos(pos), "duplicate method %s", m.name)
247 err.addf(atPos(mpos[other.(*Func)]), "other declaration of method %s", m.name)
248 err.report()
249 }
250 }).describef(atPos(pos), "duplicate method check for %s", m.name)
251 }
252 }
253 }
254
255 for _, m := range ityp.methods {
256 addMethod(m.pos, m, true)
257 }
258
259
260 allTerms := allTermlist
261 allComparable := false
262 for i, typ := range ityp.embeddeds {
263
264
265 var pos token.Pos
266 if ityp.embedPos != nil {
267 pos = (*ityp.embedPos)[i]
268 }
269 var comparable bool
270 var terms termlist
271 switch u := under(typ).(type) {
272 case *Interface:
273
274 assert(!isTypeParam(typ))
275 tset := computeInterfaceTypeSet(check, pos, u)
276
277 if pos.IsValid() && check != nil && check.isImportedConstraint(typ) && !check.verifyVersionf(atPos(pos), go1_18, "embedding constraint interface %s", typ) {
278 continue
279 }
280 comparable = tset.comparable
281 for _, m := range tset.methods {
282 addMethod(pos, m, false)
283 }
284 terms = tset.terms
285 case *Union:
286 if pos.IsValid() && check != nil && !check.verifyVersionf(atPos(pos), go1_18, "embedding interface element %s", u) {
287 continue
288 }
289 tset := computeUnionTypeSet(check, unionSets, pos, u)
290 if tset == &invalidTypeSet {
291 continue
292 }
293 assert(!tset.comparable)
294 assert(len(tset.methods) == 0)
295 terms = tset.terms
296 default:
297 if !isValid(u) {
298 continue
299 }
300 if pos.IsValid() && check != nil && !check.verifyVersionf(atPos(pos), go1_18, "embedding non-interface type %s", typ) {
301 continue
302 }
303 terms = termlist{{false, typ}}
304 }
305
306
307
308
309 allTerms, allComparable = intersectTermLists(allTerms, allComparable, terms, comparable)
310 }
311
312 ityp.tset.comparable = allComparable
313 if len(allMethods) != 0 {
314 sortMethods(allMethods)
315 ityp.tset.methods = allMethods
316 }
317 ityp.tset.terms = allTerms
318
319 return ityp.tset
320 }
321
322
323
324
325
326
327
328 func intersectTermLists(xterms termlist, xcomp bool, yterms termlist, ycomp bool) (termlist, bool) {
329 terms := xterms.intersect(yterms)
330
331
332 comp := xcomp || ycomp
333 if comp && !terms.isAll() {
334
335 i := 0
336 for _, t := range terms {
337 assert(t.typ != nil)
338 if comparable(t.typ, false , nil, nil) {
339 terms[i] = t
340 i++
341 }
342 }
343 terms = terms[:i]
344 if !terms.isAll() {
345 comp = false
346 }
347 }
348 assert(!comp || terms.isAll())
349 return terms, comp
350 }
351
352 func sortMethods(list []*Func) {
353 sort.Sort(byUniqueMethodName(list))
354 }
355
356 func assertSortedMethods(list []*Func) {
357 if !debug {
358 panic("assertSortedMethods called outside debug mode")
359 }
360 if !sort.IsSorted(byUniqueMethodName(list)) {
361 panic("methods not sorted")
362 }
363 }
364
365
366 type byUniqueMethodName []*Func
367
368 func (a byUniqueMethodName) Len() int { return len(a) }
369 func (a byUniqueMethodName) Less(i, j int) bool { return a[i].less(&a[j].object) }
370 func (a byUniqueMethodName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
371
372
373
374
375 var invalidTypeSet _TypeSet
376
377
378
379 func computeUnionTypeSet(check *Checker, unionSets map[*Union]*_TypeSet, pos token.Pos, utyp *Union) *_TypeSet {
380 if tset, _ := unionSets[utyp]; tset != nil {
381 return tset
382 }
383
384
385 unionSets[utyp] = new(_TypeSet)
386
387 var allTerms termlist
388 for _, t := range utyp.terms {
389 var terms termlist
390 u := under(t.typ)
391 if ui, _ := u.(*Interface); ui != nil {
392
393 assert(!isTypeParam(t.typ))
394 terms = computeInterfaceTypeSet(check, pos, ui).terms
395 } else if !isValid(u) {
396 continue
397 } else {
398 if t.tilde && !Identical(t.typ, u) {
399
400
401 t = nil
402 }
403 terms = termlist{(*term)(t)}
404 }
405
406
407 allTerms = allTerms.union(terms)
408 if len(allTerms) > maxTermCount {
409 if check != nil {
410 check.errorf(atPos(pos), InvalidUnion, "cannot handle more than %d union terms (implementation limitation)", maxTermCount)
411 }
412 unionSets[utyp] = &invalidTypeSet
413 return unionSets[utyp]
414 }
415 }
416 unionSets[utyp].terms = allTerms
417
418 return unionSets[utyp]
419 }
420
View as plain text