dotnet / dotnet/fsharp

Better warning messages on quotations of "large" disjunctive pattern matches

Open
#7,589 4 comments 0 reactions 1 assignee Claimed by @TIHan View on GitHub
Area-Quotations Bug Impact-Medium
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

With
F# Interactive version 10.6.0.0 for F# 4.7 and
F# Interactive version 10.5.0.0 for F# 4.6

```fsharp
type MyType =
{
A : bool
B : double
C : double
}
let (|Q|_|) (x : double) : unit option = None
let q =
<@@
match None, None, Unchecked.defaultof<_> with
| None, Some p, {A = true; B = Q; C = Q} -> ()
| None, Some p, {A = true; B = x; C = Q} -> ()
| None, Some p, {A = true; B = Q; C = x} -> ()
| Some o, None, {A = true; B = Q; C = Q} -> ()
| Some o, None, {A = true; B = x; C = Q} -> ()
| Some o, None, {A = true; B = x; C = Q} -> ()
| Some o, None, {A = true; B = Q; C = x} -> ()
| Some o, None, {A = true; B = Q; C = x} -> ()
| _ -> printfn ""
()
@@>

```

Produces the error:

```
error FS0193: internal error: Data size must be > 0 and < 0x3f0000
```

This also compiles fine to a 23MB release build binary. It seems the `Expr` expansion of a match is not feasible. Each condition is causing a full branch which includes all cases below it. For example consider:

```fsharp

open FSharp.Quotations

let count n q =
let rec loop q =
match q with
| Patterns.Value((:? uint64 as x),_) when x = n -> 1
| ExprShape.ShapeCombination(a, args) -> args |> List.sumBy loop
| ExprShape.ShapeLambda(v, body) -> loop body
| ExprShape.ShapeVar(v) -> 0
loop q

<@
match None, Unchecked.defaultof<_> with
| None, {A = true; B = Q; C = Q} -> 1UL
| _ -> 100UL
@>
|> count 100UL
```

Here the `100UL` branch gets counted 4 times, With

```fsharp

<@
match None, Unchecked.defaultof<_> with
| None, {A = true; B = Q; C = Q} -> 1UL
| None, {A = true; B = Q; C = Q} -> 2UL
| _ -> 100UL
@>
```

the `2UL` branch now counts to 4 and the `100UL` branch is now at 16. This quickly explodes with relatively simple match blocks.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.