mirror of
https://github.com/golang/go
synced 2024-11-19 02:14:43 -07:00
go/typechecker: use append
R=rsc CC=golang-dev https://golang.org/cl/2736044
This commit is contained in:
parent
6062515a93
commit
098e94173a
@ -9,7 +9,6 @@
|
|||||||
package typechecker
|
package typechecker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"container/vector"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/token"
|
"go/token"
|
||||||
@ -122,21 +121,20 @@ func (tc *typechecker) checkPackage(pkg *ast.Package) {
|
|||||||
// TODO(gri) there's no file scope at the moment since we ignore imports
|
// TODO(gri) there's no file scope at the moment since we ignore imports
|
||||||
|
|
||||||
// phase 1: declare all global objects; also collect all function and method declarations
|
// phase 1: declare all global objects; also collect all function and method declarations
|
||||||
var funcs vector.Vector
|
var funcs []*ast.FuncDecl
|
||||||
for _, file := range pkg.Files {
|
for _, file := range pkg.Files {
|
||||||
for _, decl := range file.Decls {
|
for _, decl := range file.Decls {
|
||||||
tc.declGlobal(decl)
|
tc.declGlobal(decl)
|
||||||
if f, isFunc := decl.(*ast.FuncDecl); isFunc {
|
if f, isFunc := decl.(*ast.FuncDecl); isFunc {
|
||||||
funcs.Push(f)
|
funcs = append(funcs, f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// phase 2: bind methods to their receiver base types
|
// phase 2: bind methods to their receiver base types
|
||||||
for _, decl := range funcs {
|
for _, m := range funcs {
|
||||||
d := decl.(*ast.FuncDecl)
|
if m.Recv != nil {
|
||||||
if d.Recv != nil {
|
tc.bindMethod(m)
|
||||||
tc.bindMethod(d)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,9 +147,8 @@ func (tc *typechecker) checkPackage(pkg *ast.Package) {
|
|||||||
assert(len(tc.cyclemap) == 0)
|
assert(len(tc.cyclemap) == 0)
|
||||||
|
|
||||||
// 4: sequentially typecheck function and method bodies
|
// 4: sequentially typecheck function and method bodies
|
||||||
for _, decl := range funcs {
|
for _, f := range funcs {
|
||||||
d := decl.(*ast.FuncDecl)
|
tc.checkBlock(f.Body.List, f.Name.Obj.Type)
|
||||||
tc.checkBlock(d.Body.List, d.Name.Obj.Type)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pkg.Scope = tc.topScope
|
pkg.Scope = tc.topScope
|
||||||
|
@ -27,7 +27,6 @@
|
|||||||
package typechecker
|
package typechecker
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"container/vector"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
@ -58,9 +57,7 @@ var errRx = regexp.MustCompile(`^/\* *ERROR *"([^"]*)" *\*/$`)
|
|||||||
// expectedErrors collects the regular expressions of ERROR comments
|
// expectedErrors collects the regular expressions of ERROR comments
|
||||||
// found in the package files of pkg and returns them in sorted order
|
// found in the package files of pkg and returns them in sorted order
|
||||||
// (by filename and position).
|
// (by filename and position).
|
||||||
func expectedErrors(t *testing.T, pkg *ast.Package) scanner.ErrorList {
|
func expectedErrors(t *testing.T, pkg *ast.Package) (list scanner.ErrorList) {
|
||||||
var list vector.Vector
|
|
||||||
|
|
||||||
// scan all package files
|
// scan all package files
|
||||||
for filename := range pkg.Files {
|
for filename := range pkg.Files {
|
||||||
src, err := ioutil.ReadFile(filename)
|
src, err := ioutil.ReadFile(filename)
|
||||||
@ -80,21 +77,15 @@ func expectedErrors(t *testing.T, pkg *ast.Package) scanner.ErrorList {
|
|||||||
case token.COMMENT:
|
case token.COMMENT:
|
||||||
s := errRx.FindSubmatch(lit)
|
s := errRx.FindSubmatch(lit)
|
||||||
if len(s) == 2 {
|
if len(s) == 2 {
|
||||||
list.Push(&scanner.Error{prev, string(s[1])})
|
list = append(list, &scanner.Error{prev, string(s[1])})
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
prev = pos
|
prev = pos
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
sort.Sort(list) // multiple files may not be sorted
|
||||||
// convert list
|
return
|
||||||
errs := make(scanner.ErrorList, len(list))
|
|
||||||
for i, e := range list {
|
|
||||||
errs[i] = e.(*scanner.Error)
|
|
||||||
}
|
|
||||||
sort.Sort(errs) // multiple files may not be sorted
|
|
||||||
return errs
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user