Implement some pattern match type-checking errors · JavaDerek/unison@eff2dfc · GitHub
Skip to content

Commit eff2dfc

Browse files
committed
Implement some pattern match type-checking errors
1 parent a31db74 commit eff2dfc

3 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎parser-typechecker/src/Unison/PrintError.hs‎

Lines changed: 12 additions & 0 deletions

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ import Unison.Typechecker.TypeVar qualified as TypeVar
117117
import Unison.Typechecker.Variance (Variance (..), defaultVariances)
118118
import Unison.Var (Var)
119119
import Unison.Var qualified as Var
120+
import Witherable qualified as Wither
120121

121122
type TypeVar v loc = TypeVar.TypeVar (B.Blank loc) v
122123

@@ -503,7 +504,13 @@ data Cause v loc
503504
(Type v loc {- the type we expected there -})
504505
(Type v loc {- record literal missing the field -})
505506
(Type v loc {- record literal which has the type -})
506-
507+
| PatternMatchedMissingField
508+
(Text {- the field name we tried to match, but wasn't in the type -})
509+
(Pattern loc {- the place we matched on the missing field -})
510+
(Type v loc {- The type of the record which is missing the field -})
511+
| RecordPatternMatchOnNonRecordType
512+
(Pattern loc {- the place we matched on the non-record -})
513+
(Type v loc {- The type which is not a record -})
507514
deriving (Show)
508515

509516
errorTerms :: ErrorNote v loc -> [Term v loc]
@@ -1732,7 +1739,22 @@ checkPattern ::
17321739
checkPattern tx ty | (debugEnabled || debugPatternsEnabled) && traceShow ("checkPattern" :: String, tx, ty) False = undefined
17331740
checkPattern scrutineeType p =
17341741
case p of
1735-
Pattern.RecordLiteral {} -> error "Record patterns not yet implemented"
1742+
Pattern.RecordLiteral _loc fieldPatterns -> do
1743+
case scrutineeType of
1744+
Type.Record' fieldTypes -> do
1745+
vs <-
1746+
Align.align fieldTypes fieldPatterns
1747+
& Map.traverseWithKey
1748+
( \fieldName -> \case
1749+
This _ -> pure $ Nothing
1750+
-- We have a pattern, but the expected type does not.
1751+
That p -> lift . failWith $ PatternMatchedMissingField fieldName p scrutineeType
1752+
These fieldTyp fieldPat -> Just <$> checkPattern fieldTyp fieldPat
1753+
)
1754+
<&> Wither.catMaybes
1755+
pure $ fold vs
1756+
_ -> do
1757+
lift . failWith $ RecordPatternMatchOnNonRecordType p scrutineeType
17361758
Pattern.Unbound _ -> pure []
17371759
Pattern.Var loc -> do
17381760
v <- getAdvance p

‎unison-cli/src/Unison/LSP/FileAnalysis.hs‎

Lines changed: 17 additions & 0 deletions

0 commit comments

Comments
 (0)