Skip to content

Commit f3abe54

Browse files
authored
Merge pull request github#17794 from owen-mc/go/ast-viewer-typeparamdecl
Go: Fix type param declarations in AST viewer
2 parents b0376d5 + bacf448 commit f3abe54

File tree

9 files changed

+155
-3
lines changed

9 files changed

+155
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The AST viewer now shows type parameter declarations in the correct place in the AST.

go/ql/lib/semmle/go/AST.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class AstNode extends @node, Locatable {
5555
kind = "commentgroup" and result = this.(File).getCommentGroup(i)
5656
or
5757
kind = "comment" and result = this.(CommentGroup).getComment(i)
58+
or
59+
kind = "typeparamdecl" and result = this.(TypeParamDeclParent).getTypeParameterDecl(i)
5860
}
5961

6062
/**

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,11 @@ other.go:
639639
# 11| Type = int
640640
# 11| 0: [Ident, VariableName] myNested
641641
# 11| Type = func() int
642+
# 8| 3: [TypeParamDecl] type parameter declaration
643+
# 8| 0: [Ident, TypeName] int
644+
# 8| Type = int
645+
# 8| 1: [Ident, TypeName] U
646+
# 8| Type = U
642647
# 15| 5: [VarDecl] variable declaration
643648
# 15| 0: [ValueSpec] value declaration specifier
644649
# 15| 0: [Ident, VariableName] x
@@ -648,3 +653,32 @@ other.go:
648653
# 15| 2: [IntLit] 0
649654
# 15| Type = int
650655
# 15| Value = [IntLit] 0
656+
# 17| 6: [TypeDecl] type declaration
657+
# 17| 0: [TypeSpec] type declaration specifier
658+
# 17| 0: [Ident, TypeName] myType
659+
# 17| Type = myType
660+
# 17| 1: [ArrayTypeExpr] array type
661+
# 17| Type = []T
662+
# 17| 0: [Ident, TypeName] T
663+
# 17| Type = T
664+
# 17| 2: [TypeParamDecl] type parameter declaration
665+
# 17| 0: [TypeSetLiteralExpr] type set literal
666+
# 17| Type = ~string
667+
# 17| 0: [Ident, TypeName] string
668+
# 17| Type = string
669+
# 17| 1: [Ident, TypeName] T
670+
# 17| Type = T
671+
# 19| 7: [MethodDecl] function declaration
672+
# 19| 0: [FunctionName, Ident] f
673+
# 19| Type = func()
674+
# 19| 1: [FuncTypeExpr] function type
675+
# 19| 2: [ReceiverDecl] receiver declaration
676+
# 19| 0: [GenericTypeInstantiationExpr] generic type instantiation expression
677+
# 19| Type = myType
678+
# 19| 0: [Ident, TypeName] myType
679+
# 19| Type = myType
680+
# 19| 1: [Ident, TypeName] U
681+
# 19| Type = U
682+
# 19| 1: [Ident, VariableName] m
683+
# 19| Type = myType
684+
# 19| 3: [BlockStmt] block statement

go/ql/test/library-tests/semmle/go/PrintAst/PrintAstExcludeComments.expected

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,11 @@ other.go:
619619
# 11| Type = int
620620
# 11| 0: [Ident, VariableName] myNested
621621
# 11| Type = func() int
622+
# 8| 3: [TypeParamDecl] type parameter declaration
623+
# 8| 0: [Ident, TypeName] int
624+
# 8| Type = int
625+
# 8| 1: [Ident, TypeName] U
626+
# 8| Type = U
622627
# 15| 5: [VarDecl] variable declaration
623628
# 15| 0: [ValueSpec] value declaration specifier
624629
# 15| 0: [Ident, VariableName] x
@@ -628,3 +633,32 @@ other.go:
628633
# 15| 2: [IntLit] 0
629634
# 15| Type = int
630635
# 15| Value = [IntLit] 0
636+
# 17| 6: [TypeDecl] type declaration
637+
# 17| 0: [TypeSpec] type declaration specifier
638+
# 17| 0: [Ident, TypeName] myType
639+
# 17| Type = myType
640+
# 17| 1: [ArrayTypeExpr] array type
641+
# 17| Type = []T
642+
# 17| 0: [Ident, TypeName] T
643+
# 17| Type = T
644+
# 17| 2: [TypeParamDecl] type parameter declaration
645+
# 17| 0: [TypeSetLiteralExpr] type set literal
646+
# 17| Type = ~string
647+
# 17| 0: [Ident, TypeName] string
648+
# 17| Type = string
649+
# 17| 1: [Ident, TypeName] T
650+
# 17| Type = T
651+
# 19| 7: [MethodDecl] function declaration
652+
# 19| 0: [FunctionName, Ident] f
653+
# 19| Type = func()
654+
# 19| 1: [FuncTypeExpr] function type
655+
# 19| 2: [ReceiverDecl] receiver declaration
656+
# 19| 0: [GenericTypeInstantiationExpr] generic type instantiation expression
657+
# 19| Type = myType
658+
# 19| 0: [Ident, TypeName] myType
659+
# 19| Type = myType
660+
# 19| 1: [Ident, TypeName] U
661+
# 19| Type = U
662+
# 19| 1: [Ident, VariableName] m
663+
# 19| Type = myType
664+
# 19| 3: [BlockStmt] block statement

go/ql/test/library-tests/semmle/go/PrintAst/PrintAstNestedFunction.expected

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ other.go:
5656
# 11| Type = int
5757
# 11| 0: [Ident, VariableName] myNested
5858
# 11| Type = func() int
59+
# 8| 3: [TypeParamDecl] type parameter declaration
60+
# 8| 0: [Ident, TypeName] int
61+
# 8| Type = int
62+
# 8| 1: [Ident, TypeName] U
63+
# 8| Type = U
5964
# 15| 2: [VarDecl] variable declaration
6065
# 15| 0: [ValueSpec] value declaration specifier
6166
# 15| 0: [Ident, VariableName] x
@@ -65,3 +70,18 @@ other.go:
6570
# 15| 2: [IntLit] 0
6671
# 15| Type = int
6772
# 15| Value = [IntLit] 0
73+
# 17| 3: [TypeDecl] type declaration
74+
# 17| 0: [TypeSpec] type declaration specifier
75+
# 17| 0: [Ident, TypeName] myType
76+
# 17| Type = myType
77+
# 17| 1: [ArrayTypeExpr] array type
78+
# 17| Type = []T
79+
# 17| 0: [Ident, TypeName] T
80+
# 17| Type = T
81+
# 17| 2: [TypeParamDecl] type parameter declaration
82+
# 17| 0: [TypeSetLiteralExpr] type set literal
83+
# 17| Type = ~string
84+
# 17| 0: [Ident, TypeName] string
85+
# 17| Type = string
86+
# 17| 1: [Ident, TypeName] T
87+
# 17| Type = T

go/ql/test/library-tests/semmle/go/PrintAst/PrintAstRestrictFile.expected

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ other.go:
4141
# 11| Type = int
4242
# 11| 0: [Ident, VariableName] myNested
4343
# 11| Type = func() int
44+
# 8| 3: [TypeParamDecl] type parameter declaration
45+
# 8| 0: [Ident, TypeName] int
46+
# 8| Type = int
47+
# 8| 1: [Ident, TypeName] U
48+
# 8| Type = U
4449
# 15| 5: [VarDecl] variable declaration
4550
# 15| 0: [ValueSpec] value declaration specifier
4651
# 15| 0: [Ident, VariableName] x
@@ -50,3 +55,32 @@ other.go:
5055
# 15| 2: [IntLit] 0
5156
# 15| Type = int
5257
# 15| Value = [IntLit] 0
58+
# 17| 6: [TypeDecl] type declaration
59+
# 17| 0: [TypeSpec] type declaration specifier
60+
# 17| 0: [Ident, TypeName] myType
61+
# 17| Type = myType
62+
# 17| 1: [ArrayTypeExpr] array type
63+
# 17| Type = []T
64+
# 17| 0: [Ident, TypeName] T
65+
# 17| Type = T
66+
# 17| 2: [TypeParamDecl] type parameter declaration
67+
# 17| 0: [TypeSetLiteralExpr] type set literal
68+
# 17| Type = ~string
69+
# 17| 0: [Ident, TypeName] string
70+
# 17| Type = string
71+
# 17| 1: [Ident, TypeName] T
72+
# 17| Type = T
73+
# 19| 7: [MethodDecl] function declaration
74+
# 19| 0: [FunctionName, Ident] f
75+
# 19| Type = func()
76+
# 19| 1: [FuncTypeExpr] function type
77+
# 19| 2: [ReceiverDecl] receiver declaration
78+
# 19| 0: [GenericTypeInstantiationExpr] generic type instantiation expression
79+
# 19| Type = myType
80+
# 19| 0: [Ident, TypeName] myType
81+
# 19| Type = myType
82+
# 19| 1: [Ident, TypeName] U
83+
# 19| Type = U
84+
# 19| 1: [Ident, VariableName] m
85+
# 19| Type = myType
86+
# 19| 3: [BlockStmt] block statement

go/ql/test/library-tests/semmle/go/PrintAst/PrintAstRestrictFunction.expected

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
other.go:
2+
# 8| [TypeParamDecl] type parameter declaration
3+
# 8| 0: [Ident, TypeName] int
4+
# 8| Type = int
5+
# 8| 1: [Ident, TypeName] U
6+
# 8| Type = U
17
go.mod:
28
# 0| [GoModFile] go.mod
39
# 1| 0: [GoModModuleLine] go.mod module line
@@ -45,3 +51,18 @@ other.go:
4551
# 15| 2: [IntLit] 0
4652
# 15| Type = int
4753
# 15| Value = [IntLit] 0
54+
# 17| 3: [TypeDecl] type declaration
55+
# 17| 0: [TypeSpec] type declaration specifier
56+
# 17| 0: [Ident, TypeName] myType
57+
# 17| Type = myType
58+
# 17| 1: [ArrayTypeExpr] array type
59+
# 17| Type = []T
60+
# 17| 0: [Ident, TypeName] T
61+
# 17| Type = T
62+
# 17| 2: [TypeParamDecl] type parameter declaration
63+
# 17| 0: [TypeSetLiteralExpr] type set literal
64+
# 17| Type = ~string
65+
# 17| 0: [Ident, TypeName] string
66+
# 17| Type = string
67+
# 17| 1: [Ident, TypeName] T
68+
# 17| Type = T
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
module codeql-go-tests/printast
22

3-
go 1.14
4-
3+
go 1.18

go/ql/test/library-tests/semmle/go/PrintAst/other.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ func main() {}
55
func f() {}
66
func g() {}
77

8-
func hasNested() {
8+
func hasNested[U int]() {
99

1010
myNested := func() int { return 1 }
1111
myNested()
1212

1313
}
1414

1515
var x int = 0
16+
17+
type myType[T ~string] []T
18+
19+
func (m myType[U]) f() {}

0 commit comments

Comments
 (0)