@@ -2,14 +2,11 @@
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Error.Canonicalize
3module Reporting.Error.Canonicalize
4 ( Error(..)
4 ( Error(..)
6 , VarKind(..)
7 , VarKind(..)
7 , VarProblem(..)
8 , VarProblem(..)
8 , argMismatch
9 , argMismatch
14 , toReport
11 , toReport
15 , extractSuggestions
12 , extractSuggestions
@@ -18,16 +15,16 @@ module Reporting.Error.Canonicalize
19import Control.Arrow ((***))
16import Control.Arrow ((***))
20import qualified Data.Char as Char
17import qualified Data.Char as Char
18+import qualified Data.Map as Map
21import qualified Data.Text as Text
19import qualified Data.Text as Text
22import Data.Text (Text)
20import Data.Text (Text)
24−import qualified AST.Declaration as Decl
22+import qualified AST.Binop as Binop
25import qualified AST.Expression.Canonical as Canonical
23import qualified AST.Expression.Canonical as Canonical
26−import qualified AST.Helpers as Help (isOp)
27import qualified AST.Module.Name as ModuleName
24import qualified AST.Module.Name as ModuleName
28−import qualified AST.Pattern as P
29import qualified AST.Type as Type
25import qualified AST.Type as Type
30import qualified AST.Variable as Var
26import qualified AST.Variable as Var
27+import qualified Elm.Name as N
31import qualified Reporting.Annotation as A
28import qualified Reporting.Annotation as A
32import qualified Reporting.Region as Region
29import qualified Reporting.Region as Region
33import qualified Reporting.Render.Type as RenderType
30import qualified Reporting.Render.Type as RenderType
@@ -42,15 +39,33 @@ import Reporting.Helpers ( Doc, (<>), dullyellow, fillSep, green, i2t, indent, r
44 = Var VarError
41 = Var VarError
42+ | AliasArgMismatch Var.Canonical Int Int
43+ | AliasRecursion N.Name [N.Name] Type.Raw [N.Name]
45 | BadRecursion Region.Region Canonical.Def [Canonical.Def]
44 | BadRecursion Region.Region Canonical.Def [Canonical.Def]
46− | BadInfix Region.Region Int Var.Canonical Decl.Assoc Var.Canonical Decl.Assoc
45+ | BadInfix Region.Region Int Var.Canonical Binop.Associativity Var.Canonical Binop.Associativity
47 | Pattern PatternError
46 | Pattern PatternError
48− | Alias AliasError
47+ | DuplicateDecl N.Name
49− | Import ModuleName.Raw ImportError
48+ | DuplicateType N.Name
49+ | DuplicateCtor N.Name CtorType CtorType
50+ | DuplicateBinop N.Name
51+ | DuplicateField N.Name
52+ | DuplicateAliasArg N.Name N.Name [N.Name]
53+ | DuplicateUnionArg N.Name N.Name [N.Name]
54+ | ImportNotFound ModuleName.Raw [ModuleName.Raw]
55+ | ImportCtorNotFound N.Name N.Name
56+ | ImportOpenAlias N.Name
57+ | ImportExposingNotFound ModuleName.Raw N.Name [N.Name]
50 | Export Text [Text]
58 | Export Text [Text]
51 | DuplicateExport Text
59 | DuplicateExport Text
52 | Port PortError
60 | Port PortError
53 | BadPort Text Type.Canonical
61 | BadPort Text Type.Canonical
62+ | TypeVarsUnboundInUnion N.Name [N.Name] [A.Located N.Name]
63+ | TypeVarsMessedUpInAlias N.Name [N.Name] [A.Located N.Name] [A.Located N.Name]
@@ -83,7 +98,8 @@ variable kind name problem suggestions =
85data VarKind
100data VarKind
86− = BadValue
101+ = BadOp
87 | BadPattern
103 | BadPattern
88 | BadType
104 | BadType
@@ -91,11 +107,11 @@ data VarKind
91toKindInfo :: VarKind -> Text -> ( Doc, Doc, Doc )
107toKindInfo :: VarKind -> Text -> ( Doc, Doc, Doc )
92toKindInfo kind name =
108toKindInfo kind name =
93 case kind of
109 case kind of
94− BadValue ->
110+ BadOp ->
95− if Help.isOp name then
96− ( "an", "operator", text $ "(" <> name <> ")" )
111+ ( "an", "operator", text $ "(" <> name <> ")" )
98− ( "a", "value", text name )
114+ ( "a", "value", text name )
100 BadPattern ->
116 BadPattern ->
101 ( "a", "pattern", text name )
117 ( "a", "pattern", text name )
@@ -118,40 +134,6 @@ argMismatch name expected actual =
125− = ModuleNotFound [ModuleName.Raw]
126− | ValueNotFound Text [Text]
129−moduleNotFound :: ModuleName.Raw -> [ModuleName.Raw] -> Error
130−moduleNotFound name possibilities =
131− Import name (ModuleNotFound possibilities)
134−valueNotFound :: ModuleName.Raw -> Text -> [Text] -> Error
135−valueNotFound name value possibilities =
136− Import name (ValueNotFound value possibilities)
144− = ArgMismatch Var.Canonical Int Int
145− | SelfRecursive Text [Text] Type.Raw
146− | MutuallyRecursive [(Region.Region, Text, [Text], Type.Raw)]
149−alias :: Var.Canonical -> Int -> Int -> Error
150−alias name expected actual =
151− Alias (ArgMismatch name expected actual)
@@ -178,25 +160,53 @@ extractSuggestions err =
178 Var (VarError _ _ _ suggestions) ->
160 Var (VarError _ _ _ suggestions) ->
179 Just suggestions
161 Just suggestions
163+ AliasArgMismatch _ _ _ ->
166+ AliasRecursion _ _ _ _ ->
181 BadRecursion _ _ _ ->
169 BadRecursion _ _ _ ->
184 BadInfix _ _ _ _ _ _ ->
172 BadInfix _ _ _ _ _ _ ->
187− Pattern _ ->
175+ DuplicateDecl _ ->
181+ DuplicateCtor _ _ _ ->
190− Alias _ ->
187+ DuplicateField _ ->
193− Import _ importError ->
190+ DuplicateAliasArg _ _ _ ->
194− case importError of
191+ Nothing
193+ DuplicateUnionArg _ _ _ ->
195− ModuleNotFound suggestions ->
199+ ImportNotFound _ suggestions ->
196− Just (map ModuleName.toText suggestions)
200+ Just (map ModuleName.toText suggestions)
202+ ImportExposingNotFound _ _ suggestions ->
205+ ImportCtorNotFound _ _ ->
198− ValueNotFound _ suggestions ->
208+ ImportOpenAlias _ ->
199− Just suggestions
209+ Nothing
201 Export _ suggestions ->
211 Export _ suggestions ->
202 Just suggestions
212 Just suggestions
@@ -210,6 +220,12 @@ extractSuggestions err =
210 BadPort _ _ ->
220 BadPort _ _ ->
223+ TypeVarsUnboundInUnion _ _ _ ->
226+ TypeVarsMessedUpInAlias _ _ _ _ ->
215-- TO REPORT
231-- TO REPORT
@@ -241,61 +257,71 @@ toReport localizer err =
261+ error "TODO DuplicateDecl"
264+ error "TODO DuplicateType"
266+ DuplicateCtor _ _ _ ->
267+ error "TODO DuplicateCtor"
270+ error "TODO DuplicateBinop"
273+ error "TODO DuplicateField"
275+ DuplicateAliasArg _ _ _ ->
276+ error "TODO DuplicateAliasArg"
278+ DuplicateUnionArg _ _ _ ->
279+ error "TODO DuplicateUnionArg"
244 Pattern patternError ->
281 Pattern patternError ->
245 case patternError of
282 case patternError of
246 PatternArgMismatch var expected actual ->
283 PatternArgMismatch var expected actual ->
247 argMismatchReport "Pattern" var expected actual
284 argMismatchReport "Pattern" var expected actual
251− ArgMismatch var expected actual ->
286+ AliasArgMismatch var expected actual ->
252− argMismatchReport "Type" var expected actual
287+ argMismatchReport "Type" var expected actual
254− SelfRecursive name tvars tipe ->
289+ AliasRecursion name args tipe others ->
255− Report.report
290+ aliasRecursionReport localizer name args tipe others
257− Nothing
292+ ImportNotFound name suggestions ->
258− "This type alias is recursive, forming an infinite type!"
262− "When I expand a recursive type alias, it just keeps getting bigger and bigger.\
263− \ So dealiasing results in an infinitely large type! Try this instead:"
265− RenderType.decl localizer name tvars [(name, [unsafePromote tipe])]
267− "This is kind of a subtle distinction. I suggested the naive fix, but you can\n"
268− <> "often do something a bit nicer. So I would recommend reading more at:\n"
269− <> Help.hintLink "recursive-alias"
273− MutuallyRecursive aliases ->
277− "This type alias is part of a mutually recursive set of type aliases."
279− [ text "The following type aliases are mutually recursive:"
280− , indent 4 (Help.drawCycle (map (\(_, name, _, _) -> name) aliases))
282− "You need to convert at least one `type alias` into a `type`. This is a kind of\
283− \ subtle distinction, so definitely read up on this before you make a fix: "
284− <> Help.hintLink "recursive-alias"
288− Import name importError ->
290− ModuleNotFound suggestions ->
291− Report.report "IMPORT ERROR" Nothing
293+ Report.report "IMPORT ERROR" Nothing
292− ("Could not find a module named `" <> ModuleName.toText name <> "`")
294+ ("Could not find a module named `" <> ModuleName.toText name <> "`")
293− (Help.maybeYouWant Nothing suggestions)
295+ (Help.maybeYouWant Nothing suggestions)
297+ ImportCtorNotFound ctor tipe ->
298+ Report.report "IMPORT ERROR" Nothing
299+ ("This is not the syntax for exposing a `" <> tipe <> "` type constructor:")
302+ "Delete that. You can expose the `" <> ctor
303+ <> "` type constructors by saying:"
304+ , indent 4 (green (text (tipe <> "(..)")))
306+ "This means that every type constructors of `" <> tipe
307+ <> "` is exposed. No need to list them separately."
311+ ImportOpenAlias name ->
312+ Report.report "IMPORT ERROR" Nothing
313+ ("Cannot expose `" <> name <> "` like this:")
315+ "The (..) syntax exposes type constructors, but a type alias like `"
316+ <> name <> "` has no constructors! It is just an alternate name for\
317+ \ an existing type. Remove the (..) part and it should work."
319+ -- TODO add docs about the difference between types and type aliases
295− ValueNotFound value suggestions ->
321+ ImportExposingNotFound name value suggestions ->
296− Report.report "IMPORT ERROR" Nothing
322+ Report.report "IMPORT ERROR" Nothing
297− ("Module `" <> ModuleName.toText name <> "` does not expose `" <> value <> "`")
323+ ("Module `" <> ModuleName.toText name <> "` does not expose `" <> value <> "`")
298− (Help.maybeYouWant Nothing suggestions)
324+ (Help.maybeYouWant Nothing suggestions)
300 Export name suggestions ->
326 Export name suggestions ->
301 Report.report "EXPOSING ERROR" Nothing
327 Report.report "EXPOSING ERROR" Nothing
@@ -343,6 +369,14 @@ toReport localizer err =
372+ TypeVarsUnboundInUnion _ _ _ ->
373+ error "TODO TypeVarsUnboundInUnion"
375+ TypeVarsMessedUpInAlias _ _ _ _ ->
376+ error "TODO TypeVarsMessedUpInAlias"
348-- VAR ERROR
382-- VAR ERROR
@@ -503,11 +537,45 @@ badMutualRecursion region name names =
504defToText :: Canonical.Def -> Text
538defToText :: Canonical.Def -> Text
505defToText (Canonical.Def _ pattern _ _) =
539defToText (Canonical.Def _ pattern _ _) =
506− P.toText pattern
540+ error "TODO P.toText" pattern
544+-- BAD ALIAS RECURSION
547+aliasRecursionReport :: RenderType.Localizer -> N.Name -> [N.Name] -> Type.Raw -> [N.Name] -> Report.Report
548+aliasRecursionReport localizer name args tipe others =
551+ Report.report "ALIAS PROBLEM" Nothing
552+ "This type alias is recursive, forming an infinite type!"
556+ "When I expand a recursive type alias, it just keeps getting bigger and bigger.\
557+ \ So dealiasing results in an infinitely large type! Try this instead:"
559+ RenderType.decl localizer name args [(name, [unsafePromote tipe])]
561+ "This is kind of a subtle distinction. I suggested the naive fix, but you can\n"
562+ <> "often do something a bit nicer. So I would recommend reading more at:\n"
563+ <> Help.hintLink "recursive-alias"
510−-- TYPE COERCION
567+ _ ->
568+ Report.report "ALIAS PROBLEM" Nothing
569+ "This type alias is part of a mutually recursive set of type aliases."
571+ [ text "The following type aliases are mutually recursive:"
572+ , indent 4 (Help.drawCycle (name:others))
574+ "You need to convert at least one `type alias` into a `type`. This is a kind of\
575+ \ subtle distinction, so definitely read up on this before you make a fix: "
576+ <> Help.hintLink "recursive-alias"
513unsafePromote :: Type.Raw -> Type.Canonical
581unsafePromote :: Type.Raw -> Type.Canonical
@@ -519,9 +587,19 @@ unsafePromote (A.A _ rawType) =
519 Type.RVar x ->
587 Type.RVar x ->
520 Type.Var x
588 Type.Var x
522− Type.RType (A.A _ (Var.Raw name)) args ->
590+ Type.RType _ _ name args ->
523− Type.Type (Var.local name) (map unsafePromote args)
591+ Type.Type (error "TODO unsafePromote better") name (map unsafePromote args)
525 Type.RRecord fields ext ->
593 Type.RRecord fields ext ->
526− Type.Record (map (A.drop *** unsafePromote) fields) (fmap unsafePromote ext)
595+ (Map.fromList (map (A.drop *** unsafePromote) fields))
527−
596+ (fmap unsafePromote ext)
601+ Type.RTuple a b cs ->
605+ (map unsafePromote cs)