Skip to content

Commit dfe0ef9

Browse files
griesemerfindleyr
authored andcommitted
[dev.typeparams] go/types, types2: revert fancy struct printing (fixes x/tools tests)
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 <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 2f37939 commit dfe0ef9

File tree

3 files changed

+13
-68
lines changed

3 files changed

+13
-68
lines changed

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

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,14 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) {
126126
if i > 0 {
127127
buf.WriteString("; ")
128128
}
129-
buf.WriteString(f.name)
130-
if f.embedded {
131-
// emphasize that the embedded field's name
132-
// doesn't match the field's type name
133-
if f.name != embeddedFieldName(f.typ) {
134-
buf.WriteString(" /* = ")
135-
writeType(buf, f.typ, qf, visited)
136-
buf.WriteString(" */")
137-
}
138-
} else {
129+
// This doesn't do the right thing for embedded type
130+
// aliases where we should print the alias name, not
131+
// the aliased type (see issue #44410).
132+
if !f.embedded {
133+
buf.WriteString(f.name)
139134
buf.WriteByte(' ')
140-
writeType(buf, f.typ, qf, visited)
141135
}
136+
writeType(buf, f.typ, qf, visited)
142137
if tag := t.Tag(i); tag != "" {
143138
fmt.Fprintf(buf, " %q", tag)
144139
}
@@ -423,25 +418,6 @@ func writeSignature(buf *bytes.Buffer, sig *Signature, qf Qualifier, visited []T
423418
writeTuple(buf, sig.results, false, qf, visited)
424419
}
425420

426-
// embeddedFieldName returns an embedded field's name given its type.
427-
// The result is "" if the type doesn't have an embedded field name.
428-
func embeddedFieldName(typ Type) string {
429-
switch t := typ.(type) {
430-
case *Basic:
431-
return t.name
432-
case *Named:
433-
return t.obj.name
434-
case *Pointer:
435-
// *T is ok, but **T is not
436-
if _, ok := t.base.(*Pointer); !ok {
437-
return embeddedFieldName(t.base)
438-
}
439-
case *instance:
440-
return t.base.obj.name
441-
}
442-
return "" // not a (pointer to) a defined type
443-
}
444-
445421
// subscript returns the decimal (utf8) representation of x using subscript digits.
446422
func subscript(x uint64) string {
447423
const w = len("₀") // all digits 0...9 have the same utf8 width

src/go/internal/gccgoimporter/importer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ var importerTests = [...]importerTest{
9494
{pkgpath: "nointerface", name: "I", want: "type I int"},
9595
{pkgpath: "issue29198", name: "FooServer", gccgoVersion: 7, want: "type FooServer struct{FooServer *FooServer; user string; ctx context.Context}"},
9696
{pkgpath: "issue30628", name: "Apple", want: "type Apple struct{hey sync.RWMutex; x int; RQ [517]struct{Count uintptr; NumBytes uintptr; Last uintptr}}"},
97-
{pkgpath: "issue31540", name: "S", gccgoVersion: 7, want: "type S struct{b int; A2 /* = map[Y]Z */}"},
97+
{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)
9898
{pkgpath: "issue34182", name: "T1", want: "type T1 struct{f *T2}"},
9999
{pkgpath: "notinheap", name: "S", want: "type S struct{}"},
100100
}

src/go/types/typestring.go

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -126,26 +126,14 @@ func writeType(buf *bytes.Buffer, typ Type, qf Qualifier, visited []Type) {
126126
if i > 0 {
127127
buf.WriteString("; ")
128128
}
129-
// For compatibility with versions < go1.16, qualify the field name
130-
// of embedded fields with the package name. Various tests (such as
131-
// in x/tools/cmd/guru) depend on this output; and x/tools packages
132-
// are run against earlier versions of Go.
133-
if n, _ := f.typ.(*Named); f.embedded && n != nil && n.obj != nil && n.obj.pkg != nil {
134-
writePackage(buf, n.obj.pkg, qf)
135-
}
136-
buf.WriteString(f.name)
137-
if f.embedded {
138-
// emphasize that the embedded field's name
139-
// doesn't match the field's type name
140-
if f.name != embeddedFieldName(f.typ) {
141-
buf.WriteString(" /* = ")
142-
writeType(buf, f.typ, qf, visited)
143-
buf.WriteString(" */")
144-
}
145-
} else {
129+
// This doesn't do the right thing for embedded type
130+
// aliases where we should print the alias name, not
131+
// the aliased type (see issue #44410).
132+
if !f.embedded {
133+
buf.WriteString(f.name)
146134
buf.WriteByte(' ')
147-
writeType(buf, f.typ, qf, visited)
148135
}
136+
writeType(buf, f.typ, qf, visited)
149137
if tag := t.Tag(i); tag != "" {
150138
fmt.Fprintf(buf, " %q", tag)
151139
}
@@ -431,25 +419,6 @@ func writeSignature(buf *bytes.Buffer, sig *Signature, qf Qualifier, visited []T
431419
writeTuple(buf, sig.results, false, qf, visited)
432420
}
433421

434-
// embeddedFieldName returns an embedded field's name given its type.
435-
// The result is "" if the type doesn't have an embedded field name.
436-
func embeddedFieldName(typ Type) string {
437-
switch t := typ.(type) {
438-
case *Basic:
439-
return t.name
440-
case *Named:
441-
return t.obj.name
442-
case *Pointer:
443-
// *T is ok, but **T is not
444-
if _, ok := t.base.(*Pointer); !ok {
445-
return embeddedFieldName(t.base)
446-
}
447-
case *instance:
448-
return t.base.obj.name
449-
}
450-
return "" // not a (pointer to) a defined type
451-
}
452-
453422
// subscript returns the decimal (utf8) representation of x using subscript digits.
454423
func subscript(x uint64) string {
455424
const w = len("₀") // all digits 0...9 have the same utf8 width

0 commit comments

Comments
 (0)