From fccaf8c467f785e42fa30f4650b98fd0a1c6df6e Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Tue, 4 Mar 2014 14:32:55 -0800 Subject: [PATCH] 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 --- go/types/check_test.go | 27 +++++++++++++++++++++------ go/types/testdata/stmt0.src | 4 ++-- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/go/types/check_test.go b/go/types/check_test.go index 0d0afce8..da21986f 100644 --- a/go/types/check_test.go +++ b/go/types/check_test.go @@ -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 // 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 // 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 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: for { @@ -154,8 +157,13 @@ func errMap(t *testing.T, testname string, files []*ast.File) map[string][]strin if lit[1] == '*' { lit = lit[:len(lit)-2] // strip trailing */ } - if s := errRx.FindStringSubmatch(lit[2:]); len(s) == 2 { - errmap[prev] = append(errmap[prev], strings.TrimSpace(s[1])) + if s := errRx.FindStringSubmatch(lit[2:]); len(s) == 3 { + 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: // ignore automatically inserted semicolon @@ -164,7 +172,14 @@ func errMap(t *testing.T, testname string, files []*ast.File) map[string][]strin } fallthrough 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) } } } diff --git a/go/types/testdata/stmt0.src b/go/types/testdata/stmt0.src index 6b6349b2..c42bd36a 100644 --- a/go/types/testdata/stmt0.src +++ b/go/types/testdata/stmt0.src @@ -213,7 +213,7 @@ func selects() { } 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 gos() var c chan int @@ -222,7 +222,7 @@ func gos() { } 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 defers() var c chan int