Skip to content

Unexpected parsing errors with array literals as array-index expressions in method calls #7004

@mklement0

Description

@mklement0

Using a literal array of indices as an index expression breaks in method calls.

It looks like the , that's part of the array literal is misconstrued as a method-argument separator.

Steps to reproduce

[string]::join(' ', (0, 1, 2)[0, 1])

Expected behavior

0 1

Actual behavior

At line:1 char:32
+ [string]::join(' ', (0, 1, 2)[0, 1])
+                                ~
Missing ']' after array index expression.
At line:1 char:35
+ [string]::join(' ', (0, 1, 2)[0, 1])
+                                   ~
Missing ')' in method call.
At line:1 char:35
+ [string]::join(' ', (0, 1, 2)[0, 1])
+                                   ~
Unexpected token ']' in expression or statement.
At line:1 char:36
+ [string]::join(' ', (0, 1, 2)[0, 1])
+                                    ~
Unexpected token ')' in expression or statement.
+ CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndSquareBracket

To make the command work, you currently have to enclose either the entire expression in (...):

[string]::join(' ', ((0, 1, 2)[0, 1]))  # OK

or just the array inside [...]:

[string]::join(' ', (0, 1, 2)[(0, 1)]) # OK

or use a variable:

$a = 0, 1; [string]::join(' ', (0, 1, 2)[$a]) # OK

Also, a single index works as expected:

[string]::join(' ', (0, 1, 2)[0])  # OK

Using the -join operator makes the problem go away:

(0, 1, 2)[0, 1] -join ' '  # OK

In short: avoiding a , in the index expression is needed to make the problem go away; such a , appears to be mistaken for a method-argument separator.

Environment data

PowerShell Core v6.1.0-preview.2 on macOS 10.13.4
PowerShell Core v6.1.0-preview.2 on Ubuntu 16.04.4 LTS
PowerShell Core v6.1.0-preview.2 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)
Windows PowerShell v5.1.17134.48 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions