Skip to content

Commit

Permalink
Update extractor to deal with type parameters on alias types
Browse files Browse the repository at this point in the history
  • Loading branch information
owen-mc committed Jan 10, 2025
1 parent 0e9f079 commit 7b4805d
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 4 deletions.
8 changes: 4 additions & 4 deletions go/extractor/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,12 +475,12 @@ func extractObjects(tw *trap.Writer, scope *types.Scope, scopeLabel trap.Label)
populateTypeParamParents(funcObj.Type().(*types.Signature).TypeParams(), obj)
populateTypeParamParents(funcObj.Type().(*types.Signature).RecvTypeParams(), obj)
}
// Populate type parameter parents for named types. Note that we
// skip type aliases as the original type should be the parent
// of any type parameters.
if typeNameObj, ok := obj.(*types.TypeName); ok && !typeNameObj.IsAlias() {
// Populate type parameter parents for named types.
if typeNameObj, ok := obj.(*types.TypeName); ok {
if tp, ok := typeNameObj.Type().(*types.Named); ok {
populateTypeParamParents(tp.TypeParams(), obj)
} else if tp, ok := typeNameObj.Type().(*types.Alias); ok {
populateTypeParamParents(tp.TypeParams(), obj)
}
}
extractObject(tw, obj, lbl)
Expand Down
8 changes: 8 additions & 0 deletions go/ql/test/library-tests/semmle/go/Types/Aliases.expected
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ entities
| aliases.go | aliases.go:22:6:22:6 | T | 1 | aliases.go:19:6:19:7 | S3 |
| aliases.go | aliases.go:25:6:25:6 | H | 1 | file://:0:0:0:0 | signature type |
| aliases.go | aliases.go:25:8:25:11 | Afs3 | 1 | aliases.go:19:6:19:7 | S3 |
| aliases.go | aliases.go:29:6:29:11 | MyType | 1 | aliases.go:29:6:29:11 | MyType |
| aliases.go | aliases.go:29:13:29:19 | MyTypeT | 1 | file://:0:0:0:0 | type parameter type |
| aliases.go | aliases.go:29:34:29:34 | x | 1 | file://:0:0:0:0 | type parameter type |
| aliases.go | aliases.go:32:6:32:16 | MyTypeAlias | 1 | aliases.go:29:6:29:11 | MyType |
| aliases.go | aliases.go:32:18:32:29 | MyTypeAliasT | 1 | file://:0:0:0:0 | type parameter type |
| aliases.go | aliases.go:34:6:34:19 | useMyTypeAlias | 1 | file://:0:0:0:0 | signature type |
| aliases.go | aliases.go:34:21:34:21 | a | 1 | aliases.go:29:6:29:11 | MyType |
| aliases.go | aliases.go:35:2:35:2 | b | 1 | aliases.go:29:6:29:11 | MyType |
#select
| F | func(struct { x int }) int |
| G | func(struct { x int }) int |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
| aliases.go:6:26:6:26 | x | package codeql-go-types |
| aliases.go:8:26:8:26 | x | package codeql-go-types |
| aliases.go:19:17:19:17 | x | package codeql-go-types |
| aliases.go:29:34:29:34 | x | package codeql-go-types |
| cyclic.go:4:3:4:3 | s | package codeql-go-types |
| cyclic.go:8:3:8:3 | u | package codeql-go-types |
| cyclic.go:9:2:9:2 | f | package codeql-go-types |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
| aliases.go:6:26:6:26 | x | codeql-go-types.S3 | x |
| aliases.go:8:26:8:26 | x | codeql-go-types.S3 | x |
| aliases.go:19:17:19:17 | x | codeql-go-types.S3 | x |
| aliases.go:29:34:29:34 | x | codeql-go-types.MyType | x |
| cyclic.go:4:3:4:3 | s | codeql-go-types.s | s |
| cyclic.go:8:3:8:3 | u | codeql-go-types.t | u |
| cyclic.go:8:3:8:3 | u | codeql-go-types.u | u |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
| aliases.go:6:26:6:26 | x | codeql-go-types | S3 | x |
| aliases.go:8:26:8:26 | x | codeql-go-types | S3 | x |
| aliases.go:19:17:19:17 | x | codeql-go-types | S3 | x |
| aliases.go:29:34:29:34 | x | codeql-go-types | MyType | x |
| cyclic.go:4:3:4:3 | s | codeql-go-types | s | s |
| cyclic.go:8:3:8:3 | u | codeql-go-types | t | u |
| cyclic.go:8:3:8:3 | u | codeql-go-types | u | u |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
| aliases.go:32:38:32:57 | generic type instantiation expression | aliases.go:32:38:32:43 | MyType | 0 | aliases.go:32:45:32:56 | MyTypeAliasT |
| aliases.go:34:23:34:41 | generic type instantiation expression | aliases.go:34:23:34:33 | MyTypeAlias | 0 | aliases.go:34:35:34:40 | string |
| aliases.go:35:7:35:25 | generic type instantiation expression | aliases.go:35:7:35:17 | MyTypeAlias | 0 | aliases.go:35:19:35:24 | string |
| generic.go:12:16:12:40 | generic type instantiation expression | generic.go:12:16:12:37 | CircularGenericStruct1 | 0 | generic.go:12:39:12:39 | T |
| generic.go:16:7:16:31 | generic type instantiation expression | generic.go:16:7:16:28 | CircularGenericStruct1 | 0 | generic.go:16:30:16:30 | T |
| generic.go:20:14:20:30 | generic type instantiation expression | generic.go:20:14:20:27 | GenericStruct1 | 0 | generic.go:20:29:20:29 | S |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
| aliases.go:19:6:19:7 | S3 | codeql-go-types.S3 |
| aliases.go:29:6:29:11 | MyType | codeql-go-types.MyType |
| cyclic.go:3:6:3:6 | s | codeql-go-types.s |
| cyclic.go:7:6:7:6 | t | codeql-go-types.t |
| cyclic.go:12:6:12:6 | u | codeql-go-types.u |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
| aliases.go:10:1:12:1 | function declaration | 1 |
| aliases.go:14:1:16:1 | function declaration | 1 |
| aliases.go:25:1:27:1 | function declaration | 1 |
| aliases.go:34:1:38:1 | function declaration | 1 |
| depth.go:22:1:25:1 | function declaration | 0 |
| generic.go:70:1:72:1 | function declaration | 1 |
| generic.go:74:1:80:1 | function declaration | 1 |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
| aliases.go:10:1:12:1 | function declaration | 1 |
| aliases.go:14:1:16:1 | function declaration | 1 |
| aliases.go:25:1:27:1 | function declaration | 1 |
| aliases.go:34:1:38:1 | function declaration | 1 |
| depth.go:22:1:25:1 | function declaration | 0 |
| generic.go:70:1:72:1 | function declaration | 1 |
| generic.go:74:1:80:1 | function declaration | 0 |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
| aliases.go:19:6:19:7 | S3 | aliases.go:6:18:6:37 | struct type | x | int |
| aliases.go:19:6:19:7 | S3 | aliases.go:8:18:8:37 | struct type | x | int |
| aliases.go:19:6:19:7 | S3 | aliases.go:19:9:19:23 | struct type | x | int |
| aliases.go:29:6:29:11 | MyType | aliases.go:29:26:29:44 | struct type | x | MyTypeT |
| cyclic.go:3:6:3:6 | s | cyclic.go:3:8:5:1 | struct type | s | * s |
| cyclic.go:7:6:7:6 | t | cyclic.go:7:8:10:1 | struct type | f | int |
| cyclic.go:7:6:7:6 | t | cyclic.go:7:8:10:1 | struct type | t | t |
Expand Down
1 change: 1 addition & 0 deletions go/ql/test/library-tests/semmle/go/Types/Types.expected
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
| aliases.go:19:6:19:7 | S3 | S3 |
| aliases.go:29:6:29:11 | MyType | MyType |
| cyclic.go:3:6:3:6 | s | s |
| cyclic.go:7:6:7:6 | t | t |
| cyclic.go:12:6:12:6 | u | u |
Expand Down

0 comments on commit 7b4805d

Please sign in to comment.