@@ -132,9 +132,7 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
132132 pkgsNotFound := make ([]string , 0 , len (pkgs ))
133133
134134 // 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 ) {
138136 log .Printf ("Processing package %s." , pkg .PkgPath )
139137
140138 if _ , ok := pkgInfos [pkg .PkgPath ]; ! ok {
@@ -200,10 +198,8 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
200198 noExtractRe := regexp .MustCompile (`.*(^|` + sep + `)(\.\.|vendor)($|` + sep + `).*` )
201199
202200 // 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 {
207203 pkgInfo := pkgInfos [pkg .PkgPath ]
208204 relDir , err := filepath .Rel (root , pkgInfo .PkgDir )
209205 if err != nil || noExtractRe .MatchString (relDir ) {
@@ -401,15 +397,15 @@ func extractObjects(tw *trap.Writer, scope *types.Scope, scopeLabel trap.Label)
401397 // do not appear as objects in any scope, so they have to be dealt
402398 // with separately in extractMethods.
403399 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 )
406402 }
407403 // Populate type parameter parents for named types. Note that we
408404 // skip type aliases as the original type should be the parent
409405 // of any type parameters.
410406 if typeNameObj , ok := obj .(* types.TypeName ); ok && ! typeNameObj .IsAlias () {
411407 if tp , ok := typeNameObj .Type ().(* types.Named ); ok {
412- populateTypeParamParents (tw , tp .TypeParams (), obj )
408+ populateTypeParamParents (tp .TypeParams (), obj )
413409 }
414410 }
415411 extractObject (tw , obj , lbl )
@@ -435,8 +431,8 @@ func extractMethod(tw *trap.Writer, meth *types.Func) trap.Label {
435431 if ! exists {
436432 // Populate type parameter parents for methods. They do not appear as
437433 // 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 )
440436 extractObject (tw , meth , methlbl )
441437 }
442438
@@ -494,7 +490,7 @@ func extractObject(tw *trap.Writer, obj types.Object, lbl trap.Label) {
494490func extractObjectTypes (tw * trap.Writer ) {
495491 // calling `extractType` on a named type will extract all methods defined
496492 // 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.
498494 changed := true
499495 for changed {
500496 changed = tw .ForEachObject (extractObjectType )
@@ -870,6 +866,10 @@ func extractExpr(tw *trap.Writer, expr ast.Expr, parent trap.Label, idx int) {
870866 kind = dbscheme .IdentExpr .Index ()
871867 dbscheme .LiteralsTable .Emit (tw , lbl , expr .Name , expr .Name )
872868 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.
873873 if def != nil {
874874 defTyp := extractType (tw , def .Type ())
875875 objlbl , exists := tw .Labeler .LookupObjectID (def , defTyp )
@@ -1010,7 +1010,10 @@ func extractExpr(tw *trap.Writer, expr ast.Expr, parent trap.Label, idx int) {
10101010 }
10111011 kind = dbscheme .TypeAssertExpr .Index ()
10121012 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+ }
10141017 case * ast.CallExpr :
10151018 if expr == nil {
10161019 return
@@ -1126,11 +1129,9 @@ func extractExpr(tw *trap.Writer, expr ast.Expr, parent trap.Label, idx int) {
11261129// each child over its preceding child (usually either 1 for assigning increasing indices, or
11271130// -1 for decreasing indices)
11281131func 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
11341135 }
11351136}
11361137
@@ -1386,13 +1387,10 @@ func extractStmt(tw *trap.Writer, stmt ast.Stmt, parent trap.Label, idx int) {
13861387// each child over its preceding child (usually either 1 for assigning increasing indices, or
13871388// -1 for decreasing indices)
13881389func 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
13941393 }
1395-
13961394}
13971395
13981396// extractDecl extracts AST information for the given declaration
@@ -1941,7 +1939,7 @@ func extractTypeParamDecls(tw *trap.Writer, fields *ast.FieldList, parent trap.L
19411939}
19421940
19431941// 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 ) {
19451943 if typeparams != nil {
19461944 for idx := 0 ; idx < typeparams .Len (); idx ++ {
19471945 setTypeParamParent (typeparams .At (idx ), parent )
@@ -1963,16 +1961,6 @@ func getObjectBeingUsed(tw *trap.Writer, ident *ast.Ident) types.Object {
19631961 }
19641962}
19651963
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-
19761964// trackInstantiatedStructFields tries to give the fields of an instantiated
19771965// struct type underlying `tp` the same labels as the corresponding fields of
19781966// the generic struct type. This is so that when we come across the
0 commit comments