From a9369c5d35cea41c8eb50f682dcd665432de53c3 Mon Sep 17 00:00:00 2001 From: Bruce Payette Date: Wed, 11 Jul 2018 13:52:20 -0700 Subject: [PATCH] Fix for #7267 - an erroneous parse error is generated when break is used in a switch statment in a finally block --- .../engine/parser/SemanticChecks.cs | 4 ++-- test/powershell/Language/Parser/Parser.Tests.ps1 | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/parser/SemanticChecks.cs b/src/System.Management.Automation/engine/parser/SemanticChecks.cs index 0e59989d16b..8a12d3173a7 100644 --- a/src/System.Management.Automation/engine/parser/SemanticChecks.cs +++ b/src/System.Management.Automation/engine/parser/SemanticChecks.cs @@ -619,9 +619,9 @@ private void CheckForFlowOutOfFinally(Ast ast, string label) // If label is not null, we have a break/continue where we know the loop label at compile // time. If we can match the label before finding the finally, then we're not flowing out // of the finally. - if (label != null && parent is LoopStatementAst) + if (label != null && parent is LabeledStatementAst) { - if (LoopFlowException.MatchLoopLabel(label, ((LoopStatementAst)parent).Label ?? string.Empty)) + if (LoopFlowException.MatchLoopLabel(label, ((LabeledStatementAst)parent).Label ?? string.Empty)) break; } diff --git a/test/powershell/Language/Parser/Parser.Tests.ps1 b/test/powershell/Language/Parser/Parser.Tests.ps1 index 87db9dbfe56..a8ff8b9e86b 100644 --- a/test/powershell/Language/Parser/Parser.Tests.ps1 +++ b/test/powershell/Language/Parser/Parser.Tests.ps1 @@ -457,6 +457,14 @@ foo``u{2195}abc $result | Should -Be "Finally", "System.ArgumentException" } + It "Test that a break statement in a finally block results in a ParseException" { + { ExecuteCommand 'try {} finally { break }' } | Should -Throw -ErrorId ParseException + } + + It "Test that a switch statement with a break in a finally doesn't trigger a parse error" { + ExecuteCommand 'try {"success"} finally {switch (1) {foo {break}}}' | Should -BeExactly 'success' + } + It "Test that null can be passed to a method that expects a reference type. (line 1439)" { $result = ExecuteCommand '$test = "String";$test.CompareTo($())' $result | Should -Be 1