fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

Error for extra implicit conversion is needed

Open Lanayx opened this issue 3 years ago • 5 comments

Right now implicit type conversion happens only one-step (like int-> float or float -> Nullable<float>), but when we try to do two-step conversion (e.g. int -> Nullable<float>) there is no warning on compile time and we've exception in runtime.

Repro steps

type C() =
    member val X = System.Nullable(1.0) with get, set
C(X = 1)

Expected behavior

The code either works or gives compiler warning

Actual behavior

The code compiles successfully but in runtime the exception is raised

Unhandled exception. System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Nullable`1[System.Double]'.

Known workarounds

Fix the code (e.g. write C(X = 1.0)) after facing the error in the runtime

Related information

dotnet 6.0.201 fsharp 6.0.3

Lanayx avatar Apr 15 '22 10:04 Lanayx

The same as or related to https://github.com/dotnet/fsharp/issues/12414.

kerams avatar Apr 15 '22 17:04 kerams

@kerams probably not the same, since the fix was merged long time ago

Lanayx avatar Apr 15 '22 21:04 Lanayx

Yes I can't see a duplicate of this bug - the corresponding code with named argument works, so it's specific to setter-properties

type C(x: System.Nullable<int>) =
    member val Y = System.Nullable(1.0) with get, set
C(x = 1)

dsyme avatar Apr 20 '22 02:04 dsyme

Specifically this pattern https://github.com/dotnet/fsharp/issues/12994#issuecomment-1103390228 bit us today again, given the compiler is effectively outputting invalid IL could this version of the bug also be fixed for 7.0?

@vzarytovskii

I might be able to help here with some guidance, the previous PR fixing the first kind should already help a lot but I probably need a few pointers to where the constructor logic lives.

NinoFloris avatar Aug 04 '22 16:08 NinoFloris

I guess @dsyme can give you pointers quicker than me. I will look at it otherwise.

vzarytovskii avatar Aug 04 '22 16:08 vzarytovskii