diff --git a/src/cmd/compile/internal/gc/reflect.go b/src/cmd/compile/internal/gc/reflect.go index da7bd56afe4..4bad490a641 100644 --- a/src/cmd/compile/internal/gc/reflect.go +++ b/src/cmd/compile/internal/gc/reflect.go @@ -1277,7 +1277,7 @@ ok: } ot = dcommontype(s, ot, t) - var pkg *Pkg + pkg := localpkg if t.Sym != nil { pkg = t.Sym.Pkg } diff --git a/src/reflect/all_test.go b/src/reflect/all_test.go index ebd352ca46a..0ce6588e988 100644 --- a/src/reflect/all_test.go +++ b/src/reflect/all_test.go @@ -2321,6 +2321,33 @@ func TestImportPath(t *testing.T) { } } +func TestFieldPkgPath(t *testing.T) { + typ := TypeOf(struct { + Exported string + unexported string + OtherPkgFields + }{}) + for _, test := range []struct { + index []int + pkgPath string + anonymous bool + }{ + {[]int{0}, "", false}, // Exported + {[]int{1}, "reflect_test", false}, // unexported + {[]int{2}, "", true}, // OtherPkgFields + {[]int{2, 0}, "", false}, // OtherExported + {[]int{2, 1}, "reflect", false}, // otherUnexported + } { + f := typ.FieldByIndex(test.index) + if got, want := f.PkgPath, test.pkgPath; got != want { + t.Errorf("Field(%d).PkgPath = %q, want %q", test.index, got, want) + } + if got, want := f.Anonymous, test.anonymous; got != want { + t.Errorf("Field(%d).Anonymous = %v, want %v", test.index, got, want) + } + } +} + func TestVariadicType(t *testing.T) { // Test example from Type documentation. var f func(x int, y ...float64) diff --git a/src/reflect/export_test.go b/src/reflect/export_test.go index 9db6967ffa6..ddc64b46be0 100644 --- a/src/reflect/export_test.go +++ b/src/reflect/export_test.go @@ -94,3 +94,8 @@ func FirstMethodNameBytes(t Type) *byte { } return m.name.bytes } + +type OtherPkgFields struct { + OtherExported int + otherUnexported int +}