Skip to content

Commit 7feb424

Browse files
committed
cmd/compile: fix PtrTo(t) for unnamed t with embedded fields
Fixes #8427. Change-Id: I826a3bc4519845ad30d6dbaf058fe7ed7bee8db0 Reviewed-on: https://go-review.googlesource.com/12233 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent bed6326 commit 7feb424

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/cmd/compile/internal/gc/reflect.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,10 +760,11 @@ func dcommontype(s *Sym, ot int, t *Type) int {
760760
}
761761

762762
var sptr *Sym
763-
if t.Sym != nil && !Isptr[t.Etype] {
764-
sptr = dtypesym(Ptrto(t))
763+
tptr := Ptrto(t)
764+
if !Isptr[t.Etype] && (t.Sym != nil || methods(tptr) != nil) {
765+
sptr = dtypesym(tptr)
765766
} else {
766-
sptr = weaktypesym(Ptrto(t))
767+
sptr = weaktypesym(tptr)
767768
}
768769

769770
// All (non-reflect-allocated) Types share the same zero object.

src/reflect/all_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4721,3 +4721,16 @@ func TestTypeOfTypeOf(t *testing.T) {
47214721
check("PtrTo", PtrTo(TypeOf(T{})))
47224722
check("SliceOf", SliceOf(TypeOf(T{})))
47234723
}
4724+
4725+
type XM struct{}
4726+
4727+
func (*XM) String() string { return "" }
4728+
4729+
func TestPtrToMethods(t *testing.T) {
4730+
var y struct{ XM }
4731+
yp := New(TypeOf(y)).Interface()
4732+
_, ok := yp.(fmt.Stringer)
4733+
if !ok {
4734+
t.Fatal("does not implement Stringer, but should")
4735+
}
4736+
}

0 commit comments

Comments
 (0)