@@ -2,31 +2,28 @@
22{-# LANGUAGE OverloadedStrings #-}
33module Reporting.Error.Canonicalize
44 ( Error(..)
5+ , Args(..)
56 , CtorType(..)
7+ , CycleNode(..)
8+ , InvalidPayload(..)
69 , variable
710 , VarKind(..)
811 , VarProblem(..)
9− , argMismatch
10− , port
1112 , toReport
1213 , extractSuggestions
1314 )
1415 where
1516
16−import Control.Arrow ((***))
17+
1718import qualified Data.Char as Char
18−import qualified Data.Map as Map
1919import qualified Data.Text as Text
2020import Data.Text (Text)
2121
22−import qualified AST.Binop as Binop
23−import qualified AST.Expression.Canonical as Canonical
24−import qualified AST.Module.Name as ModuleName
22+import qualified AST.Expression.Canonical as Can
2523import qualified AST.Type as Type
26−import qualified AST.Variable as Var
2724import qualified Elm.Name as N
2825import qualified Reporting.Annotation as A
29−import qualified Reporting.Region as Region
26+import qualified Reporting.Region as R
3027import qualified Reporting.Render.Type as RenderType
3128import qualified Reporting.Report as Report
3229import qualified Reporting.Helpers as Help
@@ -38,29 +35,43 @@ import Reporting.Helpers ( Doc, (<>), dullyellow, fillSep, green, i2t, indent, r
3835
3936
4037data Error
41− = Var VarError
42− | AliasArgMismatch Var.Canonical Int Int
43− | AliasRecursion N.Name [N.Name] Type.Raw [N.Name]
44− | BadRecursion Region.Region Canonical.Def [Canonical.Def]
45− | BadInfix Region.Region Int Var.Canonical Binop.Associativity Var.Canonical Binop.Associativity
46− | Pattern PatternError
47− | DuplicateDecl N.Name
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]
58− | Export Text [Text]
59− | DuplicateExport Text
60− | Port PortError
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]
38+ = Var VarError
39+ | Binop N.Name N.Name
40+ | DuplicateArg N.Name
41+ | DuplicateDecl N.Name
42+ | DuplicateType N.Name
43+ | DuplicateCtor N.Name CtorType CtorType
44+ | DuplicateBinop N.Name
45+ | DuplicateField N.Name
46+ | DuplicateAliasArg N.Name N.Name [N.Name]
47+ | DuplicateUnionArg N.Name N.Name [N.Name]
48+ | DuplicateBindingName N.Name
49+ | EffectNotFound N.Name
50+ | EffectFunctionNotFound N.Name
51+ | ExportDuplicate N.Name
52+ | ExportNotFound VarKind N.Name [N.Name]
53+ | ExportOpenAlias N.Name
54+ | ImportNotFound N.Name [N.Name]
55+ | ImportCtorNotFound N.Name N.Name
56+ | ImportOpenAlias N.Name
57+ | ImportExposingNotFound N.Name N.Name [N.Name]
58+ | PortPayloadInvalid N.Name Type.Canonical InvalidPayload
59+ | PortTypeInvalid N.Name Type.Canonical
60+ | RecursiveAlias N.Name [N.Name] Type.Raw [N.Name]
61+ | RecursiveDecl R.Region N.Name [N.Name]
62+ | RecursiveLet [CycleNode]
63+ | Shadowing N.Name
64+ | TooFew Args N.Name Int Int
65+ | TooMany Args N.Name Int Int R.Region
66+ | TupleLargerThanThree R.Region
67+ | TypeVarsUnboundInUnion N.Name [N.Name] [A.Located N.Name]
68+ | TypeVarsMessedUpInAlias N.Name [N.Name] [A.Located N.Name] [A.Located N.Name]
69+
70+
71+data Args
72+ = UnionArgs
73+ | AliasArgs
74+ | PatternArgs
6475
6576
6677data CtorType
@@ -68,6 +79,19 @@ data CtorType
6879 | RecordCtor
6980
7081
82+data CycleNode
83+ = CycleFunc (A.Located N.Name)
84+ | CycleValue (A.Located N.Name)
85+ | CyclePattern Can.Pattern
86+
87+
88+data InvalidPayload
89+ = ExtendedRecord
90+ | Function
91+ | TypeVariable N.Name
92+ | UnsupportedType N.Name
93+
94+
7195
7296-- VARIABLES
7397
@@ -121,36 +145,6 @@ toKindInfo kind name =
121145
122146
123147
124−-- PATTERN
125−
126−
127−data PatternError
128− = PatternArgMismatch Var.Canonical Int Int
129−
130−
131−argMismatch :: Var.Canonical -> Int -> Int -> Error
132−argMismatch name expected actual =
133− Pattern (PatternArgMismatch name expected actual)
134−
135−
136−
137−-- PORTS
138−
139−
140−data PortError =
141− PortError
142− { _portName :: Text
143− , _portType :: Type.Canonical
144− , _portMessage :: Maybe Text
145− }
146−
147−
148−port :: Text -> Type.Canonical -> Maybe Text -> Error
149−port name tipe maybeMessage =
150− Port (PortError name tipe maybeMessage)
151−
152−
153−
154148-- EXTRACT SUGGESTIONS
155149
156150
@@ -160,16 +154,10 @@ extractSuggestions err =
160154 Var (VarError _ _ _ suggestions) ->
161155 Just suggestions
162156
163− AliasArgMismatch _ _ _ ->
164− Nothing
165−
166− AliasRecursion _ _ _ _ ->
167− Nothing
168−
169− BadRecursion _ _ _ ->
157+ Binop _ _ ->
170158 Nothing
171159
172− BadInfix _ _ _ _ _ _ ->
160+ DuplicateArg _ ->
173161 Nothing
174162
175163 DuplicateDecl _ ->
@@ -193,11 +181,26 @@ extractSuggestions err =
193181 DuplicateUnionArg _ _ _ ->
194182 Nothing
195183
196− Pattern _ ->
184+ DuplicateBindingName _ ->
185+ Nothing
186+
187+ EffectNotFound _ ->
188+ Nothing
189+
190+ EffectFunctionNotFound _ ->
191+ Nothing
192+
193+ ExportDuplicate _ ->
194+ Nothing
195+
196+ ExportNotFound _ _ suggestions ->
197+ Just suggestions
198+
199+ ExportOpenAlias _ ->
197200 Nothing
198201
199202 ImportNotFound _ suggestions ->
200− Just (map ModuleName.toText suggestions)
203+ Just suggestions
201204
202205 ImportExposingNotFound _ _ suggestions ->
203206 Just suggestions
@@ -208,16 +211,31 @@ extractSuggestions err =
208211 ImportOpenAlias _ ->
209212 Nothing
210213
211− Export _ suggestions ->
212− Just suggestions
214+ PortPayloadInvalid _ _ _ ->
215+ Nothing
216+
217+ PortTypeInvalid _ _ ->
218+ Nothing
219+
220+ RecursiveAlias _ _ _ _ ->
221+ Nothing
222+
223+ RecursiveDecl _ _ _ ->
224+ Nothing
225+
226+ RecursiveLet _ ->
227+ Nothing
213228
214− DuplicateExport _ ->
229+ Shadowing _ ->
215230 Nothing
216231
217− Port _ ->
232+ TooFew _ _ _ _ ->
218233 Nothing
219234
220− BadPort _ _ ->
235+ TooMany _ _ _ _ _ ->
236+ Nothing
237+
238+ TupleLargerThanThree _ ->
221239 Nothing
222240
223241 TypeVarsUnboundInUnion _ _ _ ->
@@ -237,19 +255,11 @@ toReport localizer err =
237255 Var varError ->
238256 varErrorToReport varError
239257
240− BadRecursion region def defs ->
241− case defs of
242− [] ->
243− badSelfRecursion region (defToText def)
244−
245− _ ->
246− badMutualRecursion region (defToText def) (map (defToText) defs)
247−
248− BadInfix region _prec (Var.Canonical _ name1) _assoc1 (Var.Canonical _ name2) _assoc2 ->
258+ Binop op1 op2 ->
249259 Report.report
250260 "INFIX PROBLEM"
251− (Just region)
252− ("You cannot mix (" <> name1 <> ") and (" <> name2 <> ") without parentheses." )
261+ Nothing
262+ ("You cannot mix (" <> op1 <> ") and (" <> op2 <> ") without parentheses." )
253263 (
254264 Help.stack
255265 [ reflowParagraph $
@@ -257,6 +267,9 @@ toReport localizer err =
257267 ]
258268 )
259269
270+ DuplicateArg _ ->
271+ error "TODO DuplicateArg"
272+
260273 DuplicateDecl _ ->
261274 error "TODO DuplicateDecl"
262275
@@ -278,20 +291,34 @@ toReport localizer err =
278291 DuplicateUnionArg _ _ _ ->
279292 error "TODO DuplicateUnionArg"
280293
281− Pattern patternError ->
282− case patternError of
283− PatternArgMismatch var expected actual ->
284− argMismatchReport "Pattern" var expected actual
294+ DuplicateBindingName _ ->
295+ error "TODO DuplicateBindingName"
285296
286− AliasArgMismatch var expected actual ->
287− argMismatchReport "Type" var expected actual
297+ EffectNotFound _ ->
298+ error "TODO EffectNotFound"
288299
289− AliasRecursion name args tipe others ->
290− aliasRecursionReport localizer name args tipe others
300+ EffectFunctionNotFound _ ->
301+ error "TODO EffectFunctionNotFound"
302+
303+ ExportDuplicate name ->
304+ Report.report "EXPOSING ERROR" Nothing
305+ ("You are trying to expose `" <> name <> "` multiple times!")
306+ "Remove duplicates until there is only one listed."
307+
308+ ExportNotFound kind rawName suggestions ->
309+ let (a, thing, name) = toKindInfo kind rawName in
310+ Report.reportDoc "EXPOSING ERROR" Nothing
311+ ["You","are","trying","to","expose",a,thing,"named"
312+ ,name,"but","I","cannot","find","its","definition."
313+ ]
314+ (Help.maybeYouWant Nothing suggestions)
315+
316+ ExportOpenAlias name ->
317+ error "TODO" name
291318
292319 ImportNotFound name suggestions ->
293320 Report.report "IMPORT ERROR" Nothing
294− ("Could not find a module named `" <> ModuleName.toText name <> "`")
321+ ("Could not find a module named `" <> name <> "`")
295322 (Help.maybeYouWant Nothing suggestions)
296323
297324 ImportCtorNotFound ctor tipe ->
@@ -320,30 +347,33 @@ toReport localizer err =
320347
321348 ImportExposingNotFound name value suggestions ->
322349 Report.report "IMPORT ERROR" Nothing
323− ("Module `" <> ModuleName.toText name <> "` does not expose `" <> value <> "`")
324− (Help.maybeYouWant Nothing suggestions)
325−
326− Export name suggestions ->
327− Report.report "EXPOSING ERROR" Nothing
328− ("Could not expose `" <> name <> "` which is not defined in this module.")
350+ ("Module `" <> name <> "` does not expose `" <> value <> "`")
329351 (Help.maybeYouWant Nothing suggestions)
330352
331− DuplicateExport name ->
332− Report.report "EXPOSING ERROR" Nothing
333− ("You are trying to expose `" <> name <> "` multiple times!")
334− "Remove duplicates until there is only one listed."
335−
336− Port (PortError name tipe maybeMessage) ->
353+ PortPayloadInvalid portName tipe invalidPayload ->
337354 let
338355 context =
339− maybe "" (" the following " <> ) maybeMessage
356+ case invalidPayload of
357+ ExtendedRecord ->
358+ "an extended record"
359+
360+ Function ->
361+ "a function"
362+
363+ TypeVariable name ->
364+ const "an unknown type" (error "TODO" name)
365+ -- "Notice the `" <> name <> "` type variable. It must be a concrete type, like Int or Float."
366+
367+ UnsupportedType name ->
368+ "a `" <> name <> "` value"
340369 in
341370 Report.report
342371 "PORT ERROR"
343372 Nothing
344− ("Port `" <> name <> "` is trying to communicate an unsupported type."
373+ ("Port `" <> portName <> "` is trying to transmit " <> context <> ":"
345374 )
346− ( Help.stack
375+ (error "TODO" tipe)
376+ {- Help.stack
347377 [ text ("The specific unsupported type is" <> context <> ":")
348378 , indent 4 (RenderType.toDoc localizer tipe)
349379 , text "The types of values that can flow through in and out of Elm include:"
@@ -352,9 +382,9 @@ toReport localizer err =
352382 \ Tuples, Json.Values, and concrete records."
353383 -- TODO add a note about custom decoders and encoders when they exist!
354384 ]
355− )
385+ -}
356386
357− BadPort name tipe ->
387+ PortTypeInvalid name tipe ->
358388 Report.report
359389 "PORT ERROR"
360390 Nothing
@@ -369,6 +399,35 @@ toReport localizer err =
369399 ]
370400 )
371401
402+ RecursiveAlias name args tipe others ->
403+ aliasRecursionReport localizer name args tipe others
404+
405+ RecursiveDecl region name names ->
406+ error "TODO" region name names
407+
408+ RecursiveLet cycles ->
409+ case cycles of
410+ [] ->
411+ error "TODO"
412+
413+ [_] ->
414+ error "TODO" badSelfRecursion
415+
416+ _ ->
417+ error "TODO" badMutualRecursion
418+
419+ Shadowing _ ->
420+ error "TODO Shadowing"
421+
422+ TooFew _ _ _ _ ->
423+ error "TODO TooFew"
424+
425+ TooMany _ _ _ _ _ ->
426+ error "TODO TooMany"
427+
428+ TupleLargerThanThree _ ->
429+ error "TODO TupleLargerThanThree"
430+
372431 TypeVarsUnboundInUnion _ _ _ ->
373432 error "TODO TypeVarsUnboundInUnion"
374433
@@ -377,8 +436,6 @@ toReport localizer err =
377436
378437
379438
380−
381−
382439-- VAR ERROR
383440
384441
@@ -467,8 +524,8 @@ modHint =
467524-- ARG MISMATCH
468525
469526
470−argMismatchReport :: Text -> Var.Canonical -> Int -> Int -> Report.Report
471−argMismatchReport kind var expected actual =
527+_argMismatchReport :: Text -> N.Name -> Int -> Int -> Report.Report
528+_argMismatchReport kind name expected actual =
472529 let
473530 numArgs =
474531 "too "
@@ -478,7 +535,7 @@ argMismatchReport kind var expected actual =
478535 Report.report
479536 (Text.map Char.toUpper numArgs)
480537 Nothing
481− ( kind <> " " <> Var.toText var <> " has " <> numArgs <> "."
538+ ( kind <> " " <> name <> " has " <> numArgs <> "."
482539 )
483540 ( text $
484541 "Expecting " <> i2t expected <> ", but got " <> i2t actual <> "."
@@ -489,7 +546,7 @@ argMismatchReport kind var expected actual =
489546-- BAD RECURSION
490547
491548
492−badSelfRecursion :: Region.Region -> Text -> Report.Report
549+badSelfRecursion :: R.Region -> Text -> Report.Report
493550badSelfRecursion region name =
494551 let
495552 header =
@@ -517,7 +574,7 @@ badSelfRecursionHelp intro body =
517574 ++ map text (Text.words body)
518575
519576
520−badMutualRecursion :: Region.Region -> Text -> [Text] -> Report.Report
577+badMutualRecursion :: R.Region -> Text -> [Text] -> Report.Report
521578badMutualRecursion region name names =
522579 let
523580 header =
@@ -535,11 +592,6 @@ badMutualRecursion region name names =
535592 ]
536593
537594
538−defToText :: Canonical.Def -> Text
539−defToText (Canonical.Def _ pattern _ _) =
540− error "TODO P.toText" pattern
541−
542−
543595
544596-- BAD ALIAS RECURSION
545597
@@ -556,7 +608,7 @@ aliasRecursionReport localizer name args tipe others =
556608 "When I expand a recursive type alias, it just keeps getting bigger and bigger.\
557609 \ So dealiasing results in an infinitely large type! Try this instead:"
558610 , indent 4 $
559− RenderType.decl localizer name args [(name, [unsafePromote tipe])]
611+ RenderType.decl localizer name args [(name, [error "TODO" tipe])]
560612 , text $
561613 "This is kind of a subtle distinction. I suggested the naive fix, but you can\n"
562614 <> "often do something a bit nicer. So I would recommend reading more at:\n"
@@ -576,30 +628,3 @@ aliasRecursionReport localizer name args tipe others =
576628 <> Help.hintLink "recursive-alias"
577629 ]
578630 )
579−
580−
581−unsafePromote :: Type.Raw -> Type.Canonical
582−unsafePromote (A.A _ rawType) =
583− case rawType of
584− Type.RLambda arg result ->
585− Type.Lambda (unsafePromote arg) (unsafePromote result)
586−
587− Type.RVar x ->
588− Type.Var x
589−
590− Type.RType _ _ name args ->
591− Type.Type (error "TODO unsafePromote better") name (map unsafePromote args)
592−
593− Type.RRecord fields ext ->
594− Type.Record
595− (Map.fromList (map (A.drop *** unsafePromote) fields))
596− (fmap unsafePromote ext)
597−
598− Type.RUnit ->
599− Type.Unit
600−
601− Type.RTuple a b cs ->
602− Type.Tuple
603− (unsafePromote a)
604− (unsafePromote b)
605− (map unsafePromote cs)