Skip to content

Tuple argument are handled inconsistently #2923

Closed

Description

@manofstick

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:

internal class clo@6-1 : FSharpFunc<Tuple<int, int>, bool>
{
	public Tuple<int, int> outer;

	[DebuggerNonUserCode, CompilerGenerated]
	internal clo@6-1(Tuple<int, int> outer)
	{
		this.outer = outer;
	}

	public override bool Invoke(Tuple<int, int> inner)
	{
		return object.ReferenceEquals(this.outer, inner);
	}
}

And for Seq.exists the following is created:

internal class main@9-1 : FSharpFunc<Tuple<int, int>, bool>
{
	public Tuple<int, int> outer;

	[DebuggerNonUserCode, CompilerGenerated]
	internal main@9-1(Tuple<int, int> outer)
	{
		this.outer = outer;
	}

	public override bool Invoke(Tuple<int, int> tupledArg)
	{
		int item = tupledArg.Item1;
		int item2 = tupledArg.Item2;
		Tuple<int, int> objB = new Tuple<int, int>(item, item2);
		return object.ReferenceEquals(this.outer, objB);
	}
}

Activity

  1. manofstick commented on Apr 26, 2017

    @manofstick
    ContributorAuthor

    In the slightly modified case:

    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...

  2. dsyme commented on Apr 26, 2017

    @dsyme
    Contributor

    @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 :)

  3. manofstick commented on Apr 27, 2017

    @manofstick
    ContributorAuthor

    @dsyme

    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.

  4. dsyme commented on Apr 27, 2017

    @dsyme
    Contributor

    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.

    100% agreed

    let's track those as specific other issues?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions