Skip to content

Commit 34f2290

Browse files
authored
Merge pull request #22681 from michaelnebel/csharp/patterntypes
C#: Add types for pattern expressions.
2 parents 605dc1c + bb800d9 commit 34f2290

14 files changed

Lines changed: 295 additions & 24 deletions

File tree

‎csharp/extractor/Semmle.Extraction.CSharp/CodeAnalysisExtensions/SymbolExtensions.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,18 @@ public static AnnotatedTypeSymbol GetType(this Context cx, Microsoft.CodeAnalysi
785785
return new AnnotatedTypeSymbol(info.Type.DisambiguateType(), info.Nullability.Annotation);
786786
}
787787

788+
/// <summary>
789+
/// Gets the converted type of a syntax node, or default if it could not be determined.
790+
/// </summary>
791+
/// <param name="cx">Extractor context.</param>
792+
/// <param name="node">The syntax node to determine the converted type for.</param>
793+
/// <returns>The converted type symbol of the node, or default.</returns>
794+
public static AnnotatedTypeSymbol GetConvertedType(this Context cx, Microsoft.CodeAnalysis.CSharp.CSharpSyntaxNode node)
795+
{
796+
var info = GetTypeInfo(cx, node);
797+
return new AnnotatedTypeSymbol(info.ConvertedType.DisambiguateType(), info.ConvertedNullability.Annotation);
798+
}
799+
788800
/// <summary>
789801
/// Gets the annotated type arguments of an INamedTypeSymbol.
790802
/// This has not yet been exposed on the public API.

‎csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/BinaryPattern.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
88
internal class BinaryPattern : Expression
99
{
1010
public BinaryPattern(Context cx, BinaryPatternSyntax syntax, IExpressionParentEntity parent, int child) :
11-
base(new ExpressionInfo(cx, null, cx.CreateLocation(syntax.GetLocation()), GetKind(syntax.OperatorToken, syntax), parent, child, isCompilerGenerated: false, null))
11+
base(new ExpressionInfo(cx, cx.GetConvertedType(syntax), cx.CreateLocation(syntax.GetLocation()), GetKind(syntax.OperatorToken, syntax), parent, child, isCompilerGenerated: false, null))
1212
{
1313
Pattern.Create(cx, syntax.Left, this, 0);
1414
Pattern.Create(cx, syntax.Right, this, 1);
@@ -24,4 +24,4 @@ private static ExprKind GetKind(SyntaxToken operatorToken, BinaryPatternSyntax s
2424
};
2525
}
2626
}
27-
}
27+
}

‎csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/ListPattern.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
77
internal class ListPattern : Expression
88
{
99
internal ListPattern(Context cx, ListPatternSyntax syntax, IExpressionParentEntity parent, int child) :
10-
base(new ExpressionInfo(cx, null, cx.CreateLocation(syntax.GetLocation()), ExprKind.LIST_PATTERN, parent, child, isCompilerGenerated: false, null))
10+
base(new ExpressionInfo(cx, cx.GetConvertedType(syntax), cx.CreateLocation(syntax.GetLocation()), ExprKind.LIST_PATTERN, parent, child, isCompilerGenerated: false, null))
1111
{
1212
syntax.Patterns.ForEach((p, i) => Pattern.Create(cx, p, this, i));
1313
}
1414
}
15-
}
15+
}

‎csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/RecursivePattern.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal class RecursivePattern : Expression
1515
/// <param name="parent">The parent pattern/expression.</param>
1616
/// <param name="child">The child index of this pattern.</param>
1717
public RecursivePattern(Context cx, RecursivePatternSyntax syntax, IExpressionParentEntity parent, int child) :
18-
base(new ExpressionInfo(cx, null, cx.CreateLocation(syntax.GetLocation()), ExprKind.RECURSIVE_PATTERN, parent, child, isCompilerGenerated: false, null))
18+
base(new ExpressionInfo(cx, cx.GetConvertedType(syntax), cx.CreateLocation(syntax.GetLocation()), ExprKind.RECURSIVE_PATTERN, parent, child, isCompilerGenerated: false, null))
1919
{
2020
// Extract the type access
2121
if (syntax.Type is TypeSyntax t)

‎csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/RelationalPattern.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
88
internal class RelationalPattern : Expression
99
{
1010
public RelationalPattern(Context cx, RelationalPatternSyntax syntax, IExpressionParentEntity parent, int child) :
11-
base(new ExpressionInfo(cx, null, cx.CreateLocation(syntax.GetLocation()), GetKind(syntax.OperatorToken), parent, child, isCompilerGenerated: false, null))
11+
base(new ExpressionInfo(cx, cx.GetConvertedType(syntax), cx.CreateLocation(syntax.GetLocation()), GetKind(syntax.OperatorToken), parent, child, isCompilerGenerated: false, null))
1212
{
1313
Expression.Create(cx, syntax.Expression, this, 0);
1414
}

‎csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/SlicePattern.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
66
internal class SlicePattern : Expression
77
{
88
public SlicePattern(Context cx, SlicePatternSyntax syntax, IExpressionParentEntity parent, int child) :
9-
base(new ExpressionInfo(cx, null, cx.CreateLocation(syntax.GetLocation()), ExprKind.SLICE_PATTERN, parent, child, isCompilerGenerated: false, null))
9+
base(new ExpressionInfo(cx, cx.GetConvertedType(syntax), cx.CreateLocation(syntax.GetLocation()), ExprKind.SLICE_PATTERN, parent, child, isCompilerGenerated: false, null))
1010
{
1111
if (syntax.Pattern is not null)
1212
{
1313
Pattern.Create(cx, syntax.Pattern, this, 0);
1414
}
1515
}
1616
}
17-
}
17+
}

