Skip to content

Commit 2db31ef

Browse files
griesemerRobert Griesemer
authored and
Robert Griesemer
committed
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 <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]>
1 parent 499458f commit 2db31ef

File tree

1 file changed

+56
-25
lines changed

1 file changed

+56
-25
lines changed

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

+56-25
Original file line numberDiff line numberDiff line change
@@ -497,14 +497,14 @@ func TestIssue43088(t *testing.T) {
497497
// _ T2
498498
// }
499499
// }
500-
n1 := NewTypeName(syntax.Pos{}, nil, "T1", nil)
500+
n1 := NewTypeName(nopos, nil, "T1", nil)
501501
T1 := NewNamed(n1, nil, nil)
502-
n2 := NewTypeName(syntax.Pos{}, nil, "T2", nil)
502+
n2 := NewTypeName(nopos, nil, "T2", nil)
503503
T2 := NewNamed(n2, nil, nil)
504-
s1 := NewStruct([]*Var{NewField(syntax.Pos{}, nil, "_", T2, false)}, nil)
504+
s1 := NewStruct([]*Var{NewField(nopos, nil, "_", T2, false)}, nil)
505505
T1.SetUnderlying(s1)
506-
s2 := NewStruct([]*Var{NewField(syntax.Pos{}, nil, "_", T2, false)}, nil)
507-
s3 := NewStruct([]*Var{NewField(syntax.Pos{}, nil, "_", s2, false)}, nil)
506+
s2 := NewStruct([]*Var{NewField(nopos, nil, "_", T2, false)}, nil)
507+
s3 := NewStruct([]*Var{NewField(nopos, nil, "_", s2, false)}, nil)
508508
T2.SetUnderlying(s3)
509509

510510
// These calls must terminate (no endless recursion).
@@ -535,38 +535,69 @@ func TestIssue44515(t *testing.T) {
535535
}
536536

537537
func TestIssue43124(t *testing.T) {
538-
testenv.MustHaveGoBuild(t)
538+
// TODO(rFindley) move this to testdata by enhancing support for importing.
539+
540+
testenv.MustHaveGoBuild(t) // The go command is needed for the importer to determine the locations of stdlib .a files.
539541

540542
// All involved packages have the same name (template). Error messages should
541543
// disambiguate between text/template and html/template by printing the full
542544
// path.
543545
const (
544546
asrc = `package a; import "text/template"; func F(template.Template) {}; func G(int) {}`
545-
bsrc = `package b; import ("a"; "html/template"); func _() { a.F(template.Template{}) }`
546-
csrc = `package c; import ("a"; "html/template"); func _() { a.G(template.Template{}) }`
547-
)
547+
bsrc = `
548+
package b
548549
549-
a := mustTypecheck(asrc, nil, nil)
550-
conf := Config{Importer: importHelper{pkg: a, fallback: defaultImporter()}}
550+
import (
551+
"a"
552+
"html/template"
553+
)
551554
555+
func _() {
552556
// Packages should be fully qualified when there is ambiguity within the
553557
// error string itself.
554-
_, err := typecheck(bsrc, &conf, nil)
555-
if err == nil {
556-
t.Fatal("package b had no errors")
557-
}
558-
if !strings.Contains(err.Error(), "text/template") || !strings.Contains(err.Error(), "html/template") {
559-
t.Errorf("type checking error for b does not disambiguate package template: %q", err)
560-
}
558+
a.F(template /* ERRORx "cannot use.*html/template.* as .*text/template" */ .Template{})
559+
}
560+
`
561+
csrc = `
562+
package c
561563
562-
// ...and also when there is any ambiguity in reachable packages.
563-
_, err = typecheck(csrc, &conf, nil)
564-
if err == nil {
565-
t.Fatal("package c had no errors")
566-
}
567-
if !strings.Contains(err.Error(), "html/template") {
568-
t.Errorf("type checking error for c does not disambiguate package template: %q", err)
564+
import (
565+
"a"
566+
"fmt"
567+
"html/template"
568+
)
569+
570+
// go.dev/issue/46905: make sure template is not the first package qualified.
571+
var _ fmt.Stringer = 1 // ERRORx "cannot use 1.*as fmt\\.Stringer"
572+
573+
// Packages should be fully qualified when there is ambiguity in reachable
574+
// packages. In this case both a (and for that matter html/template) import
575+
// text/template.
576+
func _() { a.G(template /* ERRORx "cannot use .*html/template.*Template" */ .Template{}) }
577+
`
578+
579+
tsrc = `
580+
package template
581+
582+
import "text/template"
583+
584+
type T int
585+
586+
// Verify that the current package name also causes disambiguation.
587+
var _ T = template /* ERRORx "cannot use.*text/template.* as T value" */.Template{}
588+
`
589+
)
590+
591+
a := mustTypecheck(asrc, nil, nil)
592+
imp := importHelper{pkg: a, fallback: defaultImporter()}
593+
594+
withImporter := func(cfg *Config) {
595+
cfg.Importer = imp
569596
}
597+
598+
testFiles(t, []string{"b.go"}, [][]byte{[]byte(bsrc)}, 0, false, withImporter)
599+
testFiles(t, []string{"c.go"}, [][]byte{[]byte(csrc)}, 0, false, withImporter)
600+
testFiles(t, []string{"t.go"}, [][]byte{[]byte(tsrc)}, 0, false, withImporter)
570601
}
571602

572603
func TestIssue50646(t *testing.T) {

0 commit comments

Comments
 (0)