diff --git a/go/ql/lib/change-notes/2024-10-17-ast-viewer-type-param-decl.md b/go/ql/lib/change-notes/2024-10-17-ast-viewer-type-param-decl.md new file mode 100644 index 000000000000..4d5fe89cee08 --- /dev/null +++ b/go/ql/lib/change-notes/2024-10-17-ast-viewer-type-param-decl.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* The AST viewer now shows type parameter declarations in the correct place in the AST. diff --git a/go/ql/lib/semmle/go/AST.qll b/go/ql/lib/semmle/go/AST.qll index 64c6231d1054..c5e388ba000f 100644 --- a/go/ql/lib/semmle/go/AST.qll +++ b/go/ql/lib/semmle/go/AST.qll @@ -55,6 +55,8 @@ class AstNode extends @node, Locatable { kind = "commentgroup" and result = this.(File).getCommentGroup(i) or kind = "comment" and result = this.(CommentGroup).getComment(i) + or + kind = "typeparamdecl" and result = this.(TypeParamDeclParent).getTypeParameterDecl(i) } /** diff --git a/go/ql/test/library-tests/semmle/go/PrintAst/PrintAst.expected b/go/ql/test/library-tests/semmle/go/PrintAst/PrintAst.expected index 41815e473a03..66aa26430633 100644 --- a/go/ql/test/library-tests/semmle/go/PrintAst/PrintAst.expected +++ b/go/ql/test/library-tests/semmle/go/PrintAst/PrintAst.expected @@ -639,6 +639,11 @@ other.go: # 11| Type = int # 11| 0: [Ident, VariableName] myNested # 11| Type = func() int +# 8| 3: [TypeParamDecl] type parameter declaration +# 8| 0: [Ident, TypeName] int +# 8| Type = int +# 8| 1: [Ident, TypeName] U +# 8| Type = U # 15| 5: [VarDecl] variable declaration # 15| 0: [ValueSpec] value declaration specifier # 15| 0: [Ident, VariableName] x @@ -648,3 +653,32 @@ other.go: # 15| 2: [IntLit] 0 # 15| Type = int # 15| Value = [IntLit] 0 +# 17| 6: [TypeDecl] type declaration +# 17| 0: [TypeSpec] type declaration specifier +# 17| 0: [Ident, TypeName] myType +# 17| Type = myType +# 17| 1: [ArrayTypeExpr] array type +# 17| Type = []T +# 17| 0: [Ident, TypeName] T +# 17| Type = T +# 17| 2: [TypeParamDecl] type parameter declaration +# 17| 0: [TypeSetLiteralExpr] type set literal +# 17| Type = ~string +# 17| 0: [Ident, TypeName] string +# 17| Type = string +# 17| 1: [Ident, TypeName] T +# 17| Type = T +# 19| 7: [MethodDecl] function declaration +# 19| 0: [FunctionName, Ident] f +# 19| Type = func() +# 19| 1: [FuncTypeExpr] function type +# 19| 2: [ReceiverDecl] receiver declaration +# 19| 0: [GenericTypeInstantiationExpr] generic type instantiation expression +# 19| Type = myType +# 19| 0: [Ident, TypeName] myType +# 19| Type = myType +# 19| 1: [Ident, TypeName] U +# 19| Type = U +# 19| 1: [Ident, VariableName] m +# 19| Type = myType +# 19| 3: [BlockStmt] block statement diff --git a/go/ql/test/library-tests/semmle/go/PrintAst/PrintAstExcludeComments.expected b/go/ql/test/library-tests/semmle/go/PrintAst/PrintAstExcludeComments.expected index 570ce08f2394..099aa4e6144f 100644 --- a/go/ql/test/library-tests/semmle/go/PrintAst/PrintAstExcludeComments.expected +++ b/go/ql/test/library-tests/semmle/go/PrintAst/PrintAstExcludeComments.expected @@ -619,6 +619,11 @@ other.go: # 11| Type = int # 11| 0: [Ident, VariableName] myNested # 11| Type = func() int +# 8| 3: [TypeParamDecl] type parameter declaration +# 8| 0: [Ident, TypeName] int +# 8| Type = int +# 8| 1: [Ident, TypeName] U +# 8| Type = U # 15| 5: [VarDecl] variable declaration # 15| 0: [ValueSpec] value declaration specifier # 15| 0: [Ident, VariableName] x @@ -628,3 +633,32 @@ other.go: # 15| 2: [IntLit] 0 # 15| Type = int # 15| Value = [IntLit] 0 +# 17| 6: [TypeDecl] type declaration +# 17| 0: [TypeSpec] type declaration specifier +# 17| 0: [Ident, TypeName] myType +# 17| Type = myType +# 17| 1: [ArrayTypeExpr] array type +# 17| Type = []T +# 17| 0: [Ident, TypeName] T +# 17| Type = T +# 17| 2: [TypeParamDecl] type parameter declaration +# 17| 0: [TypeSetLiteralExpr] type set literal +# 17| Type = ~string +# 17| 0: [Ident, TypeName] string +# 17| Type = string +# 17| 1: [Ident, TypeName] T +# 17| Type = T +# 19| 7: [MethodDecl] function declaration +# 19| 0: [FunctionName, Ident] f +# 19| Type = func() +# 19| 1: [FuncTypeExpr] function type +# 19| 2: [ReceiverDecl] receiver declaration +# 19| 0: [GenericTypeInstantiationExpr] generic type instantiation expression +# 19| Type = myType +# 19| 0: [Ident, TypeName] myType +# 19| Type = myType +# 19| 1: [Ident, TypeName] U +# 19| Type = U +# 19| 1: [Ident, VariableName] m +# 19| Type = myType +# 19| 3: [BlockStmt] block statement diff --git a/go/ql/test/library-tests/semmle/go/PrintAst/PrintAstNestedFunction.expected b/go/ql/test/library-tests/semmle/go/PrintAst/PrintAstNestedFunction.expected index c8a01b1ba97a..9be02352b2b7 100644 --- a/go/ql/test/library-tests/semmle/go/PrintAst/PrintAstNestedFunction.expected +++ b/go/ql/test/library-tests/semmle/go/PrintAst/PrintAstNestedFunction.expected @@ -56,6 +56,11 @@ other.go: # 11| Type = int # 11| 0: [Ident, VariableName] myNested # 11| Type = func() int +# 8| 3: [TypeParamDecl] type parameter declaration +# 8| 0: [Ident, TypeName] int +# 8| Type = int +# 8| 1: [Ident, TypeName] U +# 8| Type = U # 15| 2: [VarDecl] variable declaration # 15| 0: [ValueSpec] value declaration specifier # 15| 0: [Ident, VariableName] x @@ -65,3 +70,18 @@ other.go: # 15| 2: [IntLit] 0 # 15| Type = int # 15| Value = [IntLit] 0 +# 17| 3: [TypeDecl] type declaration +# 17| 0: [TypeSpec] type declaration specifier +# 17| 0: [Ident, TypeName] myType +# 17| Type = myType +# 17| 1: [ArrayTypeExpr] array type +# 17| Type = []T +# 17| 0: [Ident, TypeName] T +# 17| Type = T +# 17| 2: [TypeParamDecl] type parameter declaration +# 17| 0: [TypeSetLiteralExpr] type set literal +# 17| Type = ~string +# 17| 0: [Ident, TypeName] string +# 17| Type = string +# 17| 1: [Ident, TypeName] T +# 17| Type = T diff --git a/go/ql/test/library-tests/semmle/go/PrintAst/PrintAstRestrictFile.expected b/go/ql/test/library-tests/semmle/go/PrintAst/PrintAstRestrictFile.expected index 1981fde0357e..45e6789e0139 100644 --- a/go/ql/test/library-tests/semmle/go/PrintAst/PrintAstRestrictFile.expected +++ b/go/ql/test/library-tests/semmle/go/PrintAst/PrintAstRestrictFile.expected @@ -41,6 +41,11 @@ other.go: # 11| Type = int # 11| 0: [Ident, VariableName] myNested # 11| Type = func() int +# 8| 3: [TypeParamDecl] type parameter declaration +# 8| 0: [Ident, TypeName] int +# 8| Type = int +# 8| 1: [Ident, TypeName] U +# 8| Type = U # 15| 5: [VarDecl] variable declaration # 15| 0: [ValueSpec] value declaration specifier # 15| 0: [Ident, VariableName] x @@ -50,3 +55,32 @@ other.go: # 15| 2: [IntLit] 0 # 15| Type = int # 15| Value = [IntLit] 0 +# 17| 6: [TypeDecl] type declaration +# 17| 0: [TypeSpec] type declaration specifier +# 17| 0: [Ident, TypeName] myType +# 17| Type = myType +# 17| 1: [ArrayTypeExpr] array type +# 17| Type = []T +# 17| 0: [Ident, TypeName] T +# 17| Type = T +# 17| 2: [TypeParamDecl] type parameter declaration +# 17| 0: [TypeSetLiteralExpr] type set literal +# 17| Type = ~string +# 17| 0: [Ident, TypeName] string +# 17| Type = string +# 17| 1: [Ident, TypeName] T +# 17| Type = T +# 19| 7: [MethodDecl] function declaration +# 19| 0: [FunctionName, Ident] f +# 19| Type = func() +# 19| 1: [FuncTypeExpr] function type +# 19| 2: [ReceiverDecl] receiver declaration +# 19| 0: [GenericTypeInstantiationExpr] generic type instantiation expression +# 19| Type = myType +# 19| 0: [Ident, TypeName] myType +# 19| Type = myType +# 19| 1: [Ident, TypeName] U +# 19| Type = U +# 19| 1: [Ident, VariableName] m +# 19| Type = myType +# 19| 3: [BlockStmt] block statement diff --git a/go/ql/test/library-tests/semmle/go/PrintAst/PrintAstRestrictFunction.expected b/go/ql/test/library-tests/semmle/go/PrintAst/PrintAstRestrictFunction.expected index 5783960cedc2..3073a43383a3 100644 --- a/go/ql/test/library-tests/semmle/go/PrintAst/PrintAstRestrictFunction.expected +++ b/go/ql/test/library-tests/semmle/go/PrintAst/PrintAstRestrictFunction.expected @@ -1,3 +1,9 @@ +other.go: +# 8| [TypeParamDecl] type parameter declaration +# 8| 0: [Ident, TypeName] int +# 8| Type = int +# 8| 1: [Ident, TypeName] U +# 8| Type = U go.mod: # 0| [GoModFile] go.mod # 1| 0: [GoModModuleLine] go.mod module line @@ -45,3 +51,18 @@ other.go: # 15| 2: [IntLit] 0 # 15| Type = int # 15| Value = [IntLit] 0 +# 17| 3: [TypeDecl] type declaration +# 17| 0: [TypeSpec] type declaration specifier +# 17| 0: [Ident, TypeName] myType +# 17| Type = myType +# 17| 1: [ArrayTypeExpr] array type +# 17| Type = []T +# 17| 0: [Ident, TypeName] T +# 17| Type = T +# 17| 2: [TypeParamDecl] type parameter declaration +# 17| 0: [TypeSetLiteralExpr] type set literal +# 17| Type = ~string +# 17| 0: [Ident, TypeName] string +# 17| Type = string +# 17| 1: [Ident, TypeName] T +# 17| Type = T diff --git a/go/ql/test/library-tests/semmle/go/PrintAst/go.mod b/go/ql/test/library-tests/semmle/go/PrintAst/go.mod index c3149650c4c1..a443fbece394 100644 --- a/go/ql/test/library-tests/semmle/go/PrintAst/go.mod +++ b/go/ql/test/library-tests/semmle/go/PrintAst/go.mod @@ -1,4 +1,3 @@ module codeql-go-tests/printast -go 1.14 - +go 1.18 diff --git a/go/ql/test/library-tests/semmle/go/PrintAst/other.go b/go/ql/test/library-tests/semmle/go/PrintAst/other.go index 9d60eff98bdc..caf66812d601 100644 --- a/go/ql/test/library-tests/semmle/go/PrintAst/other.go +++ b/go/ql/test/library-tests/semmle/go/PrintAst/other.go @@ -5,7 +5,7 @@ func main() {} func f() {} func g() {} -func hasNested() { +func hasNested[U int]() { myNested := func() int { return 1 } myNested() @@ -13,3 +13,7 @@ func hasNested() { } var x int = 0 + +type myType[T ~string] []T + +func (m myType[U]) f() {}