Skip to content

Commit 8fcc614

Browse files
committed
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 <[email protected]> Trust: Dan Scales <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Dan Scales <[email protected]>
1 parent 5d5e50c commit 8fcc614

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/cmd/compile/internal/types2/api_test.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ import (
1717
. "cmd/compile/internal/types2"
1818
)
1919

20-
func unimplemented() {
21-
panic("unimplemented")
22-
}
23-
2420
// genericPkg is a source prefix for packages that contain generic code.
2521
const genericPkg = "package generic_"
2622

@@ -1168,8 +1164,6 @@ func (m testImporter) Import(path string) (*Package, error) {
11681164
}
11691165

11701166
func TestSelection(t *testing.T) {
1171-
t.Skip("requires fixes around source positions")
1172-
11731167
selections := make(map[*syntax.SelectorExpr]*Selection)
11741168

11751169
imports := make(testImporter)
@@ -1293,11 +1287,9 @@ func main() {
12931287
for e, sel := range selections {
12941288
_ = sel.String() // assertion: must not panic
12951289

1296-
unimplemented()
1297-
_ = e
1298-
// start := fset.Position(e.Pos()).Offset
1299-
// end := fset.Position(e.End()).Offset
1300-
// syntax := mainSrc[start:end] // (all SelectorExprs are in main, not lib)
1290+
start := indexFor(mainSrc, syntax.StartPos(e))
1291+
end := indexFor(mainSrc, syntax.EndPos(e))
1292+
segment := mainSrc[start:end] // (all SelectorExprs are in main, not lib)
13011293

13021294
direct := "."
13031295
if sel.Indirect() {
@@ -1307,13 +1299,11 @@ func main() {
13071299
sel.String(),
13081300
fmt.Sprintf("%s%v", direct, sel.Index()),
13091301
}
1310-
unimplemented()
1311-
_ = got
1312-
// want := wantOut[syntax]
1313-
// if want != got {
1314-
// t.Errorf("%s: got %q; want %q", syntax, got, want)
1315-
// }
1316-
// delete(wantOut, syntax)
1302+
want := wantOut[segment]
1303+
if want != got {
1304+
t.Errorf("%s: got %q; want %q", segment, got, want)
1305+
}
1306+
delete(wantOut, segment)
13171307

13181308
// We must explicitly assert properties of the
13191309
// Signature's receiver since it doesn't participate
@@ -1323,17 +1313,29 @@ func main() {
13231313
got := sig.Recv().Type()
13241314
want := sel.Recv()
13251315
if !Identical(got, want) {
1326-
unimplemented()
1327-
// t.Errorf("%s: Recv() = %s, want %s", syntax, got, want)
1316+
t.Errorf("%s: Recv() = %s, want %s", segment, got, want)
13281317
}
13291318
} else if sig != nil && sig.Recv() != nil {
13301319
t.Errorf("%s: signature has receiver %s", sig, sig.Recv().Type())
13311320
}
13321321
}
13331322
// Assert that all wantOut entries were used exactly once.
1334-
for syntax := range wantOut {
1335-
t.Errorf("no syntax.Selection found with syntax %q", syntax)
1323+
for segment := range wantOut {
1324+
t.Errorf("no syntax.Selection found with syntax %q", segment)
1325+
}
1326+
}
1327+
1328+
// indexFor returns the index into s corresponding to the position pos.
1329+
func indexFor(s string, pos syntax.Pos) int {
1330+
i, line := 0, 1 // string index and corresponding line
1331+
target := int(pos.Line())
1332+
for line < target && i < len(s) {
1333+
if s[i] == '\n' {
1334+
line++
1335+
}
1336+
i++
13361337
}
1338+
return i + int(pos.Col()-1) // columns are 1-based
13371339
}
13381340

13391341
func TestIssue8518(t *testing.T) {

0 commit comments

Comments
 (0)