Skip to content

Commit 9715ffd

Browse files
authored
Merge pull request #18205 from michaelnebel/csharp/narrowpatterntypes
C#: Narrow pattern types
2 parents f897614 + 57c3b57 commit 9715ffd

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

csharp/.vscode/launch.json

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"env": {},
7676
"stopAtEntry": true,
7777
"justMyCode": false,
78+
"requireExactSource": false,
7879
"suppressJITOptimizations": true
7980
},
8081
]

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

+11-12
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ internal static Expression Create(Context cx, PatternSyntax syntax, IExpressionP
2828
case DeclarationPatternSyntax declPattern:
2929
// Creates a single local variable declaration.
3030
{
31-
if (declPattern.Designation is VariableDesignationSyntax designation)
31+
switch (declPattern.Designation)
3232
{
33-
if (cx.GetModel(syntax).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
34-
{
35-
var type = symbol.GetAnnotatedType();
36-
return VariableDeclaration.Create(cx, symbol, type, declPattern.Type, cx.CreateLocation(syntax.GetLocation()), false, parent, child);
37-
}
38-
if (designation is DiscardDesignationSyntax)
39-
{
33+
case SingleVariableDesignationSyntax singleDesignation:
34+
if (cx.GetModel(syntax).GetDeclaredSymbol(singleDesignation) is ILocalSymbol symbol)
35+
{
36+
var type = symbol.GetAnnotatedType();
37+
return VariableDeclaration.Create(cx, symbol, type, declPattern.Type, cx.CreateLocation(syntax.GetLocation()), false, parent, child);
38+
}
39+
throw new InternalError(singleDesignation, "Unable to get the declared symbol of the declaration pattern designation.");
40+
case DiscardDesignationSyntax _:
4041
return Expressions.TypeAccess.Create(cx, declPattern.Type, parent, child);
41-
}
42-
throw new InternalError(designation, "Designation pattern not handled");
42+
default:
43+
throw new InternalError($"declaration pattern designation of type {declPattern.Designation.GetType()} is unhandled");
4344
}
44-
throw new InternalError(declPattern, "Declaration pattern not handled");
4545
}
4646

4747
case RecursivePatternSyntax recPattern:
@@ -59,7 +59,6 @@ internal static Expression Create(Context cx, PatternSyntax syntax, IExpressionP
5959
if (cx.GetModel(syntax).GetDeclaredSymbol(varDesignation) is ILocalSymbol symbol)
6060
{
6161
var type = symbol.GetAnnotatedType();
62-
6362
return VariableDeclaration.Create(cx, symbol, type, null, cx.CreateLocation(syntax.GetLocation()), true, parent, child);
6463
}
6564

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public RecursivePattern(Context cx, RecursivePatternSyntax syntax, IExpressionPa
2222
Expressions.TypeAccess.Create(cx, t, this, 1);
2323

2424
// Extract the local variable declaration
25-
if (syntax.Designation is VariableDesignationSyntax designation && cx.GetModel(syntax).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
25+
if (syntax.Designation is SingleVariableDesignationSyntax designation && cx.GetModel(syntax).GetDeclaredSymbol(designation) is ILocalSymbol symbol)
2626
{
2727
var type = symbol.GetAnnotatedType();
2828

0 commit comments

Comments
 (0)