aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2021-08-22 14:55:12 -0700
committerRobert Griesemer <gri@golang.org>2021-08-22 22:30:37 +0000
commit8fcc614360cac2c23aafeb2fdc637c72ff1d7dfd (patch)
treed518f409e42fa2b6884bc06863beffc04bdf7467 /src/cmd/compile
parent5d5e50c3db2ad15bef4ddc4e2f946d0628deda2d (diff)
downloadgo-8fcc614360cac2c23aafeb2fdc637c72ff1d7dfd.tar.gz
go-8fcc614360cac2c23aafeb2fdc637c72ff1d7dfd.zip
cmd/compile/internal/types2: enable TestSelection API test
This test was never fully ported from go/types. Implement a conversion function from syntax.Pos to string index so that the test can be enabled again. Also renamed the local variable syntax to segment to avoid confusion with the syntax package. Change-Id: I1b34e50ec138403798efb14c828545780f565507 Reviewed-on: https://go-review.googlesource.com/c/go/+/344253 Trust: Robert Griesemer <gri@golang.org> Trust: Dan Scales <danscales@google.com> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Dan Scales <danscales@google.com>
Diffstat (limited to 'src/cmd/compile')
-rw-r--r--src/cmd/compile/internal/types2/api_test.go46
1 files changed, 24 insertions, 22 deletions
diff --git a/src/cmd/compile/internal/types2/api_test.go b/src/cmd/compile/internal/types2/api_test.go
index 3ed2799a84..269b06f08a 100644
--- a/src/cmd/compile/internal/types2/api_test.go
+++ b/src/cmd/compile/internal/types2/api_test.go
@@ -17,10 +17,6 @@ import (
. "cmd/compile/internal/types2"
)
-func unimplemented() {
- panic("unimplemented")
-}
-
// genericPkg is a source prefix for packages that contain generic code.
const genericPkg = "package generic_"
@@ -1168,8 +1164,6 @@ func (m testImporter) Import(path string) (*Package, error) {
}
func TestSelection(t *testing.T) {
- t.Skip("requires fixes around source positions")
-
selections := make(map[*syntax.SelectorExpr]*Selection)
imports := make(testImporter)
@@ -1293,11 +1287,9 @@ func main() {
for e, sel := range selections {
_ = sel.String() // assertion: must not panic
- unimplemented()
- _ = e
- // start := fset.Position(e.Pos()).Offset
- // end := fset.Position(e.End()).Offset
- // syntax := mainSrc[start:end] // (all SelectorExprs are in main, not lib)
+ start := indexFor(mainSrc, syntax.StartPos(e))
+ end := indexFor(mainSrc, syntax.EndPos(e))
+ segment := mainSrc[start:end] // (all SelectorExprs are in main, not lib)
direct := "."
if sel.Indirect() {
@@ -1307,13 +1299,11 @@ func main() {
sel.String(),
fmt.Sprintf("%s%v", direct, sel.Index()),
}
- unimplemented()
- _ = got
- // want := wantOut[syntax]
- // if want != got {
- // t.Errorf("%s: got %q; want %q", syntax, got, want)
- // }
- // delete(wantOut, syntax)
+ want := wantOut[segment]
+ if want != got {
+ t.Errorf("%s: got %q; want %q", segment, got, want)
+ }
+ delete(wantOut, segment)
// We must explicitly assert properties of the
// Signature's receiver since it doesn't participate
@@ -1323,17 +1313,29 @@ func main() {
got := sig.Recv().Type()
want := sel.Recv()
if !Identical(got, want) {
- unimplemented()
- // t.Errorf("%s: Recv() = %s, want %s", syntax, got, want)
+ t.Errorf("%s: Recv() = %s, want %s", segment, got, want)
}
} else if sig != nil && sig.Recv() != nil {
t.Errorf("%s: signature has receiver %s", sig, sig.Recv().Type())
}
}
// Assert that all wantOut entries were used exactly once.
- for syntax := range wantOut {
- t.Errorf("no syntax.Selection found with syntax %q", syntax)
+ for segment := range wantOut {
+ t.Errorf("no syntax.Selection found with syntax %q", segment)
+ }
+}
+
+// indexFor returns the index into s corresponding to the position pos.
+func indexFor(s string, pos syntax.Pos) int {
+ i, line := 0, 1 // string index and corresponding line
+ target := int(pos.Line())
+ for line < target && i < len(s) {
+ if s[i] == '\n' {
+ line++
+ }
+ i++
}
+ return i + int(pos.Col()-1) // columns are 1-based
}
func TestIssue8518(t *testing.T) {