reflect: unnamed interface types have no name

Fixes #15468

Change-Id: I8723171f87774a98d5e80e7832ebb96dd1fbea74
Reviewed-on: https://go-review.googlesource.com/22524
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
David Crawshaw 2016-04-27 12:49:27 -04:00
parent 74a9bad638
commit 217be5b35d
3 changed files with 21 additions and 10 deletions

View file

@ -5659,20 +5659,25 @@ type nameTest struct {
}
var nameTests = []nameTest{
{int32(0), "int32"},
{D1{}, "D1"},
{[]D1{}, ""},
{(chan D1)(nil), ""},
{(func() D1)(nil), ""},
{(<-chan D1)(nil), ""},
{(chan<- D1)(nil), ""},
{TheNameOfThisTypeIsExactly255BytesLongSoWhenTheCompilerPrependsTheReflectTestPackageNameAndExtraStarTheLinkerRuntimeAndReflectPackagesWillHaveToCorrectlyDecodeTheSecondLengthByte0123456789_0123456789_0123456789_0123456789_0123456789_012345678(0), "TheNameOfThisTypeIsExactly255BytesLongSoWhenTheCompilerPrependsTheReflectTestPackageNameAndExtraStarTheLinkerRuntimeAndReflectPackagesWillHaveToCorrectlyDecodeTheSecondLengthByte0123456789_0123456789_0123456789_0123456789_0123456789_012345678"},
{(*int32)(nil), "int32"},
{(*D1)(nil), "D1"},
{(*[]D1)(nil), ""},
{(*chan D1)(nil), ""},
{(*func() D1)(nil), ""},
{(*<-chan D1)(nil), ""},
{(*chan<- D1)(nil), ""},
{(*interface{})(nil), ""},
{(*interface {
F()
})(nil), ""},
{(*TheNameOfThisTypeIsExactly255BytesLongSoWhenTheCompilerPrependsTheReflectTestPackageNameAndExtraStarTheLinkerRuntimeAndReflectPackagesWillHaveToCorrectlyDecodeTheSecondLengthByte0123456789_0123456789_0123456789_0123456789_0123456789_012345678)(nil), "TheNameOfThisTypeIsExactly255BytesLongSoWhenTheCompilerPrependsTheReflectTestPackageNameAndExtraStarTheLinkerRuntimeAndReflectPackagesWillHaveToCorrectlyDecodeTheSecondLengthByte0123456789_0123456789_0123456789_0123456789_0123456789_012345678"},
}
func TestNames(t *testing.T) {
for _, test := range nameTests {
if got := TypeOf(test.v).Name(); got != test.want {
t.Errorf("%T Name()=%q, want %q", test.v, got, test.want)
typ := TypeOf(test.v).Elem()
if got := typ.Name(); got != test.want {
t.Errorf("%v Name()=%q, want %q", typ, got, test.want)
}
}
}

View file

@ -867,6 +867,9 @@ func (t *rtype) Name() string {
if hasPrefix(s, "func(") {
return ""
}
if hasPrefix(s, "interface {") {
return ""
}
switch s[0] {
case '[', '*', '<':
return ""

View file

@ -132,6 +132,9 @@ func (t *_type) name() string {
if hasPrefix(s, "func(") {
return ""
}
if hasPrefix(s, "interface {") {
return ""
}
switch s[0] {
case '[', '*', '<':
return ""