Skip to content

switch statement report incorrect error position when it fails to evaluate the condition #7150

Description

Steps to reproduce

PS> cat .\about.ps1
$Test = 1;
switch ($NeverUsedBefore[0]) {
    "a" {};
}

Expected behavior

Run about.ps1 reports the correct script location.

PS> .\about.ps1
Cannot index into a null array.
At F:\tmp\about.ps1:2 char:9
+ switch ($NeverUsedBefore[0]) {
+         ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray

Actual behavior

Run about.ps1 reports the wrong script location.

PS> .\about.ps1
Cannot index into a null array.
At F:\tmp\about.ps1:1 char:1
+ $Test = 1;
+ ~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.1
PSEdition                      Core
GitCommitId                    v6.0.1
OS                             Microsoft Windows 10.0.17134
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. added
    Issue-Bug
    WG-Language
    on Jun 23, 2018
  2. iricigor commented on Jun 23, 2018

    @iricigor

    Another example, but with for statement

    $test = 1
    for ($null[0]; $i -lt 5; $i++) {$i}

    It reports an error on line 1. I think wrong error location is reported for the same reason as switch statement.

    Cannot index into a null array.
    At D:\badFor.ps1:1 char:1
    + $test = 1
    + ~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : NullArray
    
  3. daxian-dbw commented on Jun 26, 2018

    @daxian-dbw
    MemberAuthor

    Igor (@iricigor) Thanks for the report. I didn't see your comment when merging the PR. Will submit another PR to address the issue with for loop.

  4. daxian-dbw commented on Jun 26, 2018

    @daxian-dbw
    MemberAuthor

    Reverted the original fix in #7151. The fix caused 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.

    Here is an example:

    PS:4> .\about.ps1
    Entering debug mode. Use h or ? for help.
    
    Hit Line breakpoint on 'F:\tmp\about.ps1:1'
    
    At F:\tmp\about.ps1:1 char:1
    + $Test = 1;
    + ~~~~~~~~~
    PS:4>> v
    At F:\tmp\about.ps1:2 char:1
    + $b = 1..10
    + ~~~~~~~~~~
    PS:4>> v
    At F:\tmp\about.ps1:3 char:9
    + switch ($b) {
    +         ~~
    PS:4>> v
    At F:\tmp\about.ps1:3 char:9
    + switch ($b) {
    +         ~~
    PS:4>> v
    At F:\tmp\about.ps1:4 char:5
    +     "a" {};
    +     ~~~
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-BugResolution-FixedWG-Language

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions