| 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 typesinternal_test |
| 6 | |
| 7 | import ( |
| 8 | "go/ast" |
| 9 | "go/parser" |
| 10 | "go/token" |
| 11 | "go/types" |
| 12 | "maps" |
| 13 | "slices" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | |
| 17 | "golang.org/x/tools/go/types/typeutil" |
| 18 | "golang.org/x/tools/internal/typesinternal" |
| 19 | ) |
| 20 | |
| 21 | const elementSrc = ` |
| 22 | package p |
| 23 | |
| 24 | type A = int |
| 25 | |
| 26 | type B = *map[chan int][]func() [2]bool |
| 27 | |
| 28 | type C = T |
| 29 | |
| 30 | type T struct{ x int } |
| 31 | func (T) method() uint |
| 32 | func (*T) ptrmethod() complex128 |
| 33 | |
| 34 | type D = A |
| 35 | |
| 36 | type E = struct{ x int } |
| 37 | |
| 38 | type F = func(int8, int16) (int32, int64) |
| 39 | |
| 40 | type G = struct { U } |
| 41 | |
| 42 | type U struct{} |
| 43 | func (U) method() uint32 |
| 44 | |
| 45 | ` |
| 46 | |
| 47 | func TestForEachElement(t *testing.T) { |
| 48 | fset := token.NewFileSet() |
| 49 | f, err := parser.ParseFile(fset, "a.go", elementSrc, 0) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) // parse error |
| 52 | } |
| 53 | var config types.Config |
| 54 | pkg, err := config.Check(f.Name.Name, fset, []*ast.File{f}, nil) |
| 55 | if err != nil { |
| 56 | t.Fatal(err) // type error |
| 57 | } |
| 58 | |
| 59 | tests := []struct { |
| 60 | name string // name of a type alias whose RHS type's elements to compute |
| 61 | want []string // strings of types that are/are not elements (! => not) |
| 62 | }{ |
| 63 | // simple type |
| 64 | {"A", []string{"int"}}, |
| 65 | |
| 66 | // compound type |
| 67 | {"B", []string{ |
| 68 | "*map[chan int][]func() [2]bool", |
| 69 | "map[chan int][]func() [2]bool", |
| 70 | "chan int", |
| 71 | "int", |
| 72 | "[]func() [2]bool", |
| 73 | "func() [2]bool", |
| 74 | "[2]bool", |
| 75 | "bool", |
| 76 | }}, |
| 77 | |
| 78 | // defined struct type with methods, incl. pointer methods. |
| 79 | // Observe that it descends into the field type, but |
| 80 | // the result does not include the struct type itself. |
| 81 | // (This follows the Go toolchain behavior, and finesses the need |
| 82 | // to create wrapper methods for that struct type.) |
| 83 | {"C", []string{"T", "*T", "int", "uint", "complex128", "!struct{x int}"}}, |
| 84 | |
| 85 | // alias type |
| 86 | {"D", []string{"int"}}, |
| 87 | |
| 88 | // struct type not beneath a defined type |
| 89 | {"E", []string{"struct{x int}", "int"}}, |
| 90 | |
| 91 | // signature types: the params/results tuples |
| 92 | // are traversed but not included. |
| 93 | {"F", []string{"func(int8, int16) (int32, int64)", |
| 94 | "int8", "int16", "int32", "int64"}}, |
| 95 | |
| 96 | // struct with embedded field that has methods |
| 97 | {"G", []string{"*U", "struct{U}", "uint32", "U"}}, |
| 98 | } |
| 99 | var msets typeutil.MethodSetCache |
| 100 | for _, test := range tests { |
| 101 | tname, ok := pkg.Scope().Lookup(test.name).(*types.TypeName) |
| 102 | if !ok { |
| 103 | t.Errorf("no such type %q", test.name) |
| 104 | continue |
| 105 | } |
| 106 | T := types.Unalias(tname.Type()) |
| 107 | |
| 108 | toStr := func(T types.Type) string { |
| 109 | return types.TypeString(T, func(*types.Package) string { return "" }) |
| 110 | } |
| 111 | |
| 112 | got := make(map[string]bool) |
| 113 | set := new(typeutil.Map) // for de-duping |
| 114 | set2 := new(typeutil.Map) // for consistency check |
| 115 | typesinternal.ForEachElement(set, &msets, T, func(elem types.Type) { |
| 116 | got[toStr(elem)] = true |
| 117 | set2.Set(elem, true) |
| 118 | }) |
| 119 | |
| 120 | // Assert that set==set2, meaning f(x) was |
| 121 | // called for each x in the de-duping map. |
| 122 | if set.Len() != set2.Len() { |
| 123 | t.Errorf("ForEachElement called f %d times yet de-dup set has %d elements", |
| 124 | set2.Len(), set.Len()) |
| 125 | } else { |
| 126 | set.Iterate(func(key types.Type, _ any) { |
| 127 | if set2.At(key) == nil { |
| 128 | t.Errorf("ForEachElement did not call f(%v)", key) |
| 129 | } |
| 130 | }) |
| 131 | } |
| 132 | |
| 133 | // Assert than all expected (and no unexpected) elements were found. |
| 134 | fail := false |
| 135 | for _, typstr := range test.want { |
| 136 | found := got[typstr] |
| 137 | typstr, unwanted := strings.CutPrefix(typstr, "!") |
| 138 | if found && unwanted { |
| 139 | fail = true |
| 140 | t.Errorf("ForEachElement(%s): unwanted element %q", T, typstr) |
| 141 | } else if !found && !unwanted { |
| 142 | fail = true |
| 143 | t.Errorf("ForEachElement(%s): element %q not found", T, typstr) |
| 144 | } |
| 145 | } |
| 146 | if fail { |
| 147 | t.Logf("got elements:\n%s", strings.Join(slices.Sorted(maps.Keys(got)), "\n")) |
| 148 | } |
| 149 | } |
| 150 | } |