/
ByrefTests.fs
373 lines (312 loc) · 11.5 KB
/
ByrefTests.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace FSharp.Compiler.UnitTests
open NUnit.Framework
open FSharp.Test.Utilities
open FSharp.Test.Utilities.Utilities
open FSharp.Compiler.SourceCodeServices
open FSharp.Test.Utilities
open FSharp.Test.Utilities.Compiler
open FSharp.Tests
[<TestFixture>]
module ByrefTests =
[<Test>]
let ``No defensive copy on .NET struct`` () =
CompilerAssert.Pass
"""
open System
open System.Runtime.CompilerServices
let f (x: DateTime) = x.ToLocalTime()
let f2 () =
let x = DateTime.Now
x.ToLocalTime()
[<Extension; AbstractClass; Sealed>]
type Extensions =
[<Extension>]
static member Test(x: inref<DateTime>) = &x
[<Extension>]
static member Test2(x: byref<DateTime>) = &x
let test (x: inref<DateTime>) =
x.Test()
let test2 (x: byref<DateTime>) =
x.Test2()
let test3 (x: byref<DateTime>) =
x.Test()
let test4 () =
DateTime.Now.Test()
let test5 (x: inref<DateTime>) =
&x.Test()
let test6 () =
DateTime.Now.Test().Test().Test()
"""
[<Test>]
let ``Extension method scope errors`` () =
CompilerAssert.TypeCheckWithErrors
"""
open System
open System.Runtime.CompilerServices
[<Extension; AbstractClass; Sealed>]
type Extensions =
[<Extension>]
static member Test(x: inref<DateTime>) = &x
let f1 () =
&DateTime.Now.Test()
let f2 () =
let result =
let dt = DateTime.Now
&dt.Test()
result
let f3 () =
Extensions.Test(let dt = DateTime.Now in &dt)
let f4 () =
let dt = DateTime.Now
&Extensions.Test(&dt)
let f5 () =
&Extensions.Test(let dt = DateTime.Now in &dt)
"""
[|
(
FSharpErrorSeverity.Error,
3228,
(12, 6, 12, 25),
"The address of a value returned from the expression cannot be used at this point. This is to ensure the address of the local value does not escape its scope."
)
(
FSharpErrorSeverity.Error,
3228,
(17, 10, 17, 19),
"The address of a value returned from the expression cannot be used at this point. This is to ensure the address of the local value does not escape its scope."
)
(
FSharpErrorSeverity.Error,
3228,
(21, 5, 21, 50),
"The address of a value returned from the expression cannot be used at this point. This is to ensure the address of the local value does not escape its scope."
)
(
FSharpErrorSeverity.Error,
3228,
(25, 6, 25, 26),
"The address of a value returned from the expression cannot be used at this point. This is to ensure the address of the local value does not escape its scope."
)
(
FSharpErrorSeverity.Error,
3228,
(28, 6, 28, 51),
"The address of a value returned from the expression cannot be used at this point. This is to ensure the address of the local value does not escape its scope."
)
|]
// TODO: A better way to test the ones below are to use a custom struct in C# code that contains explicit use of their "readonly" keyword.
#if NETCOREAPP
// NETCORE makes DateTime a readonly struct; therefore, it should not error.
[<Test>]
let ``No defensive copy on .NET struct - netcore`` () =
CompilerAssert.Pass
"""
open System
let f (x: inref<DateTime>) = x.ToLocalTime()
let f2 () =
let x = DateTime.Now
let y = &x
y.ToLocalTime()
let f3 (x: inref<DateTime>) = &x
let f4 (x: inref<DateTime>) =
(f3 &x).ToLocalTime()
open System.Runtime.CompilerServices
[<Extension; AbstractClass; Sealed>]
type Extensions =
[<Extension>]
static member Test(x: inref<DateTime>) = &x
let test1 () =
DateTime.Now.Test().Date
let test2 () =
DateTime.Now.Test().Test().Date.Test().Test().Date.Test()
"""
#else
// Note: Currently this is assuming NET472. That may change which might break these tests. Consider using custom C# code.
[<Test>]
let ``Defensive copy on .NET struct for inref`` () =
CompilerAssert.TypeCheckWithErrors
"""
open System
let f (x: inref<DateTime>) = x.ToLocalTime()
let f2 () =
let x = DateTime.Now
let y = &x
y.ToLocalTime()
let f3 (x: inref<DateTime>) = &x
let f4 (x: inref<DateTime>) =
(f3 &x).ToLocalTime()
open System.Runtime.CompilerServices
[<Extension; AbstractClass; Sealed>]
type Extensions =
[<Extension>]
static member Test(x: inref<DateTime>) = &x
let test1 () =
DateTime.Now.Test().Date
"""
[|
(
FSharpErrorSeverity.Warning,
52,
(3, 30, 3, 45),
"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"
)
(
FSharpErrorSeverity.Warning,
52,
(7, 5, 7, 20),
"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"
)
(
FSharpErrorSeverity.Warning,
52,
(10, 5, 10, 26),
"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"
)
(
FSharpErrorSeverity.Warning,
52,
(20, 5, 20, 29),
"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"
)
|]
#endif
#if NETCOREAPP
[<Test>]
let ``Consume CSharp interface with a method that has a readonly byref`` () =
let cs =
"""
using System;
using System.Buffers;
namespace Example
{
public interface IMessageReader
{
bool TryParseMessage(in byte input);
}
}
"""
let fs =
"""
module Module1
open Example
type MyClass() =
interface IMessageReader with
member this.TryParseMessage(input: inref<byte>): bool =
failwith "Not Implemented"
"""
let csCmpl =
CompilationUtil.CreateCSharpCompilation(cs, CSharpLanguageVersion.CSharp8, TargetFramework.NetCoreApp31)
|> CompilationReference.Create
let fsCmpl =
Compilation.Create(fs, SourceKind.Fsx, Library, cmplRefs = [csCmpl])
CompilerAssert.Compile fsCmpl
#endif
[<Test>]
let ``Can take native address to get a nativeptr of a mutable value`` () =
CompilerAssert.Pass
"""
#nowarn "51"
let test () =
let mutable x = 1
let y = &&x
()
"""
[<Test>]
let ``Cannot take native address to get a nativeptr of an immmutable value`` () =
CompilerAssert.TypeCheckWithErrors
"""
#nowarn "51"
let test () =
let x = 1
let y = &&x
()
""" [|
(FSharpErrorSeverity.Error, 256, (6, 13, 6, 16), "A value must be mutable in order to mutate the contents or take the address of a value type, e.g. 'let mutable x = ...'")
|]
[<Test>]
let ``Returning an 'inref<_>' from a property should emit System.Runtime.CompilerServices.IsReadOnlyAttribute on the return type of the signature`` () =
let src =
"""
module Test
type C() =
let x = 59
member _.X: inref<_> = &x
"""
let verifyProperty = """.property instance int32& modreq([runtime]System.Runtime.InteropServices.InAttribute)
X()
{
.custom instance void [runtime]System.Runtime.CompilerServices.IsReadOnlyAttribute::.ctor() = ( 01 00 00 00 )
.get instance int32& modreq([runtime]System.Runtime.InteropServices.InAttribute) Test/C::get_X()
}"""
let verifyMethod = """.method public hidebysig specialname
instance int32& modreq([runtime]System.Runtime.InteropServices.InAttribute)
get_X() cil managed
{
.param [0]
.custom instance void [runtime]System.Runtime.CompilerServices.IsReadOnlyAttribute::.ctor() = ( 01 00 00 00 )"""
FSharp src
|> compile
|> verifyIL [verifyProperty;verifyMethod]
|> ignore
[<Test>]
let ``Returning an 'inref<_>' from a generic method should emit System.Runtime.CompilerServices.IsReadOnlyAttribute on the return type of the signature`` () =
let src =
"""
module Test
type C<'T>() =
let x = Unchecked.defaultof<'T>
member _.X<'U>(): inref<'T> = &x
"""
let verifyMethod = """.method public hidebysig instance !T& modreq([runtime]System.Runtime.InteropServices.InAttribute)
X<U>() cil managed
{
.param [0]
.custom instance void [runtime]System.Runtime.CompilerServices.IsReadOnlyAttribute::.ctor() = ( 01 00 00 00 )"""
FSharp src
|> compile
|> verifyIL [verifyMethod]
|> ignore
[<Test>]
let ``Returning an 'inref<_>' from an abstract generic method should emit System.Runtime.CompilerServices.IsReadOnlyAttribute on the return type of the signature`` () =
let src =
"""
module Test
[<AbstractClass>]
type C<'T>() =
abstract X<'U> : unit -> inref<'U>
"""
let verifyMethod = """.method public hidebysig abstract virtual
instance !!U& modreq([runtime]System.Runtime.InteropServices.InAttribute)
X<U>() cil managed
{
.param [0]
.custom instance void [runtime]System.Runtime.CompilerServices.IsReadOnlyAttribute::.ctor() = ( 01 00 00 00 ) """
FSharp src
|> compile
|> verifyIL [verifyMethod]
|> ignore
[<Test>]
let ``Returning an 'inref<_>' from an abstract property should emit System.Runtime.CompilerServices.IsReadOnlyAttribute on the return type of the signature`` () =
let src =
"""
module Test
type C =
abstract X: inref<int>
"""
let verifyProperty = """.property instance int32& modreq([runtime]System.Runtime.InteropServices.InAttribute)
X()
{
.custom instance void [runtime]System.Runtime.CompilerServices.IsReadOnlyAttribute::.ctor() = ( 01 00 00 00 )
.get instance int32& modreq([runtime]System.Runtime.InteropServices.InAttribute) Test/C::get_X()
}"""
let verifyMethod = """.method public hidebysig specialname abstract virtual
instance int32& modreq([runtime]System.Runtime.InteropServices.InAttribute)
get_X() cil managed
{
.param [0]
.custom instance void [runtime]System.Runtime.CompilerServices.IsReadOnlyAttribute::.ctor() = ( 01 00 00 00 )"""
FSharp src
|> compile
|> verifyIL [verifyProperty;verifyMethod]
|> ignore