Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected Compiler Errors with Nullable Value Tuples #5217

Closed
AaronEshbach opened this issue Jun 20, 2018 · 3 comments
Closed

Unexpected Compiler Errors with Nullable Value Tuples #5217

AaronEshbach opened this issue Jun 20, 2018 · 3 comments
Labels

Comments

@AaronEshbach
Copy link

AaronEshbach commented Jun 20, 2018

Usage of struct tuples (struct (0,0)) with the System.Nullable<T> type seems to cause unexpected compiler errors with F#. When attempting to directly instantiate a Nullable(struct (0,0)), the error A generic construct requires that the type 'struct (a * b)' have a public default constructor is reported. When attempting to instantiate a Nullable using a struct tuple bound to an identifier, such as Nullable(x), the error internal error: tcrefOfAppTy (Failure) is thrown. Simply unboxing the struct tuple as a ValueTuple<int,int> seems to resolve both of these errors.

Repro steps

  1. Execute the following code in F# interactive:
let x = System.Nullable(struct (0, 0))

This produces the error: error FS0001: A generic construct requires that the type 'struct ('a * 'b)' have a public default constructor

  1. Execute the following code in F# interactive:
let x = struct(0, 0)
System.Nullable(x)

This produces the error error FS0073: internal error: tcrefOfAppTy (Failure)

  1. Execute the following code in F# interactive:
let x = struct(0, 0)
let y = System.Nullable(struct (0, 0) |> unbox<System.ValueTuple<int,int>>)
let z = System.Nullable(x |> unbox<System.ValueTuple<int,int>>)

This executes without errors.

Expected behavior

Creating instances of Nullable with a struct tuple should work without error, equivalent to the behavior for using System.ValueTuple.

Actual behavior

Different compiler errors are reported when attempting to instantiate a Nullable with a struct tuple.

Known workarounds

Unboxing the struct tuple to a System.ValueTuple resolves the compiler errors and executes as expected.

Related information

Please see this StackOverflow post for the original report of this behavior.

Windows 10
.NET Framework 4.7.1
Visual Studio 2017

@cartermp cartermp added this to the 16.0 milestone Aug 29, 2018
@cartermp cartermp modified the milestones: 16.0, 16.1 Feb 21, 2019
@cartermp cartermp modified the milestones: 16.1, 16.2 Apr 23, 2019
@cartermp cartermp modified the milestones: 16.2, Backlog Apr 30, 2019
@JanWosnitza
Copy link
Contributor

JanWosnitza commented Jun 8, 2020

Accidentally confirmed that this is still an issue.
I just stumbled upon this issue in F# 4.7.0.0.
I consider it a major hickup for C# F# interop.

Visual F# Tools 10.9.1.0 for F# 4.7 16.6.0-beta.20217.4+1c969cac25e2d38d71872efe6c8226029e42bb59
Microsoft Visual F# Tools 10.9.1.0 for F# 4.7

Though there's a simple workaround: create an explicit struct instead.
e.g. instead of defining the function signature like:

abstract (string Name, int Value)? GetStuff();

do it like this:

struct Stuff
{
    public string Name;
    public int Value;
}

abstract Stuff? GetStuff();
override this.GetStuff() =
    Nullable(Stuff(Name="Answer", Value=42))

@cartermp
Copy link
Contributor

cartermp commented Jun 8, 2020

Just to note that the internal compiler error is not present anymore, and is instead:

image

@dsyme
Copy link
Contributor

dsyme commented Aug 26, 2020

Treating as duplicate of #7618

@dsyme dsyme closed this as completed Aug 26, 2020
@cartermp cartermp removed this from the Backlog milestone Aug 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants