diff options
author | Sebastian Graf <sebastian.graf@kit.edu> | 2020-02-20 16:48:16 +0100 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2020-02-27 16:22:45 -0500 |
commit | 74311e10bbb6ced9cd3093c8949f2535a715d8f6 (patch) | |
tree | 28ce759f7f4699dac70c24e32b303a4fa77dc198 /testsuite/tests/pmcheck | |
parent | 817f93eac4d13f680e8e3e7a25eb403b1864f82e (diff) | |
download | haskell-74311e10bbb6ced9cd3093c8949f2535a715d8f6.tar.gz |
PmCheck: Implement Long-distance information with Covered sets
Consider
```hs
data T = A | B | C
f :: T -> Int
f A = 1
f x = case x of
A -> 2
B -> 3
C -> 4
```
Clearly, the RHS returning 2 is redundant. But we don't currently see
that, because our approximation to the covered set of the inner case
expression just picks up the positive information from surrounding
pattern matches. It lacks the context sensivity that `x` can't be `A`
anymore!
Therefore, we adopt the conceptually and practically superior approach
of reusing the covered set of a particular GRHS from an outer pattern
match. In this case, we begin checking the `case` expression with the
covered set of `f`s second clause, which encodes the information that
`x` can't be `A` anymore. After this MR, we will successfully warn about
the RHS returning 2 being redundant.
Perhaps surprisingly, this was a great simplification to the code of
both the coverage checker and the desugarer.
Found a redundant case alternative in `unix` submodule, so we have to
bump it with a fix.
Metric Decrease:
T12227
Diffstat (limited to 'testsuite/tests/pmcheck')
3 files changed, 16 insertions, 0 deletions
diff --git a/testsuite/tests/pmcheck/should_compile/LongDistanceInfo.hs b/testsuite/tests/pmcheck/should_compile/LongDistanceInfo.hs new file mode 100644 index 0000000000..d38e2afb0d --- /dev/null +++ b/testsuite/tests/pmcheck/should_compile/LongDistanceInfo.hs @@ -0,0 +1,10 @@ +module Lib where + +data T = A | B | C + +f :: T -> Int +f A = 1 +f x = case x of + A -> 2 + B -> 3 + C -> 4 diff --git a/testsuite/tests/pmcheck/should_compile/LongDistanceInfo.stderr b/testsuite/tests/pmcheck/should_compile/LongDistanceInfo.stderr new file mode 100644 index 0000000000..dd40d2af6f --- /dev/null +++ b/testsuite/tests/pmcheck/should_compile/LongDistanceInfo.stderr @@ -0,0 +1,4 @@ + +LongDistanceInfo.hs:8:3: warning: [-Woverlapping-patterns (in -Wdefault)] + Pattern match is redundant + In a case alternative: A -> ... diff --git a/testsuite/tests/pmcheck/should_compile/all.T b/testsuite/tests/pmcheck/should_compile/all.T index dcb9ca4081..6295fa77ab 100644 --- a/testsuite/tests/pmcheck/should_compile/all.T +++ b/testsuite/tests/pmcheck/should_compile/all.T @@ -149,6 +149,8 @@ test('CaseOfKnownCon', [], compile, ['-fwarn-incomplete-patterns -fwarn-overlapping-patterns']) test('TooManyDeltas', [], compile, ['-fmax-pmcheck-models=0 -fwarn-incomplete-patterns -fwarn-overlapping-patterns']) +test('LongDistanceInfo', [], compile, + ['-fwarn-incomplete-patterns -fwarn-overlapping-patterns']) # Series (inspired) by Luke Maranget |