Skip to content

Commit 87114ec

Browse files
authored
Specializing Operators.op_UnaryPlus for witness info (#9769)
* Initial work to fix op_UnaryPlus witness info * Added UnaryPlusDynamic * update surface area * Only enable type provider test on net472 * Removing UnaryPlusDynamic * fixing build * fix comment * Update prim-types.fs
1 parent b3c3ad7 commit 87114ec

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

src/fsharp/TcGlobals.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,11 @@ type public TcGlobals(compilingFslib: bool, ilg:ILGlobals, fslibCcu: CcuThunk, d
16011601
Some (g.array_set_info, [ety], argExprs)
16021602
| "get_Item", [sty; _; _], _, [_; _] when isStringTy g sty ->
16031603
Some (g.getstring_info, [], argExprs)
1604+
| "op_UnaryPlus", [aty], _, [_] ->
1605+
// Call Operators.id
1606+
let info = makeOtherIntrinsicValRef (fslib_MFOperators_nleref, "id", None, None, [vara], ([[varaTy]], varaTy))
1607+
let tyargs = [aty]
1608+
Some (info, tyargs, argExprs)
16041609
| _ ->
16051610
None
16061611

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
3+
namespace FSharp.Compiler.UnitTests
4+
5+
open NUnit.Framework
6+
open FSharp.Test.Utilities.Compiler
7+
open FSharp.Quotations.Patterns
8+
9+
[<TestFixture>]
10+
module CodeQuotationsTests =
11+
12+
[<Test>]
13+
let ``Quotation on op_UnaryPlus(~+) compiles and runs`` () =
14+
Fsx """
15+
open FSharp.Linq.RuntimeHelpers
16+
open FSharp.Quotations.Patterns
17+
open FSharp.Quotations.DerivedPatterns
18+
19+
let eval q = LeafExpressionConverter.EvaluateQuotation q
20+
21+
let inline f x = <@ (~+) x @>
22+
let x = <@ f 1 @>
23+
let y : unit =
24+
match f 1 with
25+
| Call(_, methInfo, _) when methInfo.Name = "op_UnaryPlus" ->
26+
()
27+
| e ->
28+
failwithf "did not expect expression for 'y': %A" e
29+
let z : unit =
30+
match f 5 with
31+
| (CallWithWitnesses(_, methInfo, methInfoW, _, _) as e) when methInfo.Name = "op_UnaryPlus" && methInfoW.Name = "op_UnaryPlus$W" ->
32+
if ((eval e) :?> int) = 5 then
33+
()
34+
else
35+
failwith "did not expect evaluation false"
36+
| e ->
37+
failwithf "did not expect expression for 'z': %A" e
38+
"""
39+
|> asExe
40+
|> withOptions ["--langversion:preview"]
41+
|> compileAndRun
42+
43+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
3+
namespace FSharp.Compiler.UnitTests
4+
5+
open NUnit.Framework
6+
open FSharp.Test.Utilities.Compiler
7+
open FSharp.Tests
8+
9+
#if !NETCOREAPP
10+
11+
[<TestFixture>]
12+
module WitnessTests =
13+
14+
[<Test>]
15+
let ``Witness expressions are created as a result of compiling the type provider tests`` () =
16+
let dir = Core.getTestsDirectory "typeProviders/helloWorld"
17+
Fsx (sprintf """
18+
#load @"%s"
19+
""" (dir ++ "provider.fsx"))
20+
|> asExe
21+
|> ignoreWarnings
22+
|> withOptions ["--langversion:preview"]
23+
|> compile
24+
|> shouldSucceed
25+
|> ignore
26+
#endif
27+
28+

tests/fsharp/FSharpSuite.Tests.fsproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
<Compile Include="Compiler\Warnings\ExperimentalAttributeTests.fs" />
4242
<Compile Include="Compiler\Warnings\PatternMatchingWarningTests.fs" />
4343
<Compile Include="Compiler\SourceTextTests.fs" />
44+
<Compile Include="Compiler\Language\WitnessTests.fs" />
45+
<Compile Include="Compiler\Language\CodeQuotationTests.fs" />
4446
<Compile Include="Compiler\Language\CompilerDirectiveTests.fs" />
4547
<Compile Include="Compiler\Language\DefaultInterfaceMemberTests.fs" />
4648
<Compile Include="Compiler\Language\OptionalInteropTests.fs" />

0 commit comments

Comments
 (0)