-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Make switch statement report correct error position when it fails to evaluate the condition #7151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| } | ||
|
|
||
| It "swtich condition MoveNext failure should report correct error position" { | ||
| $code = @' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo swtich -> switch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
| Set-Content -Path $testFile -Encoding Ascii -Value @' | ||
| $test = 1 | ||
| switch ($nullVar[0]) { | ||
| "a" {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could use $null here switch ($null[0]) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed. Thanks
| { | ||
| exprs.Add(UpdatePosition(stmt.Condition)); | ||
| } | ||
| exprs.Add(UpdatePosition(stmt.Condition)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So simple 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@daxian-dbw what was the bug here btw? (As in why does this change fix it?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting that the previous code went out of its way to only work for foreach loops
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: swtich
rjmholt
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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