@@ -2371,6 +2371,7 @@ func StructOf(fields []StructField) Type {
2371
2371
2372
2372
lastzero := uintptr (0 )
2373
2373
repr = append (repr , "struct {" ... )
2374
+ pkgpath := ""
2374
2375
for i , field := range fields {
2375
2376
if field .Name == "" {
2376
2377
panic ("reflect.StructOf: field " + strconv .Itoa (i ) + " has no name" )
@@ -2381,11 +2382,18 @@ func StructOf(fields []StructField) Type {
2381
2382
if field .Type == nil {
2382
2383
panic ("reflect.StructOf: field " + strconv .Itoa (i ) + " has no type" )
2383
2384
}
2384
- f := runtimeStructField (field )
2385
+ f , fpkgpath := runtimeStructField (field )
2385
2386
ft := f .typ
2386
2387
if ft .kind & kindGCProg != 0 {
2387
2388
hasGCProg = true
2388
2389
}
2390
+ if fpkgpath != "" {
2391
+ if pkgpath == "" {
2392
+ pkgpath = fpkgpath
2393
+ } else if pkgpath != fpkgpath {
2394
+ panic ("reflect.Struct: fields with different PkgPath " + pkgpath + " and " + fpkgpath )
2395
+ }
2396
+ }
2389
2397
2390
2398
// Update string and hash
2391
2399
name := f .name .name ()
@@ -2617,6 +2625,9 @@ func StructOf(fields []StructField) Type {
2617
2625
prototype := * (* * structType )(unsafe .Pointer (& istruct ))
2618
2626
* typ = * prototype
2619
2627
typ .fields = fs
2628
+ if pkgpath != "" {
2629
+ typ .pkgPath = newName (pkgpath , "" , false )
2630
+ }
2620
2631
2621
2632
// Look in cache.
2622
2633
if ts , ok := structLookupCache .m .Load (hash ); ok {
@@ -2741,7 +2752,10 @@ func StructOf(fields []StructField) Type {
2741
2752
return addToCache (& typ .rtype )
2742
2753
}
2743
2754
2744
- func runtimeStructField (field StructField ) structField {
2755
+ // runtimeStructField takes a StructField value passed to StructOf and
2756
+ // returns both the corresponding internal representation, of type
2757
+ // structField, and the pkgpath value to use for this field.
2758
+ func runtimeStructField (field StructField ) (structField , string ) {
2745
2759
if field .Anonymous && field .PkgPath != "" {
2746
2760
panic ("reflect.StructOf: field \" " + field .Name + "\" is anonymous but has PkgPath set" )
2747
2761
}
@@ -2762,11 +2776,12 @@ func runtimeStructField(field StructField) structField {
2762
2776
}
2763
2777
2764
2778
resolveReflectType (field .Type .common ()) // install in runtime
2765
- return structField {
2779
+ f := structField {
2766
2780
name : newName (field .Name , string (field .Tag ), exported ),
2767
2781
typ : field .Type .common (),
2768
2782
offsetEmbed : offsetEmbed ,
2769
2783
}
2784
+ return f , field .PkgPath
2770
2785
}
2771
2786
2772
2787
// typeptrdata returns the length in bytes of the prefix of t
0 commit comments