-
Notifications
You must be signed in to change notification settings - Fork 864
Expand file tree
/
Copy pathFSharpQuotations.fs
More file actions
362 lines (293 loc) · 13.8 KB
/
Copy pathFSharpQuotations.fs
File metadata and controls
362 lines (293 loc) · 13.8 KB
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
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
// Various tests for Microsoft.FSharp.Quotations
namespace FSharp.Core.UnitTests.Quotations
open System
open FSharp.Core.UnitTests
open FSharp.Core.UnitTests.Collections
open FSharp.Core.UnitTests.LibraryTestFx
open Xunit
open FSharp.Quotations
open FSharp.Quotations.Patterns
open FSharp.Linq.RuntimeHelpers
type E = Microsoft.FSharp.Quotations.Expr;;
type StaticIndexedPropertyTest() =
static member IdxProp with get (n : int) = n + 1
type QuotationEnum =
| A = 1
| B = 2
module Check =
let argumentException f =
let mutable ex = false
try
f () |> ignore
with
| :? System.ArgumentException-> ex <- true
Assert.True(ex, "InvalidOperationException expected")
type FSharpQuotationsTests() =
[<Fact>]
member x.MethodInfoNRE() =
let f() =
E.Call(null, []) |> ignore
CheckThrowsArgumentNullException f
[<Fact>]
member x.FieldInfoNRE() =
let f() =
E.FieldGet(null) |> ignore
CheckThrowsArgumentNullException f
[<Fact>]
member x.ConstructorNRE() =
let f() =
E.NewObject(null,[]) |> ignore
CheckThrowsArgumentNullException f
[<Fact>]
member x.PropertyInfoNRE() =
let f() =
E.PropertyGet(null,[]) |> ignore
CheckThrowsArgumentNullException f
[<Fact>]
member x.UnionCaseInfoNRE() =
let f() =
E.NewUnionCase(Unchecked.defaultof<Microsoft.FSharp.Reflection.UnionCaseInfo>,[]) |> ignore
CheckThrowsArgumentNullException f
[<Fact>]
member x.ReShapeTypechecking_Let() =
let q0 = <@ let a = 1 in a @>
match q0 with
| ExprShape.ShapeCombination(shape, [value;lambda]) ->
let goodValue = <@ 2 @>
ExprShape.RebuildShapeCombination(shape, [goodValue;lambda]) |> ignore
| _ -> Assert.Fail()
let q1 = <@ let a = 1 in a @>
match q1 with
| ExprShape.ShapeCombination(shape, [value;lambda]) ->
let wrongValue = <@ "!" @>
Check.argumentException(fun () -> ExprShape.RebuildShapeCombination(shape, [wrongValue;lambda]))
| _ -> Assert.Fail()
[<Fact>]
member x.ReShapeStaticIndexedProperties() =
let q0 = <@ StaticIndexedPropertyTest.IdxProp 5 @>
match q0 with
| ExprShape.ShapeCombination(shape, args) ->
try
ExprShape.RebuildShapeCombination(shape, args) |> ignore
with
| _ -> Assert.Fail()
| _ -> Assert.Fail()
[<Fact>]
member x.GetConstructorFiltersOutStaticConstructor() =
ignore <@ System.Exception() @>
[<Fact>]
member x.``NewStructTuple literal should be recognized by NewStructTuple active pattern`` () =
match <@ struct(1, "") @> with
| NewStructTuple [ Value(:? int as i, _) ; Value(:? string as s, _) ] when i = 1 && s = "" -> ()
| _ -> Assert.Fail()
[<Fact>]
member x.``NewStructTuple literal should be recognized by NewTuple active pattern`` () =
match <@ struct(1, "") @> with
| NewTuple [ Value(:? int as i, _) ; Value(:? string as s, _) ] when i = 1 && s = "" -> ()
| _ -> Assert.Fail()
[<Fact>]
member x.``Quotation of an enum value preserves the enum type`` () =
// Related to https://github.com/dotnet/fsharp/issues/995: an enum literal is quoted as a
// Value node carrying the enum type, not the bare underlying integer.
match <@ QuotationEnum.B @> with
| Value(v, t) ->
Assert.Equal(typeof<QuotationEnum>, t)
Assert.Equal(box QuotationEnum.B, v)
| _ -> Assert.Fail()
[<Fact>]
member x.``NewTuple literal should not be recognized by NewStructTuple active pattern`` () =
match <@ (1, "") @> with
| NewStructTuple _ -> Assert.Fail()
| _ -> ()
[<Fact>]
member x.``NewStructTuple should be recognized by NewStructTuple active pattern`` () =
let expr = Expr.NewStructTuple(typeof<struct(_ * _)>.Assembly, [ <@@ 1 @@>; <@@ "" @@> ])
match expr with
| NewStructTuple [ Value(:? int as i, _) ; Value(:? string as s, _) ] when i = 1 && s = "" -> ()
| _ -> Assert.Fail()
[<Fact>]
member x.``NewStructTuple should be recognized by NewTuple active pattern`` () =
let expr = Expr.NewStructTuple(typeof<struct(_ * _)>.Assembly, [ <@@ 1 @@>; <@@ "" @@> ])
match expr with
| NewTuple [ Value(:? int as i, _) ; Value(:? string as s, _) ] when i = 1 && s = "" -> ()
| _ -> Assert.Fail()
[<Fact>]
member x.``NewStructTuple without assembly should be recognized by NewStructTuple active pattern`` () =
let expr = Expr.NewStructTuple([ <@@ 1 @@>; <@@ "" @@> ])
match expr with
| NewStructTuple [ Value(:? int as i, _) ; Value(:? string as s, _) ] when i = 1 && s = "" -> ()
| _ -> Assert.Fail()
[<Fact>]
member x.``NewStructTuple without assembly should be recognized by NewTuple active pattern`` () =
let expr = Expr.NewStructTuple([ <@@ 1 @@>; <@@ "" @@> ])
match expr with
| NewTuple [ Value(:? int as i, _) ; Value(:? string as s, _) ] when i = 1 && s = "" -> ()
| _ -> Assert.Fail()
[<Fact>]
member x.``NewTuple should not be recognized by NewStructTuple active pattern`` () =
let expr = Expr.NewTuple [ <@@ 1 @@>; <@@ "" @@> ]
match expr with
| NewStructTuple _ -> Assert.Fail()
| _ -> ()
/// This fixture is here to test handling of EqualityConditionalOn and ComparisonConditionalOn.
/// We don't generate witnesses for equality and comparison if they're conditional; the tests
/// assert that code gen doesn't fail in those cases.
[<RequireQualifiedAccess>]
module TestConditionalConstraints =
open FSharp.Linq.RuntimeHelpers
let eval q = LeafExpressionConverter.EvaluateQuotation q
type DiscriminatedUnionWithGeneric<'a> =
| Case of 'a
[<NoComparison>]
type ThingWithNoComparison =
| NoComparison
[<NoEquality ; NoComparison>]
type ThingWithNoEquality =
| NoEquality
override this.ToString () =
"NoEquality"
let inline compare< ^T when ^T : comparison> (x : ^T) (y : ^T) : bool =
x < y
let inline equate< ^T when ^T : equality> (x : ^T) (y : ^T) : bool =
x = y
[<Fact>]
let ``SRTP quotations can consume conditionally constrained types `` () =
// Just normal calls, no quotation
Assert.False (equate (DiscriminatedUnionWithGeneric.Case 3) (DiscriminatedUnionWithGeneric.Case 4))
Assert.True (equate (DiscriminatedUnionWithGeneric.Case 3) (DiscriminatedUnionWithGeneric.Case 3))
Assert.True (compare (DiscriminatedUnionWithGeneric.Case 3) (DiscriminatedUnionWithGeneric.Case 4))
// Typed quotation, int
<@ equate (DiscriminatedUnionWithGeneric.Case 3) (DiscriminatedUnionWithGeneric.Case 4) @>
|> eval
|> unbox<bool>
|> Assert.False
<@ equate (DiscriminatedUnionWithGeneric.Case 3) (DiscriminatedUnionWithGeneric.Case 3) @>
|> eval
|> unbox<bool>
|> Assert.True
<@ compare (DiscriminatedUnionWithGeneric.Case 3) (DiscriminatedUnionWithGeneric.Case 4) @>
|> eval
|> unbox<bool>
|> Assert.True
// Untyped quotation, int
<@@ equate (DiscriminatedUnionWithGeneric.Case 3) (DiscriminatedUnionWithGeneric.Case 4) @@>
|> eval
|> unbox<bool>
|> Assert.False
<@@ equate (DiscriminatedUnionWithGeneric.Case 3) (DiscriminatedUnionWithGeneric.Case 3) @@>
|> eval
|> unbox<bool>
|> Assert.True
<@@ compare (DiscriminatedUnionWithGeneric.Case 3) (DiscriminatedUnionWithGeneric.Case 4) @@>
|> eval
|> unbox<bool>
|> Assert.True
// Typed and untyped quotation, ThingWithNoComparison
<@ equate ThingWithNoComparison.NoComparison ThingWithNoComparison.NoComparison @>
|> eval
|> unbox<bool>
|> Assert.True
<@@ equate ThingWithNoComparison.NoComparison ThingWithNoComparison.NoComparison @@>
|> eval
|> unbox<bool>
|> Assert.True
// Typed and untyped quotation, ThingWithNoEquality
<@ (fun x -> x.ToString ()) ThingWithNoEquality.NoEquality @>
|> eval
|> unbox<string>
|> fun s -> Assert.AreEqual (s, "NoEquality")
<@@ (fun x -> x.ToString ()) ThingWithNoEquality.NoEquality @@>
|> eval
|> unbox<string>
|> fun s -> Assert.AreEqual (s, "NoEquality")
// This test isn't quotation-related, but it *is* closely related to the quotation test: both are checking
// we can cope without witnesses.
[<Fact>]
let ``Reflective invocations of conditionally constrained types throw with a reasonable error`` () =
let compare = typeof<ThingWithNoComparison>.DeclaringType.GetMethod "compare"
let compare = compare.MakeGenericMethod([| typeof<ThingWithNoComparison> |])
let exc =
try
compare.Invoke (null, [|ThingWithNoComparison.NoComparison ; ThingWithNoComparison.NoComparison|])
|> ignore<obj>
None
with
| exc ->
Some exc
Assert.Contains ("does not implement the System.IComparable interface", exc.Value.InnerException.Message, StringComparison.Ordinal)
// This test isn't quotation-related, but it *is* closely related to the quotation test: both are checking
// we can cope without witnesses.
[<Fact>]
let ``We still use Object.ReferenceEquals for non-equatable methods when reflectively invoked`` () =
let equate = typeof<ThingWithNoComparison>.DeclaringType.GetMethod "equate"
let equate = equate.MakeGenericMethod([| typeof<ThingWithNoEquality> |])
let anotherOne = Activator.CreateInstance (typeof<ThingWithNoEquality>, nonPublic=true)
equate.Invoke (null, [| ThingWithNoEquality.NoEquality ; anotherOne |])
|> unbox<bool>
|> Assert.False
// Tests for issues #11131 and #15648 - anonymous record field ordering
// When anonymous records have fields in non-alphabetical order, the LINQ expression
// should not contain Invoke patterns that LINQ providers can't translate.
[<Fact>]
let ``Anonymous record with non-alphabetical field order produces clean LINQ expression - issues 11131 and 15648`` () =
// Non-alphabetical order - B before A
let q = <@ fun (x: int) -> {| B = x; A = x + 1 |} @>
let linqExpr = LeafExpressionConverter.QuotationToExpression q
let exprStr = linqExpr.ToString()
Assert.DoesNotContain(".Invoke(", exprStr)
[<Fact>]
let ``Nested anonymous record produces clean LINQ expression`` () =
// Nested anonymous record with non-alphabetical field order
let q = <@ fun (x: int) -> {| Outer = {| B = x; A = x + 1 |} |} @>
let linqExpr = LeafExpressionConverter.QuotationToExpression q
let exprStr = linqExpr.ToString()
Assert.DoesNotContain(".Invoke(", exprStr)
[<Fact>]
let ``Both anonymous record field orders produce equivalent results`` () =
// Alphabetical order
let qAlpha = <@ fun (x: int) -> {| A = x + 1; B = x |} @>
// Non-alphabetical order
let qNonAlpha = <@ fun (x: int) -> {| B = x; A = x + 1 |} @>
let linqAlpha = LeafExpressionConverter.QuotationToExpression qAlpha
let linqNonAlpha = LeafExpressionConverter.QuotationToExpression qNonAlpha
let exprAlpha = linqAlpha.ToString()
let exprNonAlpha = linqNonAlpha.ToString()
// Neither should contain Invoke
Assert.DoesNotContain(".Invoke(", exprAlpha)
Assert.DoesNotContain(".Invoke(", exprNonAlpha)
// Tests for issue #16918 - Array indexing generates GetArray instead of proper array index expression
// When array indexing is used in LINQ expressions, it should generate proper array index
// expressions that LINQ providers can translate, not GetArray method calls.
type ArrayTestUnfold = { c: string }
type ArrayTestDoc = { p: string; u: ArrayTestUnfold[] }
[<Fact>]
let ``Array indexing produces ArrayIndex expression not GetArray - issue 16918`` () =
// Array access like x.u.[0] should NOT produce GetArray call
let q = <@ fun (x: ArrayTestDoc) -> x.u.[0] @>
let linqExpr = LeafExpressionConverter.QuotationToExpression q
let exprStr = linqExpr.ToString()
// Should NOT contain GetArray
Assert.DoesNotContain("GetArray", exprStr)
// Should produce x.u[0] style array index expression
Assert.Contains("[0]", exprStr)
[<Fact>]
let ``Nested array member access produces clean LINQ expression - issue 16918`` () =
// x.u[0].c should generate proper expression tree without GetArray
let q = <@ fun (x: ArrayTestDoc) -> x.u.[0].c @>
let linqExpr = LeafExpressionConverter.QuotationToExpression q
let exprStr = linqExpr.ToString()
// Should NOT contain GetArray
Assert.DoesNotContain("GetArray", exprStr)
// Should contain array index
Assert.Contains("[0]", exprStr)
// Should contain property access
Assert.Contains(".c", exprStr)
[<Fact>]
let ``Array indexing with variable index produces clean expression`` () =
// Array access with variable index
let q = <@ fun (x: int[]) (i: int) -> x.[i] @>
let linqExpr = LeafExpressionConverter.QuotationToExpression q
let exprStr = linqExpr.ToString()
// Should NOT contain GetArray
Assert.DoesNotContain("GetArray", exprStr)