]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/types/generate_test.go
go/types, types2: do not mutate arguments in NewChecker
[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         "array.go":        nil,
99         "basic.go":        nil,
100         "chan.go":         nil,
101         "const.go":        func(f *ast.File) { fixTokenPos(f) },
102         "context.go":      nil,
103         "context_test.go": nil,
104         "gccgosizes.go":   nil,
105         "hilbert_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"`, `"go/types"`) },
106         "infer.go": func(f *ast.File) {
107                 fixTokenPos(f)
108                 fixInferSig(f)
109         },
110         // "initorder.go": fixErrErrorfCall, // disabled for now due to unresolved error_ use implications for gopls
111         "instantiate.go":      func(f *ast.File) { fixTokenPos(f); fixCheckErrorfCall(f) },
112         "instantiate_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"`, `"go/types"`) },
113         "lookup.go":           func(f *ast.File) { fixTokenPos(f) },
114         "main_test.go":        nil,
115         "map.go":              nil,
116         "named.go":            func(f *ast.File) { fixTokenPos(f); fixTraceSel(f) },
117         "object.go":           func(f *ast.File) { fixTokenPos(f); renameIdent(f, "NewTypeNameLazy", "_NewTypeNameLazy") },
118         "object_test.go":      func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"`, `"go/types"`) },
119         "objset.go":           nil,
120         "package.go":          nil,
121         "pointer.go":          nil,
122         "predicates.go":       nil,
123         "scope.go": func(f *ast.File) {
124                 fixTokenPos(f)
125                 renameIdent(f, "Squash", "squash")
126                 renameIdent(f, "InsertLazy", "_InsertLazy")
127         },
128         "selection.go":     nil,
129         "sizes.go":         func(f *ast.File) { renameIdent(f, "IsSyncAtomicAlign64", "_IsSyncAtomicAlign64") },
130         "slice.go":         nil,
131         "subst.go":         func(f *ast.File) { fixTokenPos(f); fixTraceSel(f) },
132         "termlist.go":      nil,
133         "termlist_test.go": nil,
134         "tuple.go":         nil,
135         "typelists.go":     nil,
136         "typeparam.go":     nil,
137         "typeterm_test.go": nil,
138         "typeterm.go":      nil,
139         "under.go":         nil,
140         "unify.go":         fixSprintf,
141         "universe.go":      fixGlobalTypVarDecl,
142         "util_test.go":     fixTokenPos,
143         "validtype.go":     nil,
144         "version_test.go":  nil,
145 }
146
147 // TODO(gri) We should be able to make these rewriters more configurable/composable.
148 //           For now this is a good starting point.
149
150 // renameIdent renames an identifier.
151 // Note: This doesn't change the use of the identifier in comments.
152 func renameIdent(f *ast.File, from, to string) {
153         ast.Inspect(f, func(n ast.Node) bool {
154                 switch n := n.(type) {
155                 case *ast.Ident:
156                         if n.Name == from {
157                                 n.Name = to
158                         }
159                         return false
160                 }
161                 return true
162         })
163 }
164
165 // renameImportPath renames an import path.
166 func renameImportPath(f *ast.File, from, to string) {
167         ast.Inspect(f, func(n ast.Node) bool {
168                 switch n := n.(type) {
169                 case *ast.ImportSpec:
170                         if n.Path.Kind == token.STRING && n.Path.Value == from {
171                                 n.Path.Value = to
172                                 return false
173                         }
174                 }
175                 return true
176         })
177 }
178
179 // fixTokenPos changes imports of "cmd/compile/internal/syntax" to "go/token",
180 // uses of syntax.Pos to token.Pos, and calls to x.IsKnown() to x.IsValid().
181 func fixTokenPos(f *ast.File) {
182         ast.Inspect(f, func(n ast.Node) bool {
183                 switch n := n.(type) {
184                 case *ast.ImportSpec:
185                         // rewrite import path "cmd/compile/internal/syntax" to "go/token"
186                         if n.Path.Kind == token.STRING && n.Path.Value == `"cmd/compile/internal/syntax"` {
187                                 n.Path.Value = `"go/token"`
188                                 return false
189                         }
190                 case *ast.SelectorExpr:
191                         // rewrite syntax.Pos to token.Pos
192                         if x, _ := n.X.(*ast.Ident); x != nil && x.Name == "syntax" && n.Sel.Name == "Pos" {
193                                 x.Name = "token"
194                                 return false
195                         }
196                 case *ast.CallExpr:
197                         // rewrite x.IsKnown() to x.IsValid()
198                         if fun, _ := n.Fun.(*ast.SelectorExpr); fun != nil && fun.Sel.Name == "IsKnown" && len(n.Args) == 0 {
199                                 fun.Sel.Name = "IsValid"
200                                 return false
201                         }
202                 }
203                 return true
204         })
205 }
206
207 // fixInferSig updates the Checker.infer signature to use a positioner instead of a token.Position
208 // as first argument, renames the argument from "pos" to "posn", and updates a few internal uses of
209 // "pos" to "posn" and "posn.Pos()" respectively.
210 func fixInferSig(f *ast.File) {
211         ast.Inspect(f, func(n ast.Node) bool {
212                 switch n := n.(type) {
213                 case *ast.FuncDecl:
214                         if n.Name.Name == "infer" || n.Name.Name == "infer1" || n.Name.Name == "infer2" {
215                                 // rewrite (pos token.Pos, ...) to (posn positioner, ...)
216                                 par := n.Type.Params.List[0]
217                                 if len(par.Names) == 1 && par.Names[0].Name == "pos" {
218                                         par.Names[0] = newIdent(par.Names[0].Pos(), "posn")
219                                         par.Type = newIdent(par.Type.Pos(), "positioner")
220                                         return true
221                                 }
222                         }
223                 case *ast.CallExpr:
224                         if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil {
225                                 switch selx.Sel.Name {
226                                 case "renameTParams":
227                                         // rewrite check.renameTParams(pos, ... ) to check.renameTParams(posn.Pos(), ... )
228                                         if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "pos" {
229                                                 pos := n.Args[0].Pos()
230                                                 fun := &ast.SelectorExpr{X: newIdent(pos, "posn"), Sel: newIdent(pos, "Pos")}
231                                                 arg := &ast.CallExpr{Fun: fun, Lparen: pos, Args: nil, Ellipsis: token.NoPos, Rparen: pos}
232                                                 n.Args[0] = arg
233                                                 return false
234                                         }
235                                 case "errorf", "infer1", "infer2":
236                                         // rewrite check.errorf(pos, ...) to check.errorf(posn, ...)
237                                         // rewrite check.infer1(pos, ...) to check.infer1(posn, ...)
238                                         // rewrite check.infer2(pos, ...) to check.infer2(posn, ...)
239                                         if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "pos" {
240                                                 pos := n.Args[0].Pos()
241                                                 arg := newIdent(pos, "posn")
242                                                 n.Args[0] = arg
243                                                 return false
244                                         }
245                                 }
246                         }
247                 }
248                 return true
249         })
250 }
251
252 // fixErrErrorfCall updates calls of the form err.errorf(obj, ...) to err.errorf(obj.Pos(), ...).
253 func fixErrErrorfCall(f *ast.File) {
254         ast.Inspect(f, func(n ast.Node) bool {
255                 switch n := n.(type) {
256                 case *ast.CallExpr:
257                         if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil {
258                                 if ident, _ := selx.X.(*ast.Ident); ident != nil && ident.Name == "err" {
259                                         switch selx.Sel.Name {
260                                         case "errorf":
261                                                 // rewrite err.errorf(obj, ... ) to err.errorf(obj.Pos(), ... )
262                                                 if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "obj" {
263                                                         pos := n.Args[0].Pos()
264                                                         fun := &ast.SelectorExpr{X: ident, Sel: newIdent(pos, "Pos")}
265                                                         arg := &ast.CallExpr{Fun: fun, Lparen: pos, Args: nil, Ellipsis: token.NoPos, Rparen: pos}
266                                                         n.Args[0] = arg
267                                                         return false
268                                                 }
269                                         }
270                                 }
271                         }
272                 }
273                 return true
274         })
275 }
276
277 // fixCheckErrorfCall updates calls of the form check.errorf(pos, ...) to check.errorf(atPos(pos), ...).
278 func fixCheckErrorfCall(f *ast.File) {
279         ast.Inspect(f, func(n ast.Node) bool {
280                 switch n := n.(type) {
281                 case *ast.CallExpr:
282                         if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil {
283                                 if ident, _ := selx.X.(*ast.Ident); ident != nil && ident.Name == "check" {
284                                         switch selx.Sel.Name {
285                                         case "errorf":
286                                                 // rewrite check.errorf(pos, ... ) to check.errorf(atPos(pos), ... )
287                                                 if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "pos" {
288                                                         pos := n.Args[0].Pos()
289                                                         fun := newIdent(pos, "atPos")
290                                                         arg := &ast.CallExpr{Fun: fun, Lparen: pos, Args: []ast.Expr{ident}, Ellipsis: token.NoPos, Rparen: pos}
291                                                         n.Args[0] = arg
292                                                         return false
293                                                 }
294                                         }
295                                 }
296                         }
297                 }
298                 return true
299         })
300 }
301
302 // fixTraceSel renames uses of x.Trace to x.trace, where x for any x with a Trace field.
303 func fixTraceSel(f *ast.File) {
304         ast.Inspect(f, func(n ast.Node) bool {
305                 switch n := n.(type) {
306                 case *ast.SelectorExpr:
307                         // rewrite x.Trace to x._Trace (for Config.Trace)
308                         if n.Sel.Name == "Trace" {
309                                 n.Sel.Name = "_Trace"
310                                 return false
311                         }
312                 }
313                 return true
314         })
315 }
316
317 // fixGlobalTypVarDecl changes the global Typ variable from an array to a slice
318 // (in types2 we use an array for efficiency, in go/types it's a slice and we
319 // cannot change that).
320 func fixGlobalTypVarDecl(f *ast.File) {
321         ast.Inspect(f, func(n ast.Node) bool {
322                 switch n := n.(type) {
323                 case *ast.ValueSpec:
324                         // rewrite type Typ = [...]Type{...} to type Typ = []Type{...}
325                         if len(n.Names) == 1 && n.Names[0].Name == "Typ" && len(n.Values) == 1 {
326                                 n.Values[0].(*ast.CompositeLit).Type.(*ast.ArrayType).Len = nil
327                                 return false
328                         }
329                 }
330                 return true
331         })
332 }
333
334 // fixSprintf adds an extra nil argument for the *token.FileSet parameter in sprintf calls.
335 func fixSprintf(f *ast.File) {
336         ast.Inspect(f, func(n ast.Node) bool {
337                 switch n := n.(type) {
338                 case *ast.CallExpr:
339                         if fun, _ := n.Fun.(*ast.Ident); fun != nil && fun.Name == "sprintf" && len(n.Args) >= 4 /* ... args */ {
340                                 n.Args = insert(n.Args, 1, newIdent(n.Args[1].Pos(), "nil"))
341                                 return false
342                         }
343                 }
344                 return true
345         })
346 }
347
348 // newIdent returns a new identifier with the given position and name.
349 func newIdent(pos token.Pos, name string) *ast.Ident {
350         id := ast.NewIdent(name)
351         id.NamePos = pos
352         return id
353 }
354
355 // insert inserts x at list[at] and moves the remaining elements up.
356 func insert(list []ast.Expr, at int, x ast.Expr) []ast.Expr {
357         list = append(list, nil)
358         copy(list[at+1:], list[at:])
359         list[at] = x
360         return list
361 }