Title: Improve mutable struct detection · Issue #12430 · PowerShell/PowerShell · GitHub
Open Graph Title: Improve mutable struct detection · Issue #12430 · PowerShell/PowerShell
X Title: Improve mutable struct detection · Issue #12430 · PowerShell/PowerShell
Description: Summary of the new feature/enhancement When assigning a struct to a variable, some checks are done to determine if the struct is mutable. The current code path checks if there are: Any public instance fields Any public instance propertie...
Open Graph Description: Summary of the new feature/enhancement When assigning a struct to a variable, some checks are done to determine if the struct is mutable. The current code path checks if there are: Any public insta...
X Description: Summary of the new feature/enhancement When assigning a struct to a variable, some checks are done to determine if the struct is mutable. The current code path checks if there are: Any public insta...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/12430
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Improve mutable struct detection","articleBody":"# Summary of the new feature/enhancement\r\n\r\nWhen assigning a struct to a variable, some checks are done to determine if the struct is mutable. The current code path checks if there are:\r\n\r\n1. Any public instance fields\r\n1. Any public instance properties with a set accessor\r\n1. Any public instance methods\r\n\r\nThis can be simplified by checking if the struct is decorated with the `IsReadOnlyAttribute`. This would allow PS to assume any struct that wasn't declared explicitly as read only with the C# `readonly` modifier ([SharpLab example](https://sharplab.io/#v2:D4AQzABATgpghgEwPYDsA2BPCBnALlAVwGNcIBZDAZX2NIG8BfIA)) to be mutable. This would also probably result in *less* false positives because most structs have a public field (even if `initonly`) or a public method, even these explicitly `readonly` structs.\r\n\r\n# Proposed technical implementation details (optional)\r\n\r\nThis method:\r\n\r\nhttps://github.com/PowerShell/PowerShell/blob/947bddfe0474429672745b939d0ec511392e5879/src/System.Management.Automation/engine/runtime/Binding/Binders.cs#L2030-L2063\r\n\r\nShould be changed like this:\r\n\r\n```diff\r\ndiff --git a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs\r\nindex b54fbdfd5..7a0254ccb 100644\r\n--- a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs\r\n+++ b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs\r\n@@ -2034,32 +2034,7 @@ namespace System.Management.Automation.Language\r\n return false;\r\n }\r\n \r\n- // If there are any fields, the type is mutable.\r\n- if (type.GetFields(BindingFlags.Public | BindingFlags.Instance).Any())\r\n- {\r\n- return true;\r\n- }\r\n-\r\n- // If there are any properties with setters, the type is mutable.\r\n- var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);\r\n- for (int index = 0; index \u003c properties.Length; index++)\r\n- {\r\n- var property = properties[index];\r\n- if (property.CanWrite)\r\n- {\r\n- return true;\r\n- }\r\n- }\r\n-\r\n- // If there are any methods other than the property getters, the type might\r\n- // be mutable, so assume the type is mutable.\r\n- var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);\r\n- if (methods.Length != properties.Length)\r\n- {\r\n- return true;\r\n- }\r\n-\r\n- return false;\r\n+ return !type.IsDefined\u003cSystem.Runtime.CompilerServices.IsReadOnlyAttribute\u003e();\r\n }\r\n \r\n private static readonly ConcurrentDictionary\u003cType, bool\u003e s_mutableValueTypesWithInstanceMembers =\r\n```\r\n\r\nRelated issue #12411\r\n\r\n/cc @mklement0 ","author":{"url":"https://github.com/SeeminglyScience","@type":"Person","name":"SeeminglyScience"},"datePublished":"2020-04-22T18:22:29.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":19},"url":"https://github.com/12430/PowerShell/issues/12430"}
| 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:bb5a96ba-a185-6e65-d404-e5d9476bcf3f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B3F4:B267E:113DF8D:179422F:6A520F71 |
| html-safe-nonce | daaab152b8444901a2fbab215b99b5ce00c9eca0081997bb8186648b22d44608 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCM0Y0OkIyNjdFOjExM0RGOEQ6MTc5NDIyRjo2QTUyMEY3MSIsInZpc2l0b3JfaWQiOiIzNTc1ODUwMDc4NjIyMDYwNDAxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | cfaa29a4875577d521bdb5a8f4893510e0b42b0bf66e5e32050fa3db1859c9c3 |
| hovercard-subject-tag | issue:604966395 |
| 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/12430/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fe1e00240bb1782622bdef3390b6169aa05812b64e417c60e5ef372e035691dd/PowerShell/PowerShell/issues/12430 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fe1e00240bb1782622bdef3390b6169aa05812b64e417c60e5ef372e035691dd/PowerShell/PowerShell/issues/12430 |
| og:image:alt | Summary of the new feature/enhancement When assigning a struct to a variable, some checks are done to determine if the struct is mutable. The current code path checks if there are: Any public insta... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | SeeminglyScience |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| 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 | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width