Skip to content

Commit b66628c

Browse files
authored
F# 7 fixes (#14294)
1 parent 2adeb15 commit b66628c

File tree

3 files changed

+112
-7
lines changed

3 files changed

+112
-7
lines changed

src/Compiler/TypedTree/TcGlobals.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,12 +1831,12 @@ type TcGlobals(
18311831
let info = makeOtherIntrinsicValRef (fslib_MFOperators_nleref, "atan2", None, Some "Atan2", [vara; varb], ([[varaTy]; [varaTy]], varbTy))
18321832
let tyargs = [aty;bty]
18331833
Some (info, tyargs, argExprs)
1834-
| "get_Zero", _, Some aty, [_] ->
1834+
| "get_Zero", _, Some aty, ([] | [_]) ->
18351835
// Call LanguagePrimitives.GenericZero
18361836
let info = makeOtherIntrinsicValRef (fslib_MFLanguagePrimitives_nleref, "GenericZero", None, None, [vara], ([], varaTy))
18371837
let tyargs = [aty]
18381838
Some (info, tyargs, [])
1839-
| "get_One", _, Some aty, [_] ->
1839+
| "get_One", _, Some aty, ([] | [_]) ->
18401840
// Call LanguagePrimitives.GenericOne
18411841
let info = makeOtherIntrinsicValRef (fslib_MFLanguagePrimitives_nleref, "GenericOne", None, None, [vara], ([], varaTy))
18421842
let tyargs = [aty]

tests/FSharp.Compiler.ComponentTests/Conformance/TypesAndTypeConstraints/IWSAMsAndSRTPs/IWSAMsAndSRTPsTests.fs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,38 @@ let setupCompilation compilation =
1919
|> withReferences [typesModule]
2020

2121

22+
[<Fact>]
23+
let ``Srtp call Zero property returns valid result`` () =
24+
Fsx """
25+
let inline zero<'T when 'T: (static member Zero: 'T)> = 'T.Zero
26+
let result = zero<int>
27+
if result <> 0 then failwith $"Something's wrong: {result}"
28+
"""
29+
|> runFsi
30+
|> shouldSucceed
31+
32+
[<Fact>]
33+
let ``Srtp call to custom property returns valid result`` () =
34+
FSharp """
35+
module Foo
36+
type Foo =
37+
static member Bar = 1
38+
39+
type HasBar<'T when 'T: (static member Bar: int)> = 'T
40+
41+
let inline bar<'T when HasBar<'T>> =
42+
'T.Bar
43+
44+
[<EntryPoint>]
45+
let main _ =
46+
let result = bar<Foo>
47+
if result <> 0 then
48+
failwith $"Unexpected result: {result}"
49+
0
50+
"""
51+
|> asExe
52+
|> compileAndRun
53+
2254
#if !NETCOREAPP
2355
[<Theory(Skip = "IWSAMs are not supported by NET472.")>]
2456
#else
@@ -775,7 +807,11 @@ module ``Active patterns`` =
775807

776808
module ``Suppression of System Numerics interfaces on unitized types`` =
777809

778-
[<Fact(Skip = "Solution needs to be updated to .NET 7")>]
810+
#if !NETCOREAPP
811+
[<Fact(Skip = "IWSAMs are not supported by NET472.")>]
812+
#else
813+
[<Fact>]
814+
#endif
779815
let Baseline () =
780816
Fsx """
781817
open System.Numerics
@@ -785,16 +821,19 @@ module ``Suppression of System Numerics interfaces on unitized types`` =
785821
|> compile
786822
|> shouldSucceed
787823

788-
[<Theory(Skip = "Solution needs to be updated to .NET 7")>]
824+
#if !NETCOREAPP
825+
[<Theory(Skip = "IWSAMs are not supported by NET472.")>]
826+
#else
827+
[<Theory>]
789828
[<InlineData("IAdditionOperators", 3)>]
790829
[<InlineData("IAdditiveIdentity", 2)>]
791830
[<InlineData("IBinaryFloatingPointIeee754", 1)>]
792831
[<InlineData("IBinaryNumber", 1)>]
793832
[<InlineData("IBitwiseOperators", 3)>]
794-
[<InlineData("IComparisonOperators", 2)>]
833+
[<InlineData("IComparisonOperators", 3)>]
795834
[<InlineData("IDecrementOperators", 1)>]
796835
[<InlineData("IDivisionOperators", 3)>]
797-
[<InlineData("IEqualityOperators", 2)>]
836+
[<InlineData("IEqualityOperators", 3)>]
798837
[<InlineData("IExponentialFunctions", 1)>]
799838
[<InlineData("IFloatingPoint", 1)>]
800839
[<InlineData("IFloatingPointIeee754", 1)>]
@@ -814,6 +853,7 @@ module ``Suppression of System Numerics interfaces on unitized types`` =
814853
[<InlineData("ITrigonometricFunctions", 1)>]
815854
[<InlineData("IUnaryNegationOperators", 2)>]
816855
[<InlineData("IUnaryPlusOperators", 2)>]
856+
#endif
817857
let ``Unitized type shouldn't be compatible with System.Numerics.I*`` name paramCount =
818858
let typeParams = Seq.replicate paramCount "'T" |> String.concat ","
819859
let genericType = $"{name}<{typeParams}>"

tests/FSharp.Compiler.ComponentTests/Interop/RequiredAndInitOnlyProperties.fs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ open System
88

99
module ``Required and init-only properties`` =
1010

11+
let csharpRecord =
12+
CSharp """
13+
namespace RequiredAndInitOnlyProperties
14+
{
15+
public record Recd();
16+
17+
}""" |> withCSharpLanguageVersion CSharpLanguageVersion.Preview |> withName "csLib"
18+
1119
let csharpBaseClass =
1220
CSharp """
1321
namespace RequiredAndInitOnlyProperties
@@ -228,7 +236,7 @@ let main _ =
228236
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"
229237
]
230238

