Source file
src/go/types/check_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package types_test
31
32 import (
33 "bytes"
34 "flag"
35 "fmt"
36 "go/ast"
37 "go/parser"
38 "go/scanner"
39 "go/token"
40 "internal/buildcfg"
41 "internal/testenv"
42 "internal/types/errors"
43 "os"
44 "path/filepath"
45 "reflect"
46 "regexp"
47 "runtime"
48 "strconv"
49 "strings"
50 "testing"
51
52 . "go/types"
53 )
54
55 var (
56 haltOnError = flag.Bool("halt", false, "halt on error")
57 verifyErrors = flag.Bool("verify", false, "verify errors (rather than list them) in TestManual")
58 )
59
60 var fset = token.NewFileSet()
61
62 func parseFiles(t *testing.T, filenames []string, srcs [][]byte, mode parser.Mode) ([]*ast.File, []error) {
63 var files []*ast.File
64 var errlist []error
65 for i, filename := range filenames {
66 file, err := parser.ParseFile(fset, filename, srcs[i], mode)
67 if file == nil {
68 t.Fatalf("%s: %s", filename, err)
69 }
70 files = append(files, file)
71 if err != nil {
72 if list, _ := err.(scanner.ErrorList); len(list) > 0 {
73 for _, err := range list {
74 errlist = append(errlist, err)
75 }
76 } else {
77 errlist = append(errlist, err)
78 }
79 }
80 }
81 return files, errlist
82 }
83
84 func unpackError(fset *token.FileSet, err error) (token.Position, string) {
85 switch err := err.(type) {
86 case *scanner.Error:
87 return err.Pos, err.Msg
88 case Error:
89 return fset.Position(err.Pos), err.Msg
90 }
91 panic("unreachable")
92 }
93
94
95 func absDiff(x, y int) int {
96 if x < y {
97 return y - x
98 }
99 return x - y
100 }
101
102
103
104
105 func parseFlags(src []byte, flags *flag.FlagSet) error {
106
107 const prefix = "//"
108 if !bytes.HasPrefix(src, []byte(prefix)) {
109 return nil
110 }
111 src = src[len(prefix):]
112 if i := bytes.Index(src, []byte("-")); i < 0 || len(bytes.TrimSpace(src[:i])) != 0 {
113 return nil
114 }
115 end := bytes.Index(src, []byte("\n"))
116 const maxLen = 256
117 if end < 0 || end > maxLen {
118 return fmt.Errorf("flags comment line too long")
119 }
120
121 return flags.Parse(strings.Fields(string(src[:end])))
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135 func testFiles(t *testing.T, filenames []string, srcs [][]byte, manual bool, opts ...func(*Config)) {
136
137 testFilesImpl(t, filenames, srcs, manual, opts...)
138 if !manual {
139 t.Setenv("GODEBUG", "gotypesalias=0")
140 testFilesImpl(t, filenames, srcs, manual, opts...)
141 }
142 }
143
144 func testFilesImpl(t *testing.T, filenames []string, srcs [][]byte, manual bool, opts ...func(*Config)) {
145 if len(filenames) == 0 {
146 t.Fatal("no source files")
147 }
148
149
150 files, errlist := parseFiles(t, filenames, srcs, parser.AllErrors)
151 pkgName := "<no package>"
152 if len(files) > 0 {
153 pkgName = files[0].Name.Name
154 }
155 listErrors := manual && !*verifyErrors
156 if listErrors && len(errlist) > 0 {
157 t.Errorf("--- %s:", pkgName)
158 for _, err := range errlist {
159 t.Error(err)
160 }
161 }
162
163
164 var conf Config
165 *boolFieldAddr(&conf, "_Trace") = manual && testing.Verbose()
166 conf.Importer = defaultImporter(fset)
167 conf.Error = func(err error) {
168 if *haltOnError {
169 defer panic(err)
170 }
171 if listErrors {
172 t.Error(err)
173 return
174 }
175
176
177 if !strings.Contains(err.Error(), ": \t") {
178 errlist = append(errlist, err)
179 }
180 }
181
182
183 for _, opt := range opts {
184 opt(&conf)
185 }
186
187
188 var goexperiment, gotypesalias string
189 flags := flag.NewFlagSet("", flag.PanicOnError)
190 flags.StringVar(&conf.GoVersion, "lang", "", "")
191 flags.StringVar(&goexperiment, "goexperiment", "", "")
192 flags.BoolVar(&conf.FakeImportC, "fakeImportC", false, "")
193 flags.StringVar(&gotypesalias, "gotypesalias", "", "")
194 if err := parseFlags(srcs[0], flags); err != nil {
195 t.Fatal(err)
196 }
197
198 if goexperiment != "" {
199 revert := setGOEXPERIMENT(goexperiment)
200 defer revert()
201 }
202
203
204 if gotypesalias != "" {
205 t.Setenv("GODEBUG", "gotypesalias="+gotypesalias)
206 }
207
208
209 info := Info{
210 Types: make(map[ast.Expr]TypeAndValue),
211 Instances: make(map[*ast.Ident]Instance),
212 Defs: make(map[*ast.Ident]Object),
213 Uses: make(map[*ast.Ident]Object),
214 Implicits: make(map[ast.Node]Object),
215 Selections: make(map[*ast.SelectorExpr]*Selection),
216 Scopes: make(map[ast.Node]*Scope),
217 FileVersions: make(map[*ast.File]string),
218 }
219
220
221 conf.Check(pkgName, fset, files, &info)
222 if listErrors {
223 return
224 }
225
226
227 errmap := make(map[string]map[int][]comment)
228 for i, filename := range filenames {
229 if m := commentMap(srcs[i], regexp.MustCompile("^ ERRORx? ")); len(m) > 0 {
230 errmap[filename] = m
231 }
232 }
233
234
235 var indices []int
236 for _, err := range errlist {
237 gotPos, gotMsg := unpackError(fset, err)
238
239
240 filename := gotPos.Filename
241 filemap := errmap[filename]
242 line := gotPos.Line
243 var errList []comment
244 if filemap != nil {
245 errList = filemap[line]
246 }
247
248
249 indices = indices[:0]
250 for i, want := range errList {
251 pattern, substr := strings.CutPrefix(want.text, " ERROR ")
252 if !substr {
253 var found bool
254 pattern, found = strings.CutPrefix(want.text, " ERRORx ")
255 if !found {
256 panic("unreachable")
257 }
258 }
259 unquoted, err := strconv.Unquote(strings.TrimSpace(pattern))
260 if err != nil {
261 t.Errorf("%s:%d:%d: invalid ERROR pattern (cannot unquote %s)", filename, line, want.col, pattern)
262 continue
263 }
264 if substr {
265 if !strings.Contains(gotMsg, unquoted) {
266 continue
267 }
268 } else {
269 rx, err := regexp.Compile(unquoted)
270 if err != nil {
271 t.Errorf("%s:%d:%d: %v", filename, line, want.col, err)
272 continue
273 }
274 if !rx.MatchString(gotMsg) {
275 continue
276 }
277 }
278 indices = append(indices, i)
279 }
280 if len(indices) == 0 {
281 t.Errorf("%s: no error expected: %q", gotPos, gotMsg)
282 continue
283 }
284
285
286
287 index := -1
288 var delta int
289 for _, i := range indices {
290 if d := absDiff(gotPos.Column, errList[i].col); index < 0 || d < delta {
291 index, delta = i, d
292 }
293 }
294
295
296 const colDelta = 0
297 if delta > colDelta {
298 t.Errorf("%s: got col = %d; want %d", gotPos, gotPos.Column, errList[index].col)
299 }
300
301
302 if n := len(errList) - 1; n > 0 {
303
304 copy(errList[index:], errList[index+1:])
305 filemap[line] = errList[:n]
306 } else {
307
308 delete(filemap, line)
309 }
310
311
312 if len(filemap) == 0 {
313 delete(errmap, filename)
314 }
315 }
316
317
318 if len(errmap) > 0 {
319 t.Errorf("--- %s: unreported errors:", pkgName)
320 for filename, filemap := range errmap {
321 for line, errList := range filemap {
322 for _, err := range errList {
323 t.Errorf("%s:%d:%d: %s", filename, line, err.col, err.text)
324 }
325 }
326 }
327 }
328 }
329
330 func readCode(err Error) errors.Code {
331 v := reflect.ValueOf(err)
332 return errors.Code(v.FieldByName("go116code").Int())
333 }
334
335
336
337 func boolFieldAddr(conf *Config, name string) *bool {
338 v := reflect.Indirect(reflect.ValueOf(conf))
339 return (*bool)(v.FieldByName(name).Addr().UnsafePointer())
340 }
341
342
343
344 func stringFieldAddr(conf *Config, name string) *string {
345 v := reflect.Indirect(reflect.ValueOf(conf))
346 return (*string)(v.FieldByName(name).Addr().UnsafePointer())
347 }
348
349
350
351
352
353 func setGOEXPERIMENT(goexperiment string) func() {
354 exp, err := buildcfg.ParseGOEXPERIMENT(runtime.GOOS, runtime.GOARCH, goexperiment)
355 if err != nil {
356 panic(err)
357 }
358 old := buildcfg.Experiment
359 buildcfg.Experiment = *exp
360 return func() { buildcfg.Experiment = old }
361 }
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377 func TestManual(t *testing.T) {
378 testenv.MustHaveGoBuild(t)
379
380 filenames := flag.Args()
381 if len(filenames) == 0 {
382 filenames = []string{filepath.FromSlash("testdata/manual.go")}
383 }
384
385 info, err := os.Stat(filenames[0])
386 if err != nil {
387 t.Fatalf("TestManual: %v", err)
388 }
389
390 DefPredeclaredTestFuncs()
391 if info.IsDir() {
392 if len(filenames) > 1 {
393 t.Fatal("TestManual: must have only one directory argument")
394 }
395 testDir(t, filenames[0], true)
396 } else {
397 testPkg(t, filenames, true)
398 }
399 }
400
401 func TestLongConstants(t *testing.T) {
402 format := `package longconst; const _ = %s /* ERROR "constant overflow" */; const _ = %s // ERROR "excessively long constant"`
403 src := fmt.Sprintf(format, strings.Repeat("1", 9999), strings.Repeat("1", 10001))
404 testFiles(t, []string{"longconst.go"}, [][]byte{[]byte(src)}, false)
405 }
406
407 func withSizes(sizes Sizes) func(*Config) {
408 return func(cfg *Config) {
409 cfg.Sizes = sizes
410 }
411 }
412
413
414
415
416 func TestIndexRepresentability(t *testing.T) {
417 const src = `package index; var s []byte; var _ = s[int64 /* ERRORx "int64\\(1\\) << 40 \\(.*\\) overflows int" */ (1) << 40]`
418 testFiles(t, []string{"index.go"}, [][]byte{[]byte(src)}, false, withSizes(&StdSizes{4, 4}))
419 }
420
421 func TestIssue47243_TypedRHS(t *testing.T) {
422
423
424 const src = `package issue47243; var a uint64; var _ = a << uint64(4294967296)`
425 testFiles(t, []string{"p.go"}, [][]byte{[]byte(src)}, false, withSizes(&StdSizes{4, 4}))
426 }
427
428 func TestCheck(t *testing.T) {
429 old := buildcfg.Experiment.RangeFunc
430 defer func() {
431 buildcfg.Experiment.RangeFunc = old
432 }()
433 buildcfg.Experiment.RangeFunc = true
434
435 DefPredeclaredTestFuncs()
436 testDirFiles(t, "../../internal/types/testdata/check", false)
437 }
438 func TestSpec(t *testing.T) { testDirFiles(t, "../../internal/types/testdata/spec", false) }
439 func TestExamples(t *testing.T) { testDirFiles(t, "../../internal/types/testdata/examples", false) }
440 func TestFixedbugs(t *testing.T) { testDirFiles(t, "../../internal/types/testdata/fixedbugs", false) }
441 func TestLocal(t *testing.T) { testDirFiles(t, "testdata/local", false) }
442
443 func testDirFiles(t *testing.T, dir string, manual bool) {
444 testenv.MustHaveGoBuild(t)
445 dir = filepath.FromSlash(dir)
446
447 fis, err := os.ReadDir(dir)
448 if err != nil {
449 t.Error(err)
450 return
451 }
452
453 for _, fi := range fis {
454 path := filepath.Join(dir, fi.Name())
455
456
457 if fi.IsDir() {
458 testDir(t, path, manual)
459 } else {
460 t.Run(filepath.Base(path), func(t *testing.T) {
461 testPkg(t, []string{path}, manual)
462 })
463 }
464 }
465 }
466
467 func testDir(t *testing.T, dir string, manual bool) {
468 testenv.MustHaveGoBuild(t)
469
470 fis, err := os.ReadDir(dir)
471 if err != nil {
472 t.Error(err)
473 return
474 }
475
476 var filenames []string
477 for _, fi := range fis {
478 filenames = append(filenames, filepath.Join(dir, fi.Name()))
479 }
480
481 t.Run(filepath.Base(dir), func(t *testing.T) {
482 testPkg(t, filenames, manual)
483 })
484 }
485
486 func testPkg(t *testing.T, filenames []string, manual bool) {
487 srcs := make([][]byte, len(filenames))
488 for i, filename := range filenames {
489 src, err := os.ReadFile(filename)
490 if err != nil {
491 t.Fatalf("could not read %s: %v", filename, err)
492 }
493 srcs[i] = src
494 }
495 testFiles(t, filenames, srcs, manual)
496 }
497
View as plain text