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(fset, srcFilename, nil, parser.ParseComments)
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, fset, 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 "assignments.go": func(f *ast.File) {
105 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
106 renameSelectorExprs(f, "syntax.Name->ast.Ident", "ident.Value->ident.Name", "ast.Pos->token.Pos")
107 renameIdents(f, "syntax->ast", "poser->positioner", "nopos->noposn")
108 },
109 "array.go": nil,
110 "api_predicates.go": nil,
111 "basic.go": nil,
112 "builtins.go": func(f *ast.File) {
113 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
114 renameIdents(f, "syntax->ast")
115 renameSelectors(f, "ArgList->Args")
116 fixSelValue(f)
117 fixAtPosCall(f)
118 },
119 "builtins_test.go": func(f *ast.File) {
120 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`, `"cmd/compile/internal/types2"->"go/types"`)
121 renameSelectorExprs(f, "syntax.Name->ast.Ident", "p.Value->p.Name")
122 renameIdents(f, "syntax->ast")
123 },
124 "chan.go": nil,
125 "const.go": fixTokenPos,
126 "context.go": nil,
127 "context_test.go": nil,
128 "conversions.go": nil,
129 "cycles.go": func(f *ast.File) {
130 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
131 renameSelectorExprs(f, "syntax.Name->ast.Ident", "rhs.Value->rhs.Name")
132 renameSelectors(f, "Trace->_Trace")
133 },
134 "errors_test.go": func(f *ast.File) { renameIdents(f, "nopos->noposn") },
135 "errsupport.go": nil,
136 "gccgosizes.go": nil,
137 "gcsizes.go": func(f *ast.File) { renameIdents(f, "IsSyncAtomicAlign64->_IsSyncAtomicAlign64") },
138 "hilbert_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"->"go/types"`) },
139 "infer.go": func(f *ast.File) { fixTokenPos(f); fixInferSig(f) },
140 "initorder.go": nil,
141
142 "instantiate.go": func(f *ast.File) { fixTokenPos(f); fixCheckErrorfCall(f) },
143 "instantiate_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"->"go/types"`) },
144 "literals.go": func(f *ast.File) {
145 insertImportPath(f, `"go/token"`)
146 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
147 renameSelectorExprs(f,
148 "syntax.IntLit->token.INT", "syntax.FloatLit->token.FLOAT", "syntax.ImagLit->token.IMAG",
149 "syntax.Name->ast.Ident", "key.Value->key.Name", "atyp.Elem->atyp.Elt")
150 renameIdents(f, "syntax->ast")
151 renameSelectors(f, "ElemList->Elts")
152 },
153 "lookup.go": fixTokenPos,
154 "main_test.go": nil,
155 "map.go": nil,
156 "mono.go": func(f *ast.File) {
157 fixTokenPos(f)
158 insertImportPath(f, `"go/ast"`)
159 renameSelectorExprs(f, "syntax.Expr->ast.Expr")
160 },
161 "named.go": func(f *ast.File) { fixTokenPos(f); renameSelectors(f, "Trace->_Trace") },
162 "object.go": func(f *ast.File) { fixTokenPos(f); renameIdents(f, "NewTypeNameLazy->_NewTypeNameLazy") },
163
164
165 "objset.go": nil,
166 "operand.go": func(f *ast.File) {
167 insertImportPath(f, `"go/token"`)
168 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
169 renameSelectorExprs(f,
170 "syntax.Pos->token.Pos", "syntax.LitKind->token.Token",
171 "syntax.IntLit->token.INT", "syntax.FloatLit->token.FLOAT",
172 "syntax.ImagLit->token.IMAG", "syntax.RuneLit->token.CHAR",
173 "syntax.StringLit->token.STRING")
174 renameIdents(f, "syntax->ast")
175 },
176 "package.go": nil,
177 "pointer.go": nil,
178 "predicates.go": nil,
179 "range.go": func(f *ast.File) {
180 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
181 renameSelectorExprs(f, "syntax.Name->ast.Ident", "syntax.ForStmt->ast.RangeStmt", "ident.Value->ident.Name")
182 renameIdents(f, "syntax->ast", "poser->positioner")
183 },
184 "recording.go": func(f *ast.File) {
185 renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
186 renameSelectorExprs(f, "syntax.Name->ast.Ident")
187 renameIdents(f, "syntax->ast")
188 fixAtPosCall(f)
189 },
190 "scope.go": func(f *ast.File) { fixTokenPos(f); renameIdents(f, "InsertLazy->_InsertLazy") },
191 "selection.go": nil,
192 "sizes.go": func(f *ast.File) { renameIdents(f, "IsSyncAtomicAlign64->_IsSyncAtomicAlign64") },
193 "slice.go": nil,
194 "subst.go": func(f *ast.File) { fixTokenPos(f); renameSelectors(f, "Trace->_Trace") },
195 "termlist.go": nil,
196 "termlist_test.go": nil,
197 "trie.go": nil,
198 "trie_test.go": nil,
199 "tuple.go": nil,
200 "typelists.go": nil,
201 "typeset.go": func(f *ast.File) { fixTokenPos(f); renameSelectors(f, "Trace->_Trace") },
202 "typeparam.go": nil,
203 "typeterm_test.go": nil,
204 "typeterm.go": nil,
205 "typestring.go": nil,
206 "under.go": nil,
207 "unify.go": fixSprintf,
208 "universe.go": fixGlobalTypVarDecl,
209 "util_test.go": fixTokenPos,
210 "validtype.go": func(f *ast.File) { fixTokenPos(f); renameSelectors(f, "Trace->_Trace") },
211 }
212
213
214
215
216
217 type renameMap map[string]string
218
219
220 func makeRenameMap(renames ...string) renameMap {
221 m := make(renameMap)
222 for _, r := range renames {
223 s := strings.Split(r, "->")
224 if len(s) != 2 {
225 panic("invalid rename entry: " + r)
226 }
227 m[s[0]] = s[1]
228 }
229 return m
230 }
231
232
233 func (m renameMap) rename(s *string) {
234 if r, ok := m[*s]; ok {
235 *s = r
236 }
237 }
238
239
240
241 func (m renameMap) renameSel(n *ast.SelectorExpr) {
242 if a, _ := n.X.(*ast.Ident); a != nil {
243 a_x := a.Name + "." + n.Sel.Name
244 if r, ok := m[a_x]; ok {
245 b_y := strings.Split(r, ".")
246 if len(b_y) != 2 {
247 panic("invalid selector expression: " + r)
248 }
249 a.Name = b_y[0]
250 n.Sel.Name = b_y[1]
251 }
252 }
253 }
254
255
256
257 func renameIdents(f *ast.File, renames ...string) {
258 m := makeRenameMap(renames...)
259 ast.Inspect(f, func(n ast.Node) bool {
260 switch n := n.(type) {
261 case *ast.Ident:
262 m.rename(&n.Name)
263 return false
264 }
265 return true
266 })
267 }
268
269
270 func renameSelectors(f *ast.File, renames ...string) {
271 m := makeRenameMap(renames...)
272 ast.Inspect(f, func(n ast.Node) bool {
273 switch n := n.(type) {
274 case *ast.SelectorExpr:
275 m.rename(&n.Sel.Name)
276 return false
277 }
278 return true
279 })
280
281 }
282
283
284
285 func renameSelectorExprs(f *ast.File, renames ...string) {
286 m := makeRenameMap(renames...)
287 ast.Inspect(f, func(n ast.Node) bool {
288 switch n := n.(type) {
289 case *ast.SelectorExpr:
290 m.renameSel(n)
291 return false
292 }
293 return true
294 })
295 }
296
297
298 func renameImportPath(f *ast.File, renames ...string) {
299 m := makeRenameMap(renames...)
300 ast.Inspect(f, func(n ast.Node) bool {
301 switch n := n.(type) {
302 case *ast.ImportSpec:
303 if n.Path.Kind != token.STRING {
304 panic("invalid import path")
305 }
306 m.rename(&n.Path.Value)
307 return false
308 }
309 return true
310 })
311 }
312
313
314
315 func insertImportPath(f *ast.File, path string) {
316 for _, d := range f.Decls {
317 if g, _ := d.(*ast.GenDecl); g != nil && g.Tok == token.IMPORT {
318 g.Specs = append(g.Specs, &ast.ImportSpec{Path: &ast.BasicLit{ValuePos: g.End(), Kind: token.STRING, Value: path}})
319 return
320 }
321 }
322 panic("no import declaration present")
323 }
324
325
326
327 func fixTokenPos(f *ast.File) {
328 m := makeRenameMap(`"cmd/compile/internal/syntax"->"go/token"`, "syntax.Pos->token.Pos", "IsKnown->IsValid")
329 ast.Inspect(f, func(n ast.Node) bool {
330 switch n := n.(type) {
331 case *ast.ImportSpec:
332
333 if n.Path.Kind != token.STRING {
334 panic("invalid import path")
335 }
336 m.rename(&n.Path.Value)
337 return false
338 case *ast.SelectorExpr:
339
340 m.renameSel(n)
341 case *ast.CallExpr:
342
343 if fun, _ := n.Fun.(*ast.SelectorExpr); fun != nil && len(n.Args) == 0 {
344 m.rename(&fun.Sel.Name)
345 return false
346 }
347 }
348 return true
349 })
350 }
351
352
353 func fixSelValue(f *ast.File) {
354 ast.Inspect(f, func(n ast.Node) bool {
355 switch n := n.(type) {
356 case *ast.SelectorExpr:
357 if n.Sel.Name == "Value" {
358 if selx, _ := n.X.(*ast.SelectorExpr); selx != nil && selx.Sel.Name == "Sel" {
359 n.Sel.Name = "Name"
360 return false
361 }
362 }
363 }
364 return true
365 })
366 }
367
368
369
370
371 func fixInferSig(f *ast.File) {
372 ast.Inspect(f, func(n ast.Node) bool {
373 switch n := n.(type) {
374 case *ast.FuncDecl:
375 if n.Name.Name == "infer" {
376
377 par := n.Type.Params.List[0]
378 if len(par.Names) == 1 && par.Names[0].Name == "pos" {
379 par.Names[0] = newIdent(par.Names[0].Pos(), "posn")
380 par.Type = newIdent(par.Type.Pos(), "positioner")
381 return true
382 }
383 }
384 case *ast.CallExpr:
385 if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil {
386 switch selx.Sel.Name {
387 case "renameTParams":
388
389 if isIdent(n.Args[0], "pos") {
390 pos := n.Args[0].Pos()
391 fun := &ast.SelectorExpr{X: newIdent(pos, "posn"), Sel: newIdent(pos, "Pos")}
392 arg := &ast.CallExpr{Fun: fun, Lparen: pos, Args: nil, Ellipsis: token.NoPos, Rparen: pos}
393 n.Args[0] = arg
394 return false
395 }
396 case "addf":
397
398 if isIdent(n.Args[0], "pos") {
399 pos := n.Args[0].Pos()
400 arg := newIdent(pos, "posn")
401 n.Args[0] = arg
402 return false
403 }
404 case "allowVersion":
405
406 if isIdent(n.Args[0], "pos") {
407 pos := n.Args[0].Pos()
408 arg := newIdent(pos, "posn")
409 n.Args[0] = arg
410 return false
411 }
412 }
413 }
414 }
415 return true
416 })
417 }
418
419
420
421 func fixAtPosCall(f *ast.File) {
422 ast.Inspect(f, func(n ast.Node) bool {
423 switch n := n.(type) {
424 case *ast.CallExpr:
425 if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil && selx.Sel.Name == "dump" {
426 for i, arg := range n.Args {
427 if call, _ := arg.(*ast.CallExpr); call != nil {
428
429 if isIdent(call.Fun, "atPos") {
430 pos := call.Args[0].Pos()
431 fun := &ast.SelectorExpr{X: call.Args[0], Sel: newIdent(pos, "Pos")}
432 n.Args[i] = &ast.CallExpr{Fun: fun, Lparen: pos, Rparen: pos}
433 return false
434 }
435 }
436 }
437 }
438 }
439 return true
440 })
441 }
442
443
444 func fixErrErrorfCall(f *ast.File) {
445 ast.Inspect(f, func(n ast.Node) bool {
446 switch n := n.(type) {
447 case *ast.CallExpr:
448 if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil {
449 if isIdent(selx.X, "err") {
450 switch selx.Sel.Name {
451 case "errorf":
452
453 if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "obj" {
454 pos := n.Args[0].Pos()
455 fun := &ast.SelectorExpr{X: ident, Sel: newIdent(pos, "Pos")}
456 n.Args[0] = &ast.CallExpr{Fun: fun, Lparen: pos, Rparen: pos}
457 return false
458 }
459 }
460 }
461 }
462 }
463 return true
464 })
465 }
466
467
468 func fixCheckErrorfCall(f *ast.File) {
469 ast.Inspect(f, func(n ast.Node) bool {
470 switch n := n.(type) {
471 case *ast.CallExpr:
472 if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil {
473 if isIdent(selx.X, "check") {
474 switch selx.Sel.Name {
475 case "errorf":
476
477 if ident := asIdent(n.Args[0], "pos"); ident != nil {
478 pos := n.Args[0].Pos()
479 fun := newIdent(pos, "atPos")
480 n.Args[0] = &ast.CallExpr{Fun: fun, Lparen: pos, Args: []ast.Expr{ident}, Rparen: pos}
481 return false
482 }
483 }
484 }
485 }
486 }
487 return true
488 })
489 }
490
491
492
493
494 func fixGlobalTypVarDecl(f *ast.File) {
495 ast.Inspect(f, func(n ast.Node) bool {
496 switch n := n.(type) {
497 case *ast.ValueSpec:
498
499 if len(n.Names) == 1 && n.Names[0].Name == "Typ" && len(n.Values) == 1 {
500 n.Values[0].(*ast.CompositeLit).Type.(*ast.ArrayType).Len = nil
501 return false
502 }
503 }
504 return true
505 })
506 }
507
508
509 func fixSprintf(f *ast.File) {
510 ast.Inspect(f, func(n ast.Node) bool {
511 switch n := n.(type) {
512 case *ast.CallExpr:
513 if isIdent(n.Fun, "sprintf") && len(n.Args) >= 4 {
514 n.Args = insert(n.Args, 1, newIdent(n.Args[1].Pos(), "nil"))
515 return false
516 }
517 }
518 return true
519 })
520 }
521
522
523 func asIdent(x ast.Node, name string) *ast.Ident {
524 if ident, _ := x.(*ast.Ident); ident != nil && ident.Name == name {
525 return ident
526 }
527 return nil
528 }
529
530
531 func isIdent(x ast.Node, name string) bool {
532 return asIdent(x, name) != nil
533 }
534
535
536 func newIdent(pos token.Pos, name string) *ast.Ident {
537 id := ast.NewIdent(name)
538 id.NamePos = pos
539 return id
540 }
541
542
543 func insert(list []ast.Expr, at int, x ast.Expr) []ast.Expr {
544 list = append(list, nil)
545 copy(list[at+1:], list[at:])
546 list[at] = x
547 return list
548 }
549
View as plain text