mirror of
https://github.com/golang/go
synced 2024-11-17 15:44:40 -07:00
go/types, types2: use factored parse/typecheck functions, generate hilbert_test.go
Change-Id: I4a325736d18a98bbcd02bfa3d32b1d1dd2048dc2 Reviewed-on: https://go-review.googlesource.com/c/go/+/461609 Auto-Submit: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@google.com> Run-TryBot: Robert Griesemer <gri@google.com>
This commit is contained in:
parent
354c8fb436
commit
7da4675dad
@ -7,7 +7,6 @@ package types2_test
|
||||
import (
|
||||
"cmd/compile/internal/syntax"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
. "cmd/compile/internal/types2"
|
||||
@ -172,14 +171,9 @@ func TestBuiltinSignatures(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func parseGenericSrc(path, src string) (*syntax.File, error) {
|
||||
errh := func(error) {} // dummy error handler so that parsing continues in presence of errors
|
||||
return syntax.Parse(syntax.NewFileBase(path), strings.NewReader(src), errh, nil, 0)
|
||||
}
|
||||
|
||||
func testBuiltinSignature(t *testing.T, name, src0, want string) {
|
||||
src := fmt.Sprintf(`package p; import "unsafe"; type _ unsafe.Pointer /* use unsafe */; func _[P ~[]byte]() { %s }`, src0)
|
||||
f, err := parseGenericSrc("", src)
|
||||
f, err := parse("", src)
|
||||
if err != nil {
|
||||
t.Errorf("%s: %s", src0, err)
|
||||
return
|
||||
|
@ -10,9 +10,6 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"cmd/compile/internal/syntax"
|
||||
. "cmd/compile/internal/types2"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -28,19 +25,7 @@ func TestHilbert(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
// parse source
|
||||
f, err := syntax.Parse(syntax.NewFileBase("hilbert.go"), bytes.NewReader(src), nil, nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// type-check file
|
||||
DefPredeclaredTestFuncs() // define assert built-in
|
||||
conf := Config{Importer: defaultImporter()}
|
||||
_, err = conf.Check(f.PkgName.Value, []*syntax.File{f}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mustTypecheck("hilbert.go", string(src), nil)
|
||||
}
|
||||
|
||||
func program(n int, out string) []byte {
|
||||
|
@ -15,11 +15,7 @@ import (
|
||||
|
||||
func checkMono(t *testing.T, body string) error {
|
||||
src := "package x; import `unsafe`; var _ unsafe.Pointer;\n" + body
|
||||
file, err := syntax.Parse(syntax.NewFileBase("x.go"), strings.NewReader(src), nil, nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
files := []*syntax.File{file}
|
||||
files := []*syntax.File{mustParse("x.go", src)}
|
||||
|
||||
var buf strings.Builder
|
||||
conf := types2.Config{
|
||||
|
@ -8,7 +8,6 @@ import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/importer"
|
||||
"go/parser"
|
||||
"testing"
|
||||
|
||||
. "go/types"
|
||||
@ -173,11 +172,9 @@ func TestBuiltinSignatures(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// parseGenericSrc in types2 is not necessary. We can just parse in testBuiltinSignature below.
|
||||
|
||||
func testBuiltinSignature(t *testing.T, name, src0, want string) {
|
||||
src := fmt.Sprintf(`package p; import "unsafe"; type _ unsafe.Pointer /* use unsafe */; func _[P ~[]byte]() { %s }`, src0)
|
||||
f, err := parser.ParseFile(fset, "", src, 0)
|
||||
f, err := parse(fset, "", src)
|
||||
if err != nil {
|
||||
t.Errorf("%s: %s", src0, err)
|
||||
return
|
||||
|
@ -89,6 +89,7 @@ var filemap = map[string]action{
|
||||
"context.go": nil,
|
||||
"context_test.go": nil,
|
||||
"gccgosizes.go": nil,
|
||||
"hilbert_test.go": nil,
|
||||
"instantiate_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"`, `"go/types"`) },
|
||||
"lookup.go": nil,
|
||||
"main_test.go": nil,
|
||||
|
@ -1,3 +1,5 @@
|
||||
// Code generated by "go run generator.go"; DO NOT EDIT.
|
||||
|
||||
// Copyright 2013 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
@ -8,14 +10,8 @@ import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/importer"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
. "go/types"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -31,20 +27,7 @@ func TestHilbert(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
// parse source
|
||||
fset := token.NewFileSet()
|
||||
f, err := parser.ParseFile(fset, "hilbert.go", src, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// type-check file
|
||||
DefPredeclaredTestFuncs() // define assert built-in
|
||||
conf := Config{Importer: importer.Default()}
|
||||
_, err = conf.Check(f.Name.Name, fset, []*ast.File{f}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mustTypecheck("hilbert.go", string(src), nil)
|
||||
}
|
||||
|
||||
func program(n int, out string) []byte {
|
||||
@ -84,7 +67,7 @@ type gen struct {
|
||||
bytes.Buffer
|
||||
}
|
||||
|
||||
func (g *gen) p(format string, args ...any) {
|
||||
func (g *gen) p(format string, args ...interface{}) {
|
||||
fmt.Fprintf(&g.Buffer, format, args...)
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,6 @@ import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/importer"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"go/types"
|
||||
"strings"
|
||||
@ -18,11 +17,8 @@ import (
|
||||
|
||||
func checkMono(t *testing.T, body string) error {
|
||||
fset := token.NewFileSet()
|
||||
file, err := parser.ParseFile(fset, "x.go", "package x; import `unsafe`; var _ unsafe.Pointer;\n"+body, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
files := []*ast.File{file}
|
||||
src := "package x; import `unsafe`; var _ unsafe.Pointer;\n" + body
|
||||
files := []*ast.File{mustParse(fset, "x.go", src)}
|
||||
|
||||
var buf strings.Builder
|
||||
conf := types.Config{
|
||||
|
Loading…
Reference in New Issue
Block a user