Skip to content

Commit 32ea94e

Browse files
authored
Merge pull request github#16123 from owen-mc/go/misc-trivial-fixes
Go: miscellaneous trivial fixes
2 parents b8b8e2b + 7fc5265 commit 32ea94e

File tree

4 files changed

+53
-41
lines changed

4 files changed

+53
-41
lines changed

go/extractor/extractor.go

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {
494490
func 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)
11281131
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
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)
13881389
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
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

go/ql/lib/semmle/go/Expr.qll

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -754,13 +754,19 @@ class SliceExpr extends @sliceexpr, Expr {
754754
*
755755
* ```go
756756
* x.(T)
757+
* x.(type)
757758
* ```
758759
*/
759760
class TypeAssertExpr extends @typeassertexpr, Expr {
760761
/** Gets the base expression whose type is being asserted. */
761762
Expr getExpr() { result = this.getChildExpr(0) }
762763

763-
/** Gets the expression representing the asserted type. */
764+
/**
765+
* Gets the expression representing the asserted type.
766+
*
767+
* Note that this is not defined when the type assertion is of the form
768+
* `x.(type)`, as found in type switches.
769+
*/
764770
Expr getTypeExpr() { result = this.getChildExpr(1) }
765771

766772
override predicate mayHaveOwnSideEffects() { any() }

go/ql/lib/semmle/go/Scopes.qll

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,13 @@ class Entity extends @object {
119119
*/
120120
Scope getScope() { objectscopes(this, result) }
121121

122-
/** Gets the declaring identifier for this entity. */
122+
/**
123+
* Gets the declaring identifier for this entity, if any.
124+
*
125+
* Note that type switch statements which define a new variable in the guard
126+
* actually have a new variable (of the right type) implicitly declared at
127+
* the beginning of each case clause, and these do not have a declaration.
128+
*/
123129
Ident getDeclaration() { result.declares(this) }
124130

125131
/** Gets a reference to this entity. */

go/ql/lib/semmle/go/Stmt.qll

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,13 +747,25 @@ class IfStmt extends @ifstmt, Stmt, ScopeNode {
747747
* ```
748748
*/
749749
class CaseClause extends @caseclause, Stmt, ScopeNode {
750-
/** Gets the `i`th expression of this `case` clause (0-based). */
750+
/**
751+
* Gets the `i`th expression of this `case` clause (0-based).
752+
*
753+
* Note that the default clause does not have any expressions.
754+
*/
751755
Expr getExpr(int i) { result = this.getChildExpr(-(i + 1)) }
752756

753-
/** Gets an expression of this `case` clause. */
757+
/**
758+
* Gets an expression of this `case` clause, if any.
759+
*
760+
* Note that the default clause does not have any expressions.
761+
*/
754762
Expr getAnExpr() { result = this.getAChildExpr() }
755763

756-
/** Gets the number of expressions of this `case` clause. */
764+
/**
765+
* Gets the number of expressions of this `case` clause.
766+
*
767+
* Note that the default clause does not have any expressions.
768+
*/
757769
int getNumExpr() { result = this.getNumChildExpr() }
758770

759771
/** Gets the `i`th statement of this `case` clause (0-based). */

0 commit comments

Comments
 (0)