go.tools/go/types: adjust tests for better parser error positions

The parser may return error positions referring to positions
immediately after rather than at a token position. Provide
mechanism to identify those positions in test cases.

Also: Don't compute position strings for each token in test
cases. Should speed up Check test.

Pending CL 70190046 in main repo.

LGTM=adonovan
R=adonovan
CC=golang-codereviews
https://golang.org/cl/71330043
This commit is contained in:
Robert Griesemer 2014-03-04 14:32:55 -08:00
parent 5536d2ae3e
commit fccaf8c467
2 changed files with 23 additions and 8 deletions

View File

@ -122,9 +122,11 @@ func parseFiles(t *testing.T, filenames []string) ([]*ast.File, []error) {
// ERROR comments must start with text `ERROR "rx"` or `ERROR rx` where // ERROR comments must start with text `ERROR "rx"` or `ERROR rx` where
// rx is a regular expression that matches the expected error message. // rx is a regular expression that matches the expected error message.
// Space around "rx" or rx is ignored. // Space around "rx" or rx is ignored. Use the form `ERROR HERE "rx"`
// for error messages that are located immediately after rather than
// at a token's position.
// //
var errRx = regexp.MustCompile(`^ *ERROR *"?([^"]*)"?`) var errRx = regexp.MustCompile(`^ *ERROR *(HERE)? *"?([^"]*)"?`)
// errMap collects the regular expressions of ERROR comments found // errMap collects the regular expressions of ERROR comments found
// in files and returns them as a map of error positions to error messages. // in files and returns them as a map of error positions to error messages.
@ -142,7 +144,8 @@ func errMap(t *testing.T, testname string, files []*ast.File) map[string][]strin
var s scanner.Scanner var s scanner.Scanner
s.Init(fset.AddFile(filename, -1, len(src)), src, nil, scanner.ScanComments) s.Init(fset.AddFile(filename, -1, len(src)), src, nil, scanner.ScanComments)
var prev string // position string of last non-comment, non-semicolon token var prev token.Pos // position of last non-comment, non-semicolon token
var here token.Pos // position immediately after the token at position prev
scanFile: scanFile:
for { for {
@ -154,8 +157,13 @@ func errMap(t *testing.T, testname string, files []*ast.File) map[string][]strin
if lit[1] == '*' { if lit[1] == '*' {
lit = lit[:len(lit)-2] // strip trailing */ lit = lit[:len(lit)-2] // strip trailing */
} }
if s := errRx.FindStringSubmatch(lit[2:]); len(s) == 2 { if s := errRx.FindStringSubmatch(lit[2:]); len(s) == 3 {
errmap[prev] = append(errmap[prev], strings.TrimSpace(s[1])) pos := prev
if s[1] == "HERE" {
pos = here
}
p := fset.Position(pos).String()
errmap[p] = append(errmap[p], strings.TrimSpace(s[2]))
} }
case token.SEMICOLON: case token.SEMICOLON:
// ignore automatically inserted semicolon // ignore automatically inserted semicolon
@ -164,7 +172,14 @@ func errMap(t *testing.T, testname string, files []*ast.File) map[string][]strin
} }
fallthrough fallthrough
default: default:
prev = fset.Position(pos).String() prev = pos
var l int // token length
if tok.IsLiteral() {
l = len(lit)
} else {
l = len(tok.String())
}
here = prev + token.Pos(l)
} }
} }
} }

View File

@ -213,7 +213,7 @@ func selects() {
} }
func gos() { func gos() {
go 1 /* ERROR "expected function/method call" */ go 1 /* ERROR HERE "function must be invoked" */
go int /* ERROR "go requires function call, not conversion" */ (0) go int /* ERROR "go requires function call, not conversion" */ (0)
go gos() go gos()
var c chan int var c chan int
@ -222,7 +222,7 @@ func gos() {
} }
func defers() { func defers() {
defer 1 /* ERROR "expected function/method call" */ defer 1 /* ERROR HERE "function must be invoked" */
defer int /* ERROR "defer requires function call, not conversion" */ (0) defer int /* ERROR "defer requires function call, not conversion" */ (0)
defer defers() defer defers()
var c chan int var c chan int