Title: Unexpected parsing errors with array literals as array-index expressions in method calls · Issue #7004 · PowerShell/PowerShell · GitHub
Open Graph Title: Unexpected parsing errors with array literals as array-index expressions in method calls · Issue #7004 · PowerShell/PowerShell
X Title: Unexpected parsing errors with array literals as array-index expressions in method calls · Issue #7004 · PowerShell/PowerShell
Description: 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]) ...
Open Graph Description: 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 reprodu...
X Description: 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 rep...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/7004
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Unexpected parsing errors with array literals as array-index expressions in method calls","articleBody":"Using a literal array of indices as an index expression breaks in method calls.\r\n\r\nIt looks like the `,` that's part of the array literal is misconstrued as a method-argument separator.\r\n\r\nSteps to reproduce\r\n------------------\r\n\r\n```powershell\r\n[string]::join(' ', (0, 1, 2)[0, 1])\r\n```\r\n\r\nExpected behavior\r\n-----------------\r\n\r\n```none\r\n0 1\r\n```\r\n\r\nActual behavior\r\n---------------\r\n\r\n```none\r\nAt line:1 char:32\r\n+ [string]::join(' ', (0, 1, 2)[0, 1])\r\n+ ~\r\nMissing ']' after array index expression.\r\nAt line:1 char:35\r\n+ [string]::join(' ', (0, 1, 2)[0, 1])\r\n+ ~\r\nMissing ')' in method call.\r\nAt line:1 char:35\r\n+ [string]::join(' ', (0, 1, 2)[0, 1])\r\n+ ~\r\nUnexpected token ']' in expression or statement.\r\nAt line:1 char:36\r\n+ [string]::join(' ', (0, 1, 2)[0, 1])\r\n+ ~\r\nUnexpected token ')' in expression or statement.\r\n+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException\r\n+ FullyQualifiedErrorId : MissingEndSquareBracket\r\n```\r\n\r\nTo make the command work, you currently have to enclose either the entire expression in `(...)`:\r\n\r\n```powershell\r\n[string]::join(' ', ((0, 1, 2)[0, 1])) # OK\r\n```\r\nor just the array inside `[...]`:\r\n\r\n```powershell\r\n[string]::join(' ', (0, 1, 2)[(0, 1)]) # OK\r\n```\r\n\r\nor use a variable:\r\n\r\n```powershell\r\n$a = 0, 1; [string]::join(' ', (0, 1, 2)[$a]) # OK\r\n```\r\n\r\nAlso, a _single_ index works as expected:\r\n\r\n```powershell\r\n[string]::join(' ', (0, 1, 2)[0]) # OK\r\n```\r\n\r\nUsing the `-join` _operator_ makes the problem go away:\r\n\r\n```powershell\r\n(0, 1, 2)[0, 1] -join ' ' # OK\r\n```\r\n\r\nIn 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.\r\n\r\nEnvironment data\r\n----------------\r\n\r\n```powershell\r\nPowerShell Core v6.1.0-preview.2 on macOS 10.13.4\r\nPowerShell Core v6.1.0-preview.2 on Ubuntu 16.04.4 LTS\r\nPowerShell Core v6.1.0-preview.2 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)\r\nWindows PowerShell v5.1.17134.48 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)\r\n```\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2018-06-05T18:52:19.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/7004/PowerShell/issues/7004"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c20e1e2a-35b5-3fb6-574e-789b12022a89 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C74A:27C161:14F5BB1:1E65305:6A54CD81 |
| html-safe-nonce | 00dd5f6418c0ddd20796b5aa10c308d9c6869c20db5720c146be38c2825a1b7f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNzRBOjI3QzE2MToxNEY1QkIxOjFFNjUzMDU6NkE1NENEODEiLCJ2aXNpdG9yX2lkIjoiMjY0NDU2OTg2MzEwOTI2NjgxNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | fe5629760f4bae576bca1b41d644441cafae64a09fd76c50dcf3503724a8c137 |
| hovercard-subject-tag | issue:329581669 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/7004/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6ded8720f0727ca9a983f422a0f6eb72127a7c15e0c3c4fd2d4b115bfaf416e3/PowerShell/PowerShell/issues/7004 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6ded8720f0727ca9a983f422a0f6eb72127a7c15e0c3c4fd2d4b115bfaf416e3/PowerShell/PowerShell/issues/7004 |
| og:image:alt | 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 reprodu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mklement0 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 6e5f2e6ff3f430aa60a674066f957bca9f2106017e1b68126417422b58ce6e1e |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | be4d48619391052c9d312c7b587bb2ac38a97f57 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width