Skip to content

Commit 4acebf0

Browse files
authored
Merge pull request #5256 from unisonweb/travis/empty-pattern-match
2 parents f559576 + 72da81f commit 4acebf0

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

parser-typechecker/src/Unison/PatternMatchCoverage.hs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ module Unison.PatternMatchCoverage
3535
)
3636
where
3737

38+
import Data.List.NonEmpty (nonEmpty)
3839
import Data.Set qualified as Set
3940
import Debug.Trace
4041
import Unison.Debug
@@ -53,34 +54,40 @@ import Unison.Util.Pretty qualified as P
5354
checkMatch ::
5455
forall vt v loc m.
5556
(Pmc vt v loc m) =>
56-
-- | the match location
57-
loc ->
5857
-- | scrutinee type
5958
Type.Type vt loc ->
6059
-- | match cases
6160
[Term.MatchCase loc (Term.Term' vt v loc)] ->
6261
-- | (redundant locations, inaccessible locations, inhabitants of uncovered refinement type)
6362
m ([loc], [loc], [Pattern ()])
64-
checkMatch matchLocation scrutineeType cases = do
63+
checkMatch scrutineeType cases = do
6564
ppe <- getPrettyPrintEnv
6665
v0 <- fresh
67-
grdtree0 <- desugarMatch matchLocation scrutineeType v0 cases
68-
doDebug (P.hang (title "desugared:") (prettyGrdTree (prettyPmGrd ppe) (\_ -> "<loc>") grdtree0)) (pure ())
69-
(uncovered, grdtree1) <- uncoverAnnotate (Set.singleton (NC.markDirty v0 $ NC.declVar v0 scrutineeType id NC.emptyNormalizedConstraints)) grdtree0
66+
mgrdtree0 <- traverse (desugarMatch scrutineeType v0) (nonEmpty cases)
67+
doDebug (P.hang (title "desugared:") (prettyGrdTreeMaybe (prettyPmGrd ppe) (\_ -> "<loc>") mgrdtree0)) (pure ())
68+
let initialUncovered = Set.singleton (NC.markDirty v0 $ NC.declVar v0 scrutineeType id NC.emptyNormalizedConstraints)
69+
(uncovered, grdtree1) <- case mgrdtree0 of
70+
Nothing -> pure (initialUncovered, Nothing)
71+
Just grdtree0 -> fmap Just <$> uncoverAnnotate initialUncovered grdtree0
7072
doDebug
7173
( P.sep
7274
"\n"
73-
[ P.hang (title "annotated:") (prettyGrdTree (NC.prettyDnf ppe) (NC.prettyDnf ppe . fst) grdtree1),
75+
[ P.hang (title "annotated:") (prettyGrdTreeMaybe (NC.prettyDnf ppe) (NC.prettyDnf ppe . fst) grdtree1),
7476
P.hang (title "uncovered:") (NC.prettyDnf ppe uncovered)
7577
]
7678
)
7779
(pure ())
7880
uncoveredExpanded <- concat . fmap Set.toList <$> traverse (expandSolution v0) (Set.toList uncovered)
7981
doDebug (P.hang (title "uncovered expanded:") (NC.prettyDnf ppe (Set.fromList uncoveredExpanded))) (pure ())
8082
let sols = map (generateInhabitants v0) uncoveredExpanded
81-
let (_accessible, inaccessible, redundant) = classify grdtree1
83+
let (_accessible, inaccessible, redundant) = case grdtree1 of
84+
Nothing -> ([], [], [])
85+
Just x -> classify x
8286
pure (redundant, inaccessible, sols)
8387
where
88+
prettyGrdTreeMaybe prettyNode prettyLeaf = \case
89+
Nothing -> "<empty>"
90+
Just x -> prettyGrdTree prettyNode prettyLeaf x
8491
title = P.bold
8592
doDebug out = case shouldDebug PatternCoverage of
8693
True -> trace (P.toAnsiUnbroken out)

parser-typechecker/src/Unison/PatternMatchCoverage/Desugar.hs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,14 @@ import Unison.Type qualified as Type
2020
desugarMatch ::
2121
forall loc vt v m.
2222
(Pmc vt v loc m) =>
23-
-- | loc of match
24-
loc ->
2523
-- | scrutinee type
2624
Type vt loc ->
2725
-- | scrutinee variable
2826
v ->
2927
-- | match cases
30-
[MatchCase loc (Term' vt v loc)] ->
28+
NonEmpty (MatchCase loc (Term' vt v loc)) ->
3129
m (GrdTree (PmGrd vt v loc) loc)
32-
desugarMatch loc0 scrutineeType v0 cs0 =
33-
traverse desugarClause cs0 >>= \case
34-
[] -> pure $ Leaf loc0
35-
x : xs -> pure $ Fork (x :| xs)
30+
desugarMatch scrutineeType v0 cs0 = Fork <$> traverse desugarClause cs0
3631
where
3732
desugarClause :: MatchCase loc (Term' vt v loc) -> m (GrdTree (PmGrd vt v loc) loc)
3833
desugarClause MatchCase {matchPattern, matchGuard} =

parser-typechecker/src/Unison/Typechecker/Context.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,7 @@ ensurePatternCoverage theMatch _theMatchType _scrutinee scrutineeType cases = do
15251525
constructorCache = mempty
15261526
}
15271527
(redundant, _inaccessible, uncovered) <- flip evalStateT pmcState do
1528-
checkMatch matchLoc scrutineeType cases
1528+
checkMatch scrutineeType cases
15291529
let checkUncovered = case Nel.nonEmpty uncovered of
15301530
Nothing -> pure ()
15311531
Just xs -> failWith (UncoveredPatterns matchLoc xs)

0 commit comments

Comments
 (0)