You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs
+154Lines changed: 154 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -5023,6 +5023,160 @@ .locals init (int V_0)
5023
5023
verifier.VerifyIL("C.Test2",expectedIl);
5024
5024
}
5025
5025
5026
+
[Fact]
5027
+
publicvoidLengthPattern_NegativeLengthTest()
5028
+
{
5029
+
varsrc=@"
5030
+
int[] a = null;
5031
+
_ = a is { Length: -1 }; // 1
5032
+
_ = a is { Length: -1 or 1 };
5033
+
_ = a is { Length: -1 } or { Length: 1 };
5034
+
5035
+
_ = a switch // 2
5036
+
{
5037
+
{ Length: -1 } => 0, // 3
5038
+
};
5039
+
5040
+
_ = a switch // 4
5041
+
{
5042
+
{ Length: -1 or 1 } => 0,
5043
+
};
5044
+
5045
+
_ = a switch // 5
5046
+
{
5047
+
{ Length: -1 } or { Length: 1 } => 0,
5048
+
};
5049
+
5050
+
_ = a switch // 6
5051
+
{
5052
+
{ Length: -1 } => 0, // 7
5053
+
{ Length: 1 } => 0,
5054
+
};
5055
+
";
5056
+
varcomp=CreateCompilation(src);
5057
+
comp.VerifyDiagnostics(
5058
+
// (3,5): error CS8518: An expression of type 'int[]' can never match the provided pattern.
5059
+
// _ = a is { Length: -1 }; // 1
5060
+
Diagnostic(ErrorCode.ERR_IsPatternImpossible,"a is { Length: -1 }").WithArguments("int[]").WithLocation(3,5),
5061
+
// (7,7): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '_' is not covered.
// (9,5): error CS8510: The pattern is unreachable. It has already been handled by a previous arm of the switch expression or it is impossible to match.
// (12,7): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{ Length: 0 }' is not covered.
// (17,7): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{ Length: 0 }' is not covered.
// (22,7): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '{ Length: 0 }' is not covered.
// (24,5): error CS8510: The pattern is unreachable. It has already been handled by a previous arm of the switch expression or it is impossible to match.
// (5,7): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern 'not null' is not covered.
// (8,5): error CS8510: The pattern is unreachable. It has already been handled by a previous arm of the switch expression or it is impossible to match.
// (3,5): error CS8518: An expression of type 'int[]' can never match the provided pattern.
5123
+
// _ = a is { Length: -1 } or { Length: -1 };
5124
+
Diagnostic(ErrorCode.ERR_IsPatternImpossible,"a is { Length: -1 } or { Length: -1 }").WithArguments("int[]").WithLocation(3,5),
5125
+
// (7,5): error CS8510: The pattern is unreachable. It has already been handled by a previous arm of the switch expression or it is impossible to match.
// (8,5): error CS8510: The pattern is unreachable. It has already been handled by a previous arm of the switch expression or it is impossible to match.
// (3,5): error CS8518: An expression of type 'int[]' can never match the provided pattern.
5149
+
// _ = a is { Length: < 0 }; // 1
5150
+
Diagnostic(ErrorCode.ERR_IsPatternImpossible,"a is { Length: < 0 }").WithArguments("int[]").WithLocation(3,5),
5151
+
// (5,7): warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern '_' is not covered.
// (7,5): error CS8510: The pattern is unreachable. It has already been handled by a previous arm of the switch expression or it is impossible to match.
// (6,5): error CS8510: The pattern is unreachable. It has already been handled by a previous arm of the switch expression or it is impossible to match.
0 commit comments