dotnet / dotnet/fsharp

Duplicate cases in match statement not producing compiler warnings if grouped

Open
#3,448 8 comments 0 reactions 0 assignees View on GitHub
Area-Compiler-PatternMatching Bug
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

Writing exhaustive pattern matches is a powerful way to prevent unintended bugs at runtime.

However due to the verbosity and repetitiveness these are often grouped. However when grouped, the exhaustiveness check seems to have an issue spotting repeated cases.

#### Repro steps
```
type Foo =
| A of int list
| B of int list
| C of float list
| D of float list

let foo = function
| A _ //Note the A Case is repeated
| C _
| D _ -> []
| A l //and here
| B l -> l

or *any* grouping i.e.

let foo = function
| A _ -> []
| C _ -> []
| D _ -> []
| A l
| B l -> l
```

#### Expected behavior

I'd expect a warning about an unreachable case A (the 2nd one)

#### Actual behavior

No warning

#### Known workarounds

Workaround 1: Dont group (at all!)
```
let foo = function
| A _ -> []
| C _ -> []
| D _ -> []
| A l -> l
| B l -> l
```

Workaround 2: Wildcard (although the reason for being explicit is lost)

```
let foo = function
| A l -> l
| B l -> l
| _ -> []
```

#### Related information

OCaml reports a unreachable case in this scenario.

F# 4.1
.NET 4.7
Visual Studio 2017 & VS Code

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.