fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

Disabling LanguageFeature.UnmanagedConstraintCsharpInterop from F# 9.0 for now

Open KevinRansom opened this issue 1 year ago • 1 comments

When compiled with F# 9.0 this code produces a runtime error:

open System

let printTypeConstraintsNative<'T when 'T : unmanaged> () = printf $"Hello: {typeof<'T>.FullName} is unmanaged"


let Main() =
    // null Seq
    let func (x:int) : 'T when 'T : unmanaged = Unchecked.defaultof<'T>
    let initFinite = Seq.init<nativeint> 3 func
    printfn "%A" initFinite
 
printTypeConstraintsNative<nativeint>()

Main()

Program output:

c:\temp>c:\kevinransom\fsharp\artifacts\bin\fsc\Release\net472\fsc C:\Users\kevinr\source\repos\ConsoleApp82\ConsoleApp82\Program.fs
Microsoft (R) F# Compiler version 12.9.100.0 for F# 9.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

c:\temp>Program.exe
Hello: System.IntPtr is unmanaged
Unhandled Exception: System.TypeLoadException: Method 'Specialize' on type 'func@8' from assembly 'Program, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' tried to implicitly override a method with weaker type parameter constraints.
   at Program.Main()
   at <StartupCode$Program>.$Program.main@()

compile with F# 8.0 works fine.

image

KevinRansom avatar Aug 07 '24 21:08 KevinRansom

Thanks for spotting this. How did you find this, is there an user report behind this?

I guess we have to change codegen when this is used within a lambda (to prevent that CLR crash)

T-Gro avatar Aug 12 '24 11:08 T-Gro