Source file
src/go/types/generate_test.go
1
2
3
4
5
6
7
8 package types_test
9
10 import (
11 "bytes"
12 "flag"
13 "fmt"
14 "go/ast"
15 "go/format"
16 "go/parser"
17 "go/token"
18 "internal/diff"
19 "os"
20 "path/filepath"
21 "runtime"
22 "strings"
23 "testing"
24 )
25
26 var filesToWrite = flag.String("write", "", `go/types files to generate, or "all" for all files`)
27
28 const (
29 srcDir = "/src/cmd/compile/internal/types2/"
30 dstDir = "/src/go/types/"
31 )
32
33
34
35
36 func TestGenerate(t *testing.T) {
37
38
39 write := *filesToWrite != ""
40 var files []string
41 if *filesToWrite != "" && *filesToWrite != "all" {
42 files = strings.Split(*filesToWrite, ",")
43 } else {
44 for file := range filemap {
45 files = append(files, file)
46 }
47 }
48
49 for _, filename := range files {
50 generate(t, filename, write)
51 }
52 }
53
54 func generate(t *testing.T, filename string, write bool) {
55
56 srcFilename := filepath.FromSlash(runtime.GOROOT() + srcDir + filename)
57 file, err := parser.ParseFile(testFSet, srcFilename, nil, parser.ParseComments|parser.SkipObjectResolution)
58 if err != nil {
59 t.Fatal(err)
60 }
61
62
63 file.Name.Name = strings.ReplaceAll(file.Name.Name, "types2", "types")
64
65
66 if action := filemap[filename]; action != nil {
67 action(file)
68 }
69
70
71 var buf bytes.Buffer
72 rel, _ := filepath.Rel(dstDir, srcDir)
73 fmt.Fprintf(&buf, "// Code generated by \"go test -run=Generate -write=all\"; DO NOT EDIT.\n")
74 fmt.Fprintf(&buf, "// Source: %s/%s\n\n", filepath.ToSlash(rel), filename)
75 if err := format.Node(&buf, testFSet, file); err != nil {
76 t.Fatal(err)
77 }
78 generatedContent := buf.Bytes()
79
80
81 dstFilename := filepath.FromSlash(runtime.GOROOT() + dstDir + filename)
82 onDiskContent, err := os.ReadFile(dstFilename)
83 if err != nil {
84 t.Fatalf("reading %q: %v", filename, err)
85 }
86
87
88 if d := diff.Diff(filename+" (on disk in "+dstDir+")", onDiskContent, filename+" (generated from "+srcDir+")", generatedContent); d != nil {
89 if write {
90 t.Logf("applying change:\n%s", d)
91 if err := os.WriteFile(dstFilename, generatedContent, 0o644); err != nil {
92 t.Fatalf("writing %q: %v", filename, err)
93 }
94 } else {
95 t.Errorf("file on disk in %s is stale:\n%s", dstDir, d)
96 }
97 }
98 }
99
100 type action func(in *ast.File)
101
102 var filemap = map[string]action{
103 "alias.go": fixTokenPos,
104 "alias_test.go": func(f *ast.File) {
105 renameImportPath(f, `"cmd/compile/internal/types2"->"go/types"`)
106 renameIdents(f, "types2->types")
107 },
108 "assignments.go": func(f *ast.File) {
109 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
110 renameSelectorExprs(f, "syntax.Name->ast.Ident", "ident.Value->ident.Name", "ast.Pos->token.Pos")
111 renameIdents(f, "syntax->ast", "poser->positioner", "nopos->noposn")
112 },
113 "array.go": nil,
114 "api_predicates.go": nil,
115 "basic.go": nil,
116 "builtins.go": func(f *ast.File) {
117 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
118 renameIdents(f, "syntax->ast")
119 renameSelectors(f, "ArgList->Args")
120 fixSelValue(f)
121 fixAtPosCall(f)
122 },
123 "builtins_test.go": func(f *ast.File) {
124 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`, `"cmd/compile/internal/types2"->"go/types"`)
125 renameSelectorExprs(f, "syntax.Name->ast.Ident", "p.Value->p.Name")
126 renameIdents(f, "syntax->ast")
127 },
128 "chan.go": nil,
129 "const.go": fixTokenPos,
130 "context.go": nil,
131 "context_test.go": nil,
132 "conversions.go": nil,
133 "cycles.go": func(f *ast.File) {
134 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
135 renameSelectorExprs(f, "syntax.Name->ast.Ident", "rhsName.Value->rhsName.Name")
136 renameSelectors(f, "Trace->_Trace")
137 },
138 "errorcalls_test.go": func(f *ast.File) {
139 insertImportPath(f, `"go/token"`)
140 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
141 renameSelectorExprs(f, "syntax.StringLit->token.STRING", "syntax.Name->ast.Ident", "n.Value->n.Name")
142 renameIdents(f, "syntax->ast")
143 renameSelectors(f, "ArgList->Args")
144 fixFSet(f)
145 },
146 "errors_test.go": func(f *ast.File) { renameIdents(f, "nopos->noposn") },
147 "errsupport.go": nil,
148 "gccgosizes.go": nil,
149 "gcsizes.go": func(f *ast.File) {
150 renameIdents(f, "IsSyncAtomicAlign64->_IsSyncAtomicAlign64", "IsSyncAtomicAlign128->_IsSyncAtomicAlign128")
151 },
152 "hilbert_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"->"go/types"`) },
153 "infer.go": func(f *ast.File) { fixTokenPos(f); fixInferSig(f) },
154 "initorder.go": nil,
155
156 "instantiate.go": func(f *ast.File) { fixTokenPos(f); fixCheckErrorfCall(f); fixSprintf(f) },
157 "instantiate_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"->"go/types"`) },
158 "literals.go": func(f *ast.File) {
159 insertImportPath(f, `"go/token"`)
160 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
161 renameSelectorExprs(f,
162 "syntax.IntLit->token.INT", "syntax.FloatLit->token.FLOAT", "syntax.ImagLit->token.IMAG",
163 "syntax.Name->ast.Ident", "key.Value->key.Name", "atyp.Elem->atyp.Elt")
164 renameIdents(f, "syntax->ast")
165 renameSelectors(f, "ElemList->Elts")
166 },
167 "lookup.go": fixTokenPos,
168 "lookup_test.go": func(f *ast.File) {
169 renameImportPath(f, `"cmd/compile/internal/types2"->"go/types"`)
170 fixFSet(f)
171 },
172 "main_test.go": nil,
173 "map.go": nil,
174 "mono.go": func(f *ast.File) {
175 fixTokenPos(f)
176 insertImportPath(f, `"go/ast"`)
177 renameSelectorExprs(f, "syntax.Expr->ast.Expr")
178 },
179 "mono_test.go": func(f *ast.File) {
180 renameImportPath(f, `"cmd/compile/internal/types2"->"go/types"`)
181 renameIdents(f, "types2->types")
182 fixFSet(f)
183 },
184 "named.go": func(f *ast.File) { fixTokenPos(f); renameSelectors(f, "Trace->_Trace") },
185 "object.go": func(f *ast.File) { fixTokenPos(f); renameIdents(f, "NewTypeNameLazy->_NewTypeNameLazy") },
186 "object_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"->"go/types"`) },
187 "objset.go": nil,
188 "operand.go": func(f *ast.File) {
189 insertImportPath(f, `"go/token"`)
190 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
191 renameSelectorExprs(f,
192 "syntax.Pos->token.Pos", "syntax.LitKind->token.Token",
193 "syntax.IntLit->token.INT", "syntax.FloatLit->token.FLOAT",
194 "syntax.ImagLit->token.IMAG", "syntax.RuneLit->token.CHAR",
195 "syntax.StringLit->token.STRING")
196 renameIdents(f, "syntax->ast")
197 },
198 "package.go": nil,
199 "pointer.go": nil,
200 "predicates.go": nil,
201 "range.go": func(f *ast.File) {
202 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
203 renameSelectorExprs(f, "syntax.Name->ast.Ident", "syntax.ForStmt->ast.RangeStmt", "ident.Value->ident.Name")
204 renameIdents(f, "syntax->ast", "poser->positioner")
205 },
206 "recording.go": func(f *ast.File) {
207 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
208 renameSelectorExprs(f, "syntax.Name->ast.Ident")
209 renameIdents(f, "syntax->ast")
210 fixAtPosCall(f)
211 },
212 "scope.go": func(f *ast.File) { fixTokenPos(f); renameIdents(f, "InsertLazy->_InsertLazy") },
213 "scope_test.go": func(f *ast.File) { fixTokenPos(f) },
214 "selection.go": nil,
215 "sizeof_test.go": nil,
216 "sizes.go": func(f *ast.File) {
217 renameIdents(f, "IsSyncAtomicAlign64->_IsSyncAtomicAlign64", "IsSyncAtomicAlign128->_IsSyncAtomicAlign128")
218 },
219 "sizes_test.go": func(f *ast.File) {
220 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`, `"cmd/compile/internal/types2"->"go/types"`)
221 renameSelectorExprs(f, "syntax.Expr->ast.Expr")
222 renameIdents(f, "types2->types")
223 fixFSet(f)
224 },
225 "slice.go": nil,
226 "subst.go": func(f *ast.File) { fixTokenPos(f); renameSelectors(f, "Trace->_Trace") },
227 "target.go": fixSprintf,
228 "termlist.go": nil,
229 "termlist_test.go": nil,
230 "trie.go": nil,
231 "trie_test.go": nil,
232 "tuple.go": nil,
233 "typelists.go": nil,
234 "typeset.go": func(f *ast.File) { fixTokenPos(f); renameSelectors(f, "Trace->_Trace") },
235 "typeset_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"->"go/types"`) },
236 "typeparam.go": nil,
237 "typeterm_test.go": nil,
238 "typeterm.go": nil,
239 "typestring.go": nil,
240 "typestring_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"->"go/types"`) },
241 "under.go": nil,
242 "unify.go": fixSprintf,
243 "union.go": func(f *ast.File) {
244 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
245 renameSelectorExprs(f, "syntax.Expr->ast.Expr")
246 },
247 "universe.go": func(f *ast.File) {
248 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
249 renameSelectorExprs(f, "syntax.CallExpr->ast.CallExpr")
250 fixGlobalTypVarDecl(f)
251 },
252 "util_test.go": fixTokenPos,
253 "validtype.go": func(f *ast.File) { fixTokenPos(f); renameSelectors(f, "Trace->_Trace") },
254 "version.go": func(f *ast.File) { renameIdents(f, "poser->positioner") },
255 }
256
257
258
259
260
261 type renameMap map[string]string
262
263
264 func makeRenameMap(renames ...string) renameMap {
265 m := make(renameMap)
266 for _, r := range renames {
267 s := strings.Split(r, "->")
268 if len(s) != 2 {
269 panic("invalid rename entry: " + r)
270 }
271 m[s[0]] = s[1]
272 }
273 return m
274 }
275
276
277 func (m renameMap) rename(s *string) {
278 if r, ok := m[*s]; ok {
279 *s = r
280 }
281 }
282
283
284
285 func (m renameMap) renameSel(n *ast.SelectorExpr) {
286 if a, _ := n.X.(*ast.Ident); a != nil {
287 a_x := a.Name + "." + n.Sel.Name
288 if r, ok := m[a_x]; ok {
289 b_y := strings.Split(r, ".")
290 if len(b_y) != 2 {
291 panic("invalid selector expression: " + r)
292 }
293 a.Name = b_y[0]
294 n.Sel.Name = b_y[1]
295 }
296 }
297 }
298
299
300
301 func renameIdents(f *ast.File, renames ...string) {
302 m := makeRenameMap(renames...)
303 ast.Inspect(f, func(n ast.Node) bool {
304 switch n := n.(type) {
305 case *ast.Ident:
306 m.rename(&n.Name)
307 return false
308 }
309 return true
310 })
311 }
312
313
314 func renameSelectors(f *ast.File, renames ...string) {
315 m := makeRenameMap(renames...)
316 ast.Inspect(f, func(n ast.Node) bool {
317 switch n := n.(type) {
318 case *ast.SelectorExpr:
319 m.rename(&n.Sel.Name)
320 return false
321 }
322 return true
323 })
324
325 }
326
327
328
329 func renameSelectorExprs(f *ast.File, renames ...string) {
330 m := makeRenameMap(renames...)
331 ast.Inspect(f, func(n ast.Node) bool {
332 switch n := n.(type) {
333 case *ast.SelectorExpr:
334 m.renameSel(n)
335 return false
336 }
337 return true
338 })
339 }
340
341
342 func renameImportPath(f *ast.File, renames ...string) {
343 m := makeRenameMap(renames...)
344 ast.Inspect(f, func(n ast.Node) bool {
345 switch n := n.(type) {
346 case *ast.ImportSpec:
347 if n.Path.Kind != token.STRING {
348 panic("invalid import path")
349 }
350 m.rename(&n.Path.Value)
351 return false
352 }
353 return true
354 })
355 }
356
357
358
359 func insertImportPath(f *ast.File, path string) {
360 for _, d := range f.Decls {
361 if g, _ := d.(*ast.GenDecl); g != nil && g.Tok == token.IMPORT {
362 g.Specs = append(g.Specs, &ast.ImportSpec{Path: &ast.BasicLit{ValuePos: g.End(), Kind: token.STRING, Value: path}})
363 return
364 }
365 }
366 panic("no import declaration present")
367 }
368
369
370
371 func fixTokenPos(f *ast.File) {
372 m := makeRenameMap(`"cmd/compile/internal/syntax"->"go/token"`, "syntax.Pos->token.Pos", "IsKnown->IsValid")
373 ast.Inspect(f, func(n ast.Node) bool {
374 switch n := n.(type) {
375 case *ast.ImportSpec:
376
377 if n.Path.Kind != token.STRING {
378 panic("invalid import path")
379 }
380 m.rename(&n.Path.Value)
381 return false
382 case *ast.SelectorExpr:
383
384 m.renameSel(n)
385 case *ast.CallExpr:
386
387 if fun, _ := n.Fun.(*ast.SelectorExpr); fun != nil && len(n.Args) == 0 {
388 m.rename(&fun.Sel.Name)
389 return false
390 }
391 }
392 return true
393 })
394 }
395
396
397 func fixSelValue(f *ast.File) {
398 ast.Inspect(f, func(n ast.Node) bool {
399 switch n := n.(type) {
400 case *ast.SelectorExpr:
401 if n.Sel.Name == "Value" {
402 if selx, _ := n.X.(*ast.SelectorExpr); selx != nil && selx.Sel.Name == "Sel" {
403 n.Sel.Name = "Name"
404 return false
405 }
406 }
407 }
408 return true
409 })
410 }
411
412
413
414
415 func fixInferSig(f *ast.File) {
416 ast.Inspect(f, func(n ast.Node) bool {
417 switch n := n.(type) {
418 case *ast.FuncDecl:
419 if n.Name.Name == "infer" {
420
421 par := n.Type.Params.List[0]
422 if len(par.Names) == 1 && par.Names[0].Name == "pos" {
423 par.Names[0] = newIdent(par.Names[0].Pos(), "posn")
424 par.Type = newIdent(par.Type.Pos(), "positioner")
425 return true
426 }
427 }
428 case *ast.CallExpr:
429 if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil {
430 switch selx.Sel.Name {
431 case "renameTParams":
432
433 if isIdent(n.Args[0], "pos") {
434 pos := n.Args[0].Pos()
435 fun := &ast.SelectorExpr{X: newIdent(pos, "posn"), Sel: newIdent(pos, "Pos")}
436 arg := &ast.CallExpr{Fun: fun, Lparen: pos, Args: nil, Ellipsis: token.NoPos, Rparen: pos}
437 n.Args[0] = arg
438 return false
439 }
440 case "addf":
441
442 if isIdent(n.Args[0], "pos") {
443 pos := n.Args[0].Pos()
444 arg := newIdent(pos, "posn")
445 n.Args[0] = arg
446 return false
447 }
448 case "allowVersion":
449
450 if isIdent(n.Args[0], "pos") {
451 pos := n.Args[0].Pos()
452 arg := newIdent(pos, "posn")
453 n.Args[0] = arg
454 return false
455 }
456 }
457 }
458 }
459 return true
460 })
461 }
462
463
464
465 func fixAtPosCall(f *ast.File) {
466 ast.Inspect(f, func(n ast.Node) bool {
467 switch n := n.(type) {
468 case *ast.CallExpr:
469 if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil && selx.Sel.Name == "dump" {
470 for i, arg := range n.Args {
471 if call, _ := arg.(*ast.CallExpr); call != nil {
472
473 if isIdent(call.Fun, "atPos") {
474 pos := call.Args[0].Pos()
475 fun := &ast.SelectorExpr{X: call.Args[0], Sel: newIdent(pos, "Pos")}
476 n.Args[i] = &ast.CallExpr{Fun: fun, Lparen: pos, Rparen: pos}
477 return false
478 }
479 }
480 }
481 }
482 }
483 return true
484 })
485 }
486
487
488 func fixErrErrorfCall(f *ast.File) {
489 ast.Inspect(f, func(n ast.Node) bool {
490 switch n := n.(type) {
491 case *ast.CallExpr:
492 if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil {
493 if isIdent(selx.X, "err") {
494 switch selx.Sel.Name {
495 case "errorf":
496
497 if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "obj" {
498 pos := n.Args[0].Pos()
499 fun := &ast.SelectorExpr{X: ident, Sel: newIdent(pos, "Pos")}
500 n.Args[0] = &ast.CallExpr{Fun: fun, Lparen: pos, Rparen: pos}
501 return false
502 }
503 }
504 }
505 }
506 }
507 return true
508 })
509 }
510
511
512 func fixCheckErrorfCall(f *ast.File) {
513 ast.Inspect(f, func(n ast.Node) bool {
514 switch n := n.(type) {
515 case *ast.CallExpr:
516 if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil {
517 if isIdent(selx.X, "check") {
518 switch selx.Sel.Name {
519 case "errorf":
520
521 if ident := asIdent(n.Args[0], "pos"); ident != nil {
522 pos := n.Args[0].Pos()
523 fun := newIdent(pos, "atPos")
524 n.Args[0] = &ast.CallExpr{Fun: fun, Lparen: pos, Args: []ast.Expr{ident}, Rparen: pos}
525 return false
526 }
527 }
528 }
529 }
530 }
531 return true
532 })
533 }
534
535
536
537
538 func fixGlobalTypVarDecl(f *ast.File) {
539 ast.Inspect(f, func(n ast.Node) bool {
540 switch n := n.(type) {
541 case *ast.ValueSpec:
542
543 if len(n.Names) == 1 && n.Names[0].Name == "Typ" && len(n.Values) == 1 {
544 n.Values[0].(*ast.CompositeLit).Type.(*ast.ArrayType).Len = nil
545 return false
546 }
547 }
548 return true
549 })
550 }
551
552
553 func fixSprintf(f *ast.File) {
554 ast.Inspect(f, func(n ast.Node) bool {
555 switch n := n.(type) {
556 case *ast.CallExpr:
557 if isIdent(n.Fun, "sprintf") && len(n.Args) >= 4 {
558 n.Args = insert(n.Args, 1, newIdent(n.Args[1].Pos(), "nil"))
559 return false
560 }
561 }
562 return true
563 })
564 }
565
566
567
568
569 func fixFSet(f *ast.File) {
570 newFSet := func(pos token.Pos) *ast.Ident { return newIdent(pos, "testFSet") }
571 ast.Inspect(f, func(n ast.Node) bool {
572 switch n := n.(type) {
573 case *ast.CallExpr:
574 switch {
575 case isIdent(n.Fun, "defaultImporter"), isIdent(n.Fun, "pkgFiles"), isIdent(n.Fun, "mustParse"):
576 pos := n.Lparen
577 n.Args = insert(n.Args, 0, newFSet(pos))
578 return false
579 case isIdent(n.Fun, "NewChecker"):
580 pos := n.Args[0].Pos()
581 n.Args = insert(n.Args, 1, newFSet(pos))
582 return false
583 }
584 if sel, _ := n.Fun.(*ast.SelectorExpr); sel != nil {
585 switch sel.Sel.Name {
586 case "Check":
587 pos := n.Args[0].Pos()
588 n.Args = insert(n.Args, 1, newFSet(pos))
589 return false
590 case "Errorf":
591 for i, arg := range n.Args {
592 if call, _ := arg.(*ast.CallExpr); call != nil && len(call.Args) == 0 {
593 if sel, _ := call.Fun.(*ast.SelectorExpr); sel != nil && sel.Sel.Name == "Pos" {
594 pos := call.Pos()
595 fun := &ast.SelectorExpr{X: newFSet(pos), Sel: newIdent(pos, "Position")}
596 n.Args[i] = &ast.CallExpr{Fun: fun, Lparen: pos, Args: []ast.Expr{call}, Rparen: pos}
597 }
598 }
599 }
600 }
601 }
602 }
603 return true
604 })
605 }
606
607
608 func asIdent(x ast.Node, name string) *ast.Ident {
609 if ident, _ := x.(*ast.Ident); ident != nil && ident.Name == name {
610 return ident
611 }
612 return nil
613 }
614
615
616 func isIdent(x ast.Node, name string) bool {
617 return asIdent(x, name) != nil
618 }
619
620
621 func newIdent(pos token.Pos, name string) *ast.Ident {
622 id := ast.NewIdent(name)
623 id.NamePos = pos
624 return id
625 }
626
627
628 func insert(list []ast.Expr, at int, x ast.Expr) []ast.Expr {
629 list = append(list, nil)
630 copy(list[at+1:], list[at:])
631 list[at] = x
632 return list
633 }
634
View as plain text