@@ -132,9 +132,7 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
132
132
pkgsNotFound := make ([]string , 0 , len (pkgs ))
133
133
134
134
// Do a post-order traversal and extract the package scope of each package
135
- packages .Visit (pkgs , func (pkg * packages.Package ) bool {
136
- return true
137
- }, func (pkg * packages.Package ) {
135
+ packages .Visit (pkgs , nil , func (pkg * packages.Package ) {
138
136
log .Printf ("Processing package %s." , pkg .PkgPath )
139
137
140
138
if _ , ok := pkgInfos [pkg .PkgPath ]; ! ok {
@@ -200,10 +198,8 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
200
198
noExtractRe := regexp .MustCompile (`.*(^|` + sep + `)(\.\.|vendor)($|` + sep + `).*` )
201
199
202
200
// extract AST information for all packages
203
- packages .Visit (pkgs , func (pkg * packages.Package ) bool {
204
- return true
205
- }, func (pkg * packages.Package ) {
206
- for root , _ := range wantedRoots {
201
+ packages .Visit (pkgs , nil , func (pkg * packages.Package ) {
202
+ for root := range wantedRoots {
207
203
pkgInfo := pkgInfos [pkg .PkgPath ]
208
204
relDir , err := filepath .Rel (root , pkgInfo .PkgDir )
209
205
if err != nil || noExtractRe .MatchString (relDir ) {
@@ -401,15 +397,15 @@ func extractObjects(tw *trap.Writer, scope *types.Scope, scopeLabel trap.Label)
401
397
// do not appear as objects in any scope, so they have to be dealt
402
398
// with separately in extractMethods.
403
399
if funcObj , ok := obj .(* types.Func ); ok {
404
- populateTypeParamParents (tw , funcObj .Type ().(* types.Signature ).TypeParams (), obj )
405
- populateTypeParamParents (tw , funcObj .Type ().(* types.Signature ).RecvTypeParams (), obj )
400
+ populateTypeParamParents (funcObj .Type ().(* types.Signature ).TypeParams (), obj )
401
+ populateTypeParamParents (funcObj .Type ().(* types.Signature ).RecvTypeParams (), obj )
406
402
}
407
403
// Populate type parameter parents for named types. Note that we
408
404
// skip type aliases as the original type should be the parent
409
405
// of any type parameters.
410
406
if typeNameObj , ok := obj .(* types.TypeName ); ok && ! typeNameObj .IsAlias () {
411
407
if tp , ok := typeNameObj .Type ().(* types.Named ); ok {
412
- populateTypeParamParents (tw , tp .TypeParams (), obj )
408
+ populateTypeParamParents (tp .TypeParams (), obj )
413
409
}
414
410
}
415
411
extractObject (tw , obj , lbl )
@@ -435,8 +431,8 @@ func extractMethod(tw *trap.Writer, meth *types.Func) trap.Label {
435
431
if ! exists {
436
432
// Populate type parameter parents for methods. They do not appear as
437
433
// objects in any scope, so they have to be dealt with separately here.
438
- populateTypeParamParents (tw , meth .Type ().(* types.Signature ).TypeParams (), meth )
439
- populateTypeParamParents (tw , meth .Type ().(* types.Signature ).RecvTypeParams (), meth )
434
+ populateTypeParamParents (meth .Type ().(* types.Signature ).TypeParams (), meth )
435
+ populateTypeParamParents (meth .Type ().(* types.Signature ).RecvTypeParams (), meth )
440
436
extractObject (tw , meth , methlbl )
441
437
}
442
438
@@ -494,7 +490,7 @@ func extractObject(tw *trap.Writer, obj types.Object, lbl trap.Label) {
494
490
func extractObjectTypes (tw * trap.Writer ) {
495
491
// calling `extractType` on a named type will extract all methods defined
496
492
// on it, which will add new objects. Therefore we need to do this first
497
- // before we loops over all objects and emit them.
493
+ // before we loop over all objects and emit them.
498
494
changed := true
499
495
for changed {
500
496
changed = tw .ForEachObject (extractObjectType )
@@ -870,6 +866,10 @@ func extractExpr(tw *trap.Writer, expr ast.Expr, parent trap.Label, idx int) {
870
866
kind = dbscheme .IdentExpr .Index ()
871
867
dbscheme .LiteralsTable .Emit (tw , lbl , expr .Name , expr .Name )
872
868
def := tw .Package .TypesInfo .Defs [expr ]
869
+ // Note that there are some cases where `expr` is in the map but `def`
870
+ // is nil. The docs for `tw.Package.TypesInfo.Defs` give the following
871
+ // examples: the package name in package clauses, or symbolic variables
872
+ // `t` in `t := x.(type)` of type switch headers.
873
873
if def != nil {
874
874
defTyp := extractType (tw , def .Type ())
875
875
objlbl , exists := tw .Labeler .LookupObjectID (def , defTyp )
@@ -1010,7 +1010,10 @@ func extractExpr(tw *trap.Writer, expr ast.Expr, parent trap.Label, idx int) {
1010
1010
}
1011
1011
kind = dbscheme .TypeAssertExpr .Index ()
1012
1012
extractExpr (tw , expr .X , lbl , 0 )
1013
- extractExpr (tw , expr .Type , lbl , 1 )
1013
+ // expr.Type can be `nil` if this is the `x.(type)` in a type switch.
1014
+ if expr .Type != nil {
1015
+ extractExpr (tw , expr .Type , lbl , 1 )
1016
+ }
1014
1017
case * ast.CallExpr :
1015
1018
if expr == nil {
1016
1019
return
@@ -1126,11 +1129,9 @@ func extractExpr(tw *trap.Writer, expr ast.Expr, parent trap.Label, idx int) {
1126
1129
// each child over its preceding child (usually either 1 for assigning increasing indices, or
1127
1130
// -1 for decreasing indices)
1128
1131
func extractExprs (tw * trap.Writer , exprs []ast.Expr , parent trap.Label , idx int , dir int ) {
1129
- if exprs != nil {
1130
- for _ , expr := range exprs {
1131
- extractExpr (tw , expr , parent , idx )
1132
- idx += dir
1133
- }
1132
+ for _ , expr := range exprs {
1133
+ extractExpr (tw , expr , parent , idx )
1134
+ idx += dir
1134
1135
}
1135
1136
}
1136
1137
@@ -1386,13 +1387,10 @@ func extractStmt(tw *trap.Writer, stmt ast.Stmt, parent trap.Label, idx int) {
1386
1387
// each child over its preceding child (usually either 1 for assigning increasing indices, or
1387
1388
// -1 for decreasing indices)
1388
1389
func extractStmts (tw * trap.Writer , stmts []ast.Stmt , parent trap.Label , idx int , dir int ) {
1389
- if stmts != nil {
1390
- for _ , stmt := range stmts {
1391
- extractStmt (tw , stmt , parent , idx )
1392
- idx += dir
1393
- }
1390
+ for _ , stmt := range stmts {
1391
+ extractStmt (tw , stmt , parent , idx )
1392
+ idx += dir
1394
1393
}
1395
-
1396
1394
}
1397
1395
1398
1396
// extractDecl extracts AST information for the given declaration
@@ -1941,7 +1939,7 @@ func extractTypeParamDecls(tw *trap.Writer, fields *ast.FieldList, parent trap.L
1941
1939
}
1942
1940
1943
1941
// populateTypeParamParents sets `parent` as the parent of the elements of `typeparams`
1944
- func populateTypeParamParents (tw * trap. Writer , typeparams * types.TypeParamList , parent types.Object ) {
1942
+ func populateTypeParamParents (typeparams * types.TypeParamList , parent types.Object ) {
1945
1943
if typeparams != nil {
1946
1944
for idx := 0 ; idx < typeparams .Len (); idx ++ {
1947
1945
setTypeParamParent (typeparams .At (idx ), parent )
@@ -1963,16 +1961,6 @@ func getObjectBeingUsed(tw *trap.Writer, ident *ast.Ident) types.Object {
1963
1961
}
1964
1962
}
1965
1963
1966
- // tryGetGenericType returns the generic type of `tp`, and a boolean indicating
1967
- // whether it is the same as `tp`.
1968
- func tryGetGenericType (tp types.Type ) (* types.Named , bool ) {
1969
- if namedType , ok := tp .(* types.Named ); ok {
1970
- originType := namedType .Origin ()
1971
- return originType , namedType == originType
1972
- }
1973
- return nil , false
1974
- }
1975
-
1976
1964
// trackInstantiatedStructFields tries to give the fields of an instantiated
1977
1965
// struct type underlying `tp` the same labels as the corresponding fields of
1978
1966
// the generic struct type. This is so that when we come across the
0 commit comments