diff options
Diffstat (limited to 'src/cmd/compile/internal/types2/issues_test.go')
-rw-r--r-- | src/cmd/compile/internal/types2/issues_test.go | 81 |
1 files changed, 56 insertions, 25 deletions
diff --git a/src/cmd/compile/internal/types2/issues_test.go b/src/cmd/compile/internal/types2/issues_test.go index 8bd42a5271..5e0ae213dc 100644 --- a/src/cmd/compile/internal/types2/issues_test.go +++ b/src/cmd/compile/internal/types2/issues_test.go @@ -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 - a := mustTypecheck(asrc, nil, nil) - conf := Config{Importer: importHelper{pkg: a, fallback: defaultImporter()}} +import ( + "a" + "html/template" +) +func _() { // 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) - } + a.F(template /* ERRORx "cannot use.*html/template.* as .*text/template" */ .Template{}) +} +` + csrc = ` +package c - // ...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) +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) + imp := importHelper{pkg: a, fallback: defaultImporter()} + + withImporter := func(cfg *Config) { + cfg.Importer = imp } + + 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) { |