on master help sign in
Add hints for type errors
9b0c310Evan Czaplicki
src/Reporting/Error/Type.hs+30−4
@@ -3,21 +3,23 @@ module Reporting.Error.Type where
3
3
4--import Data.Aeson ((.=))
4--import Data.Aeson ((.=))
5import qualified Data.Aeson as Json
5import qualified Data.Aeson as Json
6+import qualified Text.PrettyPrint as P
6
7
7import qualified AST.Type as T
8import qualified AST.Type as T
9+import qualified Reporting.PrettyPrint as P
8import qualified Reporting.Region as R
10import qualified Reporting.Region as R
9
11
10
12
11data Error
13data Error
12 = Mismatch Mismatch
14 = Mismatch Mismatch
13− | InfiniteType R.Region
15+ | InfiniteType
14 | BadMain T.Canonical
16 | BadMain T.Canonical
15
17
16
18
17data Mismatch = MismatchInfo
19data Mismatch = MismatchInfo
18− { hint :: Maybe String
20+ { _hint :: Maybe String
19− , leftType :: T.Canonical
21+ , _leftType :: T.Canonical
20− , rightType :: T.Canonical
22+ , _rightType :: T.Canonical
21 }
23 }
22
24
23
25
@@ -36,3 +38,27 @@ toJson err =
36 case err of
38 case err of
37 _ -> error "Type.toJson"
39 _ -> error "Type.toJson"
38
40
41+
42+-- HINTS
43+
44+toHint :: Error -> String
45+toHint err =
46+ case err of
47+ Mismatch (MismatchInfo maybeHint leftType rightType) ->
48+ maybe "This expression is triggering a type mismatch." id maybeHint
49+ ++ "\n\n"
50+ ++ "I am trying to infer the type, but I am getting conflicting information.\n"
51+ ++ "Some evidence suggests in has the type:\n\n"
52+ ++ P.render (P.nest 4 (P.pretty False leftType))
53+ ++ "\n\nBut other evidence suggests:\n\n"
54+ ++ P.render (P.nest 4 (P.pretty False rightType))
55+
56+ InfiniteType ->
57+ error "TODO"
58+
59+ BadMain tipe ->
60+ "The 'main' value has an unsupported type:\n\n"
61+ ++ P.render (P.nest 4 (P.pretty False tipe))
62+ ++ "\n\n"
63+ ++ "Please give me an Element, Html, (Signal Element), or (Signal Html) so\n"
64+ ++ "that I can render it on screen!"
src/Type/Unify.hs+4−4
@@ -119,7 +119,7 @@ actuallyUnify variable1 variable2 = do
119 where
119 where
120 msg =
120 msg =
121 "Looks like you want something comparable, but the only valid comparable\n\
121 "Looks like you want something comparable, but the only valid comparable\n\
122− \types are Int, Float, Char, String, lists, or tuples."
122+ \types are Int, Float, Char, String, lists, and tuples."
123
123
124 appendableError maybe =
124 appendableError maybe =
125 typeError (Just $ Maybe.fromMaybe msg maybe) variable1 variable2
125 typeError (Just $ Maybe.fromMaybe msg maybe) variable1 variable2
@@ -352,10 +352,10 @@ fieldMismatchError missingFields =
352 case Map.keys missingFields of
352 case Map.keys missingFields of
353 [] -> ""
353 [] -> ""
354 [key] ->
354 [key] ->
355− "Looks like a record is missing the field '" ++ key ++ "'.\n " ++
355+ "Looks like a record is missing the field '" ++ key ++ "'.\n"
356− "Maybe there is a misspelling in a record access or record update?"
356+ ++ "Maybe there is a misspelling?"
357 keys ->
357 keys ->
358− "Looks like one record is missing fields "
358+ "Looks like a record is missing fields "
359 ++ List.intercalate ", " (init keys) ++ ", and " ++ last keys
359 ++ List.intercalate ", " (init keys) ++ ", and " ++ last keys
360
360
361
361