diff --git a/src/cmd/compile/internal/types/fmt.go b/src/cmd/compile/internal/types/fmt.go index 3198a1f53c..1399483424 100644 --- a/src/cmd/compile/internal/types/fmt.go +++ b/src/cmd/compile/internal/types/fmt.go @@ -631,6 +631,7 @@ func fldconv(b *bytes.Buffer, f *Field, verb rune, mode fmtMode, visited map[*Ty } var name string + nameSep := " " if verb != 'S' { s := f.Sym @@ -639,9 +640,41 @@ func fldconv(b *bytes.Buffer, f *Field, verb rune, mode fmtMode, visited map[*Ty s = OrigSym(s) } - if s != nil && f.Embedded == 0 { + if s != nil { if funarg != FunargNone { name = fmt.Sprint(f.Nname) + } else if f.Embedded != 0 { + // Using type aliases and embedded fields, it's possible to + // construct types that can't be directly represented as a + // type literal. For example, given "type Int = int" (#50190), + // it would be incorrect to format "struct{ Int }" as either + // "struct{ int }" or "struct{ Int int }", because those each + // represent other, distinct types. + // + // So for the purpose of LinkString (i.e., fmtTypeID), we use + // the non-standard syntax "struct{ Int = int }" to represent + // embedded fields that have been renamed through the use of + // type aliases. + if mode == fmtTypeID { + // Compute styp, the symbol that would normally be used as + // the field name when embedding f.Type. + // TODO(mdempsky): Check for other occurences of this logic + // and deduplicate. + typ := f.Type + if typ.Sym() == nil && typ.IsPtr() { + typ = typ.Elem() + } + styp := typ.Sym() + if styp != nil && IsExported(styp.Name) { + styp = LocalPkg.Lookup(styp.Name) + } + + // If embedded field was renamed, use syntax extension. + if s != styp { + name = sconv(s, 0, mode) + nameSep = " = " + } + } } else if verb == 'L' { name = s.Name if name == ".F" { @@ -658,7 +691,7 @@ func fldconv(b *bytes.Buffer, f *Field, verb rune, mode fmtMode, visited map[*Ty if name != "" { b.WriteString(name) - b.WriteString(" ") + b.WriteString(nameSep) } if f.IsDDD() { diff --git a/test/fixedbugs/issue50190.go b/test/fixedbugs/issue50190.go new file mode 100644 index 0000000000..01ff6eacaa --- /dev/null +++ b/test/fixedbugs/issue50190.go @@ -0,0 +1,21 @@ +// run + +// Copyright 2021 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +type int float32 + +type Int = int + +type A = struct{ int } +type B = struct{ Int } + +func main() { + var x, y interface{} = A{}, B{} + if x == y { + panic("FAIL") + } +}