-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Conversation
I am wondering if this is potentially a breaking change. The runtimes don't ensure that the struct is read-only but C# compiler assumes that. Therefore in a scenario where read-only struct is used in pinvoke and actually modified are we sure the side effects are always visible? |
|
I don't right now but possibly something like this where you pass in readonly version instead of non-readonly version and runtime decides to optimize/flow it differently.
|
@jaredpar or @VSadov could likely answer better, but from my perspective, in your example you can already do that with readonly fields (just remove the 'readonly' from the struct itself), and it's not really any different from that. Interop is inherently unsafe and allows you to break out of whatever language/runtime enforcement exists. |
@dotnet-bot test Linux x64 Release Build please (#25004) |
Also, you can change readonly fields via reflection today. Runtime is not enforcing readonliness for reflection access either. C# readonly is pretty similar to C++ const in this sense. There are number of ways how one can step around it. |
6fc76f3
to
42cb818
Compare
Making
If |
Thanks for calling this out, @VladimirReshetnikov. It's not so much making |
Fixes #24900
Related to dotnet/coreclr#14789
Related to dotnet/corert#4855
cc: @jkotas, @jaredpar, @VSadov, @tmat for System.Reflection.Metadata (which has the bulk of the changes)