]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.typeparams] go/types: adjust printing of embedded struct fields (fixes x/tools...
authorRobert Griesemer <gri@golang.org>
Fri, 19 Feb 2021 01:01:54 +0000 (17:01 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 19 Feb 2021 01:22:43 +0000 (01:22 +0000)
Prior to 1.16, go/types printed an embedded struct field by simply
printing its type, which may have included a package qualification.
Just printing the type is not useful with generic types and we now
must print the actual field name derived from the type - this leads
to different output for non-generic imported embedded types. Fix by
printing a package qualification in that case.

Change-Id: I2cb2484da7732428d13fdfb5fe4ec1fa1ee813a2
Reviewed-on: https://go-review.googlesource.com/c/go/+/293961
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>

src/go/types/typestring.go

index 64bbb3350545ed05ee2f9ab88687b6cf94dab4d3..6ddba08bdc04413d9763283ac418827b9d52e2f2 100644 (file)
@@ -126,6 +126,13 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) {
                        if i > 0 {
                                buf.WriteString("; ")
                        }
+                       // For compatibility with versions < go1.16, qualify the field name
+                       // of embedded fields with the package name. Various tests (such as
+                       // in x/tools/cmd/guru) depend on this output; and x/tools packages
+                       // are run against earlier versions of Go.
+                       if n, _ := f.typ.(*Named); f.embedded && n != nil && n.obj != nil && n.obj.pkg != nil {
+                               writePackage(buf, n.obj.pkg, qf)
+                       }
                        buf.WriteString(f.name)
                        if f.embedded {
                                // emphasize that the embedded field's name