]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: make TestIssue43124 match the go/types version
authorRobert Griesemer <gri@golang.org>
Thu, 29 Jun 2023 16:48:52 +0000 (09:48 -0700)
committerRobert Griesemer <gri@google.com>
Thu, 29 Jun 2023 18:29:51 +0000 (18:29 +0000)
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>

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

index 8bd42a5271517fc9d9159fb30a71e90e72443353..5e0ae213dcb2efa2d2888118f0307ccaa44071ea 100644 (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
 
-       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) {