From 13e925e13e18c3ecae80f1588e2e97fb3f28392e Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 26 Jun 2018 12:24:11 -0700 Subject: [PATCH] Revert "Make switch statement report correct error position when it fails to evaluate the condition" (#7182) The fix causes a problem in debugging. When stepping over the scripts in debugging mode, the debugger will stop at the switch statement condition expression twice at the very beginning -- one for evaluating the condition, and one for iterating the condition. --- .../engine/parser/Compiler.cs | 5 +- .../Scripting/ErrorPosition.Tests.ps1 | 51 ------------------- 2 files changed, 4 insertions(+), 52 deletions(-) delete mode 100644 test/powershell/Language/Scripting/ErrorPosition.Tests.ps1 diff --git a/src/System.Management.Automation/engine/parser/Compiler.cs b/src/System.Management.Automation/engine/parser/Compiler.cs index 3d8ec44487..3426d326a9 100644 --- a/src/System.Management.Automation/engine/parser/Compiler.cs +++ b/src/System.Management.Automation/engine/parser/Compiler.cs @@ -4189,7 +4189,10 @@ namespace System.Management.Automation.Language // $foreach/$switch = GetEnumerator $enumerable var enumerable = NewTemp(typeof(object), "enumerable"); temps.Add(enumerable); - exprs.Add(UpdatePosition(stmt.Condition)); + if (generatingForeach) + { + exprs.Add(UpdatePosition(stmt.Condition)); + } exprs.Add( Expression.Assign(enumerable, GetRangeEnumerator(stmt.Condition.GetPureExpression()) diff --git a/test/powershell/Language/Scripting/ErrorPosition.Tests.ps1 b/test/powershell/Language/Scripting/ErrorPosition.Tests.ps1 deleted file mode 100644 index 4012e7bd0f..0000000000 --- a/test/powershell/Language/Scripting/ErrorPosition.Tests.ps1 +++ /dev/null @@ -1,51 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. - -Describe "Error position Tests" -Tags "CI" { - It "switch condition evaluation failure should report correct error position" { - $testFile = Join-Path $TestDrive "SwitchError1.ps1" - Set-Content -Path $testFile -Encoding Ascii -Value @' -$test = 1 -switch ($null[0]) { - "a" {}; -} -'@ - try { & $testFile } catch { $errorRecord = $_ } - $errorRecord | Should -Not -BeNullOrEmpty - $errorRecord.ScriptStackTrace | Should -Match "SwitchError1.ps1: line 2" - } - - It "switch condition MoveNext failure should report correct error position" { - $code = @' -using System; -using System.Collections.Generic; -namespace SwitchTest -{ - public class Test - { - public static IEnumerable GetName() - { - yield return "Hello world"; - throw new ArgumentException(); - } - } -} -'@ - $testFile = Join-Path $TestDrive "SwitchError2.ps1" - Set-Content -Path $testFile -Encoding Ascii -Value @' -$test = 1 -$enumerable = [SwitchTest.Test]::GetName() -switch ($enumerable) { - "hello world" { $test = 1; $_ } - "Yay" { $test = 2; $_ } -} -'@ - if (-not ("SwitchTest.Test" -as [type])) { - Add-Type -TypeDefinition $code - } - - try { & $testFile > $null } catch { $errorRecord = $_ } - $errorRecord | Should -Not -BeNullOrEmpty - $errorRecord.ScriptStackTrace | Should -Match "SwitchError2.ps1: line 3" - } -}