You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In F# 4.6.2 it appears that a mutable defined inside a for loop has it's definition incorrectly optimised so that it is effectively defined outside of the iteration.
This can be demonstrated with the following code.
let bug() =
for a in [1;2;3;4] do
let mutable x = null
if x = null then
x <- HashSet<int>()
x.Add a |> ignore
let expected = [a]
let actual = List.ofSeq x
if expected <> actual then
failwith "Bug"
The expected behaviour is clear from the function above. What actually happens is that the above code is compiled as if the line 'let mutable x = null' comes before the line 'for a in [1;2;3;4] do' and after the seconditeration, x holds two integers, not one, and fails.
This bug appears to have been introduced in VS2019/F# 4.6.2 - the same code in VS2017/F# 4.5.4 works fine.
My workaround for this is to replace the mutable with a reference cell. The code below does not throw.
let not_a_bug() =
for a in [1;2;3;4] do
let x = ref null
if (!x) = null then
x := HashSet<int>()
(!x).Add a |> ignore
let expected = [a]
let actual = List.ofSeq (!x)
if expected <> actual then
failwith "Bug"
In F# 4.6.2 it appears that a mutable defined inside a for loop has it's definition incorrectly optimised so that it is effectively defined outside of the iteration.
This can be demonstrated with the following code.
The expected behaviour is clear from the function above. What actually happens is that the above code is compiled as if the line 'let mutable x = null' comes before the line 'for a in [1;2;3;4] do' and after the seconditeration, x holds two integers, not one, and fails.
This bug appears to have been introduced in VS2019/F# 4.6.2 - the same code in VS2017/F# 4.5.4 works fine.
My workaround for this is to replace the mutable with a reference cell. The code below does not throw.
The text was updated successfully, but these errors were encountered: