]> Cypherpunks.ru repositories - gostls13.git/commitdiff
[dev.typeparams] go/types, types2: revert fancy struct printing (fixes x/tools tests)
authorRobert Griesemer <gri@golang.org>
Fri, 19 Feb 2021 02:06:01 +0000 (18:06 -0800)
committerRobert Findley <rfindley@google.com>
Fri, 19 Feb 2021 12:42:07 +0000 (12:42 +0000)
An embedded struct field is embedded by mentioning its type.
The fact that the field name may be different and derived
from the type doesn't matter for the struct type.

Do print the embedded type rather than the derived field
name, as we have always done in the past. Remove the fancy
new code which was just plain wrong.

The struct output printing is only relevant for debugging
and test cases. Reverting to the original code (pre-generics)
fixes a couple of x/tools tests.

Unfortunately, the original code is (also) not correct for
embedded type aliases. Adjusted a gccgoimporter test
accordingly and filed issue #44410.

This is a follow-up on https://golang.org/cl/293961 which
addressed the issue only partially and left the incorrect
code in place.

Change-Id: Icb7a89c12ef7929c221fb1a5792f144f7fcd5855
Reviewed-on: https://go-review.googlesource.com/c/go/+/293962
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/typestring.go
src/go/internal/gccgoimporter/importer_test.go
src/go/types/typestring.go

index 47b2c259e5a39b68a2a344b30a0aec250b3d5d04..af44624d2c1eb5a9f74e169e7ee61f8a69e0b944 100644 (file)
@@ -126,19 +126,14 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) {
                        if i > 0 {
                                buf.WriteString("; ")
                        }
-                       buf.WriteString(f.name)
-                       if f.embedded {
-                               // emphasize that the embedded field's name
-                               // doesn't match the field's type name
-                               if f.name != embeddedFieldName(f.typ) {
-                                       buf.WriteString(" /* = ")
-                                       writeType(buf, f.typ, qf, visited)
-                                       buf.WriteString(" */")
-                               }
-                       } else {
+                       // This doesn't do the right thing for embedded type
+                       // aliases where we should print the alias name, not
+                       // the aliased type (see issue #44410).
+                       if !f.embedded {
+                               buf.WriteString(f.name)
                                buf.WriteByte(' ')
-                               writeType(buf, f.typ, qf, visited)
                        }
+                       writeType(buf, f.typ, qf, visited)
                        if tag := t.Tag(i); tag != "" {
                                fmt.Fprintf(buf, " %q", tag)
                        }
@@ -423,25 +418,6 @@ func writeSignature(buf *bytes.Buffer, sig *Signature, qf Qualifier, visited []T
        writeTuple(buf, sig.results, false, qf, visited)
 }
 
-// embeddedFieldName returns an embedded field's name given its type.
-// The result is "" if the type doesn't have an embedded field name.
-func embeddedFieldName(typ Type) string {
-       switch t := typ.(type) {
-       case *Basic:
-               return t.name
-       case *Named:
-               return t.obj.name
-       case *Pointer:
-               // *T is ok, but **T is not
-               if _, ok := t.base.(*Pointer); !ok {
-                       return embeddedFieldName(t.base)
-               }
-       case *instance:
-               return t.base.obj.name
-       }
-       return "" // not a (pointer to) a defined type
-}
-
 // subscript returns the decimal (utf8) representation of x using subscript digits.
 func subscript(x uint64) string {
        const w = len("₀") // all digits 0...9 have the same utf8 width
index c5b520feb4c11a937597ac4e1e53180e05290d30..b3f39312be2fb2c5ed68afde9832ea9b7640ff4e 100644 (file)
@@ -94,7 +94,7 @@ var importerTests = [...]importerTest{
        {pkgpath: "nointerface", name: "I", want: "type I int"},
        {pkgpath: "issue29198", name: "FooServer", gccgoVersion: 7, want: "type FooServer struct{FooServer *FooServer; user string; ctx context.Context}"},
        {pkgpath: "issue30628", name: "Apple", want: "type Apple struct{hey sync.RWMutex; x int; RQ [517]struct{Count uintptr; NumBytes uintptr; Last uintptr}}"},
-       {pkgpath: "issue31540", name: "S", gccgoVersion: 7, want: "type S struct{b int; A2 /* = map[Y]Z */}"},
+       {pkgpath: "issue31540", name: "S", gccgoVersion: 7, want: "type S struct{b int; map[Y]Z}"}, // should want "type S struct{b int; A2}" (issue  #44410)
        {pkgpath: "issue34182", name: "T1", want: "type T1 struct{f *T2}"},
        {pkgpath: "notinheap", name: "S", want: "type S struct{}"},
 }
index 6ddba08bdc04413d9763283ac418827b9d52e2f2..4697bd31e6636a82e60189bfeca783e2b85d87af 100644 (file)
@@ -126,26 +126,14 @@ 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
-                               // doesn't match the field's type name
-                               if f.name != embeddedFieldName(f.typ) {
-                                       buf.WriteString(" /* = ")
-                                       writeType(buf, f.typ, qf, visited)
-                                       buf.WriteString(" */")
-                               }
-                       } else {
+                       // This doesn't do the right thing for embedded type
+                       // aliases where we should print the alias name, not
+                       // the aliased type (see issue #44410).
+                       if !f.embedded {
+                               buf.WriteString(f.name)
                                buf.WriteByte(' ')
-                               writeType(buf, f.typ, qf, visited)
                        }
+                       writeType(buf, f.typ, qf, visited)
                        if tag := t.Tag(i); tag != "" {
                                fmt.Fprintf(buf, " %q", tag)
                        }
@@ -431,25 +419,6 @@ func writeSignature(buf *bytes.Buffer, sig *Signature, qf Qualifier, visited []T
        writeTuple(buf, sig.results, false, qf, visited)
 }
 
-// embeddedFieldName returns an embedded field's name given its type.
-// The result is "" if the type doesn't have an embedded field name.
-func embeddedFieldName(typ Type) string {
-       switch t := typ.(type) {
-       case *Basic:
-               return t.name
-       case *Named:
-               return t.obj.name
-       case *Pointer:
-               // *T is ok, but **T is not
-               if _, ok := t.base.(*Pointer); !ok {
-                       return embeddedFieldName(t.base)
-               }
-       case *instance:
-               return t.base.obj.name
-       }
-       return "" // not a (pointer to) a defined type
-}
-
 // subscript returns the decimal (utf8) representation of x using subscript digits.
 func subscript(x uint64) string {
        const w = len("₀") // all digits 0...9 have the same utf8 width