From 566ae7c201a825f25ad4ff689914e00ea4884108 Mon Sep 17 00:00:00 2001 From: Vlad Zarytovskii Date: Thu, 10 Nov 2022 18:48:41 +0100 Subject: [PATCH 1/5] WIP: Fix for calling init-only setter via srtp call + allow calling special-named functions via srtp --- .../Interop/RequiredAndInitOnlyProperties.fs | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/FSharp.Compiler.ComponentTests/Interop/RequiredAndInitOnlyProperties.fs b/tests/FSharp.Compiler.ComponentTests/Interop/RequiredAndInitOnlyProperties.fs index a3e6b6b6ed..78f347c360 100644 --- a/tests/FSharp.Compiler.ComponentTests/Interop/RequiredAndInitOnlyProperties.fs +++ b/tests/FSharp.Compiler.ComponentTests/Interop/RequiredAndInitOnlyProperties.fs @@ -8,6 +8,14 @@ open System module ``Required and init-only properties`` = + let csharpRecord = + CSharp """ + namespace RequiredAndInitOnlyProperties + { + public record Recd(); + + }""" |> withCSharpLanguageVersion CSharpLanguageVersion.Preview |> withName "csLib" + let csharpBaseClass = CSharp """ namespace RequiredAndInitOnlyProperties @@ -259,6 +267,55 @@ let main _ = Error 810, Line 9, Col 38, Line 9, Col 40, "Init-only property 'GetInit' cannot be set outside the initialization code. See https://aka.ms/fsharp-assigning-values-to-properties-at-initialization" ] + [] + let ``F# cannot change init-only property via SRTP`` () = + + let csharpLib = csharpBaseClass + + let fsharpSource = + """ +open System +open RequiredAndInitOnlyProperties + +let inline setGetInit<'T when 'T : (member set_GetInit: int -> unit)> (a: 'T) (x: int) = a.set_GetInit(x) + +[] +let main _ = + let raio = RAIO() + setGetInit raio 111 + 0 +""" + FSharp fsharpSource + |> asExe + |> withLangVersion70 + |> withReferences [csharpLib] + |> compile + |> shouldFail + + [] + let ``F# can call special-named methods via SRTP`` () = + + let csharpLib = csharpRecord + + let fsharpSource = + """ +open System +open RequiredAndInitOnlyProperties + +let inline clone<'T when 'T : (member ``$``: unit -> 'T)> (a: 'T) = a.``$``() + +[] +let main _ = + let recd = Recd() + clone recd + 0 +""" + FSharp fsharpSource + |> asExe + |> withLangVersion70 + |> withReferences [csharpLib] + |> compile + |> shouldSucceed #if !NETCOREAPP [] From c845a73143056afe105a6813432535cf791bf8c1 Mon Sep 17 00:00:00 2001 From: Vlad Zarytovskii Date: Mon, 14 Nov 2022 15:59:37 +0100 Subject: [PATCH 2/5] Fix --- src/Compiler/TypedTree/TcGlobals.fs | 4 +-- .../IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs | 36 +++++++++++++++++-- .../Interop/RequiredAndInitOnlyProperties.fs | 6 ++-- 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/Compiler/TypedTree/TcGlobals.fs b/src/Compiler/TypedTree/TcGlobals.fs index 9eaac69024..96dc59428e 100755 --- a/src/Compiler/TypedTree/TcGlobals.fs +++ b/src/Compiler/TypedTree/TcGlobals.fs @@ -1831,12 +1831,12 @@ type TcGlobals( let info = makeOtherIntrinsicValRef (fslib_MFOperators_nleref, "atan2", None, Some "Atan2", [vara; varb], ([[varaTy]; [varaTy]], varbTy)) let tyargs = [aty;bty] Some (info, tyargs, argExprs) - | "get_Zero", _, Some aty, [_] -> + | "get_Zero", _, Some aty, ([] | [_]) -> // Call LanguagePrimitives.GenericZero let info = makeOtherIntrinsicValRef (fslib_MFLanguagePrimitives_nleref, "GenericZero", None, None, [vara], ([], varaTy)) let tyargs = [aty] Some (info, tyargs, []) - | "get_One", _, Some aty, [_] -> + | "get_One", _, Some aty, ([] | [_]) -> // Call LanguagePrimitives.GenericOne let info = makeOtherIntrinsicValRef (fslib_MFLanguagePrimitives_nleref, "GenericOne", None, None, [vara], ([], varaTy)) let tyargs = [aty] diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs index 9abba91ca2..f89695f4c8 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs @@ -19,6 +19,38 @@ let setupCompilation compilation = |> withReferences [typesModule] +[] +let ``Srtp call Zero property returns valid result`` () = + Fsx """ +let inline zero<'T when 'T: (static member Zero: 'T)> = 'T.Zero +let result = zero +if result <> 0 then failwith $"Something's wrong: {result}" + """ + |> runFsi + |> shouldSucceed + +[] +let ``Srtp call to custom property returns valid result`` () = + FSharp """ +module Foo +type Foo = + static member Bar = 1 + +type HasBar<'T when 'T: (static member Bar: int)> = 'T + +let inline bar<'T when HasBar<'T>> = + 'T.Bar + +[] +let main _ = + let result = bar + if result <> 0 then + failwith $"Unexpected result: {result}" + 0 + """ + |> asExe + |> compileAndRun + #if !NETCOREAPP [] #else @@ -775,7 +807,7 @@ module ``Active patterns`` = module ``Suppression of System Numerics interfaces on unitized types`` = - [] + [] let Baseline () = Fsx """ open System.Numerics @@ -785,7 +817,7 @@ module ``Suppression of System Numerics interfaces on unitized types`` = |> compile |> shouldSucceed - [] + [] [] [] [] diff --git a/tests/FSharp.Compiler.ComponentTests/Interop/RequiredAndInitOnlyProperties.fs b/tests/FSharp.Compiler.ComponentTests/Interop/RequiredAndInitOnlyProperties.fs index 78f347c360..96da60dd4a 100644 --- a/tests/FSharp.Compiler.ComponentTests/Interop/RequiredAndInitOnlyProperties.fs +++ b/tests/FSharp.Compiler.ComponentTests/Interop/RequiredAndInitOnlyProperties.fs @@ -268,7 +268,7 @@ let main _ = ] [] - let ``F# cannot change init-only property via SRTP`` () = + let ``F# can change init-only property via SRTP`` () = let csharpLib = csharpBaseClass @@ -290,7 +290,7 @@ let main _ = |> withLangVersion70 |> withReferences [csharpLib] |> compile - |> shouldFail + |> shouldSucceed [] let ``F# can call special-named methods via SRTP`` () = @@ -307,7 +307,7 @@ let inline clone<'T when 'T : (member ``$``: unit -> 'T)> (a: 'T) = a.``< [] let main _ = let recd = Recd() - clone recd + let _ = clone recd 0 """ FSharp fsharpSource From 8d288de8a8b1a3e749cfcc2ae02819482ccfdc32 Mon Sep 17 00:00:00 2001 From: Vlad Zarytovskii Date: Mon, 14 Nov 2022 16:46:09 +0100 Subject: [PATCH 3/5] Fixed tests --- .../IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs index f89695f4c8..b0ca13d808 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs @@ -816,17 +816,19 @@ module ``Suppression of System Numerics interfaces on unitized types`` = |> withLangVersion70 |> compile |> shouldSucceed - +#if !NETCOREAPP +[] +#else [] [] [] [] [] [] - [] + [] [] [] - [] + [] [] [] [] @@ -846,6 +848,7 @@ module ``Suppression of System Numerics interfaces on unitized types`` = [] [] [] +#endif let ``Unitized type shouldn't be compatible with System.Numerics.I*`` name paramCount = let typeParams = Seq.replicate paramCount "'T" |> String.concat "," let genericType = $"{name}<{typeParams}>" From a385c85333eff103ad187bfc8c31e23b40e5dbb7 Mon Sep 17 00:00:00 2001 From: Vlad Zarytovskii Date: Mon, 14 Nov 2022 16:49:45 +0100 Subject: [PATCH 4/5] Fixed tests --- .../IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs | 4 ++++ .../Interop/RequiredAndInitOnlyProperties.fs | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs index b0ca13d808..aa8741e749 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs @@ -807,7 +807,11 @@ module ``Active patterns`` = module ``Suppression of System Numerics interfaces on unitized types`` = +#if !NETCOREAPP + [] +#else [] +#endif let Baseline () = Fsx """ open System.Numerics diff --git a/tests/FSharp.Compiler.ComponentTests/Interop/RequiredAndInitOnlyProperties.fs b/tests/FSharp.Compiler.ComponentTests/Interop/RequiredAndInitOnlyProperties.fs index 96da60dd4a..a775b806ee 100644 --- a/tests/FSharp.Compiler.ComponentTests/Interop/RequiredAndInitOnlyProperties.fs +++ b/tests/FSharp.Compiler.ComponentTests/Interop/RequiredAndInitOnlyProperties.fs @@ -267,7 +267,11 @@ let main _ = Error 810, Line 9, Col 38, Line 9, Col 40, "Init-only property 'GetInit' cannot be set outside the initialization code. See https://aka.ms/fsharp-assigning-values-to-properties-at-initialization" ] +#if !NETCOREAPP + [] +#else [] +#endif let ``F# can change init-only property via SRTP`` () = let csharpLib = csharpBaseClass @@ -292,7 +296,11 @@ let main _ = |> compile |> shouldSucceed + #if !NETCOREAPP + [] +#else [] +#endif let ``F# can call special-named methods via SRTP`` () = let csharpLib = csharpRecord From d723bb1d4555e12e97509cc5fbf0e1157ee5cd6f Mon Sep 17 00:00:00 2001 From: Vlad Zarytovskii Date: Mon, 14 Nov 2022 17:38:39 +0100 Subject: [PATCH 5/5] Fixed tests --- .../IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs | 3 ++- .../Interop/RequiredAndInitOnlyProperties.fs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs index aa8741e749..a328c5f3df 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs @@ -820,8 +820,9 @@ module ``Suppression of System Numerics interfaces on unitized types`` = |> withLangVersion70 |> compile |> shouldSucceed + #if !NETCOREAPP -[] + [] #else [] [] diff --git a/tests/FSharp.Compiler.ComponentTests/Interop/RequiredAndInitOnlyProperties.fs b/tests/FSharp.Compiler.ComponentTests/Interop/RequiredAndInitOnlyProperties.fs index a775b806ee..0b2d4cc37a 100644 --- a/tests/FSharp.Compiler.ComponentTests/Interop/RequiredAndInitOnlyProperties.fs +++ b/tests/FSharp.Compiler.ComponentTests/Interop/RequiredAndInitOnlyProperties.fs @@ -236,7 +236,7 @@ let main _ = Error 810, Line 9, Col 5, Line 9, Col 21, "Cannot call 'set_GetInit' - a setter for init-only property, please use object initialization instead. See https://aka.ms/fsharp-assigning-values-to-properties-at-initialization" ] - #if !NETCOREAPP +#if !NETCOREAPP [] #else []