Skip to content

Commit

Permalink
Merge branch 'main' into fix17499
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinRansom authored Aug 7, 2024
2 parents 46b3e48 + 500f24f commit de6dbfe
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@
<Compile Include="Interop\StaticsInInterfaces.fs" />
<Compile Include="Interop\VisibilityTests.fs" />
<Compile Include="Interop\ByrefTests.fs" />
<Compile Include="Interop\ParamArrayMigrated.fs" />
<Compile Include="Interop\ParamArray.fs" />
<Compile Include="Scripting\Interactive.fs" />
<Compile Include="TypeChecks\SeqTypeCheckTests.fs" />
<Compile Include="TypeChecks\CheckDeclarationsTests.fs" />
Expand Down
107 changes: 107 additions & 0 deletions tests/FSharp.Compiler.ComponentTests/Interop/ParamArray.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

namespace Interop

open Xunit
open FSharp.Test
open FSharp.Test.Compiler

module ParamArray =

[<Fact>]
let ``C# 13 params enhancements`` () =
let csharp =
CSharp """
using System;
using System.Collections.Generic;
namespace CSharpAssembly;
public class CS13ParamArray
{
public static void WriteNames(params string[] names)
=> Console.WriteLine("First: " + string.Join(" + ", names));
public static void WriteNames(params List<string> names)
=> Console.WriteLine("Second: " + string.Join(" + ", names));
public static void WriteNames(params IEnumerable<string> names)
=> Console.WriteLine("Third: " + string.Join(" + ", names));
}"""
|> withCSharpLanguageVersionPreview

FSharp """
open System.Collections.Generic;
open CSharpAssembly
CS13ParamArray.WriteNames("Petr", "Jana")
CS13ParamArray.WriteNames(List["Petr"; "Jana"])
CS13ParamArray.WriteNames(["Petr"; "Jana"])
"""
|> withReferences [csharp]
|> compileExeAndRun
|> shouldSucceed
|> withStdOutContainsAllInOrder [
"First: Petr + Jana"
"Second: Petr + Jana"
"Third: Petr + Jana"
]

[<FactForNETCOREAPP>]
let ``C# 13 params enhancements - ReadOnlySpan`` () =
let csharp =
CSharp """
using System;
namespace CSharpAssembly;
public class CS13ParamArray
{
public static void WriteNames(params ReadOnlySpan<string> names)
=> Console.WriteLine(string.Join(" + ", names));
}"""
|> withCSharpLanguageVersionPreview

FSharp """
open System
open CSharpAssembly
CS13ParamArray.WriteNames(ReadOnlySpan([|"Petr"; "Jana"|]))
"""
|> withReferences [csharp]
|> compileExeAndRun
|> shouldSucceed
|> withStdOutContainsAllInOrder [ "Petr + Jana" ]

[<Fact>]
let ``C# 13 params enhancements - error when no matching overload is available`` () =
let csharp =
CSharp """
using System;
using System.Collections.Generic;
namespace CSharpAssembly;
public class CS13ParamArray
{
public static void WriteNames(params List<string> names)
=> Console.WriteLine("Second: " + string.Join(" + ", names));
public static void WriteNames(params IEnumerable<string> names)
=> Console.WriteLine("Third: " + string.Join(" + ", names));
}"""
|> withCSharpLanguageVersionPreview

FSharp """
open CSharpAssembly
CS13ParamArray.WriteNames("Petr", "Jana")
"""
|> withReferences [csharp]
|> asExe
|> compile
|> shouldFail
|> withDiagnostics [
(Error 503, Line 4, Col 1, Line 4, Col 42,
"A member or object constructor 'WriteNames' taking 2 arguments is not accessible from this code location. All accessible versions of method 'WriteNames' take 1 arguments.")
]
110 changes: 110 additions & 0 deletions tests/FSharp.Compiler.ComponentTests/Interop/ParamArrayMigrated.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

namespace Interop

open Xunit
open FSharp.Test.Compiler

module ParamArrayMigrated =

let csharp =
CSharp """
using System;
namespace CSharpAssembly
{
[AttributeUsage(AttributeTargets.All)]
public class AttributeWithParamArray : Attribute
{
public object[] Parameters;
public AttributeWithParamArray(params object[] x)
{
Parameters = x;
}
}
public class CSParamArray
{
public static int Method(params int[] allArgs)
{
int total = 0;
foreach (int i in allArgs)
total += i;
return total;
}
public static int Method<T>(params T[] args)
{
return args.Length;
}
}
}"""

