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