Debian Codegraph @

internal/typeparams/coretype.go

raw symbols SCIP
1 // Copyright 2022 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 "fmt"
9 "go/types"
10 )
11
12 // CoreType returns the core type of T or nil if T does not have a core type.
13 //
14 // As of Go1.25, the notion of a core type has been removed from the language spec.
15 // See https://go.dev/blog/coretypes for more details.
16 // TODO(mkalil): We should eventually consider removing all uses of CoreType.
17 func CoreType(T types.Type) types.Type {
18 U := T.Underlying()
19 if _, ok := U.(*types.Interface); !ok {
20 return U // for non-interface types,
21 }
22
23 terms, err := NormalTerms(U)
24 if len(terms) == 0 || err != nil {
25 // len(terms) -> empty type set of interface.
26 // err != nil => U is invalid, exceeds complexity bounds, or has an empty type set.
27 return nil // no core type.
28 }
29
30 U = terms[0].Type().Underlying()
31 var identical int // i in [0,identical) => Identical(U, terms[i].Type().Underlying())
32 for identical = 1; identical < len(terms); identical++ {
33 if !types.Identical(U, terms[identical].Type().Underlying()) {
34 break
35 }
36 }
37
38 if identical == len(terms) {
39 // From the deprecated core types spec:
40 // "There is a single type U which is the underlying type of all types in the type set of T"
41 return U
42 }
43 ch, ok := U.(*types.Chan)
44 if !ok {
45 return nil // no core type as identical < len(terms) and U is not a channel.
46 }
47 // From the deprecated core types spec:
48 // "the type chan E if T contains only bidirectional channels, or the type chan<- E or
49 // <-chan E depending on the direction of the directional channels present."
50 for chans := identical; chans < len(terms); chans++ {
51 curr, ok := terms[chans].Type().Underlying().(*types.Chan)
52 if !ok {
53 return nil
54 }
55 if !types.Identical(ch.Elem(), curr.Elem()) {
56 return nil // channel elements are not identical.
57 }
58 if ch.Dir() == types.SendRecv {
59 // ch is bidirectional. We can safely always use curr's direction.
60 ch = curr
61 } else if curr.Dir() != types.SendRecv && ch.Dir() != curr.Dir() {
62 // ch and curr are not bidirectional and not the same direction.
63 return nil
64 }
65 }
66 return ch
67 }
68
69 // NormalTerms returns a slice of terms representing the normalized structural
70 // type restrictions of a type, if any.
71 //
72 // For all types other than *types.TypeParam, *types.Interface, and
73 // *types.Union, this is just a single term with Tilde() == false and
74 // Type() == typ. For *types.TypeParam, *types.Interface, and *types.Union, see
75 // below.
76 //
77 // Structural type restrictions of a type parameter are created via
78 // non-interface types embedded in its constraint interface (directly, or via a
79 // chain of interface embeddings). For example, in the declaration type
80 // T[P interface{~int; m()}] int the structural restriction of the type
81 // parameter P is ~int.
82 //
83 // With interface embedding and unions, the specification of structural type
84 // restrictions may be arbitrarily complex. For example, consider the
85 // following:
86 //
87 // type A interface{ ~string|~[]byte }
88 //
89 // type B interface{ int|string }
90 //
91 // type C interface { ~string|~int }
92 //
93 // type T[P interface{ A|B; C }] int
94 //
95 // In this example, the structural type restriction of P is ~string|int: A|B
96 // expands to ~string|~[]byte|int|string, which reduces to ~string|~[]byte|int,
97 // which when intersected with C (~string|~int) yields ~string|int.
98 //
99 // NormalTerms computes these expansions and reductions, producing a
100 // "normalized" form of the embeddings. A structural restriction is normalized
101 // if it is a single union containing no interface terms, and is minimal in the
102 // sense that removing any term changes the set of types satisfying the
103 // constraint. It is left as a proof for the reader that, modulo sorting, there
104 // is exactly one such normalized form.
105 //
106 // Because the minimal representation always takes this form, NormalTerms
107 // returns a slice of tilde terms corresponding to the terms of the union in
108 // the normalized structural restriction. An error is returned if the type is
109 // invalid, exceeds complexity bounds, or has an empty type set. In the latter
110 // case, NormalTerms returns ErrEmptyTypeSet.
111 //
112 // NormalTerms makes no guarantees about the order of terms, except that it
113 // is deterministic.
114 func NormalTerms(T types.Type) ([]*types.Term, error) {
115 // typeSetOf(T) == typeSetOf(Unalias(T))
116 typ := types.Unalias(T)
117 if named, ok := typ.(*types.Named); ok {
118 typ = named.Underlying()
119 }
120 switch typ := typ.(type) {
121 case *types.TypeParam:
122 return StructuralTerms(typ)
123 case *types.Union:
124 return UnionTermSet(typ)
125 case *types.Interface:
126 return InterfaceTermSet(typ)
127 default:
128 return []*types.Term{types.NewTerm(false, T)}, nil
129 }
130 }
131
132 // Deref returns the type of the variable pointed to by t,
133 // if t's core type is a pointer; otherwise it returns t.
134 //
135 // Do not assume that Deref(T)==T implies T is not a pointer:
136 // consider "type T *T", for example.
137 //
138 // TODO(adonovan): ideally this would live in typesinternal, but that
139 // creates an import cycle. Move there when we melt this package down.
140 func Deref(t types.Type) types.Type {
141 if ptr, ok := CoreType(t).(*types.Pointer); ok {
142 return ptr.Elem()
143 }
144 return t
145 }
146
147 // MustDeref returns the type of the variable pointed to by t.
148 // It panics if t's core type is not a pointer.
149 //
150 // TODO(adonovan): ideally this would live in typesinternal, but that
151 // creates an import cycle. Move there when we melt this package down.
152 func MustDeref(t types.Type) types.Type {
153 if ptr, ok := CoreType(t).(*types.Pointer); ok {
154 return ptr.Elem()
155 }
156 panic(fmt.Sprintf("%v is not a pointer", t))
157 }