Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Compiler/Facilities/LanguageFeatures.fs
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ type LanguageVersion(versionText) =
LanguageFeature.BetterAnonymousRecordParsing, languageVersion100
LanguageFeature.ScopedNowarn, languageVersion100
LanguageFeature.AllowTypedLetOrUseBang, languageVersion100
LanguageFeature.UnmanagedConstraintCsharpInterop, languageVersion100
LanguageFeature.AllowAccessModifiersToAutoPropertiesGettersAndSetters, languageVersion100

// F# preview (still preview in 10.0)
LanguageFeature.UnmanagedConstraintCsharpInterop, previewVersion // not enabled because: https://github.com/dotnet/fsharp/issues/17509
LanguageFeature.FromEndSlicing, previewVersion // Unfinished features --- needs work
]

Expand Down
1 change: 1 addition & 0 deletions src/Compiler/TypedTree/TypedTreeOps.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9970,6 +9970,7 @@ let isCompiledOrWitnessPassingConstraint (g: TcGlobals) cx =
| TyparConstraint.IsNonNullableStruct _
| TyparConstraint.IsReferenceType _
| TyparConstraint.RequiresDefaultConstructor _
| TyparConstraint.IsUnmanaged _ // implies "struct" and also causes a modreq
| TyparConstraint.CoercesTo _ -> true
| TyparConstraint.MayResolveMember _ when g.langVersion.SupportsFeature LanguageFeature.WitnessPassing -> true
| _ -> false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,21 +471,72 @@ printf "%s" (CsharpStruct<int>.Hi<MultiCaseUnion>())
IL_0000: ret
} """]

[<Fact>]
let ``FSharp does not generate modreq for VBNET to consume in v7`` () =
[<FactForNETCOREAPP>]
let ``FSharp generates modreq for CSharp to consume in v9`` () =
Fsx "let testMyFunction (x: 'TUnmanaged when 'TUnmanaged : unmanaged) = ()"
|> withLangVersion70
|> withLangVersion10
|> compile
|> shouldSucceed
|> verifyIL ["""
.method public static void testMyFunction<TUnmanaged>(!!TUnmanaged x) cil managed
.method public static void testMyFunction<valuetype (class [runtime]System.ValueType modreq([runtime]System.Runtime.InteropServices.UnmanagedType)) TUnmanaged>(!!TUnmanaged x) cil managed
{
.param type TUnmanaged
.custom instance void [runtime]System.Runtime.CompilerServices.IsUnmanagedAttribute::.ctor() = ( 01 00 00 00 )

.maxstack 8
IL_0000: ret
} """]


[<Fact>]
let ``Unmanaged constraint in lambda reproduces issue 17509`` () =
// This test reproduces the issue https://github.com/dotnet/fsharp/issues/17509
// When UnmanagedConstraintCsharpInterop is enabled, it generates invalid IL
// causing a TypeLoadException at runtime
Fsx """
open System

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

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

printTypeConstraintsNative<nativeint>()
Main()
"""
|> withLangVersionPreview
|> asExe
|> compileAndRun
|> shouldSucceed
|> verifyOutput "Hello: System.IntPtr is unmanagedseq [0n; 0n; 0n]"

[<FactForNETCOREAPP>]
let ``Unmanaged constraint in lambda generates invalid IL for Specialize method with preview version`` () =
Fsx """
let Main() =
let func (x:int) : 'T when 'T : unmanaged = Unchecked.defaultof<'T>
let initFinite = Seq.init<nativeint> 3 func
printfn "%A" initFinite
Main()
"""
|> withLangVersionPreview
|> asExe
|> compile
|> shouldSucceed
|> verifyIL ["""
.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
{
.param type T
.custom instance void [runtime]System.Runtime.CompilerServices.IsUnmanagedAttribute::.ctor() = ( 01 00 00 00 )

.maxstack 8
IL_0000: ldsfld class Test/'func@3-1'<!0> class Test/'func@3-1'<!!T>::@_instance
IL_0005: ret
} """]


[<Fact>]
let ``C# can consume F#-defined struct with unmanaged constraint - valid`` () =
let fsharpLib =
Expand Down
Loading