Source file src/go/types/typeparam.go
1 // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. 2 // Source: ../../cmd/compile/internal/types2/typeparam.go 3 4 // Copyright 2011 The Go Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file. 7 8 package types 9 10 import "sync/atomic" 11 12 // Note: This is a uint32 rather than a uint64 because the 13 // respective 64 bit atomic instructions are not available 14 // on all platforms. 15 var lastID atomic.Uint32 16 17 // nextID returns a value increasing monotonically by 1 with 18 // each call, starting with 1. It may be called concurrently. 19 func nextID() uint64 { return uint64(lastID.Add(1)) } 20 21 // A TypeParam represents the type of a type parameter in a generic declaration. 22 // 23 // A TypeParam has a name; use the [TypeParam.Obj] method to access 24 // its [TypeName] object. 25 type TypeParam struct { 26 check *Checker // for lazy type bound completion 27 id uint64 // unique id, for debugging only 28 obj *TypeName // corresponding type name 29 index int // type parameter index in source order, starting at 0 30 bound Type // any type, but underlying is eventually *Interface for correct programs (see TypeParam.iface) 31 } 32 33 // NewTypeParam returns a new TypeParam. Type parameters may be set on a Named 34 // type by calling SetTypeParams. Setting a type parameter on more than one type 35 // will result in a panic. 36 // 37 // The constraint argument can be nil, and set later via SetConstraint. If the 38 // constraint is non-nil, it must be fully defined. 39 func NewTypeParam(obj *TypeName, constraint Type) *TypeParam { 40 return (*Checker)(nil).newTypeParam(obj, constraint) 41 } 42 43 // check may be nil 44 func (check *Checker) newTypeParam(obj *TypeName, constraint Type) *TypeParam { 45 // Always increment lastID, even if it is not used. 46 id := nextID() 47 if check != nil { 48 check.nextID++ 49 id = check.nextID 50 } 51 typ := &TypeParam{check: check, id: id, obj: obj, index: -1, bound: constraint} 52 if obj.typ == nil { 53 obj.typ = typ 54 } 55 // iface may mutate typ.bound, so we must ensure that iface() is called 56 // at least once before the resulting TypeParam escapes. 57 if check != nil { 58 check.needsCleanup(typ) 59 } else if constraint != nil { 60 typ.iface() 61 } 62 return typ 63 } 64 65 // Obj returns the type name for the type parameter t. 66 func (t *TypeParam) Obj() *TypeName { return t.obj } 67 68 // Index returns the index of the type param within its param list, or -1 if 69 // the type parameter has not yet been bound to a type. 70 func (t *TypeParam) Index() int { 71 return t.index 72 } 73 74 // Constraint returns the type constraint specified for t. 75 func (t *TypeParam) Constraint() Type { 76 if t.bound == nil { 77 // Before CL 825744, Constraint() returned t.bound directly, which could be nil 78 // for incomplete or uninitialized TypeParams (e.g. created via NewTypeParam 79 // without a subsequent SetConstraint). Callers (such as typeWriter, 80 // objectpath, and typeutil.Map) calling methods on the returned constraint would 81 // panic with unhandled nil-pointer dereferences. 82 // 83 // An incomplete TypeParam has no defined constraint. Constraint panics if 84 // called on an incomplete TypeParam whose constraint has not yet been set. 85 // Callers must ensure SetConstraint has been called before querying Constraint. 86 panic("incomplete TypeParam: constraint not set") 87 } 88 return t.bound 89 } 90 91 // SetConstraint sets the type constraint for t. 92 // 93 // It must be called by users of NewTypeParam after the bound's underlying is 94 // fully defined, and before using the type parameter in any way other than to 95 // form other types. Once SetConstraint returns the receiver, t is safe for 96 // concurrent use. 97 func (t *TypeParam) SetConstraint(bound Type) { 98 if bound == nil { 99 panic("nil constraint") 100 } 101 t.bound = bound 102 // iface may mutate t.bound (if bound is not an interface), so ensure that 103 // this is done before returning. 104 t.iface() 105 } 106 107 // Underlying returns the [underlying type] of the type parameter t, which is 108 // the underlying type of its constraint. This type is always an interface. 109 // 110 // [underlying type]: https://go.dev/ref/spec#Underlying_types. 111 func (t *TypeParam) Underlying() Type { 112 return t.iface() 113 } 114 115 func (t *TypeParam) String() string { return TypeString(t, nil) } 116 117 // ---------------------------------------------------------------------------- 118 // Implementation 119 120 func (t *TypeParam) cleanup() { 121 // Before CL 825744, an unresolved type parameter constraint could leave 122 // t.bound as nil, causing t.iface() below to panic on bound.Underlying(). 123 // 124 // Change incomplete type parameters to Typ[Invalid] so the 125 // published package graph never contains buried nils. 126 // 127 // Invariant: The type checker must never return Typ[Invalid] unless an error was 128 // reported. If a TypeParam created during package checking has a nil bound 129 // at cleanup time, verify that an error occurred; if not, this is an 130 // internal compiler bug, so report an error. 131 if t.bound == nil { 132 if t.check != nil && t.check.firstErr == nil { 133 t.check.internalErrorf(t.obj, "%v has nil constraint", t) 134 } 135 t.bound = Typ[Invalid] 136 } 137 t.iface() 138 t.check = nil 139 } 140 141 // iface returns the constraint interface of t. 142 func (t *TypeParam) iface() *Interface { 143 bound := t.bound 144 if bound == nil { 145 // Before CL 825744 if bound was nil, bound.Underlying() below panicked 146 // with a nil-pointer dereference. 147 // 148 // iface panics if called on an incomplete TypeParam before its constraint 149 // is set. 150 panic("incomplete TypeParam: constraint not set") 151 } 152 153 // determine constraint interface 154 var ityp *Interface 155 switch u := bound.Underlying().(type) { 156 case *Basic: 157 if !isValid(u) { 158 // error is reported elsewhere 159 return &emptyInterface 160 } 161 case *Interface: 162 if isTypeParam(bound) { 163 // error is reported in Checker.collectTypeParams 164 return &emptyInterface 165 } 166 ityp = u 167 } 168 169 // If we don't have an interface, wrap constraint into an implicit interface. 170 if ityp == nil { 171 ityp = NewInterfaceType(nil, []Type{bound}) 172 ityp.implicit = true 173 t.bound = ityp // update t.bound for next time (optimization) 174 } 175 176 // compute type set if necessary 177 if ityp.tset == nil { 178 // pos is used for tracing output; start with the type parameter position. 179 pos := t.obj.pos 180 // use the (original or possibly instantiated) type bound position if we have one 181 if n := asNamed(bound); n != nil { 182 pos = n.obj.pos 183 } 184 computeInterfaceTypeSet(t.check, pos, ityp) 185 } 186 187 return ityp 188 } 189 190 // is calls f with the specific type terms of t's constraint and reports whether 191 // all calls to f returned true. If there are no specific terms, is 192 // returns the result of f(nil). 193 func (t *TypeParam) is(f func(*term) bool) bool { 194 return t.iface().typeSet().is(f) 195 } 196 197 // typeset reports whether f(t, y) is true for all (type/underlying type) pairs of the 198 // specific type terms of t's constraint. 199 // If there are no specific terms, typeset returns f(nil, nil). 200 // In any case, typeset is guaranteed to call f at least once. 201 func (t *TypeParam) typeset(f func(t, u Type) bool) bool { 202 return t.iface().typeSet().all(f) 203 } 204