Skip to content

Commit df0330c

Browse files
committed
Add a better error message for the case of /\ on record terms or types
1 parent 53a5a3d commit df0330c

File tree

1 file changed

+70
-11
lines changed

1 file changed

+70
-11
lines changed

dhall/src/Dhall/TypeCheck.hs

Lines changed: 70 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -846,20 +846,16 @@ infer typer = loop
846846
recordTypesHaveNoFieldCollisions [] xLs' xRs'
847847
return (VConst c)
848848

849-
(_, _, VRecord _, _) -> do
849+
(VRecord _, _, _, _) -> do -- The left argument is a record term, the right argument is not. The error is in the right argument.
850850
case mk of
851-
Nothing -> die (MustCombineARecord '' l'' _L'')
852-
Just t -> die (InvalidDuplicateField t l _L'')
851+
Nothing -> die (MustCombineARecord '' r'' _R'')
852+
Just t -> die (InvalidDuplicateField t r _R'')
853853

854-
(_, _, VConst _, _) -> do
855-
case mk of
856-
Nothing -> die (MustCombineARecord '' l'' _L'')
857-
Just t -> die (InvalidDuplicateField t l _L'')
854+
(VConst _, VRecord _, _, _) -> do -- The left argument is a record type, the right argument is not. The error is in the right argument.
855+
die (CombineTypesRequiresRecordType r r'')
858856

859-
_ -> do
860-
case mk of
861-
Nothing -> die (MustCombineARecord '' r'' _R'')
862-
Just t -> die (InvalidDuplicateField t r _R'')
857+
_ -> do -- The error is in the left argument: it must be either a record term or a record type, but it is neither.
858+
die (MustCombineARecordOrRecordType '' l l'')
863859

864860

865861
CombineTypes _ l r -> do
@@ -1405,6 +1401,7 @@ data TypeMessage s a
14051401
| ListAppendMismatch (Expr s a) (Expr s a)
14061402
| MustUpdateARecord (Expr s a) (Expr s a) (Expr s a)
14071403
| MustCombineARecord Char (Expr s a) (Expr s a)
1404+
| MustCombineARecordOrRecordType Char (Expr s a) (Expr s a)
14081405
| InvalidDuplicateField Text (Expr s a) (Expr s a)
14091406
| InvalidRecordCompletion Text (Expr s a)
14101407
| CompletionSchemaMustBeARecord (Expr s a) (Expr s a)
@@ -2875,6 +2872,66 @@ prettyTypeMessage (MustCombineARecord c expression typeExpression) =
28752872
where
28762873
op = pretty c
28772874

2875+
prettyTypeMessage (MustCombineARecordOrRecordType c expression typeExpression) =
2876+
ErrorMessages {..}
2877+
where
2878+
action = "combine"
2879+
short = "You can only " <> action <> " records or record types"
2880+
2881+
hints = emptyRecordTypeHint expression
2882+
2883+
long =
2884+
"Explanation: You can " <> action <> " records or record types using the ❰" <> op <> "❱ operator, like this:\n\
2885+
\ \n\
2886+
\ \n\
2887+
\ ┌───────────────────────────────────────────┐ \n\
2888+
\ │ { foo = 1, bar = \"ABC\" } " <> op <> " { baz = True } │ \n\
2889+
\ └───────────────────────────────────────────┘ \n\
2890+
\ \n\
2891+
\ \n\
2892+
\ ┌───────────────────────────────────────────┐ \n\
2893+
\ │ { foo : Bool, bar : Text } " <> op <> " { baz : Bool } │ \n\
2894+
\ └───────────────────────────────────────────┘ \n\
2895+
\ \n\
2896+
\ \n\
2897+
\... but you cannot " <> action <> " values that are neither records nor record types.\n\
2898+
\ \n\
2899+
\For example, the following expressions are " <> _NOT <> " valid: \n\
2900+
\ \n\
2901+
\ \n\
2902+
\ ┌──────────────────────────────┐ \n\
2903+
\ │ 1 " <> op <> " { foo = 1, bar = \"ABC\" } │ \n\
2904+
\ └──────────────────────────────┘ \n\
2905+
\ ⇧ \n\
2906+
\ Invalid: This is not a record and not a record type \n\
2907+
\ \n\
2908+
\ \n\
2909+
\ ┌───────────────────────────────────────────┐ \n\
2910+
\ │ { foo = 1, bar = \"ABC\" } " <> op <> " { baz : Bool } │ \n\
2911+
\ └───────────────────────────────────────────┘ \n\
2912+
\ ⇧ \n\
2913+
\ Invalid: This is a record type and not a record\n\
2914+
\ \n\
2915+
\ \n\
2916+
\ ┌───────────────────────────────────────────┐ \n\
2917+
\ │ < baz : Bool > " <> op <> " { foo = 1, bar = \"ABC\" } │ \n\
2918+
\ └───────────────────────────────────────────┘ \n\
2919+
\ ⇧ \n\
2920+
\ Invalid: This is a union type and not a record \n\
2921+
\ \n\
2922+
\ \n\
2923+
\────────────────────────────────────────────────────────────────────────────────\n\
2924+
\ \n\
2925+
\You supplied this expression as one of the arguments: \n\
2926+
\ \n\
2927+
\" <> insert expression <> "\n\
2928+
\ \n\
2929+
\... which is not a record or a record type, but is actually a: \n\
2930+
\ \n\
2931+
\" <> insert typeExpression <> "\n"
2932+
where
2933+
op = pretty c
2934+
28782935
prettyTypeMessage (InvalidDuplicateField k expr0 expr1) =
28792936
ErrorMessages {..}
28802937
where
@@ -4921,6 +4978,8 @@ messageExpressions f m = case m of
49214978
MustUpdateARecord <$> f a <*> f b <*> f c
49224979
MustCombineARecord a b c ->
49234980
MustCombineARecord <$> pure a <*> f b <*> f c
4981+
MustCombineARecordOrRecordType a b c ->
4982+
MustCombineARecordOrRecordType <$> pure a <*> f b <*> f c
49244983
InvalidRecordCompletion a l ->
49254984
InvalidRecordCompletion a <$> f l
49264985
CompletionSchemaMustBeARecord l r ->

0 commit comments

Comments
 (0)