-
Notifications
You must be signed in to change notification settings - Fork 861
Expand file tree
/
Copy pathTypeTestsInPatternMatching.fs
More file actions
465 lines (384 loc) · 11.3 KB
/
TypeTestsInPatternMatching.fs
File metadata and controls
465 lines (384 loc) · 11.3 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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace EmittedIL
open Xunit
open FSharp.Test.Compiler
module TypeTestsInPatternMatching =
[<Fact>]
let ``Test codegen for one column of sealed types``() =
FSharp """
module Test
let TestOneColumnOfTypeTestsWithSealedTypes(x: obj) =
match x with
| :? string -> 1
| :? int -> 2
| :? bool -> 3
| :? float -> 4
| :? char -> 5
| _ -> 6
"""
|> compile
|> shouldSucceed
|> verifyIL [
"""
.method public static int32 TestOneColumnOfTypeTestsWithSealedTypes(object x) cil managed
{
.maxstack 8
IL_0000: ldarg.0
IL_0001: isinst [runtime]System.String
IL_0006: brtrue.s IL_002a
IL_0008: ldarg.0
IL_0009: isinst [runtime]System.Int32
IL_000e: brtrue.s IL_002c
IL_0010: ldarg.0
IL_0011: isinst [runtime]System.Boolean
IL_0016: brtrue.s IL_002e
IL_0018: ldarg.0
IL_0019: isinst [runtime]System.Double
IL_001e: brtrue.s IL_0030
IL_0020: ldarg.0
IL_0021: isinst [runtime]System.Char
IL_0026: brfalse.s IL_0034
IL_0028: br.s IL_0032
IL_002a: ldc.i4.1
IL_002b: ret
IL_002c: ldc.i4.2
IL_002d: ret
IL_002e: ldc.i4.3
IL_002f: ret
IL_0030: ldc.i4.4
IL_0031: ret
IL_0032: ldc.i4.5
IL_0033: ret
IL_0034: ldc.i4.6
IL_0035: ret
}
"""
]
[<Fact>]
let ``Test codegen for two columns of sealed types``() =
FSharp """
module Test
let TestTwoColumnsOfTypeTestsWithSealedTypes(x: obj, y: obj) =
match x, y with
| :? string, :? string -> 1
| :? int, :? int -> 2
| :? bool, :? bool -> 3
| :? float, :? float -> 4
| :? char, :? char -> 5
| _ -> 6
"""
|> compile
|> shouldSucceed
|> verifyIL [
"""
.method public static int32 TestTwoColumnsOfTypeTestsWithSealedTypes(object x,
object y) cil managed
{
.maxstack 3
.locals init (string V_0)
IL_0000: ldarg.0
IL_0001: isinst [runtime]System.String
IL_0006: brfalse.s IL_0014
IL_0008: ldarg.1
IL_0009: isinst [runtime]System.String
IL_000e: stloc.0
IL_000f: ldloc.0
IL_0010: brfalse.s IL_005c
IL_0012: ldc.i4.1
IL_0013: ret
IL_0014: ldarg.0
IL_0015: isinst [runtime]System.Int32
IL_001a: brfalse.s IL_0026
IL_001c: ldarg.1
IL_001d: isinst [runtime]System.Int32
IL_0022: brfalse.s IL_005c
IL_0024: ldc.i4.2
IL_0025: ret
IL_0026: ldarg.0
IL_0027: isinst [runtime]System.Boolean
IL_002c: brfalse.s IL_0038
IL_002e: ldarg.1
IL_002f: isinst [runtime]System.Boolean
IL_0034: brfalse.s IL_005c
IL_0036: ldc.i4.3
IL_0037: ret
IL_0038: ldarg.0
IL_0039: isinst [runtime]System.Double
IL_003e: brfalse.s IL_004a
IL_0040: ldarg.1
IL_0041: isinst [runtime]System.Double
IL_0046: brfalse.s IL_005c
IL_0048: ldc.i4.4
IL_0049: ret
IL_004a: ldarg.0
IL_004b: isinst [runtime]System.Char
IL_0050: brfalse.s IL_005c
IL_0052: ldarg.1
IL_0053: isinst [runtime]System.Char
IL_0058: brfalse.s IL_005c
IL_005a: ldc.i4.5
IL_005b: ret
IL_005c: ldc.i4.6
IL_005d: ret
}
"""
]
[<Fact>]
let ``Test codegen for two columns of sealed types with bind``() =
FSharp """
module Test
let TestTwoColumnsOfTypeTestsWithSealedTypes(x: obj, y: obj) =
match x, y with
| :? string as s1, (:? string as s2) -> s1.Length + s2.Length
| :? int as i1, (:? int as i2) -> i1 + i2
| :? bool as b1, (:? bool as b2) -> (if b1 then 1 else 0) + (if b2 then 1 else 0)
| :? float as f1, (:? float as f2) -> int f2 + int f2
| :? char as c1, (:? char as c2) -> int c1 + int c2
| _ -> 6
"""
|> compile
|> shouldSucceed
|> verifyIL [
"""
.method public static int32 TestTwoColumnsOfTypeTestsWithSealedTypes(object x,
object y) cil managed
{
.maxstack 4
.locals init (string V_0,
string V_1,
string V_2,
int32 V_3,
int32 V_4,
bool V_5,
bool V_6,
float64 V_7,
float64 V_8,
char V_9,
char V_10)
IL_0000: ldarg.0
IL_0001: isinst [runtime]System.String
IL_0006: brfalse.s IL_002c
IL_0008: ldarg.1
IL_0009: isinst [runtime]System.String
IL_000e: stloc.0
IL_000f: ldloc.0
IL_0010: brfalse IL_00d8
IL_0015: ldloc.0
IL_0016: stloc.1
IL_0017: ldarg.0
IL_0018: unbox.any [runtime]System.String
IL_001d: stloc.2
IL_001e: ldloc.2
IL_001f: callvirt instance int32 [runtime]System.String::get_Length()
IL_0024: ldloc.1
IL_0025: callvirt instance int32 [runtime]System.String::get_Length()
IL_002a: add
IL_002b: ret
IL_002c: ldarg.0
IL_002d: isinst [runtime]System.Int32
IL_0032: brfalse.s IL_0053
IL_0034: ldarg.1
IL_0035: isinst [runtime]System.Int32
IL_003a: brfalse IL_00d8
IL_003f: ldarg.1
IL_0040: unbox.any [runtime]System.Int32
IL_0045: stloc.3
IL_0046: ldarg.0
IL_0047: unbox.any [runtime]System.Int32
IL_004c: stloc.s V_4
IL_004e: ldloc.s V_4
IL_0050: ldloc.3
IL_0051: add
IL_0052: ret
IL_0053: ldarg.0
IL_0054: isinst [runtime]System.Boolean
IL_0059: brfalse.s IL_0088
IL_005b: ldarg.1
IL_005c: isinst [runtime]System.Boolean
IL_0061: brfalse IL_00d8
IL_0066: ldarg.1
IL_0067: unbox.any [runtime]System.Boolean
IL_006c: stloc.s V_5
IL_006e: ldarg.0
IL_006f: unbox.any [runtime]System.Boolean
IL_0074: stloc.s V_6
IL_0076: ldloc.s V_6
IL_0078: brfalse.s IL_007d
IL_007a: ldc.i4.1
IL_007b: br.s IL_007e
IL_007d: ldc.i4.0
IL_007e: ldloc.s V_5
IL_0080: brfalse.s IL_0085
IL_0082: ldc.i4.1
IL_0083: br.s IL_0086
IL_0085: ldc.i4.0
IL_0086: add
IL_0087: ret
IL_0088: ldarg.0
IL_0089: isinst [runtime]System.Double
IL_008e: brfalse.s IL_00b0
IL_0090: ldarg.1
IL_0091: isinst [runtime]System.Double
IL_0096: brfalse.s IL_00d8
IL_0098: ldarg.1
IL_0099: unbox.any [runtime]System.Double
IL_009e: stloc.s V_7
IL_00a0: ldarg.0
IL_00a1: unbox.any [runtime]System.Double
IL_00a6: stloc.s V_8
IL_00a8: ldloc.s V_7
IL_00aa: conv.i4
IL_00ab: ldloc.s V_7
IL_00ad: conv.i4
IL_00ae: add
IL_00af: ret
IL_00b0: ldarg.0
IL_00b1: isinst [runtime]System.Char
IL_00b6: brfalse.s IL_00d8
IL_00b8: ldarg.1
IL_00b9: isinst [runtime]System.Char
IL_00be: brfalse.s IL_00d8
IL_00c0: ldarg.1
IL_00c1: unbox.any [runtime]System.Char
IL_00c6: stloc.s V_9
IL_00c8: ldarg.0
IL_00c9: unbox.any [runtime]System.Char
IL_00ce: stloc.s V_10
IL_00d0: ldloc.s V_10
IL_00d2: conv.i4
IL_00d3: ldloc.s V_9
IL_00d5: conv.i4
IL_00d6: add
IL_00d7: ret
IL_00d8: ldc.i4.6
IL_00d9: ret
}
"""
]
[<Fact>]
let ``Test codegen for empty string pattern with null safety``() =
FSharp """
module Test
let TestEmptyStringPattern(x: string) =
match x with
| "" -> "empty"
| _ -> "other"
"""
|> compile
|> shouldSucceed
|> verifyIL [
"""
.method public static string TestEmptyStringPattern(string x) cil managed
{
.maxstack 8
IL_0000: ldarg.0
IL_0001: brfalse.s IL_0011
IL_0003: ldarg.0
IL_0004: callvirt instance int32 [runtime]System.String::get_Length()
IL_0009: brtrue.s IL_0011
IL_000b: ldstr "empty"
IL_0010: ret
IL_0011: ldstr "other"
IL_0016: ret
}
"""
]
// https://github.com/dotnet/fsharp/issues/19873
// Side effect of moving the empty-string optimization to the Optimizer (was previously only
// applied for `match`): `if s = ""` now produces the same null-safe length-check IL.
[<Fact>]
let ``Test codegen for if-then-else with literal empty-string equality``() =
FSharp """
module Test
let TestEmptyStringEq(x: string) =
if x = "" then "empty" else "other"
"""
|> compile
|> shouldSucceed
|> verifyIL [
"""
.method public static string TestEmptyStringEq(string x) cil managed
{
.maxstack 8
IL_0000: ldarg.0
IL_0001: brfalse.s IL_0011
IL_0003: ldarg.0
IL_0004: callvirt instance int32 [runtime]System.String::get_Length()
IL_0009: brtrue.s IL_0011
IL_000b: ldstr "empty"
IL_0010: ret
IL_0011: ldstr "other"
IL_0016: ret
}
"""
]
// #19873: under `--optimize-` `(=)` isn't inlined (only when LocalOptimizationsEnabled),
// so the call falls through to `String.Equals(s, "")` instead of the null+Length form.
[<Fact>]
let ``Test codegen for empty string pattern under --optimize-``() =
FSharp """
module Test
let TestEmptyStringPattern(x: string) =
match x with
| "" -> "empty"
| _ -> "other"
"""
|> withNoOptimize
|> compile
|> shouldSucceed
|> verifyIL [
"""
.method public static string TestEmptyStringPattern(string x) cil managed
{
.maxstack 4
.locals init (string V_0)
IL_0000: ldarg.0
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: ldstr ""
IL_0008: call bool [netstandard]System.String::Equals(string,
string)
IL_000d: brfalse.s IL_0015
IL_000f: ldstr "empty"
IL_0014: ret
IL_0015: ldstr "other"
IL_001a: ret
}
"""
]
// #19873: the optimizer doesn't see BuildSwitch's `isNullFiltered` flag, so the empty-string
// branch under `null | "" -> _` re-emits its own null check (two back-to-back `brfalse.s`).
[<Fact>]
let ``Test codegen for null-or-empty-string pattern``() =
FSharp """
module Test
let TestNullOrEmpty(x: string) =
match x with
| null | "" -> "empty"
| _ -> "other"
"""
|> compile
|> shouldSucceed
|> verifyIL [
"""
.method public static string TestNullOrEmpty(string x) cil managed
{
.maxstack 8
IL_0000: ldarg.0
IL_0001: brfalse.s IL_0014
IL_0003: ldarg.0
IL_0004: brfalse.s IL_0011
IL_0006: ldarg.0
IL_0007: callvirt instance int32 [runtime]System.String::get_Length()
IL_000c: ldc.i4.0
IL_000d: ceq
IL_000f: br.s IL_0012
IL_0011: ldc.i4.0
IL_0012: brfalse.s IL_001a
IL_0014: ldstr "empty"
IL_0019: ret
IL_001a: ldstr "other"
IL_001f: ret
}
"""
]