diff --git a/src/reflect/all_test.go b/src/reflect/all_test.go index b0a2608afb..ebd352ca46 100644 --- a/src/reflect/all_test.go +++ b/src/reflect/all_test.go @@ -5065,3 +5065,16 @@ func TestNames(t *testing.T) { } } } + +type embed struct { + EmbedWithUnexpMeth +} + +func TestNameBytesAreAligned(t *testing.T) { + typ := TypeOf(embed{}) + b := FirstMethodNameBytes(typ) + v := uintptr(unsafe.Pointer(b)) + if v%unsafe.Alignof((*byte)(nil)) != 0 { + t.Errorf("reflect.name.bytes pointer is not aligned: %x", v) + } +} diff --git a/src/reflect/export_test.go b/src/reflect/export_test.go index e518a16b53..9db6967ffa 100644 --- a/src/reflect/export_test.go +++ b/src/reflect/export_test.go @@ -70,3 +70,27 @@ func CachedBucketOf(m Type) Type { tt := (*mapType)(unsafe.Pointer(t)) return tt.bucket } + +type EmbedWithUnexpMeth struct{} + +func (EmbedWithUnexpMeth) f() {} + +type pinUnexpMeth interface { + f() +} + +var pinUnexpMethI = pinUnexpMeth(EmbedWithUnexpMeth{}) + +func FirstMethodNameBytes(t Type) *byte { + _ = pinUnexpMethI + + ut := t.uncommon() + if ut == nil { + panic("type has no methods") + } + m := ut.methods[0] + if *m.name.data(0)&(1<<2) == 0 { + panic("method name does not have pkgPath *string") + } + return m.name.bytes +}