Make switch statement report correct error position when it fails to evaluate the condition#7151
Conversation
| } | ||
|
|
||
| It "swtich condition MoveNext failure should report correct error position" { | ||
| $code = @' |
| Set-Content -Path $testFile -Encoding Ascii -Value @' | ||
| $test = 1 | ||
| switch ($nullVar[0]) { | ||
| "a" {}; |
There was a problem hiding this comment.
could use $null here switch ($null[0]) {
| { | ||
| exprs.Add(UpdatePosition(stmt.Condition)); | ||
| } | ||
| exprs.Add(UpdatePosition(stmt.Condition)); |
There was a problem hiding this comment.
@daxian-dbw what was the bug here btw? (As in why does this change fix it?)
There was a problem hiding this comment.
We generate code to update position in the script that is executing at various places, so when an error happens, the error can be associated with a position in the script. Here we didn't update the position for the condition when evaluating the condition from a switch statement.
There was a problem hiding this comment.
Interesting that the previous code went out of its way to only work for foreach loops
There was a problem hiding this comment.
Yes, I wonder why it was so? If we didn't have tests for that previously didn't we break something now?
There was a problem hiding this comment.
Yeah, I did break something 😄
When stepping over the script in debugging, now the debugger stops on switch ($b) twice. I will revert this PR.
There was a problem hiding this comment.
Don't add me to CI list 😄
| $errorRecord.ScriptStackTrace | Should -Match "SwitchError1.ps1: line 2" | ||
| } | ||
|
|
||
| It "swtich condition MoveNext failure should report correct error position" { |
rjmholt
left a comment
There was a problem hiding this comment.
@BrucePay and @SteveL-MSFT seem to have addressed all concerns 👍
| @@ -0,0 +1,51 @@ | |||
| # Copyright (c) Microsoft Corporation. All rights reserved. | |||
| # Licensed under the MIT License. | |||
|
|
|||
There was a problem hiding this comment.
Why we need new file?
It seems we can use existing.
Update: I see we have tests only for parser. Closest to this https://github.com/PowerShell/PowerShell/blob/master/test/powershell/Language/Parser/Parser.Tests.ps1#L705
There was a problem hiding this comment.
I used a new file because I couldn't find an existing test file that this category of issues can fall in.
Here we are not testing indexing into a null array, but the scenario where it fails to evaluate the switch statement's condition (it could fail for a different reason, and I just happen to use $null[0]).
As for the test you referenced, it looks more fit in Language/Scripting instead of Language/Parser because the error that's tested there is a runtime error instead of a parsing one.
There was a problem hiding this comment.
Yes, I don't found a file with runtime error tests too. And I wonder. If we need better code coverage perhaps we should rename the file from ErrorPosition to RuntimeErrors.
There was a problem hiding this comment.
Runtime errors usually are tested along with the feature that they are associated with, no need for a separate test file only for validating runtime errors.
Error position is a feature used for reporting errors, so I think it's better to have a test file of its own category.
PR Summary
Fix #7150
Make switch statement report correct error position when it fails to evaluate the condition
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.[feature]if the change is significant or affects feature tests