From 8477053c90948de8443383edd5c4f3141eddffb7 Mon Sep 17 00:00:00 2001 From: Owen Mansel-Chan Date: Wed, 27 Apr 2022 15:56:17 +0100 Subject: [PATCH] Test calling generic functions from other files --- .../GenericFunctionInstantiationExpr.expected | 14 +++++---- .../semmle/go/Function/genericFunctions.go | 12 ++++++++ .../semmle/go/Function/genericFunctions2.go | 6 ++++ .../semmle/go/Function/getParameter.expected | 23 ++++++++------- .../go/Function/getTypeParameter.expected | 29 ++++++++++--------- .../library-tests/semmle/go/Function/go.mod | 6 ++++ .../vendor/github.com/anotherpkg/README.md | 1 + .../github.com/anotherpkg/anotherpkg.go | 6 ++++ .../semmle/go/Function/vendor/modules.txt | 3 ++ 9 files changed, 69 insertions(+), 31 deletions(-) create mode 100644 ql/test/library-tests/semmle/go/Function/genericFunctions2.go create mode 100644 ql/test/library-tests/semmle/go/Function/go.mod create mode 100644 ql/test/library-tests/semmle/go/Function/vendor/github.com/anotherpkg/README.md create mode 100644 ql/test/library-tests/semmle/go/Function/vendor/github.com/anotherpkg/anotherpkg.go create mode 100644 ql/test/library-tests/semmle/go/Function/vendor/modules.txt diff --git a/ql/test/library-tests/semmle/go/Function/GenericFunctionInstantiationExpr.expected b/ql/test/library-tests/semmle/go/Function/GenericFunctionInstantiationExpr.expected index 8ee6c980c10..6528a0ae7f3 100644 --- a/ql/test/library-tests/semmle/go/Function/GenericFunctionInstantiationExpr.expected +++ b/ql/test/library-tests/semmle/go/Function/GenericFunctionInstantiationExpr.expected @@ -1,6 +1,8 @@ -| genericFunctions.go:23:2:23:33 | generic function instantiation expression | genericFunctions.go:23:2:23:28 | GenericFunctionOneTypeParam | 0 | genericFunctions.go:23:30:23:32 | int | -| genericFunctions.go:24:2:24:36 | generic function instantiation expression | genericFunctions.go:24:2:24:28 | GenericFunctionOneTypeParam | 0 | genericFunctions.go:24:30:24:35 | string | -| genericFunctions.go:42:6:42:48 | generic function instantiation expression | genericFunctions.go:42:6:42:33 | GenericFunctionTwoTypeParams | 0 | genericFunctions.go:42:35:42:40 | string | -| genericFunctions.go:42:6:42:48 | generic function instantiation expression | genericFunctions.go:42:6:42:33 | GenericFunctionTwoTypeParams | 1 | genericFunctions.go:42:43:42:47 | int64 | -| genericFunctions.go:43:6:43:50 | generic function instantiation expression | genericFunctions.go:43:6:43:33 | GenericFunctionTwoTypeParams | 0 | genericFunctions.go:43:35:43:40 | string | -| genericFunctions.go:43:6:43:50 | generic function instantiation expression | genericFunctions.go:43:6:43:33 | GenericFunctionTwoTypeParams | 1 | genericFunctions.go:43:43:43:49 | float64 | +| genericFunctions.go:25:2:25:33 | generic function instantiation expression | genericFunctions.go:25:2:25:28 | GenericFunctionOneTypeParam | 0 | genericFunctions.go:25:30:25:32 | int | +| genericFunctions.go:26:2:26:36 | generic function instantiation expression | genericFunctions.go:26:2:26:28 | GenericFunctionOneTypeParam | 0 | genericFunctions.go:26:30:26:35 | string | +| genericFunctions.go:44:6:44:48 | generic function instantiation expression | genericFunctions.go:44:6:44:33 | GenericFunctionTwoTypeParams | 0 | genericFunctions.go:44:35:44:40 | string | +| genericFunctions.go:44:6:44:48 | generic function instantiation expression | genericFunctions.go:44:6:44:33 | GenericFunctionTwoTypeParams | 1 | genericFunctions.go:44:43:44:47 | int64 | +| genericFunctions.go:45:6:45:50 | generic function instantiation expression | genericFunctions.go:45:6:45:33 | GenericFunctionTwoTypeParams | 0 | genericFunctions.go:45:35:45:40 | string | +| genericFunctions.go:45:6:45:50 | generic function instantiation expression | genericFunctions.go:45:6:45:33 | GenericFunctionTwoTypeParams | 1 | genericFunctions.go:45:43:45:49 | float64 | +| genericFunctions.go:141:6:141:41 | generic function instantiation expression | genericFunctions.go:141:6:141:33 | GenericFunctionInAnotherFile | 0 | genericFunctions.go:141:35:141:40 | string | +| genericFunctions.go:146:6:146:55 | generic function instantiation expression | genericFunctions.go:146:6:146:47 | selection of GenericFunctionInAnotherPackage | 0 | genericFunctions.go:146:49:146:54 | string | diff --git a/ql/test/library-tests/semmle/go/Function/genericFunctions.go b/ql/test/library-tests/semmle/go/Function/genericFunctions.go index efa254233a2..43d0a11dd9f 100644 --- a/ql/test/library-tests/semmle/go/Function/genericFunctions.go +++ b/ql/test/library-tests/semmle/go/Function/genericFunctions.go @@ -1,5 +1,7 @@ package main +import "github.com/anotherpkg" + type T1 map[string][]string type T2 T1 @@ -134,3 +136,13 @@ func New[Node NodeConstraint[Edge], Edge EdgeConstraint[Node]](nodes []Node) *Gr } func (g *Graph[Node, Edge]) ShortestPath(from, to Node) []Edge { return []Edge{} } + +func callFunctionsInAnotherFile() { + _ = GenericFunctionInAnotherFile[string]("world") + _ = GenericFunctionInAnotherFile("world") +} + +func callFunctionsInAnotherPackage() { + _ = anotherpkg.GenericFunctionInAnotherPackage[string]("world") + _ = anotherpkg.GenericFunctionInAnotherPackage("world") +} diff --git a/ql/test/library-tests/semmle/go/Function/genericFunctions2.go b/ql/test/library-tests/semmle/go/Function/genericFunctions2.go new file mode 100644 index 00000000000..9394357dea0 --- /dev/null +++ b/ql/test/library-tests/semmle/go/Function/genericFunctions2.go @@ -0,0 +1,6 @@ +package main + +func GenericFunctionInAnotherFile[T any](t T) T { + var r T + return r +} diff --git a/ql/test/library-tests/semmle/go/Function/getParameter.expected b/ql/test/library-tests/semmle/go/Function/getParameter.expected index 84d34db4c17..538b6ff3567 100644 --- a/ql/test/library-tests/semmle/go/Function/getParameter.expected +++ b/ql/test/library-tests/semmle/go/Function/getParameter.expected @@ -1,14 +1,15 @@ -| genericFunctions.go:7:6:7:32 | GenericFunctionOneTypeParam | 0 | genericFunctions.go:7:41:7:41 | t | -| genericFunctions.go:13:6:13:33 | GenericFunctionTwoTypeParams | 0 | genericFunctions.go:13:68:13:68 | m | -| genericFunctions.go:85:30:85:31 | f1 | -1 | genericFunctions.go:85:7:85:7 | x | -| genericFunctions.go:90:31:90:32 | g1 | -1 | genericFunctions.go:90:7:90:7 | x | -| genericFunctions.go:93:35:93:36 | f2 | -1 | genericFunctions.go:93:7:93:7 | x | -| genericFunctions.go:96:36:96:37 | g2 | -1 | genericFunctions.go:96:7:96:7 | x | -| genericFunctions.go:118:19:118:23 | MyLen | -1 | genericFunctions.go:118:7:118:7 | l | -| genericFunctions.go:132:6:132:8 | New | 0 | genericFunctions.go:132:64:132:68 | nodes | -| genericFunctions.go:136:29:136:40 | ShortestPath | 0 | genericFunctions.go:136:42:136:45 | from | -| genericFunctions.go:136:29:136:40 | ShortestPath | 1 | genericFunctions.go:136:48:136:49 | to | -| genericFunctions.go:136:29:136:40 | ShortestPath | -1 | genericFunctions.go:136:7:136:7 | g | +| genericFunctions2.go:3:6:3:33 | GenericFunctionInAnotherFile | 0 | genericFunctions2.go:3:42:3:42 | t | +| genericFunctions.go:9:6:9:32 | GenericFunctionOneTypeParam | 0 | genericFunctions.go:9:41:9:41 | t | +| genericFunctions.go:15:6:15:33 | GenericFunctionTwoTypeParams | 0 | genericFunctions.go:15:68:15:68 | m | +| genericFunctions.go:87:30:87:31 | f1 | -1 | genericFunctions.go:87:7:87:7 | x | +| genericFunctions.go:92:31:92:32 | g1 | -1 | genericFunctions.go:92:7:92:7 | x | +| genericFunctions.go:95:35:95:36 | f2 | -1 | genericFunctions.go:95:7:95:7 | x | +| genericFunctions.go:98:36:98:37 | g2 | -1 | genericFunctions.go:98:7:98:7 | x | +| genericFunctions.go:120:19:120:23 | MyLen | -1 | genericFunctions.go:120:7:120:7 | l | +| genericFunctions.go:134:6:134:8 | New | 0 | genericFunctions.go:134:64:134:68 | nodes | +| genericFunctions.go:138:29:138:40 | ShortestPath | 0 | genericFunctions.go:138:42:138:45 | from | +| genericFunctions.go:138:29:138:40 | ShortestPath | 1 | genericFunctions.go:138:48:138:49 | to | +| genericFunctions.go:138:29:138:40 | ShortestPath | -1 | genericFunctions.go:138:7:138:7 | g | | main.go:7:6:7:7 | f1 | 0 | main.go:7:9:7:9 | x | | main.go:9:12:9:13 | f2 | 0 | main.go:9:15:9:15 | x | | main.go:9:12:9:13 | f2 | 1 | main.go:9:18:9:18 | y | diff --git a/ql/test/library-tests/semmle/go/Function/getTypeParameter.expected b/ql/test/library-tests/semmle/go/Function/getTypeParameter.expected index 9756d668b19..ce72080bd6f 100644 --- a/ql/test/library-tests/semmle/go/Function/getTypeParameter.expected +++ b/ql/test/library-tests/semmle/go/Function/getTypeParameter.expected @@ -1,14 +1,15 @@ -| genericFunctions.go:7:1:10:1 | function declaration | FuncDecl | 0 | genericFunctions.go:7:34:7:38 | type parameter declaration | 0 | genericFunctions.go:7:34:7:34 | T | genericFunctions.go:7:36:7:38 | any | interface { } | -| genericFunctions.go:13:1:19:1 | function declaration | FuncDecl | 0 | genericFunctions.go:13:35:13:46 | type parameter declaration | 0 | genericFunctions.go:13:35:13:35 | K | genericFunctions.go:13:37:13:46 | comparable | comparable | -| genericFunctions.go:13:1:19:1 | function declaration | FuncDecl | 1 | genericFunctions.go:13:49:13:65 | type parameter declaration | 0 | genericFunctions.go:13:49:13:49 | V | genericFunctions.go:13:51:13:65 | type set literal | interface { int64 \| float64 } | -| genericFunctions.go:79:6:80:1 | type declaration specifier | TypeSpec | 0 | genericFunctions.go:79:21:79:25 | type parameter declaration | 0 | genericFunctions.go:79:21:79:21 | T | genericFunctions.go:79:23:79:25 | any | interface { } | -| genericFunctions.go:82:6:83:1 | type declaration specifier | TypeSpec | 0 | genericFunctions.go:82:21:82:28 | type parameter declaration | 0 | genericFunctions.go:82:21:82:21 | S | genericFunctions.go:82:26:82:28 | any | interface { } | -| genericFunctions.go:82:6:83:1 | type declaration specifier | TypeSpec | 0 | genericFunctions.go:82:21:82:28 | type parameter declaration | 1 | genericFunctions.go:82:24:82:24 | T | genericFunctions.go:82:26:82:28 | any | interface { } | -| genericFunctions.go:109:6:111:1 | type declaration specifier | TypeSpec | 0 | genericFunctions.go:109:14:109:18 | type parameter declaration | 0 | genericFunctions.go:109:14:109:14 | S | genericFunctions.go:109:16:109:18 | any | interface { } | -| genericFunctions.go:113:6:115:1 | type declaration specifier | TypeSpec | 0 | genericFunctions.go:113:11:113:15 | type parameter declaration | 0 | genericFunctions.go:113:11:113:11 | T | genericFunctions.go:113:13:113:15 | any | interface { } | -| genericFunctions.go:122:6:124:1 | type declaration specifier | TypeSpec | 0 | genericFunctions.go:122:21:122:28 | type parameter declaration | 0 | genericFunctions.go:122:21:122:24 | Edge | genericFunctions.go:122:26:122:28 | any | interface { } | -| genericFunctions.go:126:6:128:1 | type declaration specifier | TypeSpec | 0 | genericFunctions.go:126:21:126:28 | type parameter declaration | 0 | genericFunctions.go:126:21:126:24 | Node | genericFunctions.go:126:26:126:28 | any | interface { } | -| genericFunctions.go:130:6:130:73 | type declaration specifier | TypeSpec | 0 | genericFunctions.go:130:12:130:36 | type parameter declaration | 0 | genericFunctions.go:130:12:130:15 | Node | genericFunctions.go:130:17:130:36 | generic type instantiation expression | NodeConstraint | -| genericFunctions.go:130:6:130:73 | type declaration specifier | TypeSpec | 1 | genericFunctions.go:130:39:130:63 | type parameter declaration | 0 | genericFunctions.go:130:39:130:42 | Edge | genericFunctions.go:130:44:130:63 | generic type instantiation expression | EdgeConstraint | -| genericFunctions.go:132:1:134:1 | function declaration | FuncDecl | 0 | genericFunctions.go:132:10:132:34 | type parameter declaration | 0 | genericFunctions.go:132:10:132:13 | Node | genericFunctions.go:132:15:132:34 | generic type instantiation expression | NodeConstraint | -| genericFunctions.go:132:1:134:1 | function declaration | FuncDecl | 1 | genericFunctions.go:132:37:132:61 | type parameter declaration | 0 | genericFunctions.go:132:37:132:40 | Edge | genericFunctions.go:132:42:132:61 | generic type instantiation expression | EdgeConstraint | +| genericFunctions2.go:3:1:6:1 | function declaration | FuncDecl | 0 | genericFunctions2.go:3:35:3:39 | type parameter declaration | 0 | genericFunctions2.go:3:35:3:35 | T | genericFunctions2.go:3:37:3:39 | any | interface { } | +| genericFunctions.go:9:1:12:1 | function declaration | FuncDecl | 0 | genericFunctions.go:9:34:9:38 | type parameter declaration | 0 | genericFunctions.go:9:34:9:34 | T | genericFunctions.go:9:36:9:38 | any | interface { } | +| genericFunctions.go:15:1:21:1 | function declaration | FuncDecl | 0 | genericFunctions.go:15:35:15:46 | type parameter declaration | 0 | genericFunctions.go:15:35:15:35 | K | genericFunctions.go:15:37:15:46 | comparable | comparable | +| genericFunctions.go:15:1:21:1 | function declaration | FuncDecl | 1 | genericFunctions.go:15:49:15:65 | type parameter declaration | 0 | genericFunctions.go:15:49:15:49 | V | genericFunctions.go:15:51:15:65 | type set literal | interface { int64 \| float64 } | +| genericFunctions.go:81:6:82:1 | type declaration specifier | TypeSpec | 0 | genericFunctions.go:81:21:81:25 | type parameter declaration | 0 | genericFunctions.go:81:21:81:21 | T | genericFunctions.go:81:23:81:25 | any | interface { } | +| genericFunctions.go:84:6:85:1 | type declaration specifier | TypeSpec | 0 | genericFunctions.go:84:21:84:28 | type parameter declaration | 0 | genericFunctions.go:84:21:84:21 | S | genericFunctions.go:84:26:84:28 | any | interface { } | +| genericFunctions.go:84:6:85:1 | type declaration specifier | TypeSpec | 0 | genericFunctions.go:84:21:84:28 | type parameter declaration | 1 | genericFunctions.go:84:24:84:24 | T | genericFunctions.go:84:26:84:28 | any | interface { } | +| genericFunctions.go:111:6:113:1 | type declaration specifier | TypeSpec | 0 | genericFunctions.go:111:14:111:18 | type parameter declaration | 0 | genericFunctions.go:111:14:111:14 | S | genericFunctions.go:111:16:111:18 | any | interface { } | +| genericFunctions.go:115:6:117:1 | type declaration specifier | TypeSpec | 0 | genericFunctions.go:115:11:115:15 | type parameter declaration | 0 | genericFunctions.go:115:11:115:11 | T | genericFunctions.go:115:13:115:15 | any | interface { } | +| genericFunctions.go:124:6:126:1 | type declaration specifier | TypeSpec | 0 | genericFunctions.go:124:21:124:28 | type parameter declaration | 0 | genericFunctions.go:124:21:124:24 | Edge | genericFunctions.go:124:26:124:28 | any | interface { } | +| genericFunctions.go:128:6:130:1 | type declaration specifier | TypeSpec | 0 | genericFunctions.go:128:21:128:28 | type parameter declaration | 0 | genericFunctions.go:128:21:128:24 | Node | genericFunctions.go:128:26:128:28 | any | interface { } | +| genericFunctions.go:132:6:132:73 | type declaration specifier | TypeSpec | 0 | genericFunctions.go:132:12:132:36 | type parameter declaration | 0 | genericFunctions.go:132:12:132:15 | Node | genericFunctions.go:132:17:132:36 | generic type instantiation expression | NodeConstraint | +| genericFunctions.go:132:6:132:73 | type declaration specifier | TypeSpec | 1 | genericFunctions.go:132:39:132:63 | type parameter declaration | 0 | genericFunctions.go:132:39:132:42 | Edge | genericFunctions.go:132:44:132:63 | generic type instantiation expression | EdgeConstraint | +| genericFunctions.go:134:1:136:1 | function declaration | FuncDecl | 0 | genericFunctions.go:134:10:134:34 | type parameter declaration | 0 | genericFunctions.go:134:10:134:13 | Node | genericFunctions.go:134:15:134:34 | generic type instantiation expression | NodeConstraint | +| genericFunctions.go:134:1:136:1 | function declaration | FuncDecl | 1 | genericFunctions.go:134:37:134:61 | type parameter declaration | 0 | genericFunctions.go:134:37:134:40 | Edge | genericFunctions.go:134:42:134:61 | generic type instantiation expression | EdgeConstraint | diff --git a/ql/test/library-tests/semmle/go/Function/go.mod b/ql/test/library-tests/semmle/go/Function/go.mod new file mode 100644 index 00000000000..85d3db5c5a3 --- /dev/null +++ b/ql/test/library-tests/semmle/go/Function/go.mod @@ -0,0 +1,6 @@ +module codeql-go-tests/function + +go 1.18 + +require github.com/anotherpkg v0.0.0-20200203000000-0000000000000 + diff --git a/ql/test/library-tests/semmle/go/Function/vendor/github.com/anotherpkg/README.md b/ql/test/library-tests/semmle/go/Function/vendor/github.com/anotherpkg/README.md new file mode 100644 index 00000000000..8dd7c7f5401 --- /dev/null +++ b/ql/test/library-tests/semmle/go/Function/vendor/github.com/anotherpkg/README.md @@ -0,0 +1 @@ +This is a simple stub package, strictly for use in query testing. diff --git a/ql/test/library-tests/semmle/go/Function/vendor/github.com/anotherpkg/anotherpkg.go b/ql/test/library-tests/semmle/go/Function/vendor/github.com/anotherpkg/anotherpkg.go new file mode 100644 index 00000000000..6ac64bba2c6 --- /dev/null +++ b/ql/test/library-tests/semmle/go/Function/vendor/github.com/anotherpkg/anotherpkg.go @@ -0,0 +1,6 @@ +package anotherpkg + +func GenericFunctionInAnotherPackage[T any](t T) T { + var r T + return r +} diff --git a/ql/test/library-tests/semmle/go/Function/vendor/modules.txt b/ql/test/library-tests/semmle/go/Function/vendor/modules.txt new file mode 100644 index 00000000000..86277b3b019 --- /dev/null +++ b/ql/test/library-tests/semmle/go/Function/vendor/modules.txt @@ -0,0 +1,3 @@ +# github.com/anotherpkg v0.0.0-20200203000000-0000000000000 +## explicit; go 1.18 +github.com/anotherpkg