Debian Codegraph @

internal/typesinternal/element_test.go

raw symbols SCIP
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_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal_test`/
6
7 import (
8 "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/"
9 "go/parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/"
10 "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/"
11 "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/"
12 "mapsscip-go gomod github.com/golang/go/src go1.25.0 maps/"
13 "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/"
14 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
15 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
16
17 "golang.org/x/tools/go/types/typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/"
18 "golang.org/x/tools/internal/typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/"
19 )
20
21 const elementSrcscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal_test`/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 TestForEachElementscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal_test`/TestForEachElement().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
48 fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().()
49 f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, "a.go", elementSrcscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal_test`/elementSrc., 0)
50 if err != nil {
51 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) // parse error
52 }
53 var config typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Configscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#
54 pkg, err := config.Checkscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Config#Check().(f.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name., fset, []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#{f}, nil)
55 if err != nil {
56 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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 typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/.MethodSetCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/MethodSetCache#
100 for _, test := range tests {
101 tname, ok := pkg.Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Scope().().Lookupscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Lookup().(test.name).(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.TypeNamescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeName#)
102 if !ok {
103 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("no such type %q", test.name)
104 continue
105 }
106 T := 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().(tname.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().())
107
108 toStr := func(T 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#) string {
109 return typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.TypeStringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeString().(T, func(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#) string { return "" })
110 }
111
112 got := make(map[string]bool)
113 set := new(typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/.Mapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Map#) // for de-duping
114 set2 := new(typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/.Mapscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Map#) // for consistency check
115 typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.ForEachElementscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/ForEachElement().(set, &msets, T, func(elem 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#) {
116 got[toStr(elem)] = true
117 set2.Setscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Map#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.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Map#Len().() != set2.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Map#Len().() {
123 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ForEachElement called f %d times yet de-dup set has %d elements",
124 set2.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Map#Len().(), set.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Map#Len().())
125 } else {
126 set.Iteratescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Map#Iterate().(func(key 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#, _ any) {
127 if set2.Atscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Map#At().(key) == nil {
128 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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 := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.CutPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/CutPrefix().(typstr, "!")
138 if found && unwanted {
139 fail = true
140 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ForEachElement(%s): unwanted element %q", T, typstr)
141 } else if !found && !unwanted {
142 fail = true
143 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("ForEachElement(%s): element %q not found", T, typstr)
144 }
145 }
146 if fail {
147 t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("got elements:\n%s", stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Sortedscip-go gomod github.com/golang/go/src go1.25.0 slices/Sorted().(mapsscip-go gomod github.com/golang/go/src go1.25.0 maps/.Keysscip-go gomod github.com/golang/go/src go1.25.0 maps/Keys().(got)), "\n"))
148 }
149 }
150 }