on master help sign in
Add hints for type errors
commit9b0c310b56f02a64ae08f9590d15e68461ec6ae1 authorEvan Czaplicki <info@elm-lang.org> dateTue 5 May 2015, 11:48 (12 years ago) parent5ba597f
2 files changed+34−8
src/Reporting/Error/Type.hs+30−4 src/Type/Unify.hs+4−4
src/Reporting/Error/Type.hs+30−4
@@ -3,21 +3,23 @@ module Reporting.Error.Type where
33
44--import Data.Aeson ((.=))
55import qualified Data.Aeson as Json
6+import qualified Text.PrettyPrint as P
67
78import qualified AST.Type as T
9+import qualified Reporting.PrettyPrint as P
810import qualified Reporting.Region as R
911
1012
1113data Error
1214 = Mismatch Mismatch
13− | InfiniteType R.Region
15+ | InfiniteType
1416 | BadMain T.Canonical
1517
1618
1719data Mismatch = MismatchInfo
18− { hint :: Maybe String
19− , leftType :: T.Canonical
20− , rightType :: T.Canonical
20+ { _hint :: Maybe String
21+ , _leftType :: T.Canonical
22+ , _rightType :: T.Canonical
2123 }
2224
2325
@@ -36,3 +38,27 @@ toJson err =
3638 case err of
3739 _ -> error "Type.toJson"
3840
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
119119 where
120120 msg =
121121 "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."
123123
124124 appendableError maybe =
125125 typeError (Just $ Maybe.fromMaybe msg maybe) variable1 variable2
@@ -352,10 +352,10 @@ fieldMismatchError missingFields =
352352 case Map.keys missingFields of
353353 [] -> ""
354354 [key] ->
355− "Looks like a record is missing the field '" ++ key ++ "'.\n " ++
356− "Maybe there is a misspelling in a record access or record update?"
355+ "Looks like a record is missing the field '" ++ key ++ "'.\n"
356+ ++ "Maybe there is a misspelling?"
357357 keys ->
358− "Looks like one record is missing fields "
358+ "Looks like a record is missing fields "
359359 ++ List.intercalate ", " (init keys) ++ ", and " ++ last keys
360360
361361