‎csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Patterns/UnaryPattern.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace Semmle.Extraction.CSharp.Entities.Expressions
66
internal class UnaryPattern : Expression
77
{
88
public UnaryPattern(Context cx, UnaryPatternSyntax syntax, IExpressionParentEntity parent, int child) :
9-
base(new ExpressionInfo(cx, null, cx.CreateLocation(syntax.GetLocation()), ExprKind.NOT_PATTERN, parent, child, isCompilerGenerated: false, null))
9+
base(new ExpressionInfo(cx, cx.GetConvertedType(syntax), cx.CreateLocation(syntax.GetLocation()), ExprKind.NOT_PATTERN, parent, child, isCompilerGenerated: false, null))
1010
{
1111
Pattern.Create(cx, syntax.Pattern, this, 0);
1212
}
1313
}
14-
}
14+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: fix
3+
---
4+
* Fixed an issue where types for pattern expressions were not extracted correctly.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
| ListPattern.cs:7:18:7:19 | [ ... ] | Int32[] |
2+
| ListPattern.cs:8:18:8:20 | [ ... ] | Int32[] |
3+
| ListPattern.cs:8:19:8:19 | 1 | Int32 |
4+
| ListPattern.cs:9:18:9:23 | [ ... ] | Int32[] |
5+
| ListPattern.cs:9:19:9:19 | _ | Int32 |
6+
| ListPattern.cs:9:22:9:22 | 2 | Int32 |
7+
| ListPattern.cs:10:18:10:30 | [ ... ] | Int32[] |
8+
| ListPattern.cs:10:19:10:23 | Int32 y | Int32 |
9+
| ListPattern.cs:10:26:10:26 | 3 | Int32 |
10+
| ListPattern.cs:10:29:10:29 | 4 | Int32 |
11+
| ListPattern.cs:11:18:11:31 | [ ... ] | Int32[] |
12+
| ListPattern.cs:11:19:11:19 | 5 | Int32 |
13+
| ListPattern.cs:11:19:11:24 | ... or ... | Int32 |
14+
| ListPattern.cs:11:24:11:24 | 6 | Int32 |
15+
| ListPattern.cs:11:27:11:27 | _ | Int32 |
16+
| ListPattern.cs:11:30:11:30 | 7 | Int32 |
17+
| ListPattern.cs:12:18:12:31 | [ ... ] | Int32[] |
18+
| ListPattern.cs:12:19:12:23 | Int32 a | Int32 |
19+
| ListPattern.cs:12:26:12:27 | .. | Int32[] |
20+
| ListPattern.cs:12:30:12:30 | 2 | Int32 |
21+
| ListPattern.cs:13:18:13:50 | [ ... ] | Int32[] |
22+
| ListPattern.cs:13:19:13:23 | Int32 b | Int32 |
23+
| ListPattern.cs:13:26:13:46 | .. | Int32[] |
24+
| ListPattern.cs:13:29:13:46 | { ... } | Int32[] |
25+
| ListPattern.cs:13:29:13:46 | { ... } | null |
26+
| ListPattern.cs:13:39:13:39 | 2 | Int32 |
27+
| ListPattern.cs:13:39:13:44 | ... or ... | Int32 |
28+
| ListPattern.cs:13:44:13:44 | 5 | Int32 |
29+
| ListPattern.cs:13:49:13:49 | 2 | Int32 |
30+
| ListPattern.cs:20:18:20:19 | [ ... ] | String[] |
31+
| ListPattern.cs:22:18:22:22 | [ ... ] | String[] |
32+
| ListPattern.cs:22:19:22:21 | "A" | String |
33+
| ListPattern.cs:24:18:24:25 | [ ... ] | String[] |
34+
| ListPattern.cs:24:19:24:19 | _ | String |
35+
| ListPattern.cs:24:22:24:24 | "B" | String |
36+
| ListPattern.cs:26:18:26:34 | [ ... ] | String[] |
37+
| ListPattern.cs:26:19:26:23 | String y | String |
38+
| ListPattern.cs:26:26:26:28 | "C" | String |
39+
| ListPattern.cs:26:31:26:33 | "D" | String |
40+
| ListPattern.cs:28:18:28:37 | [ ... ] | String[] |
41+
| ListPattern.cs:28:19:28:21 | "E" | String |
42+
| ListPattern.cs:28:19:28:28 | ... or ... | String |
43+
| ListPattern.cs:28:26:28:28 | "F" | String |
44+
| ListPattern.cs:28:31:28:31 | _ | String |
45+
| ListPattern.cs:28:34:28:36 | "G" | String |
46+
| ListPattern.cs:30:18:30:33 | [ ... ] | String[] |
47+
| ListPattern.cs:30:19:30:23 | String a | String |
48+
| ListPattern.cs:30:26:30:27 | .. | String[] |
49+
| ListPattern.cs:30:30:30:32 | "H" | String |
50+
| ListPattern.cs:32:18:32:39 | [ ... ] | String[] |
51+
| ListPattern.cs:32:19:32:23 | String b | String |
52+
| ListPattern.cs:32:26:32:33 | .. | String[] |
53+
| ListPattern.cs:32:29:32:33 | String[] c | String[] |
54+
| ListPattern.cs:32:36:32:38 | "I" | String |
55+
| PatternMatchSpan.cs:8:19:8:23 | "ABC" | String |
56+
| PatternMatchSpan.cs:15:18:15:22 | "DEF" | String |
57+
| Strings.cs:10:13:10:14 | 42 | Int32 |
58+
| Strings.cs:11:13:11:13 | _ | Int32 |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import csharp
2+
3+
from PatternExpr pattern
4+
select pattern, pattern.getType().toString()

0 commit comments

Comments
 (0)