@@ -72,22 +72,22 @@ func BImportData(imports map[string]*types.Package, data []byte, path string) (i
72
72
// read consts
73
73
for i := p .int (); i > 0 ; i -- {
74
74
name := p .string ()
75
- typ := p .typ ()
75
+ typ := p .typ (nil )
76
76
val := p .value ()
77
77
p .declare (types .NewConst (token .NoPos , pkg , name , typ , val ))
78
78
}
79
79
80
80
// read vars
81
81
for i := p .int (); i > 0 ; i -- {
82
82
name := p .string ()
83
- typ := p .typ ()
83
+ typ := p .typ (nil )
84
84
p .declare (types .NewVar (token .NoPos , pkg , name , typ ))
85
85
}
86
86
87
87
// read funcs
88
88
for i := p .int (); i > 0 ; i -- {
89
89
name := p .string ()
90
- sig := p .typ ().(* types.Signature )
90
+ sig := p .typ (nil ).(* types.Signature )
91
91
p .int () // read and discard index of inlined function body
92
92
p .declare (types .NewFunc (token .NoPos , pkg , name , sig ))
93
93
}
@@ -97,7 +97,7 @@ func BImportData(imports map[string]*types.Package, data []byte, path string) (i
97
97
// name is parsed as part of named type and the
98
98
// type object is added to scope via respective
99
99
// named type
100
- _ = p .typ ().(* types.Named )
100
+ _ = p .typ (nil ).(* types.Named )
101
101
}
102
102
103
103
// ignore compiler-specific import data
@@ -190,7 +190,11 @@ type dddSlice struct {
190
190
func (t * dddSlice ) Underlying () types.Type { return t }
191
191
func (t * dddSlice ) String () string { return "..." + t .elem .String () }
192
192
193
- func (p * importer ) typ () types.Type {
193
+ // parent is the package which declared the type; parent == nil means
194
+ // the package currently imported. The parent package is needed for
195
+ // exported struct fields and interface methods which don't contain
196
+ // explicit package information in the export data.
197
+ func (p * importer ) typ (parent * types.Package ) types.Type {
194
198
// if the type was seen before, i is its index (>= 0)
195
199
i := p .tagOrIndex ()
196
200
if i >= 0 {
@@ -202,18 +206,18 @@ func (p *importer) typ() types.Type {
202
206
case namedTag :
203
207
// read type object
204
208
name := p .string ()
205
- tpkg : = p .pkg ()
206
- scope := tpkg .Scope ()
209
+ parent = p .pkg ()
210
+ scope := parent .Scope ()
207
211
obj := scope .Lookup (name )
208
212
209
213
// if the object doesn't exist yet, create and insert it
210
214
if obj == nil {
211
- obj = types .NewTypeName (token .NoPos , tpkg , name , nil )
215
+ obj = types .NewTypeName (token .NoPos , parent , name , nil )
212
216
scope .Insert (obj )
213
217
}
214
218
215
219
if _ , ok := obj .(* types.TypeName ); ! ok {
216
- panic (fmt .Sprintf ("pkg = %s, name = %s => %s" , tpkg , name , obj ))
220
+ panic (fmt .Sprintf ("pkg = %s, name = %s => %s" , parent , name , obj ))
217
221
}
218
222
219
223
// associate new named type with obj if it doesn't exist yet
@@ -224,7 +228,7 @@ func (p *importer) typ() types.Type {
224
228
p .record (t )
225
229
226
230
// read underlying type
227
- t0 .SetUnderlying (p .typ ())
231
+ t0 .SetUnderlying (p .typ (parent ))
228
232
229
233
// interfaces don't have associated methods
230
234
if _ , ok := t0 .Underlying ().(* types.Interface ); ok {
@@ -239,7 +243,7 @@ func (p *importer) typ() types.Type {
239
243
result , _ := p .paramList ()
240
244
p .int () // read and discard index of inlined function body
241
245
sig := types .NewSignature (recv .At (0 ), params , result , isddd )
242
- t0 .AddMethod (types .NewFunc (token .NoPos , tpkg , name , sig ))
246
+ t0 .AddMethod (types .NewFunc (token .NoPos , parent , name , sig ))
243
247
}
244
248
245
249
return t
@@ -249,21 +253,21 @@ func (p *importer) typ() types.Type {
249
253
p .record (t )
250
254
251
255
n := p .int64 ()
252
- * t = * types .NewArray (p .typ (), n )
256
+ * t = * types .NewArray (p .typ (parent ), n )
253
257
return t
254
258
255
259
case sliceTag :
256
260
t := new (types.Slice )
257
261
p .record (t )
258
262
259
- * t = * types .NewSlice (p .typ ())
263
+ * t = * types .NewSlice (p .typ (parent ))
260
264
return t
261
265
262
266
case dddTag :
263
267
t := new (dddSlice )
264
268
p .record (t )
265
269
266
- t .elem = p .typ ()
270
+ t .elem = p .typ (parent )
267
271
return t
268
272
269
273
case structTag :
@@ -274,7 +278,7 @@ func (p *importer) typ() types.Type {
274
278
fields := make ([]* types.Var , n )
275
279
tags := make ([]string , n )
276
280
for i := range fields {
277
- fields [i ] = p .field ()
281
+ fields [i ] = p .field (parent )
278
282
tags [i ] = p .string ()
279
283
}
280
284
* t = * types .NewStruct (fields , tags )
@@ -284,7 +288,7 @@ func (p *importer) typ() types.Type {
284
288
t := new (types.Pointer )
285
289
p .record (t )
286
290
287
- * t = * types .NewPointer (p .typ ())
291
+ * t = * types .NewPointer (p .typ (parent ))
288
292
return t
289
293
290
294
case signatureTag :
@@ -312,7 +316,7 @@ func (p *importer) typ() types.Type {
312
316
// read methods
313
317
methods := make ([]* types.Func , p .int ())
314
318
for i := range methods {
315
- pkg , name := p .fieldName ()
319
+ pkg , name := p .fieldName (parent )
316
320
params , isddd := p .paramList ()
317
321
result , _ := p .paramList ()
318
322
sig := types .NewSignature (nil , params , result , isddd )
@@ -327,8 +331,8 @@ func (p *importer) typ() types.Type {
327
331
t := new (types.Map )
328
332
p .record (t )
329
333
330
- key := p .typ ()
331
- val := p .typ ()
334
+ key := p .typ (parent )
335
+ val := p .typ (parent )
332
336
* t = * types .NewMap (key , val )
333
337
return t
334
338
@@ -348,7 +352,7 @@ func (p *importer) typ() types.Type {
348
352
default :
349
353
panic (fmt .Sprintf ("unexpected channel dir %d" , d ))
350
354
}
351
- val := p .typ ()
355
+ val := p .typ (parent )
352
356
* t = * types .NewChan (dir , val )
353
357
return t
354
358
@@ -357,18 +361,18 @@ func (p *importer) typ() types.Type {
357
361
}
358
362
}
359
363
360
- func (p * importer ) field () * types.Var {
361
- pkg , name := p .fieldName ()
362
- typ := p .typ ()
364
+ func (p * importer ) field (parent * types. Package ) * types.Var {
365
+ pkg , name := p .fieldName (parent )
366
+ typ := p .typ (parent )
363
367
364
368
anonymous := false
365
369
if name == "" {
366
370
// anonymous field - typ must be T or *T and T must be a type name
367
371
switch typ := deref (typ ).(type ) {
368
372
case * types.Basic : // basic types are named types
373
+ pkg = nil // // objects defined in Universe scope have no package
369
374
name = typ .Name ()
370
375
case * types.Named :
371
- pkg = p .pkgList [0 ]
372
376
name = typ .Obj ().Name ()
373
377
default :
374
378
panic ("anonymous field expected" )
@@ -379,15 +383,20 @@ func (p *importer) field() *types.Var {
379
383
return types .NewField (token .NoPos , pkg , name , typ , anonymous )
380
384
}
381
385
382
- func (p * importer ) fieldName () (* types.Package , string ) {
386
+ func (p * importer ) fieldName (parent * types.Package ) (* types.Package , string ) {
387
+ pkg := parent
388
+ if pkg == nil {
389
+ // use the imported package instead
390
+ pkg = p .pkgList [0 ]
391
+ }
383
392
name := p .string ()
384
393
if name == "" {
385
- return nil , "" // anonymous field
394
+ return pkg , "" // anonymous
386
395
}
387
- pkg := p .pkgList [0 ]
388
396
if name == "?" || name != "_" && ! exported (name ) {
397
+ // explicitly qualified field
389
398
if name == "?" {
390
- name = ""
399
+ name = "" // anonymous
391
400
}
392
401
pkg = p .pkg ()
393
402
}
@@ -415,7 +424,7 @@ func (p *importer) paramList() (*types.Tuple, bool) {
415
424
}
416
425
417
426
func (p * importer ) param (named bool ) (* types.Var , bool ) {
418
- t := p .typ ()
427
+ t := p .typ (nil )
419
428
td , isddd := t .(* dddSlice )
420
429
if isddd {
421
430
t = types .NewSlice (td .elem )
0 commit comments