[<Fact>]
let ``Valid params call`` () =
FSharp """
open System
open CSharpAssembly
// Apply the attribute
[<AttributeWithParamArray([| (0 :> obj) |])>]
type Foo() =
[<AttributeWithParamArray([| ("foo" :> obj); ("bar" :> obj) |])>]
override this.ToString() = "Stuff"
let callCSGenericMethod (a: 't[]) = CSParamArray.Method(a)
[<assembly:AttributeWithParamArray ([| |])>]
do
let getTestAttribute (t : Type) =
let tyAttributes = t.GetCustomAttributes(false)
let attrib = tyAttributes |> Array.find (fun attrib -> match attrib with :? AttributeWithParamArray -> true | _ -> false)
(attrib :?> AttributeWithParamArray)
let tyFoo = typeof<Foo>
let testAtt = getTestAttribute tyFoo
if testAtt.Parameters <> [| (0 :> obj) |] then
failwith "Attribute parameters not as expected"
let directCallWorks =
CSParamArray.Method(9, 8, 7) + CSParamArray.Method(1, 2) + CSParamArray.Method() = (9 + 8 + 7) + (1 + 2)
if not directCallWorks then
failwith "Calling C# param array method gave unexpected result"
let callParamArray (x : int array) = CSParamArray.Method(x)
let asArrayCallWorks = (callParamArray [| 9; 8; 7 |]) = (9 + 8 + 7)
if not asArrayCallWorks then
failwith "Calling C# param array method, passing args as an array, gave unexpected result"
if callCSGenericMethod [|"1";"2";"3"|] <> 3 then
failwith "Calling C# generic param array method gave unexpected result"
if CSParamArray.Method("1", "2", "3") <> CSParamArray.Method([|"1"; "2"; "3"|]) then
failwith "Calling C# generic param array in normal and expanded method gave unexpected result"
"""
|> withReferences [csharp]
|> compileExeAndRun
|> shouldSucceed

[<Fact>]
let ``Invalid params call`` () =
FSharp """
open CSharpAssembly
[<AttributeWithParamArray([|upcast 0|])>]
type Foo() =
override this.ToString() = "Stuff"
"""
|> withReferences [csharp]
|> asExe
|> compile
|> shouldFail
|> withDiagnostics [
(Error 13, Line 4, Col 29, Line 4, Col 37,
"The static coercion from type\n int \nto \n 'a \n involves an indeterminate type based on information prior to this program point. Static coercions are not allowed on some types. Further type annotations are needed.")
(Error 267, Line 4, Col 29, Line 4, Col 37,
"This is not a valid constant expression or custom attribute value")
]
3 changes: 3 additions & 0 deletions tests/FSharp.Test.Utilities/Compiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,9 @@ module rec Compiler =
| CS cs -> CS { cs with LangVersion = ver }
| _ -> failwith "Only supported in C#"

let withCSharpLanguageVersionPreview =
withCSharpLanguageVersion CSharpLanguageVersion.Preview

let withOutputType (outputType : CompileOutput) (cUnit: CompilationUnit) : CompilationUnit =
match cUnit with
| FS x -> FS { x with OutputType = outputType }
Expand Down
56 changes: 0 additions & 56 deletions tests/fsharpqa/Source/Misc/ConsumeParamArray.fsscript

This file was deleted.

35 changes: 0 additions & 35 deletions tests/fsharpqa/Source/Misc/E_ConsumeParamArray.fsscript

This file was deleted.

33 changes: 0 additions & 33 deletions tests/fsharpqa/Source/Misc/ParamArray.cs

This file was deleted.

3 changes: 0 additions & 3 deletions tests/fsharpqa/Source/Misc/env.lst
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
PRECMD="\$CSC_PIPE /target:library ParamArray.cs" SOURCE=ConsumeParamArray.fsscript # ConsumeParamArray.fsscript
PRECMD="\$CSC_PIPE /target:library ParamArray.cs" SOURCE=E_ConsumeParamArray.fsscript # E_ConsumeParamArray.fsscript

SOURCE=E_productioncoverage01.fs # E_productioncoverage01.fs
SOURCE=E_productioncoverage02.fs # E_productioncoverage02.fs
SOURCE=E_productioncoverage03.fs SCFLAGS="--test:ErrorRanges" # E_productioncoverage03.fs
Expand Down

0 comments on commit de6dbfe

Please sign in to comment.