@@ -222,13 +222,13 @@ func Inline(logf func(string, ...any), caller *Caller, callee *Callee) ([]byte,
222
222
importDecl = & ast.GenDecl {Tok : token .IMPORT }
223
223
f .Decls = prepend [ast.Decl ](importDecl , f .Decls ... )
224
224
}
225
- for _ , spec := range res .newImports {
225
+ for _ , imp := range res .newImports {
226
226
// Check that the new imports are accessible.
227
- path , _ := strconv .Unquote (spec .Path .Value )
227
+ path , _ := strconv .Unquote (imp . spec .Path .Value )
228
228
if ! canImport (caller .Types .Path (), path ) {
229
229
return nil , fmt .Errorf ("can't inline function %v as its body refers to inaccessible package %q" , callee , path )
230
230
}
231
- importDecl .Specs = append (importDecl .Specs , spec )
231
+ importDecl .Specs = append (importDecl .Specs , imp . spec )
232
232
}
233
233
}
234
234
@@ -300,8 +300,13 @@ func Inline(logf func(string, ...any), caller *Caller, callee *Callee) ([]byte,
300
300
return newSrc , nil
301
301
}
302
302
303
+ type newImport struct {
304
+ pkgName string
305
+ spec * ast.ImportSpec
306
+ }
307
+
303
308
type result struct {
304
- newImports []* ast. ImportSpec
309
+ newImports []newImport
305
310
old , new ast.Node // e.g. replace call expr by callee function body expression
306
311
}
307
312
@@ -387,14 +392,14 @@ func inline(logf func(string, ...any), caller *Caller, callee *gobCallee) (*resu
387
392
}
388
393
389
394
// localImportName returns the local name for a given imported package path.
390
- var newImports []* ast. ImportSpec
391
- localImportName := func (path string , shadows map [ string ] bool ) string {
395
+ var newImports []newImport
396
+ localImportName := func (obj * object ) string {
392
397
// Does an import exist?
393
- for _ , name := range importMap [path ] {
398
+ for _ , name := range importMap [obj . PkgPath ] {
394
399
// Check that either the import preexisted,
395
400
// or that it was newly added (no PkgName) but is not shadowed,
396
401
// either in the callee (shadows) or caller (caller.lookup).
397
- if ! shadows [name ] {
402
+ if ! obj . Shadow [name ] {
398
403
found := caller .lookup (name )
399
404
if is [* types.PkgName ](found ) || found == nil {
400
405
return name
@@ -404,7 +409,7 @@ func inline(logf func(string, ...any), caller *Caller, callee *gobCallee) (*resu
404
409
405
410
newlyAdded := func (name string ) bool {
406
411
for _ , new := range newImports {
407
- if new .Name . Name == name {
412
+ if new .pkgName == name {
408
413
return true
409
414
}
410
415
}
@@ -419,29 +424,32 @@ func inline(logf func(string, ...any), caller *Caller, callee *gobCallee) (*resu
419
424
//
420
425
// "init" is not a legal PkgName.
421
426
//
422
- // TODO(adonovan ): preserve the PkgName used
423
- // in the original source, or, for a dot import,
424
- // use the package's declared name.
425
- base := pathpkg . Base ( path )
427
+ // TODO(rfindley ): is it worth preserving local package names for callee
428
+ // imports? Are they likely to be better or worse than the name we choose
429
+ // here?
430
+ base := obj . PkgName
426
431
name := base
427
- for n := 0 ; shadows [name ] || caller .lookup (name ) != nil || newlyAdded (name ) || name == "init" ; n ++ {
432
+ for n := 0 ; obj . Shadow [name ] || caller .lookup (name ) != nil || newlyAdded (name ) || name == "init" ; n ++ {
428
433
name = fmt .Sprintf ("%s%d" , base , n )
429
434
}
430
435
431
- // TODO(adonovan): don't use a renaming import
432
- // unless the local name differs from either
433
- // the package name or the last segment of path.
434
- // This requires that we tabulate (path, declared name, local name)
435
- // triples for each package referenced by the callee.
436
- logf ("adding import %s %q" , name , path )
437
- newImports = append (newImports , & ast.ImportSpec {
438
- Name : makeIdent (name ),
436
+ logf ("adding import %s %q" , name , obj .PkgPath )
437
+ spec := & ast.ImportSpec {
439
438
Path : & ast.BasicLit {
440
439
Kind : token .STRING ,
441
- Value : strconv .Quote (path ),
440
+ Value : strconv .Quote (obj . PkgPath ),
442
441
},
442
+ }
443
+ // Use explicit pkgname (out of necessity) when it differs from the declared name,
444
+ // or (for good style) when it differs from base(pkgpath).
445
+ if name != obj .PkgName || name != pathpkg .Base (obj .PkgPath ) {
446
+ spec .Name = makeIdent (name )
447
+ }
448
+ newImports = append (newImports , newImport {
449
+ pkgName : name ,
450
+ spec : spec ,
443
451
})
444
- importMap [path ] = append (importMap [path ], name )
452
+ importMap [obj . PkgPath ] = append (importMap [obj . PkgPath ], name )
445
453
return name
446
454
}
447
455
@@ -471,8 +479,7 @@ func inline(logf func(string, ...any), caller *Caller, callee *gobCallee) (*resu
471
479
var newName ast.Expr
472
480
if obj .Kind == "pkgname" {
473
481
// Use locally appropriate import, creating as needed.
474
- newName = makeIdent (localImportName (obj .PkgPath , obj .Shadow )) // imported package
475
-
482
+ newName = makeIdent (localImportName (& obj )) // imported package
476
483
} else if ! obj .ValidPos {
477
484
// Built-in function, type, or value (e.g. nil, zero):
478
485
// check not shadowed at caller.
@@ -515,7 +522,7 @@ func inline(logf func(string, ...any), caller *Caller, callee *gobCallee) (*resu
515
522
516
523
// Form a qualified identifier, pkg.Name.
517
524
if qualify {
518
- pkgName := localImportName (obj . PkgPath , obj . Shadow )
525
+ pkgName := localImportName (& obj )
519
526
newName = & ast.SelectorExpr {
520
527
X : makeIdent (pkgName ),
521
528
Sel : makeIdent (obj .Name ),
0 commit comments