| 1 |
// Copyright 2019 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 |
// The code in this file is copied from |
| 6 |
// x/tools/gopls/internal/golang/implementation.go |
| 7 |
|
| 8 |
package typeutil |
| 9 |
|
| 10 |
import ( |
| 11 |
"fmt" |
| 12 |
"go/types" |
| 13 |
"iter" |
| 14 |
"reflect" |
| 15 |
) |
| 16 |
|
| 17 |
// Unify reports whether the types of x and y match. |
| 18 |
// |
| 19 |
// If unifier is nil, unify reports only whether it succeeded. |
| 20 |
// If unifier is non-nil, it is populated with the values |
| 21 |
// of type parameters determined during a successful unification. |
| 22 |
// If unification succeeds without binding a type parameter, that parameter |
| 23 |
// will not be present in the map. |
| 24 |
// |
| 25 |
// On entry, the unifier's contents are treated as the values of already-bound type |
| 26 |
// parameters, constraining the unification. |
| 27 |
// |
| 28 |
// For example, if unifier is an empty (not nil) map on entry, then the types |
| 29 |
// |
| 30 |
// func[T any](T, int) |
| 31 |
// |
| 32 |
// and |
| 33 |
// |
| 34 |
// func[U any](bool, U) |
| 35 |
// |
| 36 |
// will unify, with T=bool and U=int. |
| 37 |
// That is, the contents of unifier after unify returns will be |
| 38 |
// |
| 39 |
// {T: bool, U: int} |
| 40 |
// |
| 41 |
// where "T" is the type parameter T and "bool" is the basic type for bool. |
| 42 |
// |
| 43 |
// But if unifier is {T: int} is int on entry, then unification will fail, because T |
| 44 |
// does not unify with bool. |
| 45 |
// |
| 46 |
// Unify does not preserve aliases. For example, given the following: |
| 47 |
// |
| 48 |
// type String = string |
| 49 |
// type A[T] = T |
| 50 |
// |
| 51 |
// unification succeeds with T bound to string, not String. |
| 52 |
// |
| 53 |
// See also: unify in cache/methodsets/fingerprint, which implements |
| 54 |
// unification for type fingerprints, for the global index. |
| 55 |
// |
| 56 |
// BUG: literal interfaces are not handled properly. But this function is currently |
| 57 |
// used only for signatures, where such types are very rare. |
| 58 |
func Unify(x, y types.Type, unifier map[*types.TypeParam]types.Type) bool { |
| 59 |
// bindings[tp] is the binding for type parameter tp. |
| 60 |
// Although type parameters are nominally bound to types, each bindings[tp] |
| 61 |
// is a pointer to a type, so unbound variables that unify can share a binding. |
| 62 |
bindings := map[*types.TypeParam]*types.Type{} |
| 63 |
|
| 64 |
// Bindings is initialized with pointers to the provided types. |
| 65 |
for tp, t := range unifier { |
| 66 |
bindings[tp] = &t |
| 67 |
} |
| 68 |
|
| 69 |
// bindingFor returns the *types.Type in bindings for tp if tp is not nil, |
| 70 |
// creating one if needed. |
| 71 |
bindingFor := func(tp *types.TypeParam) *types.Type { |
| 72 |
if tp == nil { |
| 73 |
return nil |
| 74 |
} |
| 75 |
b := bindings[tp] |
| 76 |
if b == nil { |
| 77 |
b = new(types.Type) |
| 78 |
bindings[tp] = b |
| 79 |
} |
| 80 |
return b |
| 81 |
} |
| 82 |
|
| 83 |
// bind sets b to t if b does not occur in t. |
| 84 |
bind := func(b *types.Type, t types.Type) bool { |
| 85 |
for tp := range typeParams(t) { |
| 86 |
if b == bindings[tp] { |
| 87 |
return false // failed "occurs" check |
| 88 |
} |
| 89 |
} |
| 90 |
*b = t |
| 91 |
return true |
| 92 |
} |
| 93 |
|
| 94 |
// uni performs the actual unification. |
| 95 |
depth := 0 |
| 96 |
var uni func(x, y types.Type) bool |
| 97 |
uni = func(x, y types.Type) bool { |
| 98 |
// Panic if recursion gets too deep, to detect bugs before |
| 99 |
// overflowing the stack. |
| 100 |
depth++ |
| 101 |
defer func() { depth-- }() |
| 102 |
if depth > 100 { |
| 103 |
panic("unify: max depth exceeded") |
| 104 |
} |
| 105 |
|
| 106 |
x = types.Unalias(x) |
| 107 |
y = types.Unalias(y) |
| 108 |
|
| 109 |
tpx, _ := x.(*types.TypeParam) |
| 110 |
tpy, _ := y.(*types.TypeParam) |
| 111 |
if tpx != nil || tpy != nil { |
| 112 |
// Identical type params unify. |
| 113 |
if tpx == tpy { |
| 114 |
return true |
| 115 |
} |
| 116 |
bx := bindingFor(tpx) |
| 117 |
by := bindingFor(tpy) |
| 118 |
|
| 119 |
// If both args are type params and neither is bound, have them share a binding. |
| 120 |
if bx != nil && by != nil && *bx == nil && *by == nil { |
| 121 |
// Arbitrarily give y's binding to x. |
| 122 |
bindings[tpx] = by |
| 123 |
return true |
| 124 |
} |
| 125 |
// Treat param bindings like original args in what follows. |
| 126 |
if bx != nil && *bx != nil { |
| 127 |
x = *bx |
| 128 |
} |
| 129 |
if by != nil && *by != nil { |
| 130 |
y = *by |
| 131 |
} |
| 132 |
// If the x param is unbound, bind it to y. |
| 133 |
if bx != nil && *bx == nil { |
| 134 |
return bind(bx, y) |
| 135 |
} |
| 136 |
// If the y param is unbound, bind it to x. |
| 137 |
if by != nil && *by == nil { |
| 138 |
return bind(by, x) |
| 139 |
} |
| 140 |
// Unify the binding of a bound parameter. |
| 141 |
return uni(x, y) |
| 142 |
} |
| 143 |
|
| 144 |
// Neither arg is a type param. |
| 145 |
|
| 146 |
if reflect.TypeOf(x) != reflect.TypeOf(y) { |
| 147 |
return false // mismatched types |
| 148 |
} |
| 149 |
|
| 150 |
switch x := x.(type) { |
| 151 |
case *types.Array: |
| 152 |
y := y.(*types.Array) |
| 153 |
return x.Len() == y.Len() && |
| 154 |
uni(x.Elem(), y.Elem()) |
| 155 |
|
| 156 |
case *types.Basic: |
| 157 |
y := y.(*types.Basic) |
| 158 |
return x.Kind() == y.Kind() |
| 159 |
|
| 160 |
case *types.Chan: |
| 161 |
y := y.(*types.Chan) |
| 162 |
return x.Dir() == y.Dir() && |
| 163 |
uni(x.Elem(), y.Elem()) |
| 164 |
|
| 165 |
case *types.Interface: |
| 166 |
y := y.(*types.Interface) |
| 167 |
// TODO(adonovan,jba): fix: for correctness, we must check |
| 168 |
// that both interfaces have the same set of methods |
| 169 |
// modulo type parameters, while avoiding the risk of |
| 170 |
// unbounded interface recursion. |
| 171 |
// |
| 172 |
// Since non-empty interface literals are vanishingly |
| 173 |
// rare in methods signatures, we ignore this for now. |
| 174 |
// If more precision is needed we could compare method |
| 175 |
// names and arities, still without full recursion. |
| 176 |
return x.NumMethods() == y.NumMethods() |
| 177 |
|
| 178 |
case *types.Map: |
| 179 |
y := y.(*types.Map) |
| 180 |
return uni(x.Key(), y.Key()) && |
| 181 |
uni(x.Elem(), y.Elem()) |
| 182 |
|
| 183 |
case *types.Named: |
| 184 |
y := y.(*types.Named) |
| 185 |
if x.Origin() != y.Origin() { |
| 186 |
return false // different named types |
| 187 |
} |
| 188 |
xtargs := x.TypeArgs() |
| 189 |
ytargs := y.TypeArgs() |
| 190 |
if xtargs.Len() != ytargs.Len() { |
| 191 |
return false // arity error (ill-typed) |
| 192 |
} |
| 193 |
for i := range xtargs.Len() { |
| 194 |
if !uni(xtargs.At(i), ytargs.At(i)) { |
| 195 |
return false // mismatched type args |
| 196 |
} |
| 197 |
} |
| 198 |
return true |
| 199 |
|
| 200 |
case *types.Pointer: |
| 201 |
y := y.(*types.Pointer) |
| 202 |
return uni(x.Elem(), y.Elem()) |
| 203 |
|
| 204 |
case *types.Signature: |
| 205 |
y := y.(*types.Signature) |
| 206 |
return x.Variadic() == y.Variadic() && |
| 207 |
uni(x.Params(), y.Params()) && |
| 208 |
uni(x.Results(), y.Results()) |
| 209 |
|
| 210 |
case *types.Slice: |
| 211 |
y := y.(*types.Slice) |
| 212 |
return uni(x.Elem(), y.Elem()) |
| 213 |
|
| 214 |
case *types.Struct: |
| 215 |
y := y.(*types.Struct) |
| 216 |
if x.NumFields() != y.NumFields() { |
| 217 |
return false |
| 218 |
} |
| 219 |
for i := range x.NumFields() { |
| 220 |
xf := x.Field(i) |
| 221 |
yf := y.Field(i) |
| 222 |
if xf.Embedded() != yf.Embedded() || |
| 223 |
xf.Name() != yf.Name() || |
| 224 |
x.Tag(i) != y.Tag(i) || |
| 225 |
!xf.Exported() && xf.Pkg() != yf.Pkg() || |
| 226 |
!uni(xf.Type(), yf.Type()) { |
| 227 |
return false |
| 228 |
} |
| 229 |
} |
| 230 |
return true |
| 231 |
|
| 232 |
case *types.Tuple: |
| 233 |
y := y.(*types.Tuple) |
| 234 |
if x.Len() != y.Len() { |
| 235 |
return false |
| 236 |
} |
| 237 |
for i := range x.Len() { |
| 238 |
if !uni(x.At(i).Type(), y.At(i).Type()) { |
| 239 |
return false |
| 240 |
} |
| 241 |
} |
| 242 |
return true |
| 243 |
|
| 244 |
default: // incl. *Union, *TypeParam |
| 245 |
panic(fmt.Sprintf("unexpected Type %#v", x)) |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
if !uni(x, y) { |
| 250 |
clear(unifier) |
| 251 |
return false |
| 252 |
} |
| 253 |
|
| 254 |
// Populate the input map with the resulting types. |
| 255 |
if unifier != nil { |
| 256 |
for tparam, tptr := range bindings { |
| 257 |
unifier[tparam] = *tptr |
| 258 |
} |
| 259 |
} |
| 260 |
return true |
| 261 |
} |
| 262 |
|
| 263 |
// typeParams yields all the free type parameters within t that are relevant for |
| 264 |
// unification. |
| 265 |
// |
| 266 |
// Note: this function is tailored for the specific needs of the unification algorithm. |
| 267 |
// Don't try to use it for other purposes, see [typeparams.Free] instead. |
| 268 |
func typeParams(t types.Type) iter.Seq[*types.TypeParam] { |
| 269 |
return func(yield func(*types.TypeParam) bool) { |
| 270 |
seen := map[*types.TypeParam]bool{} // yield each type param only once |
| 271 |
|
| 272 |
// tps(t) yields each TypeParam in t and returns false to stop. |
| 273 |
var tps func(types.Type) bool |
| 274 |
tps = func(t types.Type) bool { |
| 275 |
t = types.Unalias(t) |
| 276 |
|
| 277 |
switch t := t.(type) { |
| 278 |
case *types.TypeParam: |
| 279 |
if seen[t] { |
| 280 |
return true |
| 281 |
} |
| 282 |
seen[t] = true |
| 283 |
return yield(t) |
| 284 |
|
| 285 |
case *types.Basic: |
| 286 |
return true |
| 287 |
|
| 288 |
case *types.Array: |
| 289 |
return tps(t.Elem()) |
| 290 |
|
| 291 |
case *types.Chan: |
| 292 |
return tps(t.Elem()) |
| 293 |
|
| 294 |
case *types.Interface: |
| 295 |
// TODO(jba): implement. |
| 296 |
return true |
| 297 |
|
| 298 |
case *types.Map: |
| 299 |
return tps(t.Key()) && tps(t.Elem()) |
| 300 |
|
| 301 |
case *types.Named: |
| 302 |
if t.Origin() == t { |
| 303 |
// generic type: look at type params |
| 304 |
return every(t.TypeParams().TypeParams(), |
| 305 |
func(tp *types.TypeParam) bool { return tps(tp) }) |
| 306 |
} |
| 307 |
// instantiated type: look at type args |
| 308 |
return every(t.TypeArgs().Types(), tps) |
| 309 |
|
| 310 |
case *types.Pointer: |
| 311 |
return tps(t.Elem()) |
| 312 |
|
| 313 |
case *types.Signature: |
| 314 |
return tps(t.Params()) && tps(t.Results()) |
| 315 |
|
| 316 |
case *types.Slice: |
| 317 |
return tps(t.Elem()) |
| 318 |
|
| 319 |
case *types.Struct: |
| 320 |
return every(t.Fields(), |
| 321 |
func(v *types.Var) bool { return tps(v.Type()) }) |
| 322 |
|
| 323 |
case *types.Tuple: |
| 324 |
return every(t.Variables(), |
| 325 |
func(v *types.Var) bool { return tps(v.Type()) }) |
| 326 |
|
| 327 |
default: // incl. *Union |
| 328 |
panic(fmt.Sprintf("unexpected Type %#v", t)) |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
tps(t) |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
// every reports whether every pred(t) for t in seq returns true, |
| 337 |
// stopping at the first false element. |
| 338 |
func every[T any](seq iter.Seq[T], pred func(T) bool) bool { |
| 339 |
for t := range seq { |
| 340 |
if !pred(t) { |
| 341 |
return false |
| 342 |
} |
| 343 |
} |
| 344 |
return true |
| 345 |
} |