Skip to content

Commit

Permalink
BoundIsPatternExpression.GetDecisionDagForLowering - when recreating …
Browse files Browse the repository at this point in the history
…a DAG, do that for the the pattern with any 'not's removed, as consumers expect. (#65878)

 Use the inner pattern to recreate the decision DAG for is-expressions

Fixes #65876
  • Loading branch information
alrz authored Dec 21, 2022
1 parent 76cd2f1 commit 228d04a
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ public BoundDecisionDag GetDecisionDagForLowering(CSharpCompilation compilation)
BoundDecisionDag decisionDag = this.ReachabilityDecisionDag;
if (decisionDag.ContainsAnySynthesizedNodes())
{
bool negated = this.Pattern.IsNegated(out var innerPattern);
Debug.Assert(negated == this.IsNegated);
decisionDag = DecisionDagBuilder.CreateDecisionDagForIsPattern(
compilation,
this.Syntax,
this.Expression,
this.Pattern,
innerPattern,
this.WhenTrueLabel,
this.WhenFalseLabel,
BindingDiagnosticBag.Discarded,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3090,6 +3090,163 @@ public void Test2(int[] a)
);
}

[Fact, WorkItem(65876, "https://github.com/dotnet/roslyn/issues/65876")]
public void ListPattern_Negated_03()
{
var source = """
using System;
public class C
{
static void Main()
{
Console.WriteLine(M1(new[]{1,2}));
Console.WriteLine(M1(new[]{2,1}));
Console.WriteLine(M1(new[]{1}));
Console.WriteLine(M1(new[]{0}));
Console.WriteLine(M2(new[]{1,2}));
Console.WriteLine(M2(new[]{2,1}));
Console.WriteLine(M2(new[]{1}));
Console.WriteLine(M2(new[]{0}));
}
public static bool M1(int[] a) {
return a is not ([1,2,..] or [..,2,1] or [1]);
}
public static bool M2(int[] a) {
return !(a is ([1,2,..] or [..,2,1] or [1]));
}
}
""";
var comp = CreateCompilationWithIndexAndRangeAndSpan(source, options: TestOptions.ReleaseExe);
CompileAndVerify(comp, expectedOutput: @"
False
False
False
True
False
False
False
True
");
}

[Fact]
public void ListPattern_Negated_04()
{
var source = """
using System;
public class C
{
static void Main()
{
Console.WriteLine(M1(new[]{1,2}));
Console.WriteLine(M1(new[]{2,1}));
Console.WriteLine(M1(new[]{1}));
Console.WriteLine(M1(new[]{0}));
Console.WriteLine(M2(new[]{1,2}));
Console.WriteLine(M2(new[]{2,1}));
Console.WriteLine(M2(new[]{1}));
Console.WriteLine(M2(new[]{0}));
}
public static int M1(int[] a) {
return a switch
{
not ([1,2,..] or [..,2,1] or [1]) => 1,
[1,2,..] => 2,
[..,2,1] => 3,
[1] => 4,
};
}
public static int M2(int[] a) {
switch (a)
{
case not ([1,2,..] or [..,2,1] or [1]):
return 1;
case [1,2,..]:
return 2;
case [..,2,1]:
return 3;
case [1]:
return 4;
}
}
}
""";
var comp = CreateCompilationWithIndexAndRangeAndSpan(source, options: TestOptions.ReleaseExe);
CompileAndVerify(comp, expectedOutput: @"
2
3
4
1
2
3
4
1
");
}

[Fact]
public void ListPattern_Negated_05()
{
var source = """
using System;
public class C
{
static void Main()
{
Console.WriteLine(M1(new[]{1,2}, new[]{1}));
Console.WriteLine(M1(new[]{1}, new[]{2,1}));
Console.WriteLine(M1(new[]{2,1}, new[]{1,2}));
Console.WriteLine(M1(new[]{0}, new[]{0}));
Console.WriteLine(M2(new[]{1,2}, new[]{1}));
Console.WriteLine(M2(new[]{1}, new[]{2,1}));
Console.WriteLine(M2(new[]{2,1}, new[]{1,2}));
Console.WriteLine(M2(new[]{0}, new[]{0}));
}
public static int M1(int[] a, int[] b) {
return (a, b) switch
{
(not ([1,2,..] or [..,2,1] or [1]),
not ([1,2,..] or [..,2,1] or [1])) => 1,
([1,2,..] or [1], [..,2,1] or [1]) => 2,
([..,2,1], [1,2,..]) => 3,
_ => 0
};
}
public static int M2(int[] a, int[] b) {
switch (a, b)
{
case (not ([1,2,..] or [..,2,1] or [1]),
not ([1,2,..] or [..,2,1] or [1])):
return 1;
case ([1,2,..] or [1], [..,2,1] or [1]):
return 2;
case ([..,2,1], [1,2,..]):
return 3;
default:
return 0;
}
}
}
""";
var comp = CreateCompilationWithIndexAndRangeAndSpan(source, options: TestOptions.ReleaseExe);
CompileAndVerify(comp, expectedOutput: @"
2
2
3
1
2
2
3
1
");
}

[Fact]
public void ListPattern_UseSiteErrorOnIndexerAndSlice()
{
Expand Down

0 comments on commit 228d04a

Please sign in to comment.