Skip to content

Commit 89a4cff

Browse files
authored
Merge pull request github#4662 from tamasvajk/feature/csharp9-type-pattern
C#: Extract type patterns
2 parents f70072a + 3bd6807 commit 89a4cff

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/Pattern.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ internal static Expression Create(Context cx, PatternSyntax syntax, IExpressionP
1717
case ConstantPatternSyntax constantPattern:
1818
return Expression.Create(cx, constantPattern.Expression, parent, child);
1919

20+
case TypePatternSyntax typePattern:
21+
return Expressions.TypeAccess.Create(cx, typePattern.Type, parent, child);
22+
2023
case DeclarationPatternSyntax declPattern:
2124
// Creates a single local variable declaration.
2225
{

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,52 @@ TypeParameterNullability.cs:
348348
# 24| -1: [TypeMention] T?
349349
# 24| 1: [TypeMention] T
350350
# 24| 4: [BlockStmt] {...}
351+
TypePattern.cs:
352+
# 3| [Class] TypePattern
353+
# 5| 5: [Method] M1
354+
# 5| -1: [TypeMention] object
355+
#-----| 2: (Parameters)
356+
# 5| 0: [Parameter] o1
357+
# 5| -1: [TypeMention] object
358+
# 5| 1: [Parameter] o2
359+
# 5| -1: [TypeMention] object
360+
# 6| 4: [BlockStmt] {...}
361+
# 7| 0: [LocalVariableDeclStmt] ... ...;
362+
# 7| 0: [LocalVariableDeclAndInitExpr] (Object,Object) t = ...
363+
# 7| -1: [TypeMention] (object, object)
364+
# 7| 0: [LocalVariableAccess] access to local variable t
365+
# 7| 1: [TupleExpr] (..., ...)
366+
# 7| 0: [ParameterAccess] access to parameter o1
367+
# 7| 1: [ParameterAccess] access to parameter o2
368+
# 8| 1: [IfStmt] if (...) ...
369+
# 8| 0: [IsExpr] ... is ...
370+
# 8| 0: [LocalVariableAccess] access to local variable t
371+
# 8| 1: [RecursivePatternExpr] { ... }
372+
# 8| 2: [PositionalPatternExpr] ( ... )
373+
# 8| 0: [TypeAccessPatternExpr] access to type Int32
374+
# 8| 0: [TypeMention] int
375+
# 8| 1: [TypeAccessPatternExpr] access to type String
376+
# 8| 0: [TypeMention] string
377+
# 8| 1: [BlockStmt] {...}
378+
# 9| 2: [ReturnStmt] return ...;
379+
# 9| 0: [SwitchExpr] ... switch { ... }
380+
# 9| -1: [ParameterAccess] access to parameter o1
381+
# 11| 0: [SwitchCaseExpr] ... => ...
382+
# 11| 0: [TypeAccessPatternExpr] access to type Int32
383+
# 11| 0: [TypeMention] int
384+
# 11| 2: [CastExpr] (...) ...
385+
# 11| 1: [IntLiteral] 1
386+
# 12| 1: [SwitchCaseExpr] ... => ...
387+
# 12| 0: [VariablePatternExpr] Double d
388+
# 12| 0: [TypeMention] double
389+
# 12| 2: [CastExpr] (...) ...
390+
# 12| 1: [LocalVariableAccess] access to local variable d
391+
# 13| 2: [SwitchCaseExpr] ... => ...
392+
# 13| 0: [TypeAccessPatternExpr] access to type String
393+
# 13| 0: [TypeMention] string
394+
# 13| 2: [CastExpr] (...) ...
395+
# 13| 1: [IntLiteral] 3
396+
# 14| 3: [SwitchCaseExpr] ... => ...
397+
# 14| 0: [VariablePatternExpr] Object o
398+
# 14| 0: [TypeMention] object
399+
# 14| 2: [LocalVariableAccess] access to local variable o
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
public class TypePattern
4+
{
5+
object M1(object o1, object o2)
6+
{
7+
var t = (o1, o2);
8+
if (t is (int, string)) {}
9+
return o1 switch
10+
{
11+
int => 1,
12+
double d => d,
13+
System.String => 3,
14+
System.Object o => o
15+
};
16+
}
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
| ParenthesizedPattern.cs:25:13:25:15 | T t | T |
2+
| ParenthesizedPattern.cs:26:14:26:22 | Object o1 | Object |
3+
| ParenthesizedPattern.cs:27:14:27:19 | access to type String | String |
4+
| TypePattern.cs:8:19:8:21 | access to type Int32 | Int32 |
5+
| TypePattern.cs:8:24:8:29 | access to type String | String |
6+
| TypePattern.cs:11:13:11:15 | access to type Int32 | Int32 |
7+
| TypePattern.cs:12:13:12:20 | Double d | Double |
8+
| TypePattern.cs:13:13:13:25 | access to type String | String |
9+
| TypePattern.cs:14:13:14:27 | Object o | Object |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import csharp
2+
3+
from TypePatternExpr pattern
4+
select pattern, pattern.getCheckedType().toString()

0 commit comments

Comments
 (0)