| 1 | // Copyright 2024 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 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 9 | ) |
| 10 | |
| 11 | // Free is a memoization of the set of free type parameters within a |
| 12 | // type. It makes a sequence of calls to [Free.Has] for overlapping |
| 13 | // types more efficient. The zero value is ready for use. |
| 14 | // |
| 15 | // NOTE: Adapted from go/types/infer.go. If it is later exported, factor. |
| 16 | type Freescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free# struct { |
| 17 | seenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#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#]bool |
| 18 | } |
| 19 | |
| 20 | // Has reports whether the specified type has a free type parameter. |
| 21 | func (w *Freescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#) Hasscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#Has().(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#) (res bool) { |
| 22 | // detect cycles |
| 23 | if x, ok := w.seenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#seen.[typ]; ok { |
| 24 | return x |
| 25 | } |
| 26 | if w.seenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#seen. == nil { |
| 27 | w.seenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#seen. = 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#]bool) |
| 28 | } |
| 29 | w.seenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#seen.[typ] = false |
| 30 | defer func() { |
| 31 | w.seenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#seen.[typ] = res |
| 32 | }() |
| 33 | |
| 34 | switch t := typ.(type) { |
| 35 | case nil, *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Basicscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Basic#: // TODO(gri) should nil be handled here? |
| 36 | break |
| 37 | |
| 38 | case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Aliasscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Alias#: |
| 39 | if t.TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Alias#TypeParams().().Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeParamList#Len().() > t.TypeArgsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Alias#TypeArgs().().Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeList#Len().() { |
| 40 | return true // This is an uninstantiated Alias. |
| 41 | } |
| 42 | // The expansion of an alias can have free type parameters, |
| 43 | // whether or not the alias itself has type parameters: |
| 44 | // |
| 45 | // func _[K comparable]() { |
| 46 | // type Set = map[K]bool // free(Set) = {K} |
| 47 | // type MapTo[V] = map[K]V // free(Map[foo]) = {V} |
| 48 | // } |
| 49 | // |
| 50 | // So, we must Unalias. |
| 51 | return w.Hasscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#Has().(typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Unaliasscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Unalias().(t)) |
| 52 | |
| 53 | case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Arrayscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Array#: |
| 54 | return w.Hasscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#Has().(t.Elemscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Array#Elem().()) |
| 55 | |
| 56 | case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Slicescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Slice#: |
| 57 | return w.Hasscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#Has().(t.Elemscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Slice#Elem().()) |
| 58 | |
| 59 | case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Structscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Struct#: |
| 60 | for i, n := 0, t.NumFieldsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Struct#NumFields().(); i < n; i++ { |
| 61 | if w.Hasscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#Has().(t.Fieldscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Struct#Field().(i).Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().()) { |
| 62 | return true |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Pointerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Pointer#: |
| 67 | return w.Hasscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#Has().(t.Elemscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Pointer#Elem().()) |
| 68 | |
| 69 | case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Tuplescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#: |
| 70 | n := t.Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#Len().() |
| 71 | for i := range n { |
| 72 | if w.Hasscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#Has().(t.Atscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#At().(i).Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().()) { |
| 73 | return true |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Signaturescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#: |
| 78 | // t.tparams may not be nil if we are looking at a signature |
| 79 | // of a generic function type (or an interface method) that is |
| 80 | // part of the type we're testing. We don't care about these type |
| 81 | // parameters. |
| 82 | // Similarly, the receiver of a method may declare (rather than |
| 83 | // use) type parameters, we don't care about those either. |
| 84 | // Thus, we only need to look at the input and result parameters. |
| 85 | return w.Hasscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#Has().(t.Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Params().()) || w.Hasscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#Has().(t.Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Results().()) |
| 86 | |
| 87 | 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#: |
| 88 | for i, n := 0, t.NumMethodsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Interface#NumMethods().(); i < n; i++ { |
| 89 | if w.Hasscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#Has().(t.Methodscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Interface#Method().(i).Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().()) { |
| 90 | return true |
| 91 | } |
| 92 | } |
| 93 | terms, err := InterfaceTermSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/InterfaceTermSet().(t) |
| 94 | if err != nil { |
| 95 | return false // ill typed |
| 96 | } |
| 97 | for _, term := range terms { |
| 98 | if w.Hasscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#Has().(term.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Term#Type().()) { |
| 99 | return true |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Mapscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Map#: |
| 104 | return w.Hasscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#Has().(t.Keyscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Map#Key().()) || w.Hasscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#Has().(t.Elemscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Map#Elem().()) |
| 105 | |
| 106 | case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Chanscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Chan#: |
| 107 | return w.Hasscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#Has().(t.Elemscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Chan#Elem().()) |
| 108 | |
| 109 | case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Namedscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#: |
| 110 | args := t.TypeArgsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#TypeArgs().() |
| 111 | if params := t.TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#TypeParams().(); params.Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeParamList#Len().() > args.Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeList#Len().() { |
| 112 | return true // this is an uninstantiated named type. |
| 113 | } |
| 114 | for i, n := 0, args.Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeList#Len().(); i < n; i++ { |
| 115 | if w.Hasscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#Has().(args.Atscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeList#At().(i)) { |
| 116 | return true |
| 117 | } |
| 118 | } |
| 119 | return w.Hasscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams`/Free#Has().(t.Underlyingscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#Underlying().()) // recurse for types local to parameterized functions |
| 120 | |
| 121 | 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#: |
| 122 | return true |
| 123 | |
| 124 | default: |
| 125 | panic(t) // unreachable |
| 126 | } |
| 127 | |
| 128 | return false |
| 129 | } |