Skip to content

Commit 7d38b2d

Browse files
authored
Merge pull request github#4623 from tamasvajk/feature/csharp9-type-param-nullability
C#: Add type parameter ref/value type tests
2 parents adc7bbf + 0fa3cf7 commit 7d38b2d

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

csharp/ql/test/library-tests/csharp9/PrintAst.expected

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,106 @@ NativeInt.cs:
105105
# 25| 0: [CastExpr] (...) ...
106106
# 25| 1: [LocalVariableAccess] access to local variable x
107107
# 25| 1: [LocalVariableAccess] access to local variable v
108+
TypeParameterNullability.cs:
109+
# 1| [Interface] I1
110+
# 3| [Class] A2
111+
# 5| 5: [Method] F1
112+
# 5| -1: [TypeMention] Void
113+
#-----| 1: (Type parameters)
114+
# 5| 0: [TypeParameter] T
115+
#-----| 2: (Parameters)
116+
# 5| 0: [Parameter] t
117+
# 5| 4: [BlockStmt] {...}
118+
# 6| 6: [Method] F2
119+
# 6| -1: [TypeMention] Void
120+
#-----| 1: (Type parameters)
121+
# 6| 0: [TypeParameter] T
122+
#-----| 2: (Parameters)
123+
# 6| 0: [Parameter] t
124+
# 6| -1: [TypeMention] T?
125+
# 6| 1: [TypeMention] T
126+
# 6| 4: [BlockStmt] {...}
127+
# 7| 7: [Method] F3
128+
# 7| -1: [TypeMention] Void
129+
#-----| 1: (Type parameters)
130+
# 7| 0: [TypeParameter] T
131+
#-----| 2: (Parameters)
132+
# 7| 0: [Parameter] t
133+
# 7| 4: [BlockStmt] {...}
134+
# 8| 8: [Method] F4
135+
# 8| -1: [TypeMention] Void
136+
#-----| 1: (Type parameters)
137+
# 8| 0: [TypeParameter] T
138+
#-----| 2: (Parameters)
139+
# 8| 0: [Parameter] t
140+
# 8| 4: [BlockStmt] {...}
141+
# 9| 9: [Method] F5
142+
# 9| -1: [TypeMention] Void
143+
#-----| 1: (Type parameters)
144+
# 9| 0: [TypeParameter] T
145+
#-----| 2: (Parameters)
146+
# 9| 0: [Parameter] t
147+
# 9| 4: [BlockStmt] {...}
148+
# 10| 10: [Method] F6
149+
# 10| -1: [TypeMention] Void
150+
#-----| 1: (Type parameters)
151+
# 10| 0: [TypeParameter] T
152+
#-----| 2: (Parameters)
153+
# 10| 0: [Parameter] t
154+
# 10| -1: [TypeMention] T?
155+
# 10| 1: [TypeMention] T
156+
# 10| 4: [BlockStmt] {...}
157+
# 13| [Class] B2
158+
#-----| 3: (Base types)
159+
# 13| 0: [TypeMention] A2
160+
# 15| 5: [Method] F1
161+
# 15| -1: [TypeMention] Void
162+
#-----| 1: (Type parameters)
163+
# 15| 0: [TypeParameter] T
164+
#-----| 2: (Parameters)
165+
# 15| 0: [Parameter] t
166+
# 15| 4: [BlockStmt] {...}
167+
# 16| 6: [Method] F2
168+
# 16| -1: [TypeMention] Void
169+
#-----| 1: (Type parameters)
170+
# 16| 0: [TypeParameter] T
171+
#-----| 2: (Parameters)
172+
# 16| 0: [Parameter] t
173+
# 16| -1: [TypeMention] T?
174+
# 16| 1: [TypeMention] T
175+
# 16| 4: [BlockStmt] {...}
176+
# 17| 7: [Method] F3
177+
# 17| -1: [TypeMention] Void
178+
#-----| 1: (Type parameters)
179+
# 17| 0: [TypeParameter] T
180+
#-----| 2: (Parameters)
181+
# 17| 0: [Parameter] t
182+
# 17| 4: [BlockStmt] {...}
183+
# 18| 8: [Method] F4
184+
# 18| -1: [TypeMention] Void
185+
#-----| 1: (Type parameters)
186+
# 18| 0: [TypeParameter] T
187+
#-----| 2: (Parameters)
188+
# 18| 0: [Parameter] t
189+
# 18| 4: [BlockStmt] {...}
190+
# 19| 9: [Method] F6
191+
# 19| -1: [TypeMention] Void
192+
#-----| 1: (Type parameters)
193+
# 19| 0: [TypeParameter] T
194+
#-----| 2: (Parameters)
195+
# 19| 0: [Parameter] t
196+
# 19| -1: [TypeMention] T?
197+
# 19| 1: [TypeMention] T
198+
# 19| 4: [BlockStmt] {...}
199+
# 22| [Class] B3
200+
#-----| 3: (Base types)
201+
# 22| 0: [TypeMention] A2
202+
# 24| 5: [Method] F2
203+
# 24| -1: [TypeMention] Void
204+
#-----| 1: (Type parameters)
205+
# 24| 0: [TypeParameter] T
206+
#-----| 2: (Parameters)
207+
# 24| 0: [Parameter] t
208+
# 24| -1: [TypeMention] T?
209+
# 24| 1: [TypeMention] T
210+
# 24| 4: [BlockStmt] {...}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
interface I1 {}
2+
3+
class A2
4+
{
5+
public virtual void F1<T>(T? t) { } // value or ref type
6+
public virtual void F2<T>(T? t) where T: struct { } // value type
7+
public virtual void F3<T>(T? t) where T: class { } // ref type
8+
public virtual void F4<T>(T? t) where T: A2 { } // A2, ref type
9+
public virtual void F5<T>(T? t) where T: I1 { } // I1, value or ref type
10+
public virtual void F6<T>(T? t) where T: struct, I1 { } // I1, value type
11+
}
12+
13+
class B2 : A2
14+
{
15+
public override void F1<T>(T? t) where T: default { } // value or ref type
16+
public override void F2<T>(T? t) { } // value type
17+
public override void F3<T>(T? t) where T: class { } // ref type
18+
public override void F4<T>(T? t) where T: class { } // A2, ref type
19+
public override void F6<T>(T? t) { } // I1, value type
20+
}
21+
22+
class B3 : A2
23+
{
24+
public override void F2<T>(T? t) where T: struct { } // value type
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
refType
2+
| TypeParameterNullability.cs:7:28:7:28 | T |
3+
| TypeParameterNullability.cs:8:28:8:28 | T |
4+
| TypeParameterNullability.cs:17:29:17:29 | T |
5+
| TypeParameterNullability.cs:18:29:18:29 | T |
6+
valueType
7+
| TypeParameterNullability.cs:6:28:6:28 | T |
8+
| TypeParameterNullability.cs:10:28:10:28 | T |
9+
| TypeParameterNullability.cs:16:29:16:29 | T |
10+
| TypeParameterNullability.cs:19:29:19:29 | T |
11+
| TypeParameterNullability.cs:24:29:24:29 | T |
12+
valueOrRefType
13+
| TypeParameterNullability.cs:5:28:5:28 | T |
14+
| TypeParameterNullability.cs:9:28:9:28 | T |
15+
| TypeParameterNullability.cs:15:29:15:29 | T |
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import csharp
2+
3+
query predicate refType(TypeParameter tp) {
4+
tp.fromSource() and
5+
tp.isRefType()
6+
}
7+
8+
query predicate valueType(TypeParameter tp) {
9+
tp.fromSource() and
10+
tp.isValueType()
11+
}
12+
13+
query predicate valueOrRefType(TypeParameter tp) {
14+
tp.fromSource() and
15+
not tp.isRefType() and
16+
not tp.isValueType()
17+
}

0 commit comments

Comments
 (0)