Commit 3b5ff0fb authored by Robert Griesemer's avatar Robert Griesemer

go/typechecker: delete per Go 1 plan

(go/types will be future replacement)

R=r
CC=golang-dev
https://golang.org/cl/5232052
parent 36036781
......@@ -95,7 +95,6 @@ DIRS=\
go/printer\
go/scanner\
go/token\
go/typechecker\
go/types\
gob\
hash\
......
# Copyright 2010 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.
include ../../../Make.inc
TARG=go/typechecker
GOFILES=\
scope.go\
type.go\
typechecker.go\
universe.go\
include ../../../Make.pkg
// Copyright 2010 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.
// DEPRECATED FILE - WILL GO AWAY EVENTUALLY.
//
// Scope handling is now done in go/parser.
// The functionality here is only present to
// keep the typechecker running for now.
package typechecker
import "go/ast"
func (tc *typechecker) openScope() *ast.Scope {
tc.topScope = ast.NewScope(tc.topScope)
return tc.topScope
}
func (tc *typechecker) closeScope() {
tc.topScope = tc.topScope.Outer
}
// declInScope declares an object of a given kind and name in scope and sets the object's Decl and N fields.
// It returns the newly allocated object. If an object with the same name already exists in scope, an error
// is reported and the object is not inserted.
func (tc *typechecker) declInScope(scope *ast.Scope, kind ast.ObjKind, name *ast.Ident, decl interface{}, n int) *ast.Object {
obj := ast.NewObj(kind, name.Name)
obj.Decl = decl
//obj.N = n
name.Obj = obj
if name.Name != "_" {
if alt := scope.Insert(obj); alt != nil {
tc.Errorf(name.Pos(), "%s already declared at %s", name.Name, tc.fset.Position(alt.Pos()).String())
}
}
return obj
}
// decl is the same as declInScope(tc.topScope, ...)
func (tc *typechecker) decl(kind ast.ObjKind, name *ast.Ident, decl interface{}, n int) *ast.Object {
return tc.declInScope(tc.topScope, kind, name, decl, n)
}
// find returns the object with the given name if visible in the current scope hierarchy.
// If no such object is found, an error is reported and a bad object is returned instead.
func (tc *typechecker) find(name *ast.Ident) (obj *ast.Object) {
for s := tc.topScope; s != nil && obj == nil; s = s.Outer {
obj = s.Lookup(name.Name)
}
if obj == nil {
tc.Errorf(name.Pos(), "%s not declared", name.Name)
obj = ast.NewObj(ast.Bad, name.Name)
}
name.Obj = obj
return
}
// findField returns the object with the given name if visible in the type's scope.
// If no such object is found, an error is reported and a bad object is returned instead.
func (tc *typechecker) findField(typ *Type, name *ast.Ident) (obj *ast.Object) {
// TODO(gri) This is simplistic at the moment and ignores anonymous fields.
obj = typ.Scope.Lookup(name.Name)
if obj == nil {
tc.Errorf(name.Pos(), "%s not declared", name.Name)
obj = ast.NewObj(ast.Bad, name.Name)
}
return
}
// Copyright 2010 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.
// type declarations
package P0
type (
B bool
I int32
A [10]P
T struct {
x, y P
}
P *T
R *R
F func(A) I
Y interface {
f(A) I
}
S []P
M map[I]F
C chan<- I
)
type (
a/* ERROR "illegal cycle" */ a
a/* ERROR "already declared" */ int
b/* ERROR "illegal cycle" */ c
c d
d e
e b /* ERROR "not a type" */
t *t
U V
V W
W *U
P1 *S2
P2 P1
S1 struct {
a, b, c int
u, v, a/* ERROR "already declared" */ float
}
S2/* ERROR "illegal cycle" */ struct {
x S2
}
L1 []L1
L2 []int
A1 [10]int
A2/* ERROR "illegal cycle" */ [10]A2
A3/* ERROR "illegal cycle" */ [10]struct {
x A4
}
A4 [10]A3
F1 func()
F2 func(x, y, z float)
F3 func(x, y, x /* ERROR "already declared" */ float)
F4 func() (x, y, x /* ERROR "already declared" */ float)
F5 func(x int) (x /* ERROR "already declared" */ float)
I1 interface{}
I2 interface {
m1()
}
I3 interface {
m1()
m1 /* ERROR "already declared" */ ()
}
I4 interface {
m1(x, y, x /* ERROR "already declared" */ float)
m2() (x, y, x /* ERROR "already declared" */ float)
m3(x int) (x /* ERROR "already declared" */ float)
}
I5 interface {
m1(I5)
}
C1 chan int
C2 <-chan int
C3 chan<- C3
M1 map[Last]string
M2 map[string]M2
Last int
)
// Copyright 2010 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.
// const and var declarations
package P1
const (
c1 = 0
c2 int = 0
c3, c4 = 0
)
// Copyright 2010 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.
package P3
// function and method signatures
func _() {}
func _() {}
func _(x, x /* ERROR "already declared" */ int) {}
func f() {}
func f /* ERROR "already declared" */ () {}
func (*foo /* ERROR "invalid receiver" */ ) m() {}
func (bar /* ERROR "not a type" */ ) m() {}
func f1(x, _, _ int) (_, _ float) {}
func f2(x, y, x /* ERROR "already declared" */ int) {}
func f3(x, y int) (a, b, x /* ERROR "already declared" */ int) {}
func (x *T) m1() {}
func (x *T) m1 /* ERROR "already declared" */ () {}
func (x T) m1 /* ERROR "already declared" */ () {}
func (T) m1 /* ERROR "already declared" */ () {}
func (x *T) m2(u, x /* ERROR "already declared" */ int) {}
func (x *T) m3(a, b, c int) (u, x /* ERROR "already declared" */ int) {}
// The following are disabled for now because the typechecker
// in in the process of being rewritten and cannot handle them
// at the moment
//func (T) _(x, x /* "already declared" */ int) {}
//func (T) _() (x, x /* "already declared" */ int) {}
//func (PT) _() {}
var bar int
type T struct{}
type PT (T)
// Copyright 2010 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.
// Constant declarations
package P4
const (
c0 = 0
)
// Copyright 2010 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.
package typechecker
import "go/ast"
// A Type represents a Go type.
type Type struct {
Form Form
Obj *ast.Object // corresponding type name, or nil
Scope *ast.Scope // fields and methods, always present
N uint // basic type id, array length, number of function results, or channel direction
Key, Elt *Type // map key and array, pointer, slice, map or channel element
Params *ast.Scope // function (receiver, input and result) parameters, tuple expressions (results of function calls), or nil
Expr ast.Expr // corresponding AST expression
}
// NewType creates a new type of a given form.
func NewType(form Form) *Type {
return &Type{Form: form, Scope: ast.NewScope(nil)}
}
// Form describes the form of a type.
type Form int
// The list of possible type forms.
const (
BadType Form = iota // for error handling
Unresolved // type not fully setup
Basic
Array
Struct
Pointer
Function
Method
Interface
Slice
Map
Channel
Tuple
)
var formStrings = [...]string{
BadType: "badType",
Unresolved: "unresolved",
Basic: "basic",
Array: "array",
Struct: "struct",
Pointer: "pointer",
Function: "function",
Method: "method",
Interface: "interface",
Slice: "slice",
Map: "map",
Channel: "channel",
Tuple: "tuple",
}
func (form Form) String() string { return formStrings[form] }
// The list of basic type id's.
const (
Bool = iota
Byte
Uint
Int
Float
Complex
Uintptr
String
Uint8
Uint16
Uint32
Uint64
Int8
Int16
Int32
Int64
Float32
Float64
Complex64
Complex128
// TODO(gri) ideal types are missing
)
var BasicTypes = map[uint]string{
Bool: "bool",
Byte: "byte",
Uint: "uint",
Int: "int",
Float: "float",
Complex: "complex",
Uintptr: "uintptr",
String: "string",
Uint8: "uint8",
Uint16: "uint16",
Uint32: "uint32",
Uint64: "uint64",
Int8: "int8",
Int16: "int16",
Int32: "int32",
Int64: "int64",
Float32: "float32",
Float64: "float64",
Complex64: "complex64",
Complex128: "complex128",
}
This diff is collapsed.
// Copyright 2010 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.
// This file implements a simple typechecker test harness. Packages found
// in the testDir directory are typechecked. Error messages reported by
// the typechecker are compared against the error messages expected for
// the test files.
//
// Expected errors are indicated in the test files by putting a comment
// of the form /* ERROR "rx" */ immediately following an offending token.
// The harness will verify that an error matching the regular expression
// rx is reported at that source position. Consecutive comments may be
// used to indicate multiple errors for the same token position.
//
// For instance, the following test file indicates that a "not declared"
// error should be reported for the undeclared variable x:
//
// package P0
// func f() {
// _ = x /* ERROR "not declared" */ + 1
// }
//
// If the -pkg flag is set, only packages with package names matching
// the regular expression provided via the flag value are tested.
package typechecker
import (
"flag"
"fmt"
"go/ast"
"go/parser"
"go/scanner"
"go/token"
"io/ioutil"
"os"
"regexp"
"sort"
"strings"
"testing"
)
const testDir = "./testdata" // location of test packages
var fset = token.NewFileSet()
var (
pkgPat = flag.String("pkg", ".*", "regular expression to select test packages by package name")
trace = flag.Bool("trace", false, "print package names")
)
// ERROR comments must be of the form /* ERROR "rx" */ and rx is
// a regular expression that matches the expected error message.
var errRx = regexp.MustCompile(`^/\* *ERROR *"([^"]*)" *\*/$`)
// expectedErrors collects the regular expressions of ERROR comments
// found in the package files of pkg and returns them in sorted order
// (by filename and position).
func expectedErrors(t *testing.T, pkg *ast.Package) (list scanner.ErrorList) {
// scan all package files
for filename := range pkg.Files {
src, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatalf("expectedErrors(%s): %v", pkg.Name, err)
}
var s scanner.Scanner
file := fset.AddFile(filename, fset.Base(), len(src))
s.Init(file, src, nil, scanner.ScanComments)
var prev token.Pos // position of last non-comment token
loop:
for {
pos, tok, lit := s.Scan()
switch tok {
case token.EOF:
break loop
case token.COMMENT:
s := errRx.FindStringSubmatch(lit)
if len(s) == 2 {
list = append(list, &scanner.Error{fset.Position(prev), string(s[1])})
}
default:
prev = pos
}
}
}
sort.Sort(list) // multiple files may not be sorted
return
}
func testFilter(f *os.FileInfo) bool {
return strings.HasSuffix(f.Name, ".src") && f.Name[0] != '.'
}
func checkError(t *testing.T, expected, found *scanner.Error) {
rx, err := regexp.Compile(expected.Msg)
if err != nil {
t.Errorf("%s: %v", expected.Pos, err)
return
}
match := rx.MatchString(found.Msg)
if expected.Pos.Offset != found.Pos.Offset {
if match {
t.Errorf("%s: expected error should have been at %s", expected.Pos, found.Pos)
} else {
t.Errorf("%s: error matching %q expected", expected.Pos, expected.Msg)
return
}
}
if !match {
t.Errorf("%s: %q does not match %q", expected.Pos, expected.Msg, found.Msg)
}
}
func TestTypeCheck(t *testing.T) {
flag.Parse()
pkgRx, err := regexp.Compile(*pkgPat)
if err != nil {
t.Fatalf("illegal flag value %q: %s", *pkgPat, err)
}
pkgs, err := parser.ParseDir(fset, testDir, testFilter, 0)
if err != nil {
scanner.PrintError(os.Stderr, err)
t.Fatalf("packages in %s contain syntax errors", testDir)
}
for _, pkg := range pkgs {
if !pkgRx.MatchString(pkg.Name) {
continue // only test selected packages
}
if *trace {
fmt.Println(pkg.Name)
}
xlist := expectedErrors(t, pkg)
err := CheckPackage(fset, pkg, nil)
if err != nil {
if elist, ok := err.(scanner.ErrorList); ok {
// verify that errors match
for i := 0; i < len(xlist) && i < len(elist); i++ {
checkError(t, xlist[i], elist[i])
}
// the correct number or errors must have been found
if len(xlist) != len(elist) {
fmt.Fprintf(os.Stderr, "%s\n", pkg.Name)
scanner.PrintError(os.Stderr, elist)
fmt.Fprintln(os.Stderr)
t.Errorf("TypeCheck(%s): %d errors expected but %d reported", pkg.Name, len(xlist), len(elist))
}
} else {
t.Errorf("TypeCheck(%s): %v", pkg.Name, err)
}
} else if len(xlist) > 0 {
t.Errorf("TypeCheck(%s): %d errors expected but 0 reported", pkg.Name, len(xlist))
}
}
}
// Copyright 2010 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.
package typechecker
import "go/ast"
// TODO(gri) should this be in package ast?
// The Universe scope contains all predeclared identifiers.
var Universe *ast.Scope
func def(obj *ast.Object) {
alt := Universe.Insert(obj)
if alt != nil {
panic("object declared twice")
}
}
func init() {
Universe = ast.NewScope(nil)
// basic types
for n, name := range BasicTypes {
typ := NewType(Basic)
typ.N = n
obj := ast.NewObj(ast.Typ, name)
obj.Type = typ
typ.Obj = obj
def(obj)
}
// built-in functions
// TODO(gri) implement this
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment