Skip to content

Commit 3c4b520

Browse files
owen-mcsmowton
authored andcommitted
Fix type aliases for instantiated generic types
1 parent d098bdc commit 3c4b520

File tree

5 files changed

+12
-2
lines changed

5 files changed

+12
-2
lines changed

extractor/extractor.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,10 @@ func extractObjects(tw *trap.Writer, scope *types.Scope, scopeLabel trap.Label)
369369
populateTypeParamParents(tw, funcObj.Type().(*types.Signature).TypeParams(), obj)
370370
populateTypeParamParents(tw, funcObj.Type().(*types.Signature).RecvTypeParams(), obj)
371371
}
372-
// Populate type parameter parents for named types.
373-
if typeNameObj, ok := obj.(*types.TypeName); ok {
372+
// Populate type parameter parents for named types. Note that we
373+
// skip type aliases as the original type should be the parent
374+
// of any type parameters.
375+
if typeNameObj, ok := obj.(*types.TypeName); ok && !typeNameObj.IsAlias() {
374376
if tp, ok := typeNameObj.Type().(*types.Named); ok {
375377
populateTypeParamParents(tw, tp.TypeParams(), obj)
376378
}

ql/test/library-tests/semmle/go/Types/GenericTypeInstantiationExpr.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
| generic.go:62:12:62:26 | generic type instantiation expression | generic.go:62:12:62:23 | GenericNamed | 0 | generic.go:62:25:62:25 | U |
1717
| generic.go:70:42:70:64 | generic type instantiation expression | generic.go:70:42:70:57 | GenericInterface | 0 | generic.go:70:59:70:63 | int32 |
1818
| generic.go:74:41:74:62 | generic type instantiation expression | generic.go:74:41:74:54 | GenericStruct1 | 0 | generic.go:74:56:74:61 | string |
19+
| generic.go:82:18:82:34 | generic type instantiation expression | generic.go:82:18:82:29 | GenericArray | 0 | generic.go:82:31:82:33 | int |

ql/test/library-tests/semmle/go/Types/QualifiedNames.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
| GenericMap2 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.GenericMap2 |
2020
| GenericNamed | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.GenericNamed |
2121
| GenericPointer | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.GenericPointer |
22+
| GenericSignature | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.GenericSignature |
2223
| GenericSlice | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.GenericSlice |
2324
| GenericStruct1 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.GenericStruct1 |
2425
| GenericStruct2 | github.com/github/codeql-go/ql/test/library-tests/semmle/go/Types.GenericStruct2 |

ql/test/library-tests/semmle/go/Types/Types.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
| GenericMap2 | GenericMap2 |
2020
| GenericNamed | GenericNamed |
2121
| GenericPointer | GenericPointer |
22+
| GenericSignature | GenericSignature |
2223
| GenericSlice | GenericSlice |
2324
| GenericStruct1 | GenericStruct1 |
2425
| GenericStruct2 | GenericStruct2 |

ql/test/library-tests/semmle/go/Types/generic.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,8 @@ func accessFieldsOfInstantiatedStruct(x GenericStruct1[string]) {
7878
_ = x.sliceField
7979
_ = x.mapField
8080
}
81+
82+
type TypeAlias = GenericArray[int]
83+
84+
type GenericSignature[T any] func(t T) T
85+
type GenericSignatureAlias = GenericSignature[string]

0 commit comments

Comments
 (0)