cmd/compile/internal/types2: make TestIssue43124 match the go/types version

Replace the (flaky) types2.TestIssue43124 with the code of the
(stable) go/types version of this test.

While at it, replace a handful of syntax.Pos{} with the equivalent
nopos, to further reduce differences between the two versions of
the issues_test.go file.

For #61064.

Change-Id: I69f3e4627a48c9928e335d67736cb875ba3835fc
Reviewed-on: https://go-review.googlesource.com/c/go/+/507215
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
This commit is contained in:
Robert Griesemer 2023-06-29 09:48:52 -07:00 committed by Robert Griesemer
parent 499458f7ca
commit 2db31efdba
1 changed files with 56 additions and 25 deletions

View File

@ -497,14 +497,14 @@ func TestIssue43088(t *testing.T) {
// _ T2
// }
// }
n1 := NewTypeName(syntax.Pos{}, nil, "T1", nil)
n1 := NewTypeName(nopos, nil, "T1", nil)
T1 := NewNamed(n1, nil, nil)
n2 := NewTypeName(syntax.Pos{}, nil, "T2", nil)
n2 := NewTypeName(nopos, nil, "T2", nil)
T2 := NewNamed(n2, nil, nil)
s1 := NewStruct([]*Var{NewField(syntax.Pos{}, nil, "_", T2, false)}, nil)
s1 := NewStruct([]*Var{NewField(nopos, nil, "_", T2, false)}, nil)
T1.SetUnderlying(s1)
s2 := NewStruct([]*Var{NewField(syntax.Pos{}, nil, "_", T2, false)}, nil)
s3 := NewStruct([]*Var{NewField(syntax.Pos{}, nil, "_", s2, false)}, nil)
s2 := NewStruct([]*Var{NewField(nopos, nil, "_", T2, false)}, nil)
s3 := NewStruct([]*Var{NewField(nopos, nil, "_", s2, false)}, nil)
T2.SetUnderlying(s3)
// These calls must terminate (no endless recursion).
@ -535,38 +535,69 @@ func TestIssue44515(t *testing.T) {
}
func TestIssue43124(t *testing.T) {
testenv.MustHaveGoBuild(t)
// TODO(rFindley) move this to testdata by enhancing support for importing.
testenv.MustHaveGoBuild(t) // The go command is needed for the importer to determine the locations of stdlib .a files.
// All involved packages have the same name (template). Error messages should
// disambiguate between text/template and html/template by printing the full
// path.
const (
asrc = `package a; import "text/template"; func F(template.Template) {}; func G(int) {}`
bsrc = `package b; import ("a"; "html/template"); func _() { a.F(template.Template{}) }`
csrc = `package c; import ("a"; "html/template"); func _() { a.G(template.Template{}) }`
bsrc = `
package b
import (
"a"
"html/template"
)
func _() {
// Packages should be fully qualified when there is ambiguity within the
// error string itself.
a.F(template /* ERRORx "cannot use.*html/template.* as .*text/template" */ .Template{})
}
`
csrc = `
package c
import (
"a"
"fmt"
"html/template"
)
// go.dev/issue/46905: make sure template is not the first package qualified.
var _ fmt.Stringer = 1 // ERRORx "cannot use 1.*as fmt\\.Stringer"
// Packages should be fully qualified when there is ambiguity in reachable
// packages. In this case both a (and for that matter html/template) import
// text/template.
func _() { a.G(template /* ERRORx "cannot use .*html/template.*Template" */ .Template{}) }
`
tsrc = `
package template
import "text/template"
type T int
// Verify that the current package name also causes disambiguation.
var _ T = template /* ERRORx "cannot use.*text/template.* as T value" */.Template{}
`
)
a := mustTypecheck(asrc, nil, nil)
conf := Config{Importer: importHelper{pkg: a, fallback: defaultImporter()}}
imp := importHelper{pkg: a, fallback: defaultImporter()}
// Packages should be fully qualified when there is ambiguity within the
// error string itself.
_, err := typecheck(bsrc, &conf, nil)
if err == nil {
t.Fatal("package b had no errors")
}
if !strings.Contains(err.Error(), "text/template") || !strings.Contains(err.Error(), "html/template") {
t.Errorf("type checking error for b does not disambiguate package template: %q", err)
withImporter := func(cfg *Config) {
cfg.Importer = imp
}
// ...and also when there is any ambiguity in reachable packages.
_, err = typecheck(csrc, &conf, nil)
if err == nil {
t.Fatal("package c had no errors")
}
if !strings.Contains(err.Error(), "html/template") {
t.Errorf("type checking error for c does not disambiguate package template: %q", err)
}
testFiles(t, []string{"b.go"}, [][]byte{[]byte(bsrc)}, 0, false, withImporter)
testFiles(t, []string{"c.go"}, [][]byte{[]byte(csrc)}, 0, false, withImporter)
testFiles(t, []string{"t.go"}, [][]byte{[]byte(tsrc)}, 0, false, withImporter)
}
func TestIssue50646(t *testing.T) {