Debian Codegraph @

src/go/types/stdlib_test.go

raw
1 // Copyright 2013 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 // This file tests types.Check by using it to
6 // typecheck the standard library and tests.
7
8 package types_test
9
10 import (
11 "errors"
12 "fmt"
13 "go/ast"
14 "go/build"
15 "go/importer"
16 "go/parser"
17 "go/scanner"
18 "go/token"
19 "internal/testenv"
20 "os"
21 "path/filepath"
22 "runtime"
23 "slices"
24 "strings"
25 "sync"
26 "testing"
27 "time"
28
29 . "go/types"
30 )
31
32 // The cmd/*/internal packages may have been deleted as part of a binary
33 // release. Import from source instead.
34 //
35 // (See https://golang.org/issue/43232 and
36 // https://github.com/golang/build/blob/df58bbac082bc87c4a3cdfe336d1ffe60bbaa916/cmd/release/release.go#L533-L545.)
37 //
38 // Use the same importer for all std lib tests to
39 // avoid repeated importing of the same packages.
40 var stdLibImporter = importer.ForCompiler(token.NewFileSet(), "source", nil)
41
42 func TestStdlib(t *testing.T) {
43 if testing.Short() {
44 t.Skip("skipping in short mode")
45 }
46
47 testenv.MustHaveGoBuild(t)
48
49 // Collect non-test files.
50 dirFiles := make(map[string][]string)
51 root := filepath.Join(testenv.GOROOT(t), "src")
52 walkPkgDirs(root, func(dir string, filenames []string) {
53 dirFiles[dir] = filenames
54 }, t.Error)
55
56 c := &stdlibChecker{
57 dirFiles: dirFiles,
58 pkgs: make(map[string]*futurePackage),
59 }
60
61 start := time.Now()
62
63 // Though we read files while parsing, type-checking is otherwise CPU bound.
64 //
65 // This doesn't achieve great CPU utilization as many packages may block
66 // waiting for a common import, but in combination with the non-deterministic
67 // map iteration below this should provide decent coverage of concurrent
68 // type-checking (see golang/go#47729).
69 cpulimit := make(chan struct{}, runtime.GOMAXPROCS(0))
70 var wg sync.WaitGroup
71
72 for dir := range dirFiles {
73 cpulimit <- struct{}{}
74 wg.Add(1)
75 go func() {
76 defer func() {
77 wg.Done()
78 <-cpulimit
79 }()
80
81 _, err := c.getDirPackage(dir)
82 if err != nil {
83 t.Errorf("error checking %s: %v", dir, err)
84 }
85 }()
86 }
87
88 wg.Wait()
89
90 if testing.Verbose() {
91 fmt.Println(len(dirFiles), "packages typechecked in", time.Since(start))
92 }
93 }
94
95 // stdlibChecker implements concurrent type-checking of the packages defined by
96 // dirFiles, which must define a closed set of packages (such as GOROOT/src).
97 type stdlibChecker struct {
98 dirFiles map[string][]string // non-test files per directory; must be pre-populated
99
100 mu sync.Mutex
101 pkgs map[string]*futurePackage // future cache of type-checking results
102 }
103
104 // A futurePackage is a future result of type-checking.
105 type futurePackage struct {
106 done chan struct{} // guards pkg and err
107 pkg *Package
108 err error
109 }
110
111 func (c *stdlibChecker) Import(path string) (*Package, error) {
112 panic("unimplemented: use ImportFrom")
113 }
114
115 func (c *stdlibChecker) ImportFrom(path, dir string, _ ImportMode) (*Package, error) {
116 if path == "unsafe" {
117 // unsafe cannot be type checked normally.
118 return Unsafe, nil
119 }
120
121 p, err := build.Default.Import(path, dir, build.FindOnly)
122 if err != nil {
123 return nil, err
124 }
125
126 pkg, err := c.getDirPackage(p.Dir)
127 if pkg != nil {
128 // As long as pkg is non-nil, avoid redundant errors related to failed
129 // imports. TestStdlib will collect errors once for each package.
130 return pkg, nil
131 }
132 return nil, err
133 }
134
135 // getDirPackage gets the package defined in dir from the future cache.
136 //
137 // If this is the first goroutine requesting the package, getDirPackage
138 // type-checks.
139 func (c *stdlibChecker) getDirPackage(dir string) (*Package, error) {
140 c.mu.Lock()
141 fut, ok := c.pkgs[dir]
142 if !ok {
143 // First request for this package dir; type check.
144 fut = &futurePackage{
145 done: make(chan struct{}),
146 }
147 c.pkgs[dir] = fut
148 files, ok := c.dirFiles[dir]
149 c.mu.Unlock()
150 if !ok {
151 fut.err = fmt.Errorf("no files for %s", dir)
152 } else {
153 // Using dir as the package path here may be inconsistent with the behavior
154 // of a normal importer, but is sufficient as dir is by construction unique
155 // to this package.
156 fut.pkg, fut.err = typecheckFiles(dir, files, c)
157 }
158 close(fut.done)
159 } else {
160 // Otherwise, await the result.
161 c.mu.Unlock()
162 <-fut.done
163 }
164 return fut.pkg, fut.err
165 }
166
167 // firstComment returns the contents of the first non-empty comment in
168 // the given file, "skip", or the empty string. No matter the present
169 // comments, if any of them contains a build tag, the result is always
170 // "skip". Only comments before the "package" token and within the first
171 // 4K of the file are considered.
172 func firstComment(filename string) string {
173 f, err := os.Open(filename)
174 if err != nil {
175 return ""
176 }
177 defer f.Close()
178
179 var src [4 << 10]byte // read at most 4KB
180 n, _ := f.Read(src[:])
181
182 var first string
183 var s scanner.Scanner
184 s.Init(fset.AddFile("", fset.Base(), n), src[:n], nil /* ignore errors */, scanner.ScanComments)
185 for {
186 _, tok, lit := s.Scan()
187 switch tok {
188 case token.COMMENT:
189 // remove trailing */ of multi-line comment
190 if lit[1] == '*' {
191 lit = lit[:len(lit)-2]
192 }
193 contents := strings.TrimSpace(lit[2:])
194 if strings.HasPrefix(contents, "go:build ") {
195 return "skip"
196 }
197 if first == "" {
198 first = contents // contents may be "" but that's ok
199 }
200 // continue as we may still see build tags
201
202 case token.PACKAGE, token.EOF:
203 return first
204 }
205 }
206 }
207
208 func testTestDir(t *testing.T, path string, ignore ...string) {
209 files, err := os.ReadDir(path)
210 if err != nil {
211 // cmd/distpack deletes GOROOT/test, so skip the test if it isn't present.
212 // cmd/distpack also requires GOROOT/VERSION to exist, so use that to
213 // suppress false-positive skips.
214 if _, err := os.Stat(filepath.Join(testenv.GOROOT(t), "test")); os.IsNotExist(err) {
215 if _, err := os.Stat(filepath.Join(testenv.GOROOT(t), "VERSION")); err == nil {
216 t.Skipf("skipping: GOROOT/test not present")
217 }
218 }
219 t.Fatal(err)
220 }
221
222 excluded := make(map[string]bool)
223 for _, filename := range ignore {
224 excluded[filename] = true
225 }
226
227 fset := token.NewFileSet()
228 for _, f := range files {
229 // filter directory contents
230 if f.IsDir() || !strings.HasSuffix(f.Name(), ".go") || excluded[f.Name()] {
231 continue
232 }
233
234 // get per-file instructions
235 expectErrors := false
236 filename := filepath.Join(path, f.Name())
237 goVersion := ""
238 if comment := firstComment(filename); comment != "" {
239 if strings.Contains(comment, "-goexperiment") {
240 continue // ignore this file
241 }
242 fields := strings.Fields(comment)
243 switch fields[0] {
244 case "skip", "compiledir":
245 continue // ignore this file
246 case "errorcheck":
247 expectErrors = true
248 for _, arg := range fields[1:] {
249 if arg == "-0" || arg == "-+" || arg == "-std" {
250 // Marked explicitly as not expecting errors (-0),
251 // or marked as compiling runtime/stdlib, which is only done
252 // to trigger runtime/stdlib-only error output.
253 // In both cases, the code should typecheck.
254 expectErrors = false
255 break
256 }
257 const prefix = "-lang="
258 if strings.HasPrefix(arg, prefix) {
259 goVersion = arg[len(prefix):]
260 }
261 }
262 }
263 }
264
265 // parse and type-check file
266 file, err := parser.ParseFile(fset, filename, nil, 0)
267 if err == nil {
268 conf := Config{
269 GoVersion: goVersion,
270 Importer: stdLibImporter,
271 }
272 _, err = conf.Check(filename, fset, []*ast.File{file}, nil)
273 }
274
275 if expectErrors {
276 if err == nil {
277 t.Errorf("expected errors but found none in %s", filename)
278 }
279 } else {
280 if err != nil {
281 t.Error(err)
282 }
283 }
284 }
285 }
286
287 func TestStdTest(t *testing.T) {
288 testenv.MustHaveGoBuild(t)
289
290 if testing.Short() && testenv.Builder() == "" {
291 t.Skip("skipping in short mode")
292 }
293
294 testTestDir(t, filepath.Join(testenv.GOROOT(t), "test"),
295 "cmplxdivide.go", // also needs file cmplxdivide1.go - ignore
296 "directive.go", // tests compiler rejection of bad directive placement - ignore
297 "directive2.go", // tests compiler rejection of bad directive placement - ignore
298 "embedfunc.go", // tests //go:embed
299 "embedvers.go", // tests //go:embed
300 "linkname2.go", // go/types doesn't check validity of //go:xxx directives
301 "linkname3.go", // go/types doesn't check validity of //go:xxx directives
302 )
303 }
304
305 func TestStdFixed(t *testing.T) {
306 testenv.MustHaveGoBuild(t)
307
308 if testing.Short() && testenv.Builder() == "" {
309 t.Skip("skipping in short mode")
310 }
311
312 testTestDir(t, filepath.Join(testenv.GOROOT(t), "test", "fixedbugs"),
313 "bug248.go", "bug302.go", "bug369.go", // complex test instructions - ignore
314 "bug398.go", // go/types doesn't check for anonymous interface cycles (go.dev/issue/56103)
315 "issue6889.go", // gc-specific test
316 "issue11362.go", // canonical import path check
317 "issue16369.go", // go/types handles this correctly - not an issue
318 "issue18459.go", // go/types doesn't check validity of //go:xxx directives
319 "issue18882.go", // go/types doesn't check validity of //go:xxx directives
320 "issue20027.go", // go/types does not have constraints on channel element size
321 "issue20529.go", // go/types does not have constraints on stack size
322 "issue22200.go", // go/types does not have constraints on stack size
323 "issue22200b.go", // go/types does not have constraints on stack size
324 "issue25507.go", // go/types does not have constraints on stack size
325 "issue20780.go", // go/types does not have constraints on stack size
326 "bug251.go", // go.dev/issue/34333 which was exposed with fix for go.dev/issue/34151
327 "issue42058a.go", // go/types does not have constraints on channel element size
328 "issue42058b.go", // go/types does not have constraints on channel element size
329 "issue48097.go", // go/types doesn't check validity of //go:xxx directives, and non-init bodyless function
330 "issue48230.go", // go/types doesn't check validity of //go:xxx directives
331 "issue49767.go", // go/types does not have constraints on channel element size
332 "issue49814.go", // go/types does not have constraints on array size
333 "issue56103.go", // anonymous interface cycles; will be a type checker error in 1.22
334 "issue52697.go", // go/types does not have constraints on stack size
335 "issue68054.go", // this test requires GODEBUG=gotypesalias=1
336 "issue68580.go", // this test requires GODEBUG=gotypesalias=1
337 "issue73309.go", // this test requires GODEBUG=gotypesalias=1
338 "issue73309b.go", // this test requires GODEBUG=gotypesalias=1
339
340 // These tests requires runtime/cgo.Incomplete, which is only available on some platforms.
341 // However, go/types does not know about build constraints.
342 "bug514.go",
343 "issue40954.go",
344 "issue42032.go",
345 "issue42076.go",
346 "issue46903.go",
347 "issue51733.go",
348 "notinheap2.go",
349 "notinheap3.go",
350 )
351 }
352
353 func TestStdKen(t *testing.T) {
354 testenv.MustHaveGoBuild(t)
355
356 testTestDir(t, filepath.Join(testenv.GOROOT(t), "test", "ken"))
357 }
358
359 // Package paths of excluded packages.
360 var excluded = map[string]bool{
361 "builtin": true,
362 "cmd/compile/internal/ssa/_gen": true,
363 "runtime/_mkmalloc": true,
364 "simd/archsimd/_gen/simdgen": true,
365 "simd/archsimd/_gen/unify": true,
366 }
367
368 // printPackageMu synchronizes the printing of type-checked package files in
369 // the typecheckFiles function.
370 //
371 // Without synchronization, package files may be interleaved during concurrent
372 // type-checking.
373 var printPackageMu sync.Mutex
374
375 // typecheckFiles typechecks the given package files.
376 func typecheckFiles(path string, filenames []string, importer Importer) (*Package, error) {
377 fset := token.NewFileSet()
378
379 // Parse package files.
380 var files []*ast.File
381 for _, filename := range filenames {
382 file, err := parser.ParseFile(fset, filename, nil, parser.AllErrors)
383 if err != nil {
384 return nil, err
385 }
386
387 files = append(files, file)
388 }
389
390 if testing.Verbose() {
391 printPackageMu.Lock()
392 fmt.Println("package", files[0].Name.Name)
393 for _, filename := range filenames {
394 fmt.Println("\t", filename)
395 }
396 printPackageMu.Unlock()
397 }
398
399 // Typecheck package files.
400 var errs []error
401 conf := Config{
402 Error: func(err error) {
403 errs = append(errs, err)
404 },
405 Importer: importer,
406 }
407 info := Info{Uses: make(map[*ast.Ident]Object)}
408 pkg, _ := conf.Check(path, fset, files, &info)
409 err := errors.Join(errs...)
410 if err != nil {
411 return pkg, err
412 }
413
414 // Perform checks of API invariants.
415
416 // All Objects have a package, except predeclared ones.
417 errorError := Universe.Lookup("error").Type().Underlying().(*Interface).ExplicitMethod(0) // (error).Error
418 for id, obj := range info.Uses {
419 predeclared := obj == Universe.Lookup(obj.Name()) || obj == errorError
420 if predeclared == (obj.Pkg() != nil) {
421 posn := fset.Position(id.Pos())
422 if predeclared {
423 return nil, fmt.Errorf("%s: predeclared object with package: %s", posn, obj)
424 } else {
425 return nil, fmt.Errorf("%s: user-defined object without package: %s", posn, obj)
426 }
427 }
428 }
429
430 return pkg, nil
431 }
432
433 // pkgFilenames returns the list of package filenames for the given directory.
434 func pkgFilenames(dir string, includeTest bool) ([]string, error) {
435 ctxt := build.Default
436 ctxt.CgoEnabled = false
437 pkg, err := ctxt.ImportDir(dir, 0)
438 if err != nil {
439 if _, nogo := err.(*build.NoGoError); nogo {
440 return nil, nil // no *.go files, not an error
441 }
442 return nil, err
443 }
444 if excluded[pkg.ImportPath] {
445 return nil, nil
446 }
447 if slices.Contains(strings.Split(pkg.ImportPath, "/"), "_asm") {
448 // Submodules where not all dependencies are available.
449 // See go.dev/issue/46027.
450 return nil, nil
451 }
452 var filenames []string
453 for _, name := range pkg.GoFiles {
454 filenames = append(filenames, filepath.Join(pkg.Dir, name))
455 }
456 if includeTest {
457 for _, name := range pkg.TestGoFiles {
458 filenames = append(filenames, filepath.Join(pkg.Dir, name))
459 }
460 }
461 return filenames, nil
462 }
463
464 func walkPkgDirs(dir string, pkgh func(dir string, filenames []string), errh func(args ...any)) {
465 w := walker{pkgh, errh}
466 w.walk(dir)
467 }
468
469 type walker struct {
470 pkgh func(dir string, filenames []string)
471 errh func(args ...any)
472 }
473
474 func (w *walker) walk(dir string) {
475 files, err := os.ReadDir(dir)
476 if err != nil {
477 w.errh(err)
478 return
479 }
480
481 // apply pkgh to the files in directory dir
482
483 // Don't get test files as these packages are imported.
484 pkgFiles, err := pkgFilenames(dir, false)
485 if err != nil {
486 w.errh(err)
487 return
488 }
489 if pkgFiles != nil {
490 w.pkgh(dir, pkgFiles)
491 }
492
493 // traverse subdirectories, but don't walk into testdata
494 for _, f := range files {
495 if f.IsDir() && f.Name() != "testdata" {
496 w.walk(filepath.Join(dir, f.Name()))
497 }
498 }
499 }