Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Disallow "mutable" on byref<'T> locals #677
Disallow "mutable" on byref<'T> locals
Currently, we can write code like this:
let mutable x = 1 let mutable y = &x y <- 4
For devs to avoid this confusion, we should never allow
let mutable x = 1 let y = &x y <- 4
In the future we may or may not to allow assigning byrefs, so it might look like this:
let mutable x = 1 let mutable w = 1 let mutable y = &x y <- &w
But that is not part of this suggestion.
Better consistency. Less confusion.
This is technically a breaking change, but we think most people do not make this
Affidavit (please submit!)
Please tick this by placing a cross in the box:
Please tick all that apply:
But the problem here is that
It seems this is a continuation of F# optimalizations - earlier there was an optimization for laddrof (&&): Microsoft/visualfsharp#5148 In fact this is explicitly noted in the commit:
"2. In optimization, &x (i.e. LAddrOf) is a constant value even if x is mutable and so can be propagated (so for let y = &x in ... y ... the occurrences of y get replaced by &x and y gets removed)"
My concern is as usual native memory & atomic operations with F#. Assigning byrefs (aliases) have some legitimate usage there. What about byrefs that points to unmanaged memory? This is why the NativePtr.toByRef "invented" Microsoft/visualfsharp#409 (comment)
From one of my related issue comment (dotnet/coreclr#916 (comment)):
"Unique advantage of managed pointers (ref in C#) is that they can point to unmanaged memory, and be used in many of the same ways as unmanaged pointers. For example, check this code fragment:
IntPtr p = ... Interlocked.CompareExchange(ref *(Int32*)p, 1, 0);
Thus the existing volatile and atomic operations defined on managed pointers should work fine for your scenario. The code is not as straightforward as it could be with unsafe helper method - the unsafe constructs are not straightforward in C# and .NET Core libraries on purpose, to encourage writing safe code."