diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTestBase.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTestBase.cs index 3b592a4aab8f7..1123370d1d3ed 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTestBase.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTestBase.cs @@ -15,6 +15,7 @@ using Xunit; using Roslyn.Utilities; using ReferenceEqualityComparer = Roslyn.Utilities.ReferenceEqualityComparer; +using System.Diagnostics; namespace Microsoft.CodeAnalysis.CSharp.UnitTests { @@ -439,6 +440,26 @@ protected static void AssertEmpty(SymbolInfo info) Assert.Null(info.Symbol); Assert.Equal(CandidateReason.None, info.CandidateReason); } + + protected static void VerifyDecisionDagDump(Compilation comp, string expectedDecisionDag) + where T : CSharpSyntaxNode + { +#if DEBUG + var tree = comp.SyntaxTrees.First(); + var node = tree.GetRoot().DescendantNodes().OfType().First(); + var model = (CSharpSemanticModel)comp.GetSemanticModel(tree); + var binder = model.GetEnclosingBinder(node.SpanStart); + var decisionDag = node switch + { + SwitchStatementSyntax n => ((BoundSwitchStatement)binder.BindStatement(n, BindingDiagnosticBag.Discarded)).DecisionDag, + SwitchExpressionSyntax n => ((BoundSwitchExpression)binder.BindExpression(n, BindingDiagnosticBag.Discarded)).DecisionDag, + IsPatternExpressionSyntax n => ((BoundIsPatternExpression)binder.BindExpression(n, BindingDiagnosticBag.Discarded)).DecisionDag, + var v => throw ExceptionUtilities.UnexpectedValue(v) + }; + + AssertEx.Equal(expectedDecisionDag, decisionDag.Dump()); +#endif + } #endregion helpers } } diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs index d0b6c93388af1..73d041116fc55 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs @@ -2156,12 +2156,7 @@ void Test(int[] a) Diagnostic(ErrorCode.ERR_SwitchCaseSubsumed, "[42]").WithLocation(9, 18) ); - var tree = comp.SyntaxTrees.First(); - var @switch = tree.GetRoot().DescendantNodes().OfType().Single(); - var model = (CSharpSemanticModel)comp.GetSemanticModel(tree); - var binder = model.GetEnclosingBinder(@switch.SpanStart); - var boundSwitch = (BoundSwitchStatement)binder.BindStatement(@switch, BindingDiagnosticBag.Discarded); - AssertEx.Equal( + VerifyDecisionDagDump(comp, @"[0]: t0 != null ? [1] : [9] [1]: t1 = t0.Length; [2] [2]: t1 >= 1 ? [3] : [9] @@ -2177,7 +2172,7 @@ void Test(int[] a) case [42]: break; }` -", boundSwitch.DecisionDag.Dump()); +"); } [Fact] @@ -2202,12 +2197,7 @@ void Test(int[] a, int[] b) // case ([42], [43]): Diagnostic(ErrorCode.ERR_SwitchCaseSubsumed, "([42], [43])").WithLocation(9, 18)); - var tree = comp.SyntaxTrees.First(); - var @switch = tree.GetRoot().DescendantNodes().OfType().Single(); - var model = (CSharpSemanticModel)comp.GetSemanticModel(tree); - var binder = model.GetEnclosingBinder(@switch.SpanStart); - var boundSwitch = (BoundSwitchStatement)binder.BindStatement(@switch, BindingDiagnosticBag.Discarded); - AssertEx.Equal( + VerifyDecisionDagDump(comp, @"[0]: t1 = t0.a; [1] [1]: t1 != null ? [2] : [22] [2]: t2 = t1.Length; [3] @@ -2236,7 +2226,7 @@ void Test(int[] a, int[] b) case ([42], [43]): break; }` -", boundSwitch.DecisionDag.Dump()); +"); } [Fact] @@ -2295,12 +2285,7 @@ void Test(int[] a) Diagnostic(ErrorCode.ERR_SwitchCaseSubsumed, "[1, 2, 3]").WithLocation(9, 18) ); - var tree = comp.SyntaxTrees.First(); - var @switch = tree.GetRoot().DescendantNodes().OfType().Single(); - var model = (CSharpSemanticModel)comp.GetSemanticModel(tree); - var binder = model.GetEnclosingBinder(@switch.SpanStart); - var boundSwitch = (BoundSwitchStatement)binder.BindStatement(@switch, BindingDiagnosticBag.Discarded); - AssertEx.Equal( + VerifyDecisionDagDump(comp, @"[0]: t0 != null ? [1] : [13] [1]: t1 = t0.Length; [2] [2]: t1 >= 2 ? [3] : [13] @@ -2320,7 +2305,7 @@ void Test(int[] a) case [1, 2, 3]: break; }` -", boundSwitch.DecisionDag.Dump()); +"); } [Fact] @@ -2355,12 +2340,7 @@ static void Main() comp.VerifyEmitDiagnostics(); CompileAndVerify(comp, expectedOutput: expectedOutput); - var tree = comp.SyntaxTrees.First(); - var @switch = tree.GetRoot().DescendantNodes().OfType().Single(); - var model = (CSharpSemanticModel)comp.GetSemanticModel(tree); - var binder = model.GetEnclosingBinder(@switch.SpanStart); - var boundSwitch = (BoundSwitchStatement)binder.BindStatement(@switch, BindingDiagnosticBag.Discarded); - AssertEx.Equal( + VerifyDecisionDagDump(comp, @"[0]: t0 != null ? [1] : [18] [1]: t1 = t0.Length; [2] [2]: t1 == 3 ? [3] : [12] @@ -2380,7 +2360,7 @@ static void Main() [16]: t5 == 3 ? [17] : [18] [17]: leaf `case [1, .., 3]:` [18]: leaf `default` -", boundSwitch.DecisionDag.Dump()); +"); } [Fact] @@ -2415,12 +2395,7 @@ static void Main() comp.VerifyEmitDiagnostics(); CompileAndVerify(comp, expectedOutput: expectedOutput); - var tree = comp.SyntaxTrees.First(); - var @switch = tree.GetRoot().DescendantNodes().OfType().Single(); - var model = (CSharpSemanticModel)comp.GetSemanticModel(tree); - var binder = model.GetEnclosingBinder(@switch.SpanStart); - var boundSwitch = (BoundSwitchStatement)binder.BindStatement(@switch, BindingDiagnosticBag.Discarded); - AssertEx.Equal( + VerifyDecisionDagDump(comp, @"[0]: t0 != null ? [1] : [12] [1]: t1 = t0.Length; [2] [2]: t1 == 1 ? [3] : [8] @@ -2434,7 +2409,7 @@ static void Main() [10]: t3 == 42 ? [11] : [12] [11]: leaf `case [..,42]:` [12]: leaf `default` -", boundSwitch.DecisionDag.Dump()); +"); } [Fact] @@ -2494,12 +2469,7 @@ void Test(object[] a) // case [var unreachable]: Diagnostic(ErrorCode.ERR_SwitchCaseSubsumed, "[var unreachable]").WithLocation(17, 18)); - var tree = comp.SyntaxTrees.First(); - var @switch = tree.GetRoot().DescendantNodes().OfType().First(); - var model = (CSharpSemanticModel)comp.GetSemanticModel(tree); - var binder = model.GetEnclosingBinder(@switch.SpanStart); - var boundSwitch = (BoundSwitchStatement)binder.BindStatement(@switch, BindingDiagnosticBag.Discarded); - AssertEx.Equal( + VerifyDecisionDagDump(comp, @"[0]: t0 != null ? [1] : [10] [1]: t1 = t0.Length; [2] [2]: t1 >= 1 ? [3] : [10] @@ -2518,7 +2488,7 @@ void Test(object[] a) break; }` [11]: leaf `case [.., not null]:` -", boundSwitch.DecisionDag.Dump()); +"); } [Fact] @@ -2549,12 +2519,7 @@ public static void Test(int[] a) comp.VerifyEmitDiagnostics(); CompileAndVerify(comp, expectedOutput: "2"); - var tree = comp.SyntaxTrees.First(); - var @switch = tree.GetRoot().DescendantNodes().OfType().Single(); - var model = (CSharpSemanticModel)comp.GetSemanticModel(tree); - var binder = model.GetEnclosingBinder(@switch.SpanStart); - var boundSwitch = (BoundSwitchStatement)binder.BindStatement(@switch, BindingDiagnosticBag.Discarded); - AssertEx.Equal( + VerifyDecisionDagDump(comp, @"[0]: t0 != null ? [1] : [11] [1]: t1 = t0.Length; [2] [2]: t1 >= 2 ? [3] : [11] @@ -2567,7 +2532,7 @@ public static void Test(int[] a) [9]: t3 <= 0 ? [10] : [11] [10]: leaf `case [.., <= 0, _]:` [11]: leaf `default` -", boundSwitch.DecisionDag.Dump()); +"); } [Fact] @@ -2672,12 +2637,8 @@ void Test(int[] a) // [_] => 2, // unreachable Diagnostic(ErrorCode.ERR_SwitchArmSubsumed, "[_]").WithLocation(11, 13)); - var tree = comp.SyntaxTrees.First(); - var @switch = tree.GetRoot().DescendantNodes().OfType().First(); - var model = (CSharpSemanticModel)comp.GetSemanticModel(tree); - var binder = model.GetEnclosingBinder(@switch.SpanStart); - var boundSwitch = (BoundSwitchExpression)binder.BindExpression(@switch, BindingDiagnosticBag.Discarded); - AssertEx.Equal( + VerifyDecisionDagDump(comp, + @"[0]: t0 != null ? [1] : [13] [1]: t1 = t0.Length; [2] [2]: t1 == 1 ? [3] : [12] @@ -2698,7 +2659,7 @@ void Test(int[] a) [..[>= 0]] or [..null] => 1, [_] => 2, // unreachable }` -", boundSwitch.DecisionDag.Dump()); +"); } [Fact] @@ -2725,12 +2686,7 @@ void Test(int[] a) // [var unreachable] => 5, Diagnostic(ErrorCode.ERR_SwitchArmSubsumed, "[var unreachable]").WithLocation(12, 13)); - var tree = comp.SyntaxTrees.First(); - var @switch = tree.GetRoot().DescendantNodes().OfType().Single(); - var model = (CSharpSemanticModel)comp.GetSemanticModel(tree); - var binder = model.GetEnclosingBinder(@switch.SpanStart); - var boundSwitch = (BoundSwitchExpression)binder.BindExpression(@switch, BindingDiagnosticBag.Discarded); - AssertEx.Equal( + VerifyDecisionDagDump(comp, @"[0]: t0 != null ? [1] : [15] [1]: t1 = t0.Length; [2] [2]: t1 >= 1 ? [3] : [14] @@ -2754,7 +2710,7 @@ void Test(int[] a) { Length: not 1 } => 4, [var unreachable] => 5, }` -", boundSwitch.DecisionDag.Dump()); +"); } [Fact] @@ -2807,12 +2763,7 @@ void Test(int[][] a) // case [[42]]: Diagnostic(ErrorCode.ERR_SwitchCaseSubsumed, "[[42]]").WithLocation(9, 18)); - var tree = comp.SyntaxTrees.First(); - var @switch = tree.GetRoot().DescendantNodes().OfType().Single(); - var model = (CSharpSemanticModel)comp.GetSemanticModel(tree); - var binder = model.GetEnclosingBinder(@switch.SpanStart); - var boundSwitch = (BoundSwitchStatement)binder.BindStatement(@switch, BindingDiagnosticBag.Discarded); - AssertEx.Equal( + VerifyDecisionDagDump(comp, @"[0]: t0 != null ? [1] : [27] [1]: t1 = t0.Length; [2] [2]: t1 >= 1 ? [3] : [27] @@ -2846,7 +2797,7 @@ void Test(int[][] a) case [[42]]: break; }` -", boundSwitch.DecisionDag.Dump()); +"); } [Fact]