@@ -471,21 +471,72 @@ printf "%s" (CsharpStruct<int>.Hi<MultiCaseUnion>())
471471 IL_0000: ret
472472 } """ ]
473473
474- [<Fact >]
475- let ``FSharp does not generate modreq for VBNET to consume in v7 `` () =
474+ [<FactForNETCOREAPP >]
475+ let ``FSharp generates modreq for CSharp to consume in v9 `` () =
476476 Fsx " let testMyFunction (x: 'TUnmanaged when 'TUnmanaged : unmanaged) = ()"
477- |> withLangVersion70
477+ |> withLangVersion10
478478 |> compile
479479 |> shouldSucceed
480480 |> verifyIL [ """
481- .method public static void testMyFunction<TUnmanaged>(!!TUnmanaged x) cil managed
481+ .method public static void testMyFunction<valuetype (class [runtime]System.ValueType modreq([runtime]System.Runtime.InteropServices.UnmanagedType)) TUnmanaged>(!!TUnmanaged x) cil managed
482482 {
483+ .param type TUnmanaged
484+ .custom instance void [runtime]System.Runtime.CompilerServices.IsUnmanagedAttribute::.ctor() = ( 01 00 00 00 )
483485
484486 .maxstack 8
485487 IL_0000: ret
486488 } """ ]
487489
488490
491+ [<Fact>]
492+ let ``Unmanaged constraint in lambda reproduces issue 17509`` () =
493+ // This test reproduces the issue https://github.com/dotnet/fsharp/issues/17509
494+ // When UnmanagedConstraintCsharpInterop is enabled, it generates invalid IL
495+ // causing a TypeLoadException at runtime
496+ Fsx """
497+ open System
498+
499+ let printTypeConstraintsNative<'T when 'T : unmanaged> () = printf $"Hello: {typeof<'T>.FullName} is unmanaged"
500+
501+ let Main() =
502+ let func (x:int) : 'T when 'T : unmanaged = Unchecked.defaultof<'T>
503+ let initFinite = Seq.init<nativeint> 3 func
504+ printf "%A " initFinite
505+
506+ printTypeConstraintsNative<nativeint>()
507+ Main()
508+ """
509+ |> withLangVersionPreview
510+ |> asExe
511+ |> compileAndRun
512+ |> shouldSucceed
513+ |> verifyOutput " Hello: System.IntPtr is unmanagedseq [0n; 0n; 0n]"
514+
515+ [<FactForNETCOREAPP>]
516+ let ``Unmanaged constraint in lambda generates invalid IL for Specialize method with preview version`` () =
517+ Fsx """
518+ let Main() =
519+ let func (x:int) : 'T when 'T : unmanaged = Unchecked.defaultof<'T>
520+ let initFinite = Seq.init<nativeint> 3 func
521+ printfn "%A " initFinite
522+ Main()
523+ """
524+ |> withLangVersionPreview
525+ |> asExe
526+ |> compile
527+ |> shouldSucceed
528+ |> verifyIL [ """
529+ .method assembly strict virtual instance class [FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<int32,!!T> DirectInvoke<valuetype (class [runtime]System.ValueType modreq([runtime]System.Runtime.InteropServices.UnmanagedType)) T>() cil managed
530+ {
531+ .param type T
532+ .custom instance void [runtime]System.Runtime.CompilerServices.IsUnmanagedAttribute::.ctor() = ( 01 00 00 00 )
533+
534+ .maxstack 8
535+ IL_0000: ldsfld class Test/'func@3-1'<!0> class Test/'func@3-1'<!!T>::@_instance
536+ IL_0005: ret
537+ } """ ]
538+
539+
489540 [<Fact>]
490541 let ``C # can consume F #- defined struct with unmanaged constraint - valid`` () =
491542 let fsharpLib =
0 commit comments