231-
#if !NETCOREAPP
239+
#if !NETCOREAPP
232240
[<Fact(Skip = "NET472 is unsupported runtime for this kind of test.")>]
233241
#else
234242
[<Fact>]
@@ -259,6 +267,63 @@ let main _ =
259267
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"
260268
]
261269

270+
#if !NETCOREAPP
271+
[<Fact(Skip = "IWSAMs are not supported by NET472.")>]
272+
#else
273+
[<Fact>]
274+
#endif
275+
let ``F# can change init-only property via SRTP`` () =
276+
277+
let csharpLib = csharpBaseClass
278+
279+
let fsharpSource =
280+
"""
281+
open System
282+
open RequiredAndInitOnlyProperties
283+
284+
let inline setGetInit<'T when 'T : (member set_GetInit: int -> unit)> (a: 'T) (x: int) = a.set_GetInit(x)
285+
286+
[<EntryPoint>]
287+
let main _ =
288+
let raio = RAIO()
289+
setGetInit raio 111
290+
0
291+
"""
292+
FSharp fsharpSource
293+
|> asExe
294+
|> withLangVersion70
295+
|> withReferences [csharpLib]
296+
|> compile
297+
|> shouldSucceed
298+
299+
#if !NETCOREAPP
300+
[<Fact(Skip = "IWSAMs are not supported by NET472.")>]
301+
#else
302+
[<Fact>]
303+
#endif
304+
let ``F# can call special-named methods via SRTP`` () =
305+
306+
let csharpLib = csharpRecord
307+
308+
let fsharpSource =
309+
"""
310+
open System
311+
open RequiredAndInitOnlyProperties
312+
313+
let inline clone<'T when 'T : (member ``<Clone>$``: unit -> 'T)> (a: 'T) = a.``<Clone>$``()
314+
315+
[<EntryPoint>]
316+
let main _ =
317+
let recd = Recd()
318+
let _ = clone recd
319+
0
320+
"""
321+
FSharp fsharpSource
322+
|> asExe
323+
|> withLangVersion70
324+
|> withReferences [csharpLib]
325+
|> compile
326+
|> shouldSucceed
262327

263328
#if !NETCOREAPP
264329
[<Fact(Skip = "NET472 is unsupported runtime for this kind of test.")>]

0 commit comments

Comments
 (0)