Skip to content

Commit c751c51

Browse files
authored
Merge pull request github#4614 from tamasvajk/feature/csharp9-target-typed
C#: C#9 Add target typed conditional tests
2 parents a48235e + 548f276 commit c751c51

File tree

4 files changed

+217
-0
lines changed

4 files changed

+217
-0
lines changed

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

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,145 @@ ParenthesizedPattern.cs:
335335
# 27| 0: [TypeAccessPatternExpr] access to type String
336336
# 27| 0: [TypeMention] string
337337
# 27| 2: [IntLiteral] 5
338+
TargetType.cs:
339+
# 5| [Class] TargetType
340+
# 7| 5: [Method] M2
341+
# 7| -1: [TypeMention] Void
342+
# 8| 4: [BlockStmt] {...}
343+
# 9| 0: [LocalVariableDeclStmt] ... ...;
344+
# 9| 0: [LocalVariableDeclAndInitExpr] Random rand = ...
345+
# 9| -1: [TypeMention] Random
346+
# 9| 0: [LocalVariableAccess] access to local variable rand
347+
# 9| 1: [ObjectCreation] object creation of type Random
348+
# 9| 0: [TypeMention] Random
349+
# 10| 1: [LocalVariableDeclStmt] ... ...;
350+
# 10| 0: [LocalVariableDeclAndInitExpr] Boolean condition = ...
351+
# 10| -1: [TypeMention] bool
352+
# 10| 0: [LocalVariableAccess] access to local variable condition
353+
# 10| 1: [GTExpr] ... > ...
354+
# 10| 0: [MethodCall] call to method NextDouble
355+
# 10| -1: [LocalVariableAccess] access to local variable rand
356+
# 10| 1: [DoubleLiteral] 0.5
357+
# 12| 2: [LocalVariableDeclStmt] ... ...;
358+
# 12| 0: [LocalVariableDeclAndInitExpr] Nullable<Int32> x0 = ...
359+
# 12| -1: [TypeMention] int?
360+
# 12| 1: [TypeMention] int
361+
# 12| 0: [LocalVariableAccess] access to local variable x0
362+
# 12| 1: [CastExpr] (...) ...
363+
# 12| 1: [IntLiteral] 12
364+
# 13| 3: [ExprStmt] ...;
365+
# 13| 0: [AssignExpr] ... = ...
366+
# 13| 0: [LocalVariableAccess] access to local variable x0
367+
# 13| 1: [CastExpr] (...) ...
368+
# 13| 1: [IntLiteral] 13
369+
# 14| 4: [LocalVariableDeclStmt] ... ...;
370+
# 14| 0: [LocalVariableDeclAndInitExpr] Nullable<Int32> x1 = ...
371+
# 14| -1: [TypeMention] int?
372+
# 14| 1: [TypeMention] int
373+
# 14| 0: [LocalVariableAccess] access to local variable x1
374+
# 14| 1: [NullLiteral] null
375+
# 16| 5: [LocalVariableDeclStmt] ... ...;
376+
# 16| 0: [LocalVariableDeclAndInitExpr] Nullable<Int32> x2 = ...
377+
# 16| -1: [TypeMention] int?
378+
# 16| 1: [TypeMention] int
379+
# 16| 0: [LocalVariableAccess] access to local variable x2
380+
# 16| 1: [ConditionalExpr] ... ? ... : ...
381+
# 16| 0: [LocalVariableAccess] access to local variable condition
382+
# 17| 1: [CastExpr] (...) ...
383+
# 17| 1: [IntLiteral] 12
384+
# 18| 2: [NullLiteral] null
385+
# 20| 6: [LocalVariableDeclStmt] ... ...;
386+
# 20| 0: [LocalVariableDeclAndInitExpr] Nullable<Int32> x3 = ...
387+
# 20| -1: [TypeMention] int?
388+
# 20| 1: [TypeMention] int
389+
# 20| 0: [LocalVariableAccess] access to local variable x3
390+
# 20| 1: [ConditionalExpr] ... ? ... : ...
391+
# 20| 0: [LocalVariableAccess] access to local variable condition
392+
# 21| 1: [CastExpr] (...) ...
393+
# 21| 0: [TypeAccess] access to type Nullable<Int32>
394+
# 21| 0: [TypeMention] int?
395+
# 21| 1: [TypeMention] int
396+
# 21| 1: [IntLiteral] 12
397+
# 22| 2: [NullLiteral] null
398+
# 24| 7: [LocalVariableDeclStmt] ... ...;
399+
# 24| 0: [LocalVariableDeclAndInitExpr] Nullable<Int32> x4 = ...
400+
# 24| -1: [TypeMention] int?
401+
# 24| 1: [TypeMention] int
402+
# 24| 0: [LocalVariableAccess] access to local variable x4
403+
# 24| 1: [ConditionalExpr] ... ? ... : ...
404+
# 24| 0: [LocalVariableAccess] access to local variable condition
405+
# 25| 1: [CastExpr] (...) ...
406+
# 25| 1: [IntLiteral] 12
407+
# 26| 2: [CastExpr] (...) ...
408+
# 26| 0: [TypeAccess] access to type Nullable<Int32>
409+
# 26| 0: [TypeMention] int?
410+
# 26| 1: [TypeMention] int
411+
# 26| 1: [NullLiteral] null
412+
# 28| 8: [LocalVariableDeclStmt] ... ...;
413+
# 28| 0: [LocalVariableDeclAndInitExpr] IEnumerable<Int32> xs0 = ...
414+
# 28| -1: [TypeMention] IEnumerable<Int32>
415+
# 28| 1: [TypeMention] int
416+
# 28| 0: [LocalVariableAccess] access to local variable xs0
417+
# 28| 1: [ObjectCreation] object creation of type List<Int32>
418+
# 28| -2: [TypeMention] List<Int32>
419+
# 28| 1: [TypeMention] int
420+
# 28| -1: [CollectionInitializer] { ..., ... }
421+
# 28| 0: [ElementInitializer] call to method Add
422+
# 28| 0: [IntLiteral] 0
423+
# 28| 1: [ElementInitializer] call to method Add
424+
# 28| 0: [IntLiteral] 1
425+
# 29| 9: [LocalVariableDeclStmt] ... ...;
426+
# 29| 0: [LocalVariableDeclAndInitExpr] IEnumerable<Int32> xs1 = ...
427+
# 29| -1: [TypeMention] IEnumerable<Int32>
428+
# 29| 1: [TypeMention] int
429+
# 29| 0: [LocalVariableAccess] access to local variable xs1
430+
# 29| 1: [ArrayCreation] array creation of type Int32[]
431+
# 29| -2: [TypeMention] Int32[]
432+
# 29| 1: [TypeMention] int
433+
# 29| -1: [ArrayInitializer] { ..., ... }
434+
# 29| 0: [IntLiteral] 2
435+
# 29| 1: [IntLiteral] 3
436+
# 31| 10: [LocalVariableDeclStmt] ... ...;
437+
# 31| 0: [LocalVariableDeclAndInitExpr] IEnumerable<Int32> xs2 = ...
438+
# 31| -1: [TypeMention] IEnumerable<Int32>
439+
# 31| 1: [TypeMention] int
440+
# 31| 0: [LocalVariableAccess] access to local variable xs2
441+
# 31| 1: [ConditionalExpr] ... ? ... : ...
442+
# 31| 0: [IsExpr] ... is ...
443+
# 31| 0: [LocalVariableAccess] access to local variable x2
444+
# 31| 1: [ConstantPatternExpr,NullLiteral] null
445+
# 32| 1: [ObjectCreation] object creation of type List<Int32>
446+
# 32| -2: [TypeMention] List<Int32>
447+
# 32| 1: [TypeMention] int
448+
# 32| -1: [CollectionInitializer] { ..., ... }
449+
# 32| 0: [ElementInitializer] call to method Add
450+
# 32| 0: [IntLiteral] 0
451+
# 32| 1: [ElementInitializer] call to method Add
452+
# 32| 0: [IntLiteral] 1
453+
# 33| 2: [ArrayCreation] array creation of type Int32[]
454+
# 33| -2: [TypeMention] Int32[]
455+
# 33| 1: [TypeMention] int
456+
# 33| -1: [ArrayInitializer] { ..., ... }
457+
# 33| 0: [IntLiteral] 2
458+
# 33| 1: [IntLiteral] 3
459+
# 35| 11: [LocalVariableDeclStmt] ... ...;
460+
# 35| 0: [LocalVariableDeclAndInitExpr] Nullable<Int32> c = ...
461+
# 35| -1: [TypeMention] int?
462+
# 35| 1: [TypeMention] int
463+
# 35| 0: [LocalVariableAccess] access to local variable c
464+
# 35| 1: [CastExpr] (...) ...
465+
# 35| 1: [ConditionalExpr] ... ? ... : ...
466+
# 35| 0: [LocalVariableAccess] access to local variable condition
467+
# 36| 1: [OperatorCall] call to operator implicit conversion
468+
# 36| 0: [ObjectCreation] object creation of type TargetType
469+
# 36| 0: [TypeMention] TargetType
470+
# 37| 2: [IntLiteral] 12
471+
# 40| 6: [ImplicitConversionOperator] implicit conversion
472+
# 40| -1: [TypeMention] int
473+
#-----| 2: (Parameters)
474+
# 40| 0: [Parameter] d
475+
# 40| -1: [TypeMention] TargetType
476+
# 40| 4: [IntLiteral] 0
338477
TypeParameterNullability.cs:
339478
# 1| [Interface] I1
340479
# 3| [Class] A2
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
public class TargetType
6+
{
7+
public void M2()
8+
{
9+
var rand = new Random();
10+
var condition = rand.NextDouble() > 0.5;
11+
12+
int? x0 = 12;
13+
x0 = 13;
14+
int? x1 = null;
15+
16+
int? x2 = condition
17+
? 12
18+
: null;
19+
20+
int? x3 = condition
21+
? (int?)12
22+
: null;
23+
24+
int? x4 = condition
25+
? 12
26+
: (int?)null;
27+
28+
IEnumerable<int> xs0 = new List<int>() { 0, 1 };
29+
IEnumerable<int> xs1 = new int[] { 2, 3 };
30+
31+
IEnumerable<int> xs2 = x2 is null
32+
? new List<int>() { 0, 1 }
33+
: new int[] { 2, 3 };
34+
35+
int? c = condition
36+
? new TargetType()
37+
: 12;
38+
}
39+
40+
public static implicit operator int(TargetType d) => 0;
41+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
conditional
2+
| TargetType.cs:16:19:18:18 | ... ? ... : ... | int? | int? | null |
3+
| TargetType.cs:20:19:22:18 | ... ? ... : ... | int? | int? | null |
4+
| TargetType.cs:24:19:26:24 | ... ? ... : ... | int? | int? | int? |
5+
| TargetType.cs:31:32:33:32 | ... ? ... : ... | IEnumerable<Int32> | List<Int32> | Int32[] |
6+
| TargetType.cs:35:18:37:16 | ... ? ... : ... | int | int | int |
7+
implicitCasts
8+
| TargetType.cs:12:19:12:20 | (...) ... | int? | int |
9+
| TargetType.cs:13:14:13:15 | (...) ... | int? | int |
10+
| TargetType.cs:17:15:17:16 | (...) ... | int? | int |
11+
| TargetType.cs:25:15:25:16 | (...) ... | int? | int |
12+
| TargetType.cs:35:18:37:16 | (...) ... | int? | int |
13+
implicitConversions
14+
| TargetType.cs:36:15:36:30 | call to operator implicit conversion | int | TargetType |
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import csharp
2+
3+
query predicate conditional(ConditionalExpr expr, string t, string t1, string t2) {
4+
expr.getType().toStringWithTypes() = t and
5+
expr.getThen().getType().toStringWithTypes() = t1 and
6+
expr.getElse().getType().toStringWithTypes() = t2
7+
}
8+
9+
query predicate implicitCasts(CastExpr expr, string type, string childType) {
10+
expr.getFile().getStem() = "TargetType" and
11+
expr.fromSource() and
12+
not exists(expr.getTypeAccess()) and
13+
type = expr.getType().toStringWithTypes() and
14+
childType = expr.getExpr().getType().toStringWithTypes()
15+
}
16+
17+
query predicate implicitConversions(OperatorCall opCall, string type, string childType) {
18+
opCall.getFile().getStem() = "TargetType" and
19+
opCall.fromSource() and
20+
opCall.getTarget().getFunctionName() = "op_Implicit" and
21+
opCall.getTarget().getReturnType().toStringWithTypes() = type and
22+
opCall.getAnArgument().getType().toStringWithTypes() = childType
23+
}

0 commit comments

Comments
 (0)