]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/types/generate_test.go
go/types, types2: remove local version processing in favor of go/version
[gostls13.git] / src / go / types / generate_test.go
1 // Copyright 2023 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 implements a custom generator to create various go/types
6 // source files from the corresponding types2 files.
7
8 package types_test
9
10 import (
11         "bytes"
12         "flag"
13         "go/ast"
14         "go/format"
15         "go/parser"
16         "go/token"
17         "internal/diff"
18         "os"
19         "path/filepath"
20         "runtime"
21         "strings"
22         "testing"
23 )
24
25 var filesToWrite = flag.String("write", "", `go/types files to generate, or "all" for all files`)
26
27 const (
28         srcDir = "/src/cmd/compile/internal/types2/"
29         dstDir = "/src/go/types/"
30 )
31
32 // TestGenerate verifies that generated files in go/types match their types2
33 // counterpart. If -write is set, this test actually writes the expected
34 // content to go/types; otherwise, it just compares with the existing content.
35 func TestGenerate(t *testing.T) {
36         // If filesToWrite is set, write the generated content to disk.
37         // In the special case of "all", write all files in filemap.
38         write := *filesToWrite != ""
39         var files []string // files to process
40         if *filesToWrite != "" && *filesToWrite != "all" {
41                 files = strings.Split(*filesToWrite, ",")
42         } else {
43                 for file := range filemap {
44                         files = append(files, file)
45                 }
46         }
47
48         for _, filename := range files {
49                 generate(t, filename, write)
50         }
51 }
52
53 func generate(t *testing.T, filename string, write bool) {
54         // parse src
55         srcFilename := filepath.FromSlash(runtime.GOROOT() + srcDir + filename)
56         file, err := parser.ParseFile(fset, srcFilename, nil, parser.ParseComments)
57         if err != nil {
58                 t.Fatal(err)
59         }
60
61         // fix package name
62         file.Name.Name = strings.ReplaceAll(file.Name.Name, "types2", "types")
63
64         // rewrite AST as needed
65         if action := filemap[filename]; action != nil {
66                 action(file)
67         }
68
69         // format AST
70         var buf bytes.Buffer
71         buf.WriteString("// Code generated by \"go test -run=Generate -write=all\"; DO NOT EDIT.\n\n")
72         if err := format.Node(&buf, fset, file); err != nil {
73                 t.Fatal(err)
74         }
75         generatedContent := buf.Bytes()
76
77         dstFilename := filepath.FromSlash(runtime.GOROOT() + dstDir + filename)
78         onDiskContent, err := os.ReadFile(dstFilename)
79         if err != nil {
80                 t.Fatalf("reading %q: %v", filename, err)
81         }
82
83         if d := diff.Diff(filename+" (on disk)", onDiskContent, filename+" (generated)", generatedContent); d != nil {
84                 if write {
85                         t.Logf("applying change:\n%s", d)
86                         if err := os.WriteFile(dstFilename, generatedContent, 0o644); err != nil {
87                                 t.Fatalf("writing %q: %v", filename, err)
88                         }
89                 } else {
90                         t.Errorf("generated file content does not match:\n%s", string(d))
91                 }
92         }
93 }
94
95 type action func(in *ast.File)
96
97 var filemap = map[string]action{
98         "alias.go":        nil,
99         "array.go":        nil,
100         "basic.go":        nil,
101         "chan.go":         nil,
102         "const.go":        func(f *ast.File) { fixTokenPos(f) },
103         "context.go":      nil,
104         "context_test.go": nil,
105         "gccgosizes.go":   nil,
106         "gcsizes.go":      func(f *ast.File) { renameIdent(f, "IsSyncAtomicAlign64", "_IsSyncAtomicAlign64") },
107         "hilbert_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"`, `"go/types"`) },
108         "infer.go": func(f *ast.File) {
109                 fixTokenPos(f)
110                 fixInferSig(f)
111         },
112         // "initorder.go": fixErrErrorfCall, // disabled for now due to unresolved error_ use implications for gopls
113         "instantiate.go":      func(f *ast.File) { fixTokenPos(f); fixCheckErrorfCall(f) },
114         "instantiate_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"`, `"go/types"`) },
115         "lookup.go":           func(f *ast.File) { fixTokenPos(f) },
116         "main_test.go":        nil,
117         "map.go":              nil,
118         "named.go":            func(f *ast.File) { fixTokenPos(f); fixTraceSel(f) },
119         "object.go":           func(f *ast.File) { fixTokenPos(f); renameIdent(f, "NewTypeNameLazy", "_NewTypeNameLazy") },
120         "object_test.go":      func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"`, `"go/types"`) },
121         "objset.go":           nil,
122         "package.go":          nil,
123         "pointer.go":          nil,
124         "predicates.go":       nil,
125         "scope.go": func(f *ast.File) {
126                 fixTokenPos(f)
127                 renameIdent(f, "Squash", "squash")
128                 renameIdent(f, "InsertLazy", "_InsertLazy")
129         },
130         "selection.go":     nil,
131         "sizes.go":         func(f *ast.File) { renameIdent(f, "IsSyncAtomicAlign64", "_IsSyncAtomicAlign64") },
132         "slice.go":         nil,
133         "subst.go":         func(f *ast.File) { fixTokenPos(f); fixTraceSel(f) },
134         "termlist.go":      nil,
135         "termlist_test.go": nil,
136         "tuple.go":         nil,
137         "typelists.go":     nil,
138         "typeparam.go":     nil,
139         "typeterm_test.go": nil,
140         "typeterm.go":      nil,
141         "under.go":         nil,
142         "unify.go":         fixSprintf,
143         "universe.go":      fixGlobalTypVarDecl,
144         "util_test.go":     fixTokenPos,
145         "validtype.go":     nil,
146 }
147
148 // TODO(gri) We should be able to make these rewriters more configurable/composable.
149 //           For now this is a good starting point.
150
151 // renameIdent renames an identifier.
152 // Note: This doesn't change the use of the identifier in comments.
153 func renameIdent(f *ast.File, from, to string) {
154         ast.Inspect(f, func(n ast.Node) bool {
155                 switch n := n.(type) {
156                 case *ast.Ident:
157                         if n.Name == from {
158                                 n.Name = to
159                         }
160                         return false
161                 }
162                 return true
163         })
164 }
165
166 // renameImportPath renames an import path.
167 func renameImportPath(f *ast.File, from, to string) {
168         ast.Inspect(f, func(n ast.Node) bool {
169                 switch n := n.(type) {
170                 case *ast.ImportSpec:
171                         if n.Path.Kind == token.STRING && n.Path.Value == from {
172                                 n.Path.Value = to
173                                 return false
174                         }
175                 }
176                 return true
177         })
178 }
179
180 // fixTokenPos changes imports of "cmd/compile/internal/syntax" to "go/token",
181 // uses of syntax.Pos to token.Pos, and calls to x.IsKnown() to x.IsValid().
182 func fixTokenPos(f *ast.File) {
183         ast.Inspect(f, func(n ast.Node) bool {
184                 switch n := n.(type) {
185                 case *ast.ImportSpec:
186                         // rewrite import path "cmd/compile/internal/syntax" to "go/token"
187                         if n.Path.Kind == token.STRING && n.Path.Value == `"cmd/compile/internal/syntax"` {
188                                 n.Path.Value = `"go/token"`
189                                 return false
190                         }
191                 case *ast.SelectorExpr:
192                         // rewrite syntax.Pos to token.Pos
193                         if x, _ := n.X.(*ast.Ident); x != nil && x.Name == "syntax" && n.Sel.Name == "Pos" {
194                                 x.Name = "token"
195                                 return false
196                         }
197                 case *ast.CallExpr:
198                         // rewrite x.IsKnown() to x.IsValid()
199                         if fun, _ := n.Fun.(*ast.SelectorExpr); fun != nil && fun.Sel.Name == "IsKnown" && len(n.Args) == 0 {
200                                 fun.Sel.Name = "IsValid"
201                                 return false
202                         }
203                 }
204                 return true
205         })
206 }
207
208 // fixInferSig updates the Checker.infer signature to use a positioner instead of a token.Position
209 // as first argument, renames the argument from "pos" to "posn", and updates a few internal uses of
210 // "pos" to "posn" and "posn.Pos()" respectively.
211 func fixInferSig(f *ast.File) {
212         ast.Inspect(f, func(n ast.Node) bool {
213                 switch n := n.(type) {
214                 case *ast.FuncDecl:
215                         if n.Name.Name == "infer" {
216                                 // rewrite (pos token.Pos, ...) to (posn positioner, ...)
217                                 par := n.Type.Params.List[0]
218                                 if len(par.Names) == 1 && par.Names[0].Name == "pos" {
219                                         par.Names[0] = newIdent(par.Names[0].Pos(), "posn")
220                                         par.Type = newIdent(par.Type.Pos(), "positioner")
221                                         return true
222                                 }
223                         }
224                 case *ast.CallExpr:
225                         if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil {
226                                 switch selx.Sel.Name {
227                                 case "renameTParams":
228                                         // rewrite check.renameTParams(pos, ... ) to check.renameTParams(posn.Pos(), ... )
229                                         if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "pos" {
230                                                 pos := n.Args[0].Pos()
231                                                 fun := &ast.SelectorExpr{X: newIdent(pos, "posn"), Sel: newIdent(pos, "Pos")}
232                                                 arg := &ast.CallExpr{Fun: fun, Lparen: pos, Args: nil, Ellipsis: token.NoPos, Rparen: pos}
233                                                 n.Args[0] = arg
234                                                 return false
235                                         }
236                                 case "errorf":
237                                         // rewrite check.errorf(pos, ...) to check.errorf(posn, ...)
238                                         if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "pos" {
239                                                 pos := n.Args[0].Pos()
240                                                 arg := newIdent(pos, "posn")
241                                                 n.Args[0] = arg
242                                                 return false
243                                         }
244                                 case "allowVersion":
245                                         // rewrite check.allowVersion(..., pos, ...) to check.allowVersion(..., posn, ...)
246                                         if ident, _ := n.Args[1].(*ast.Ident); ident != nil && ident.Name == "pos" {
247                                                 pos := n.Args[1].Pos()
248                                                 arg := newIdent(pos, "posn")
249                                                 n.Args[1] = arg
250                                                 return false
251                                         }
252                                 }
253                         }
254                 }
255                 return true
256         })
257 }
258
259 // fixErrErrorfCall updates calls of the form err.errorf(obj, ...) to err.errorf(obj.Pos(), ...).
260 func fixErrErrorfCall(f *ast.File) {
261         ast.Inspect(f, func(n ast.Node) bool {
262                 switch n := n.(type) {
263                 case *ast.CallExpr:
264                         if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil {
265                                 if ident, _ := selx.X.(*ast.Ident); ident != nil && ident.Name == "err" {
266                                         switch selx.Sel.Name {
267                                         case "errorf":
268                                                 // rewrite err.errorf(obj, ... ) to err.errorf(obj.Pos(), ... )
269                                                 if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "obj" {
270                                                         pos := n.Args[0].Pos()
271                                                         fun := &ast.SelectorExpr{X: ident, Sel: newIdent(pos, "Pos")}
272                                                         arg := &ast.CallExpr{Fun: fun, Lparen: pos, Args: nil, Ellipsis: token.NoPos, Rparen: pos}
273                                                         n.Args[0] = arg
274                                                         return false
275                                                 }
276                                         }
277                                 }
278                         }
279                 }
280                 return true
281         })
282 }
283
284 // fixCheckErrorfCall updates calls of the form check.errorf(pos, ...) to check.errorf(atPos(pos), ...).
285 func fixCheckErrorfCall(f *ast.File) {
286         ast.Inspect(f, func(n ast.Node) bool {
287                 switch n := n.(type) {
288                 case *ast.CallExpr:
289                         if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil {
290                                 if ident, _ := selx.X.(*ast.Ident); ident != nil && ident.Name == "check" {
291                                         switch selx.Sel.Name {
292                                         case "errorf":
293                                                 // rewrite check.errorf(pos, ... ) to check.errorf(atPos(pos), ... )
294                                                 if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "pos" {
295                                                         pos := n.Args[0].Pos()
296                                                         fun := newIdent(pos, "atPos")
297                                                         arg := &ast.CallExpr{Fun: fun, Lparen: pos, Args: []ast.Expr{ident}, Ellipsis: token.NoPos, Rparen: pos}
298                                                         n.Args[0] = arg
299                                                         return false
300                                                 }
301                                         }
302                                 }
303                         }
304                 }
305                 return true
306         })
307 }
308
309 // fixTraceSel renames uses of x.Trace to x.trace, where x for any x with a Trace field.
310 func fixTraceSel(f *ast.File) {
311         ast.Inspect(f, func(n ast.Node) bool {
312                 switch n := n.(type) {
313                 case *ast.SelectorExpr:
314                         // rewrite x.Trace to x._Trace (for Config.Trace)
315                         if n.Sel.Name == "Trace" {
316                                 n.Sel.Name = "_Trace"
317                                 return false
318                         }
319                 }
320                 return true
321         })
322 }
323
324 // fixGlobalTypVarDecl changes the global Typ variable from an array to a slice
325 // (in types2 we use an array for efficiency, in go/types it's a slice and we
326 // cannot change that).
327 func fixGlobalTypVarDecl(f *ast.File) {
328         ast.Inspect(f, func(n ast.Node) bool {
329                 switch n := n.(type) {
330                 case *ast.ValueSpec:
331                         // rewrite type Typ = [...]Type{...} to type Typ = []Type{...}
332                         if len(n.Names) == 1 && n.Names[0].Name == "Typ" && len(n.Values) == 1 {
333                                 n.Values[0].(*ast.CompositeLit).Type.(*ast.ArrayType).Len = nil
334                                 return false
335                         }
336                 }
337                 return true
338         })
339 }
340
341 // fixSprintf adds an extra nil argument for the *token.FileSet parameter in sprintf calls.
342 func fixSprintf(f *ast.File) {
343         ast.Inspect(f, func(n ast.Node) bool {
344                 switch n := n.(type) {
345                 case *ast.CallExpr:
346                         if fun, _ := n.Fun.(*ast.Ident); fun != nil && fun.Name == "sprintf" && len(n.Args) >= 4 /* ... args */ {
347                                 n.Args = insert(n.Args, 1, newIdent(n.Args[1].Pos(), "nil"))
348                                 return false
349                         }
350                 }
351                 return true
352         })
353 }
354
355 // newIdent returns a new identifier with the given position and name.
356 func newIdent(pos token.Pos, name string) *ast.Ident {
357         id := ast.NewIdent(name)
358         id.NamePos = pos
359         return id
360 }
361
362 // insert inserts x at list[at] and moves the remaining elements up.
363 func insert(list []ast.Expr, at int, x ast.Expr) []ast.Expr {
364         list = append(list, nil)
365         copy(list[at+1:], list[at:])
366         list[at] = x
367         return list
368 }