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
For a function argument that is a tuple, when the argument is referenced by the argument name, you may or may not get the same tuple (as denoted by obj.ReferenceEquals)
Repro steps
Given
let source = [(1,1); (2,2)]
source
|> List.iter (fun outer ->
source
|> List.exists (fun inner -> obj.ReferenceEquals (outer, inner)) // but Seq.exists fails!!
|> fun found -> if not found then failwith "boom!")
printfn "Hooray!"
The output is "Hooray!" but if the List.exists is change to Seq.exists then the exception condition is hit.
Expected behavior
The argument, when reference by name, should always be the original object.
Actual behavior
Sometimes, and I'm not sure the conditions under which it decides, the tuple is split and recreated in a new Tuple object. Even in optimized code. Neither of the exists function are marked as inline.
Known workarounds
Don't do reference equality checks on tuples!
Related information
Using Ilspy, it can be seen that for the List.exists case the following function object is created:
let source = [(1,1); (2,2)]
let (*inline*) referenceEquals outer inner = obj.ReferenceEquals (outer, inner)
source
|> List.iter (fun outer ->
source
|> Seq.exists (referenceEquals outer)
|> fun found -> if not found then failwith "boom!")
printfn "Hooray!"
This works, but if the inline is uncommented then it fails again...
IIRC the F# Language spec basically says that tuples may be made and consumed as much as the F# compiler likes, and ReferenceEquals is meaningless for these types. We can dig out the relevant wording if you like
No worries; but ConstraintSolver.fs should be modified to remove this use of undefined behaviour. (Looks like it had already been thought of, given the comment).
And I guess I would have also thought that the optimizer would have removed the additional tuple construct. But anyway.
No worries; but ConstraintSolver.fs should be modified to remove this use of undefined behaviour. (Looks like it had already been thought of, given the comment).
Absolutely, I had no idea.
And I guess I would have also thought that the optimizer would have removed the additional tuple construct.
Activity
manofstick commentedon Apr 26, 2017
In the slightly modified case:
This works, but if the
inlineis uncommented then it fails again...dsyme commentedon Apr 26, 2017
@manofstick I'll close this as by design.
IIRC the F# Language spec basically says that tuples may be made and consumed as much as the F# compiler likes, and ReferenceEquals is meaningless for these types. We can dig out the relevant wording if you like
thx :)
manofstick commentedon Apr 27, 2017
@dsyme
No worries; but
ConstraintSolver.fsshould be modified to remove this use of undefined behaviour. (Looks like it had already been thought of, given the comment).And I guess I would have also thought that the optimizer would have removed the additional tuple construct. But anyway.
dsyme commentedon Apr 27, 2017
Absolutely, I had no idea.
100% agreed
let's track those as specific other issues?