@@ -117,6 +117,7 @@ import Unison.Typechecker.TypeVar qualified as TypeVar
117117import Unison.Typechecker.Variance (Variance (.. ), defaultVariances )
118118import Unison.Var (Var )
119119import Unison.Var qualified as Var
120+ import Witherable qualified as Wither
120121
121122type 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
509516errorTerms :: ErrorNote v loc -> [Term v loc ]
@@ -1732,7 +1739,22 @@ checkPattern ::
17321739checkPattern tx ty | (debugEnabled || debugPatternsEnabled) && traceShow (" checkPattern" :: String , tx , ty ) False = undefined
17331740checkPattern 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
0 commit comments