Title: `switch` statement unexpectedly treats barewords as expandable strings · Issue #3668 · PowerShell/PowerShell · GitHub
Open Graph Title: `switch` statement unexpectedly treats barewords as expandable strings · Issue #3668 · PowerShell/PowerShell
X Title: `switch` statement unexpectedly treats barewords as expandable strings · Issue #3668 · PowerShell/PowerShell
Description: Note: It may be too late to change this behavior, or it may fall into Bucket 3: Unlikely Grey Area. If the former, the pitfall discussed is at least worth documenting. about_Switch states about the syntax of individual switch branches: "...
Open Graph Description: Note: It may be too late to change this behavior, or it may fall into Bucket 3: Unlikely Grey Area. If the former, the pitfall discussed is at least worth documenting. about_Switch states about the...
X Description: Note: It may be too late to change this behavior, or it may fall into Bucket 3: Unlikely Grey Area. If the former, the pitfall discussed is at least worth documenting. about_Switch states about the...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/3668
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`switch` statement unexpectedly treats barewords as expandable strings","articleBody":"Note: It may be too late to change this behavior, or it may fall into [Bucket 3: Unlikely Grey Area](https://github.com/PowerShell/PowerShell/blob/master/docs/dev-process/breaking-change-contract.md#bucket-3-unlikely-grey-area). \r\nIf the former, the pitfall discussed is at least worth documenting.\r\n\r\n[about_Switch](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Switch) states about the syntax of individual `switch` branches:\r\n\r\n> \"string\"|number|variable|{ expression } { statementlist } \r\n\r\nThe above suggests that an expression can only be used _inside script blocks_, but in practice you can also use `(...)`- and `$(...)`-enclosed expressions directly (e.g., `(1 + 2)`) and even method calls (e.g., `$PSVersionTable.PSVersion.ToString()`).\r\n\r\nSurprisingly, however, a non-numeric unquoted token that doesn't start with `(` or `$` (and doesn't break parsing) is unexpectedly treated like an _(expandable) string_ rather than as an expression:\r\n\r\nSteps to reproduce\r\n------------------\r\n\r\n```powershell\r\n0 -eq [uint32]::MinValue\r\n\r\nswitch (0) {\r\n ([uint32]::MinValue) { $true } # OK - (...) ensures use as expression\r\n default { $false }\r\n}\r\n\r\nswitch (0) {\r\n [uint32]::MinValue { $true } # !! condition treated as *string*\r\n default { $false }\r\n}\r\n\r\nswitch ('[uint32]::MinValue') {\r\n [uint32]::MinValue { $true }\r\n default { $false }\r\n}\r\n```\r\n\r\n\r\nExpected behavior\r\n-----------------\r\n\r\n```\r\nTrue\r\nTrue\r\nTrue\r\nFalse\r\n```\r\n\r\n\r\nActual behavior\r\n---------------\r\n\r\n```\r\nTrue\r\nTrue\r\nFalse\r\nTrue\r\n```\r\n\r\nThat is, unquoted `[uint32]::MinValue` is interpreted the same way as `'[uint32]::MinValue'` in this case.\r\n\r\nThis is unexpected, given that it is reasonable to conceive of the branch conditions of a `switch` statement as the RHS of an _expression_ along the lines of `\u003cswitch-value> -eq \u003cbranch-condition>`, and given that any unquoted token on the RHS of `-eq` is interpreted as an _expression_.\r\n\r\n\r\nEnvironment data\r\n----------------\r\n\r\n```powershell\r\nPowerShell Core v6.0.0-alpha (v6.0.0-alpha.18) on Darwin Kernel Version 16.5.0: Fri Mar 3 16:52:33 PST 2017; root:xnu-3789.51.2~3/RELEASE_X86_64\r\n```\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2017-05-01T02:51:08Z","url":"https://github.com/PowerShell/PowerShell/issues/3668"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:199c7448-0ae2-857f-bf2a-6d2516fc5d58 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8A92:663DE:14DC42:1D7D73:6AFE0F3E |
| html-safe-nonce | f7442a23357741d8f9751931cb2460b0844cebfbd7917b5054aac4b2e9776e52 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QTkyOjY2M0RFOjE0REM0MjoxRDdENzM6NkFGRTBGM0UiLCJ2aXNpdG9yX2lkIjoiODYzNDI5NjM3MDM5NzU4MTExOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3fc817077db9c18a632838a999aebb0d19b4e7b1131ac960d61d9fe16b319ee7 |
| hovercard-subject-tag | issue:225376243 |
| 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/3668/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5b3dc24b9cf290aff7512222f3eca47cf7b7af70a79b875e9f2f4362b2723fce/PowerShell/PowerShell/issues/3668 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5b3dc24b9cf290aff7512222f3eca47cf7b7af70a79b875e9f2f4362b2723fce/PowerShell/PowerShell/issues/3668 |
| og:image:alt | Note: It may be too late to change this behavior, or it may fall into Bucket 3: Unlikely Grey Area. If the former, the pitfall discussed is at least worth documenting. about_Switch states about the... |
| 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 | 1fd4053be37b4bd545b4f38f1cecad058c343776acf2cbc6806316f26270e55e |
| 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 | 635a57b3d0df6bc21c0f39fc7078888fb90e4066 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width