Skip to content

Commit a373efb

Browse files
TIHanKevinRansom
authored andcommitted
Fixed consuming CSharp interface with an inref parameter (#8287)
* Added inref interop test * Fixed inref interop when creating a slot sig * 3.1 to 3.0
1 parent bb46e37 commit a373efb

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

src/fsharp/infos.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ type MethInfo =
15361536
let formalRetTy = ImportReturnTypeFromMetadata amap m ilminfo.RawMetadata.Return.Type ilminfo.RawMetadata.Return.CustomAttrs ftinfo.ILScopeRef ftinfo.TypeInstOfRawMetadata formalMethTyparTys
15371537
let formalParams =
15381538
[ [ for p in ilminfo.RawMetadata.Parameters do
1539-
let paramType = ImportILTypeFromMetadata amap m ftinfo.ILScopeRef ftinfo.TypeInstOfRawMetadata formalMethTyparTys p.Type
1539+
let paramType = ImportILTypeFromMetadataWithAttributes amap m ftinfo.ILScopeRef ftinfo.TypeInstOfRawMetadata formalMethTyparTys p.Type p.CustomAttrs
15401540
yield TSlotParam(p.Name, paramType, p.IsIn, p.IsOut, p.IsOptional, []) ] ]
15411541
formalRetTy, formalParams
15421542
#if !NO_EXTENSIONTYPING

tests/fsharp/Compiler/CompilerAssert.fs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ open System.Diagnostics
88
open System.IO
99
open System.Text
1010
open System.Diagnostics
11+
open System.Collections.Generic
1112
open System.Reflection
1213
open FSharp.Compiler.Text
1314
open FSharp.Compiler.SourceCodeServices
@@ -17,6 +18,9 @@ open System.Runtime.Loader
1718
#endif
1819
open NUnit.Framework
1920
open System.Reflection.Emit
21+
open Microsoft.CodeAnalysis
22+
open Microsoft.CodeAnalysis.CSharp
23+
open FSharp.Compiler.UnitTests.Utilities
2024

2125
[<Sealed>]
2226
type ILVerifier (dllFilePath: string) =
@@ -52,12 +56,18 @@ type CompileOutput =
5256
| Library
5357
| Exe
5458

55-
type CompilationReference = private CompilationReference of Compilation * staticLink: bool with
59+
type CompilationReference =
60+
private
61+
| CompilationReference of Compilation * staticLink: bool
62+
| TestCompilationReference of TestCompilation
5663

5764
static member CreateFSharp(cmpl: Compilation, ?staticLink) =
5865
let staticLink = defaultArg staticLink false
5966
CompilationReference(cmpl, staticLink)
6067

68+
static member Create(cmpl: TestCompilation) =
69+
TestCompilationReference cmpl
70+
6171
and Compilation = private Compilation of string * SourceKind * CompileOutput * options: string[] * CompilationReference list with
6272

6373
static member Create(source, sourceKind, output, ?options, ?cmplRefs) =
@@ -257,7 +267,14 @@ let main argv = 0"""
257267
|> List.map (fun cmpl ->
258268
match cmpl with
259269
| CompilationReference (cmpl, staticLink) ->
260-
compileCompilationAux disposals ignoreWarnings cmpl, staticLink)
270+
compileCompilationAux disposals ignoreWarnings cmpl, staticLink
271+
| TestCompilationReference (cmpl) ->
272+
let tmp = Path.GetTempFileName()
273+
disposals.Add({ new IDisposable with
274+
member _.Dispose() =
275+
try File.Delete tmp with | _ -> () })
276+
cmpl.EmitAsFile tmp
277+
(([||], tmp), []), false)
261278

262279
let compilationRefs =
263280
compiledRefs

tests/fsharp/Compiler/Language/ByrefTests.fs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace FSharp.Compiler.UnitTests
44

55
open NUnit.Framework
66
open FSharp.Compiler.SourceCodeServices
7+
open FSharp.Compiler.UnitTests.Utilities
78

89
[<TestFixture>]
910
module ByrefTests =
@@ -195,4 +196,44 @@ let test1 () =
195196
"The value has been copied to ensure the original is not mutated by this operation or because the copy is implicit when returning a struct from a member and another member is then accessed"
196197
)
197198
|]
199+
#endif
200+
201+
#if NETCOREAPP
202+
[<Test>]
203+
let ``Consume CSharp interface with a method that has a readonly byref`` () =
204+
let cs =
205+
"""
206+
using System;
207+
using System.Buffers;
208+
209+
namespace Example
210+
{
211+
public interface IMessageReader
212+
{
213+
bool TryParseMessage(in byte input);
214+
}
215+
}
216+
"""
217+
let fs =
218+
"""
219+
module Module1
220+
221+
open Example
222+
223+
type MyClass() =
224+
225+
interface IMessageReader with
226+
member this.TryParseMessage(input: inref<byte>): bool =
227+
failwith "Not Implemented"
228+
"""
229+
230+
let csCmpl =
231+
CompilationUtil.CreateCSharpCompilation(cs, CSharpLanguageVersion.CSharp8, TargetFramework.NetCoreApp30)
232+
|> CompilationReference.Create
233+
234+
let fsCmpl =
235+
Compilation.Create(fs, Fsx, Library, cmplRefs = [csCmpl])
236+
237+
CompilerAssert.Compile fsCmpl
238+
198239
#endif

tests/fsharp/Compiler/Utilities.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ type CompilationUtil private () =
120120
[ CSharpSyntaxTree.ParseText (source, CSharpParseOptions lv) ],
121121
references.As<MetadataReference>().AddRange additionalReferences,
122122
CSharpCompilationOptions (OutputKind.DynamicallyLinkedLibrary))
123-
Some (TestCompilation.CSharp (c, flags))
123+
TestCompilation.CSharp (c, flags)
124124

125125
static member CreateILCompilation (source: string) =
126126
let compute =
@@ -139,4 +139,4 @@ type CompilationUtil private () =
139139
try File.Delete ilFilePath with | _ -> ()
140140
try File.Delete tmp with | _ -> ()
141141
try File.Delete dllFilePath with | _ -> ()
142-
Some (TestCompilation.IL (source, compute))
142+
TestCompilation.IL (source, compute)

0 commit comments

Comments
 (0)