@@ -20,10 +20,11 @@
2020module Language.PureScript.Errors where
2121
2222import Data.Either (lefts, rights)
23−import Data.List (intercalate, transpose, nub)
23+import Data.List (intercalate, transpose, nub, nubBy, partition)
2424import Data.Function (on)
2525#if __GLASGOW_HASKELL__ < 710
2626import Data.Foldable (fold, foldMap)
27+import Data.Traversable (traverse)
2728#else
2829import Data.Foldable (fold)
2930#endif
@@ -53,9 +54,7 @@ import qualified Text.Parsec as P
5354import qualified Text.Parsec.Error as PE
5455import Text.Parsec.Error (Message(..))
5556
56−-- |
57−-- A type of error messages
58−--
57+-- | A type of error messages
5958data SimpleErrorMessage
6059 = ErrorParsingExterns P.ParseError
6160 | ErrorParsingFFIModule FilePath
@@ -143,45 +142,54 @@ data SimpleErrorMessage
143142 | ShadowedTypeVar String
144143 | UnusedTypeVar String
145144 | WildcardInferredType Type
145+ | MissingTypeDeclaration Ident
146146 | NotExhaustivePattern [[Binder]] Bool
147147 | OverlappingPattern [[Binder]] Bool
148148 | IncompleteExhaustivityCheck
149149 | ClassOperator ProperName Ident
150150 | MisleadingEmptyTypeImport ModuleName ProperName
151151 | ImportHidingModule ModuleName
152− deriving (Show)
153−
154−-- |
155−-- Wrapper of simpler errors
156−--
157−data ErrorMessage
158− = NotYetDefined [Ident] ErrorMessage
159− | ErrorUnifyingTypes Type Type ErrorMessage
160− | ErrorInExpression Expr ErrorMessage
161− | ErrorInModule ModuleName ErrorMessage
162− | ErrorInInstance (Qualified ProperName) [Type] ErrorMessage
163− | ErrorInSubsumption Type Type ErrorMessage
164− | ErrorCheckingType Expr Type ErrorMessage
165− | ErrorCheckingKind Type ErrorMessage
166− | ErrorInferringType Expr ErrorMessage
167− | ErrorInApplication Expr Type Expr ErrorMessage
168− | ErrorInDataConstructor ProperName ErrorMessage
169− | ErrorInTypeConstructor ProperName ErrorMessage
170− | ErrorInBindingGroup [Ident] ErrorMessage
171− | ErrorInDataBindingGroup ErrorMessage
172− | ErrorInTypeSynonym ProperName ErrorMessage
173− | ErrorInValueDeclaration Ident ErrorMessage
174− | ErrorInTypeDeclaration Ident ErrorMessage
175− | ErrorInForeignImport Ident ErrorMessage
176− | PositionedError SourceSpan ErrorMessage
177− | SimpleErrorWrapper SimpleErrorMessage
178− deriving (Show)
152+ deriving Show
153+
154+-- | Error message hints, providing more detailed information about failure.
155+data ErrorMessageHint
156+ = NotYetDefined [Ident]
157+ | ErrorUnifyingTypes Type Type
158+ | ErrorInExpression Expr
159+ | ErrorInModule ModuleName
160+ | ErrorInInstance (Qualified ProperName) [Type]
161+ | ErrorInSubsumption Type Type
162+ | ErrorCheckingType Expr Type
163+ | ErrorCheckingKind Type
164+ | ErrorInferringType Expr
165+ | ErrorInApplication Expr Type Expr
166+ | ErrorInDataConstructor ProperName
167+ | ErrorInTypeConstructor ProperName
168+ | ErrorInBindingGroup [Ident]
169+ | ErrorInDataBindingGroup
170+ | ErrorInTypeSynonym ProperName
171+ | ErrorInValueDeclaration Ident
172+ | ErrorInTypeDeclaration Ident
173+ | ErrorInForeignImport Ident
174+ | PositionedError SourceSpan
175+ deriving Show
176+
177+-- | Categories of hints
178+data HintCategory
179+ = ExprHint
180+ | KindHint
181+ | CheckHint
182+ | PositionHint
183+ | OtherHint
184+ deriving (Show, Eq)
185+
186+data ErrorMessage = ErrorMessage [ErrorMessageHint] SimpleErrorMessage deriving (Show)
179187
180188instance UnificationError Type ErrorMessage where
181− occursCheckFailed t = SimpleErrorWrapper $ InfiniteType t
189+ occursCheckFailed t = ErrorMessage [] $ InfiniteType t
182190
183191instance UnificationError Kind ErrorMessage where
184− occursCheckFailed k = SimpleErrorWrapper $ InfiniteKind k
192+ occursCheckFailed k = ErrorMessage [] $ InfiniteKind k
185193
186194-- |
187195-- Get the error code for a particular error type
@@ -274,6 +282,7 @@ errorCode em = case unwrapErrorMessage em of
274282 ShadowedTypeVar{} -> "ShadowedTypeVar"
275283 UnusedTypeVar{} -> "UnusedTypeVar"
276284 WildcardInferredType{} -> "WildcardInferredType"
285+ MissingTypeDeclaration{} -> "MissingTypeDeclaration"
277286 NotExhaustivePattern{} -> "NotExhaustivePattern"
278287 OverlappingPattern{} -> "OverlappingPattern"
279288 IncompleteExhaustivityCheck{} -> "IncompleteExhaustivityCheck"
@@ -301,7 +310,7 @@ nonEmpty = not . null . runMultipleErrors
301310-- Create an error set from a single simple error message
302311--
303312errorMessage :: SimpleErrorMessage -> MultipleErrors
304−errorMessage err = MultipleErrors [SimpleErrorWrapper err]
313+errorMessage err = MultipleErrors [ErrorMessage [] err]
305314
306315
307316-- |
@@ -310,12 +319,14 @@ errorMessage err = MultipleErrors [SimpleErrorWrapper err]
310319singleError :: ErrorMessage -> MultipleErrors
311320singleError = MultipleErrors . pure
312321
313−-- |
314−-- Lift a function on ErrorMessage to a function on MultipleErrors
315−--
322+-- | Lift a function on ErrorMessage to a function on MultipleErrors
316323onErrorMessages :: (ErrorMessage -> ErrorMessage) -> MultipleErrors -> MultipleErrors
317324onErrorMessages f = MultipleErrors . map f . runMultipleErrors
318325
326+-- | Add a hint to an error message
327+addHint :: ErrorMessageHint -> MultipleErrors -> MultipleErrors
328+addHint hint = onErrorMessages $ \(ErrorMessage hints se) -> ErrorMessage (hint : hints) se
329+
319330-- | The various types of things which might need to be relabelled in errors messages.
320331data LabelType = TypeLabel | SkolemLabel String deriving (Show, Read, Eq, Ord)
321332
@@ -329,27 +340,7 @@ data Level = Error | Warning deriving Show
329340-- Extract nested error messages from wrapper errors
330341--
331342unwrapErrorMessage :: ErrorMessage -> SimpleErrorMessage
332−unwrapErrorMessage em = case em of
333− (ErrorCheckingKind _ err) -> unwrapErrorMessage err
334− (ErrorCheckingType _ _ err) -> unwrapErrorMessage err
335− (ErrorInApplication _ _ _ err) -> unwrapErrorMessage err
336− (ErrorInBindingGroup _ err) -> unwrapErrorMessage err
337− (ErrorInDataBindingGroup err) -> unwrapErrorMessage err
338− (ErrorInDataConstructor _ err) -> unwrapErrorMessage err
339− (ErrorInExpression _ err) -> unwrapErrorMessage err
340− (ErrorInForeignImport _ err) -> unwrapErrorMessage err
341− (ErrorInInstance _ _ err) -> unwrapErrorMessage err
342− (ErrorInModule _ err) -> unwrapErrorMessage err
343− (ErrorInSubsumption _ _ err) -> unwrapErrorMessage err
344− (ErrorInTypeConstructor _ err) -> unwrapErrorMessage err
345− (ErrorInTypeSynonym _ err) -> unwrapErrorMessage err
346− (ErrorInValueDeclaration _ err) -> unwrapErrorMessage err
347− (ErrorInTypeDeclaration _ err) -> unwrapErrorMessage err
348− (ErrorInferringType _ err) -> unwrapErrorMessage err
349− (ErrorUnifyingTypes _ _ err) -> unwrapErrorMessage err
350− (NotYetDefined _ err) -> unwrapErrorMessage err
351− (PositionedError _ err) -> unwrapErrorMessage err
352− (SimpleErrorWrapper sem) -> sem
343+unwrapErrorMessage (ErrorMessage _ se) = se
353344
354345replaceUnknowns :: Type -> State UnknownMap Type
355346replaceUnknowns = everywhereOnTypesM replaceTypes
@@ -365,7 +356,7 @@ replaceUnknowns = everywhereOnTypesM replaceTypes
365356 replaceTypes other = return other
366357
367358onTypesInErrorMessageM :: (Applicative m) => (Type -> m Type) -> ErrorMessage -> m ErrorMessage
368−onTypesInErrorMessageM f = g
359+onTypesInErrorMessageM f (ErrorMessage hints simple) = ErrorMessage <$> traverse gHint hints <*> gSimple simple
369360 where
370361 gSimple (InfiniteType t) = InfiniteType <$> f t
371362 gSimple (TypesDoNotUnify t1 t2) = TypesDoNotUnify <$> f t1 <*> f t2
@@ -375,396 +366,396 @@ onTypesInErrorMessageM f = g
375366 gSimple (CannotApplyFunction t e) = CannotApplyFunction <$> f t <*> pure e
376367 gSimple (InvalidInstanceHead t) = InvalidInstanceHead <$> f t
377368 gSimple other = pure other
378− g (ErrorInSubsumption t1 t2 em) = ErrorInSubsumption <$> f t1 <*> f t2 <*> g em
379− g (ErrorUnifyingTypes t1 t2 e) = ErrorUnifyingTypes <$> f t1 <*> f t2 <*> g e
380− g (ErrorCheckingType e t em) = ErrorCheckingType e <$> f t <*> g em
381− g (ErrorCheckingKind t em) = ErrorCheckingKind <$> f t <*> g em
382− g (ErrorInApplication e1 t1 e2 em) = ErrorInApplication e1 <$> f t1 <*> pure e2 <*> g em
383− g (NotYetDefined x e) = NotYetDefined x <$> g e
384− g (ErrorInExpression x e) = ErrorInExpression x <$> g e
385− g (ErrorInModule x e) = ErrorInModule x <$> g e
386− g (ErrorInInstance x y e) = ErrorInInstance x y <$> g e
387− g (ErrorInferringType x e) = ErrorInferringType x <$> g e
388− g (ErrorInDataConstructor x e) = ErrorInDataConstructor x <$> g e
389− g (ErrorInTypeConstructor x e) = ErrorInTypeConstructor x <$> g e
390− g (ErrorInBindingGroup x e) = ErrorInBindingGroup x <$> g e
391− g (ErrorInDataBindingGroup e) = ErrorInDataBindingGroup <$> g e
392− g (ErrorInTypeSynonym x e) = ErrorInTypeSynonym x <$> g e
393− g (ErrorInValueDeclaration x e) = ErrorInValueDeclaration x <$> g e
394− g (ErrorInTypeDeclaration x e) = ErrorInTypeDeclaration x <$> g e
395− g (ErrorInForeignImport x e) = ErrorInForeignImport x <$> g e
396− g (PositionedError x e) = PositionedError x <$> g e
397− g (SimpleErrorWrapper sem) = SimpleErrorWrapper <$> gSimple sem
369+ gHint (ErrorInSubsumption t1 t2) = ErrorInSubsumption <$> f t1 <*> f t2
370+ gHint (ErrorUnifyingTypes t1 t2) = ErrorUnifyingTypes <$> f t1 <*> f t2
371+ gHint (ErrorCheckingType e t) = ErrorCheckingType e <$> f t
372+ gHint (ErrorCheckingKind t) = ErrorCheckingKind <$> f t
373+ gHint (ErrorInApplication e1 t1 e2) = ErrorInApplication e1 <$> f t1 <*> pure e2
374+ gHint other = pure other
398375
399376-- |
400377-- Pretty print a single error, simplifying if necessary
401378--
402379prettyPrintSingleError :: Bool -> Level -> ErrorMessage -> State UnknownMap Box.Box
403−prettyPrintSingleError full level e = prettyPrintErrorMessage <$> onTypesInErrorMessageM replaceUnknowns (if full then e else simplifyErrorMessage e)
380+prettyPrintSingleError full level e = prettyPrintErrorMessage . positionHintsFirst . reverseHints <$> onTypesInErrorMessageM replaceUnknowns (if full then e else simplifyErrorMessage e)
404381 where
405− -- |
382+
406383 -- Pretty print an ErrorMessage
407− --
408384 prettyPrintErrorMessage :: ErrorMessage -> Box.Box
409− prettyPrintErrorMessage em =
385+ prettyPrintErrorMessage (ErrorMessage hints simple) =
410386 paras $
411− go em:suggestions em ++
387+ map renderHint hints ++
388+ renderSimpleErrorMessage simple :
389+ suggestions simple ++
412390 [line $ "See " ++ wikiUri ++ " for more information, or to contribute content related to this " ++ levelText ++ "."]
413391 where
414392 wikiUri :: String
415393 wikiUri = "https://github.com/purescript/purescript/wiki/Error-Code-" ++ errorCode e
416394
417− go :: ErrorMessage -> Box.Box
418− goSimple (CannotGetFileInfo path) =
395+ renderSimpleErrorMessage :: SimpleErrorMessage -> Box.Box
396+ renderSimpleErrorMessage (CannotGetFileInfo path) =
419397 paras [ line "Unable to read file info: "
420398 , indent . line $ path
421399 ]
422− goSimple (CannotReadFile path) =
400+ renderSimpleErrorMessage (CannotReadFile path) =
423401 paras [ line "Unable to read file: "
424402 , indent . line $ path
425403 ]
426− goSimple (CannotWriteFile path) =
404+ renderSimpleErrorMessage (CannotWriteFile path) =
427405 paras [ line "Unable to write file: "
428406 , indent . line $ path
429407 ]
430− goSimple (ErrorParsingExterns err) =
408+ renderSimpleErrorMessage (ErrorParsingExterns err) =
431409 paras [ lineWithLevel "parsing externs files: "
432− , indent . prettyPrintParseError $ err
410+ , prettyPrintParseError err
433411 ]
434− goSimple (ErrorParsingFFIModule path) =
412+ renderSimpleErrorMessage (ErrorParsingFFIModule path) =
435413 paras [ line "Unable to parse module from FFI file: "
436414 , indent . line $ path
437415 ]
438− goSimple (ErrorParsingModule err) =
416+ renderSimpleErrorMessage (ErrorParsingModule err) =
439417 paras [ line "Unable to parse module: "
440− , indent . prettyPrintParseError $ err
418+ , prettyPrintParseError err
441419 ]
442− goSimple (MissingFFIModule mn) =
420+ renderSimpleErrorMessage (MissingFFIModule mn) =
443421 line $ "Missing FFI implementations for module " ++ runModuleName mn
444− goSimple (UnnecessaryFFIModule mn path) =
422+ renderSimpleErrorMessage (UnnecessaryFFIModule mn path) =
445423 paras [ line $ "Unnecessary FFI implementations have been provided for module " ++ runModuleName mn ++ ": "
446424 , indent . line $ path
447425 ]
448− goSimple (MultipleFFIModules mn paths) =
449− paras $ [ line $ "Multiple FFI implementations have been provided for module " ++ runModuleName mn ++ ": " ]
450− ++ map (indent . line) paths
451− goSimple (InvalidExternsFile path) =
426+ renderSimpleErrorMessage (MultipleFFIModules mn paths) =
427+ paras [ line $ "Multiple FFI implementations have been provided for module " ++ runModuleName mn ++ ": "
428+ , indent . paras $ map line paths
429+ ]
430+ renderSimpleErrorMessage (InvalidExternsFile path) =
452431 paras [ line "Externs file is invalid: "
453432 , indent . line $ path
454433 ]
455− goSimple InvalidDoBind =
434+ renderSimpleErrorMessage InvalidDoBind =
456435 line "Bind statement cannot be the last statement in a do block. The last statement must be an expression."
457− goSimple InvalidDoLet =
436+ renderSimpleErrorMessage InvalidDoLet =
458437 line "Let statement cannot be the last statement in a do block. The last statement must be an expression."
459− goSimple CannotReorderOperators =
438+ renderSimpleErrorMessage CannotReorderOperators =
460439 line "Unable to reorder operators"
461− goSimple UnspecifiedSkolemScope =
440+ renderSimpleErrorMessage UnspecifiedSkolemScope =
462441 line "Skolem variable scope is unspecified"
463− goSimple OverlappingNamesInLet =
442+ renderSimpleErrorMessage OverlappingNamesInLet =
464443 line "Overlapping names in let binding."
465− goSimple (InfiniteType ty) =
444+ renderSimpleErrorMessage (InfiniteType ty) =
466445 paras [ line "An infinite type was inferred for an expression: "
467− , indent $ line $ prettyPrintType ty
446+ , indent $ typeAsBox ty
468447 ]
469− goSimple (InfiniteKind ki) =
448+ renderSimpleErrorMessage (InfiniteKind ki) =
470449 paras [ line "An infinite kind was inferred for a type: "
471− , indent $ line $ prettyPrintKind ki
450+ , indent $ kindAsBox ki
472451 ]
473− goSimple (MultipleFixities name) =
452+ renderSimpleErrorMessage (MultipleFixities name) =
474453 line $ "Multiple fixity declarations for " ++ showIdent name
475− goSimple (OrphanTypeDeclaration nm) =
454+ renderSimpleErrorMessage (OrphanTypeDeclaration nm) =
476455 line $ "Orphan type declaration for " ++ showIdent nm
477− goSimple (OrphanFixityDeclaration op) =
456+ renderSimpleErrorMessage (OrphanFixityDeclaration op) =
478457 line $ "Orphan fixity declaration for " ++ show op
479− goSimple (RedefinedModule name filenames) =
480− paras $ line ("Module " ++ runModuleName name ++ " has been defined multiple times:")
481− : map (indent . line . displaySourceSpan) filenames
482− goSimple (RedefinedIdent name) =
458+ renderSimpleErrorMessage (RedefinedModule name filenames) =
459+ paras [ line ("Module " ++ runModuleName name ++ " has been defined multiple times:")
460+ , indent . paras $ map (line . displaySourceSpan) filenames
461+ ]
462+ renderSimpleErrorMessage (RedefinedIdent name) =
483463 line $ "Name " ++ showIdent name ++ " has been defined multiple times"
484− goSimple (UnknownModule mn) =
464+ renderSimpleErrorMessage (UnknownModule mn) =
485465 line $ "Unknown module " ++ runModuleName mn
486− goSimple (UnknownType name) =
466+ renderSimpleErrorMessage (UnknownType name) =
487467 line $ "Unknown type " ++ showQualified runProperName name
488− goSimple (UnknownTypeClass name) =
468+ renderSimpleErrorMessage (UnknownTypeClass name) =
489469 line $ "Unknown type class " ++ showQualified runProperName name
490− goSimple (UnknownValue name) =
470+ renderSimpleErrorMessage (UnknownValue name) =
491471 line $ "Unknown value " ++ showQualified showIdent name
492− goSimple (UnknownTypeConstructor name) =
472+ renderSimpleErrorMessage (UnknownTypeConstructor name) =
493473 line $ "Unknown type constructor " ++ showQualified runProperName name
494− goSimple (UnknownDataConstructor dc tc) =
474+ renderSimpleErrorMessage (UnknownDataConstructor dc tc) =
495475 line $ "Unknown data constructor " ++ showQualified runProperName dc ++ foldMap ((" for type constructor " ++) . showQualified runProperName) tc
496− goSimple (UnknownImportType mn name) =
476+ renderSimpleErrorMessage (UnknownImportType mn name) =
497477 line $ "Module " ++ runModuleName mn ++ " does not export type " ++ runProperName name
498− goSimple (UnknownExportType name) =
478+ renderSimpleErrorMessage (UnknownExportType name) =
499479 line $ "Cannot export unknown type " ++ runProperName name
500− goSimple (UnknownImportTypeClass mn name) =
480+ renderSimpleErrorMessage (UnknownImportTypeClass mn name) =
501481 line $ "Module " ++ runModuleName mn ++ " does not export type class " ++ runProperName name
502− goSimple (UnknownExportTypeClass name) =
482+ renderSimpleErrorMessage (UnknownExportTypeClass name) =
503483 line $ "Cannot export unknown type class " ++ runProperName name
504− goSimple (UnknownImportValue mn name) =
484+ renderSimpleErrorMessage (UnknownImportValue mn name) =
505485 line $ "Module " ++ runModuleName mn ++ " does not export value " ++ showIdent name
506− goSimple (UnknownExportValue name) =
486+ renderSimpleErrorMessage (UnknownExportValue name) =
507487 line $ "Cannot export unknown value " ++ showIdent name
508− goSimple (UnknownExportModule name) =
488+ renderSimpleErrorMessage (UnknownExportModule name) =
509489 line $ "Cannot export unknown module " ++ runModuleName name ++ ", it either does not exist or has not been imported by the current module"
510− goSimple (UnknownImportDataConstructor mn tcon dcon) =
490+ renderSimpleErrorMessage (UnknownImportDataConstructor mn tcon dcon) =
511491 line $ "Module " ++ runModuleName mn ++ " does not export data constructor " ++ runProperName dcon ++ " for type " ++ runProperName tcon
512− goSimple (UnknownExportDataConstructor tcon dcon) =
492+ renderSimpleErrorMessage (UnknownExportDataConstructor tcon dcon) =
513493 line $ "Cannot export data constructor " ++ runProperName dcon ++ " for type " ++ runProperName tcon ++ " as it has not been declared"
514− goSimple (ConflictingImport nm mn) =
494+ renderSimpleErrorMessage (ConflictingImport nm mn) =
515495 line $ "Cannot declare " ++ show nm ++ " since another declaration of that name was imported from " ++ runModuleName mn
516− goSimple (ConflictingImports nm m1 m2) =
496+ renderSimpleErrorMessage (ConflictingImports nm m1 m2) =
517497 line $ "Conflicting imports for " ++ nm ++ " from modules " ++ runModuleName m1 ++ " and " ++ runModuleName m2
518− goSimple (ConflictingTypeDecls nm) =
498+ renderSimpleErrorMessage (ConflictingTypeDecls nm) =
519499 line $ "Conflicting type declarations for " ++ runProperName nm
520− goSimple (ConflictingCtorDecls nm) =
500+ renderSimpleErrorMessage (ConflictingCtorDecls nm) =
521501 line $ "Conflicting data constructor declarations for " ++ runProperName nm
522− goSimple (TypeConflictsWithClass nm) =
502+ renderSimpleErrorMessage (TypeConflictsWithClass nm) =
523503 line $ "Type " ++ runProperName nm ++ " conflicts with type class declaration of the same name"
524− goSimple (CtorConflictsWithClass nm) =
504+ renderSimpleErrorMessage (CtorConflictsWithClass nm) =
525505 line $ "Data constructor " ++ runProperName nm ++ " conflicts with type class declaration of the same name"
526− goSimple (ClassConflictsWithType nm) =
506+ renderSimpleErrorMessage (ClassConflictsWithType nm) =
527507 line $ "Type class " ++ runProperName nm ++ " conflicts with type declaration of the same name"
528− goSimple (ClassConflictsWithCtor nm) =
508+ renderSimpleErrorMessage (ClassConflictsWithCtor nm) =
529509 line $ "Type class " ++ runProperName nm ++ " conflicts with data constructor declaration of the same name"
530− goSimple (DuplicateModuleName mn) =
510+ renderSimpleErrorMessage (DuplicateModuleName mn) =
531511 line $ "Module " ++ runModuleName mn ++ " has been defined multiple times."
532− goSimple (DuplicateClassExport nm) =
512+ renderSimpleErrorMessage (DuplicateClassExport nm) =
533513 line $ "Duplicate export declaration for type class " ++ runProperName nm
534− goSimple (DuplicateValueExport nm) =
514+ renderSimpleErrorMessage (DuplicateValueExport nm) =
535515 line $ "Duplicate export declaration for value " ++ showIdent nm
536− goSimple (CycleInDeclaration nm) =
516+ renderSimpleErrorMessage (CycleInDeclaration nm) =
537517 line $ "Cycle in declaration of " ++ showIdent nm
538− goSimple (CycleInModules mns) =
518+ renderSimpleErrorMessage (CycleInModules mns) =
539519 line $ "Cycle in module dependencies: " ++ intercalate ", " (map runModuleName mns)
540− goSimple (CycleInTypeSynonym pn) =
520+ renderSimpleErrorMessage (CycleInTypeSynonym pn) =
541521 line $ "Cycle in type synonym" ++ foldMap ((" " ++) . runProperName) pn
542− goSimple (NameIsUndefined ident) =
522+ renderSimpleErrorMessage (NameIsUndefined ident) =
543523 line $ showIdent ident ++ " is undefined"
544− goSimple (NameNotInScope ident) =
524+ renderSimpleErrorMessage (NameNotInScope ident) =
545525 line $ showIdent ident ++ " may not be defined in the current scope"
546− goSimple (UndefinedTypeVariable name) =
526+ renderSimpleErrorMessage (UndefinedTypeVariable name) =
547527 line $ "Type variable " ++ runProperName name ++ " is undefined"
548− goSimple (PartiallyAppliedSynonym name) =
528+ renderSimpleErrorMessage (PartiallyAppliedSynonym name) =
549529 paras [ line $ "Partially applied type synonym " ++ showQualified runProperName name
550530 , line "Type synonyms must be applied to all of their type arguments."
551531 ]
552− goSimple (EscapedSkolem binding) =
532+ renderSimpleErrorMessage (EscapedSkolem binding) =
553533 paras $ [ line "A type variable has escaped its scope." ]
554534 <> foldMap (\expr -> [ line "Relevant expression: "
555− , indent $ line $ prettyPrintValue expr
535+ , indent $ prettyPrintValue expr
556536 ]) binding
557− goSimple (TypesDoNotUnify t1 t2)
537+ renderSimpleErrorMessage (TypesDoNotUnify t1 t2)
558538 = paras [ line "Cannot unify type"
559− , indent $ line $ prettyPrintType t1
539+ , indent $ typeAsBox t1
560540 , line "with type"
561− , indent $ line $ prettyPrintType t2
541+ , indent $ typeAsBox t2
562542 ]
563− goSimple (KindsDoNotUnify k1 k2) =
543+ renderSimpleErrorMessage (KindsDoNotUnify k1 k2) =
564544 paras [ line "Cannot unify kind"
565− , indent $ line $ prettyPrintKind k1
545+ , indent $ kindAsBox k1
566546 , line "with kind"
567− , indent $ line $ prettyPrintKind k2
547+ , indent $ kindAsBox k2
568548 ]
569− goSimple (ConstrainedTypeUnified t1 t2) =
549+ renderSimpleErrorMessage (ConstrainedTypeUnified t1 t2) =
570550 paras [ line "Cannot unify constrained type"
571− , indent $ line $ prettyPrintType t1
551+ , indent $ typeAsBox t1
572552 , line "with type"
573− , indent $ line $ prettyPrintType t2
574− ]
575− goSimple (OverlappingInstances nm ts (d : ds)) =
576− paras [ line $ "Overlapping instances found for " ++ showQualified runProperName nm ++ " " ++ unwords (map prettyPrintTypeAtom ts) ++ ":"
553+ , indent $ typeAsBox t2
554+ ]
555+ renderSimpleErrorMessage (OverlappingInstances nm ts (d : ds)) =
556+ paras [ line "Overlapping instances found for"
557+ , indent $ Box.hsep 1 Box.left [ line (showQualified runProperName nm)
558+ , Box.vcat Box.left (map typeAtomAsBox ts)
559+ ]
560+ , line "The following instances were found:"
577561 , indent $ paras (line (showQualified showIdent d ++ " (chosen)") : map (line . showQualified showIdent) ds)
578562 ]
579− goSimple OverlappingInstances{} = error "OverlappingInstances: empty instance list"
580− goSimple (NoInstanceFound nm ts) =
581− line $ "No instance found for " ++ showQualified runProperName nm ++ " " ++ unwords (map prettyPrintTypeAtom ts)
582− goSimple (PossiblyInfiniteInstance nm ts) =
583− line $ "Instance for " ++ showQualified runProperName nm ++ " " ++ unwords (map prettyPrintTypeAtom ts) ++ " is possibly infinite."
584− goSimple (CannotDerive nm ts) =
585− line $ "Cannot derive " ++ showQualified runProperName nm ++ " instance for " ++ unwords (map prettyPrintTypeAtom ts)
586− goSimple (CannotFindDerivingType nm) =
563+ renderSimpleErrorMessage OverlappingInstances{} = error "OverlappingInstances: empty instance list"
564+ renderSimpleErrorMessage (NoInstanceFound nm ts) =
565+ paras [ line "No instance found for"
566+ , indent $ Box.hsep 1 Box.left [ line (showQualified runProperName nm)
567+ , Box.vcat Box.left (map typeAtomAsBox ts)
568+ ]
569+ ]
570+ renderSimpleErrorMessage (PossiblyInfiniteInstance nm ts) =
571+ paras [ line "Instance for"
572+ , indent $ Box.hsep 1 Box.left [ line (showQualified runProperName nm)
573+ , Box.vcat Box.left (map typeAtomAsBox ts)
574+ ]
575+ , line "is possibly infinite."
576+ ]
577+ renderSimpleErrorMessage (CannotDerive nm ts) =
578+ paras [ line "Cannot derive an instance for"
579+ , indent $ Box.hsep 1 Box.left [ line (showQualified runProperName nm)
580+ , Box.vcat Box.left (map typeAtomAsBox ts)
581+ ]
582+ ]
583+ renderSimpleErrorMessage (CannotFindDerivingType nm) =
587584 line $ "Cannot derive instance, because the type declaration for " ++ runProperName nm ++ " could not be found."
588− goSimple (DuplicateLabel l expr) =
585+ renderSimpleErrorMessage (DuplicateLabel l expr) =
589586 paras $ [ line $ "Duplicate label " ++ show l ++ " in row." ]
590587 <> foldMap (\expr' -> [ line "Relevant expression: "
591− , indent $ line $ prettyPrintValue expr'
588+ , indent $ prettyPrintValue expr'
592589 ]) expr
593− goSimple (DuplicateTypeArgument name) =
590+ renderSimpleErrorMessage (DuplicateTypeArgument name) =
594591 line $ "Duplicate type argument " ++ show name
595− goSimple (DuplicateValueDeclaration nm) =
592+ renderSimpleErrorMessage (DuplicateValueDeclaration nm) =
596593 line $ "Duplicate value declaration for " ++ showIdent nm
597− goSimple (ArgListLengthsDiffer ident) =
594+ renderSimpleErrorMessage (ArgListLengthsDiffer ident) =
598595 line $ "Argument list lengths differ in declaration " ++ showIdent ident
599− goSimple (OverlappingArgNames ident) =
596+ renderSimpleErrorMessage (OverlappingArgNames ident) =
600597 line $ "Overlapping names in function/binder" ++ foldMap ((" in declaration" ++) . showIdent) ident
601− goSimple (MissingClassMember ident) =
598+ renderSimpleErrorMessage (MissingClassMember ident) =
602599 line $ "Member " ++ showIdent ident ++ " has not been implemented"
603− goSimple (ExtraneousClassMember ident) =
600+ renderSimpleErrorMessage (ExtraneousClassMember ident) =
604601 line $ "Member " ++ showIdent ident ++ " is not a member of the class being instantiated"
605− goSimple (ExpectedType ty kind) =
602+ renderSimpleErrorMessage (ExpectedType ty kind) =
606603 paras [ line "In a type-annotated expression x :: t, the type t must have kind *."
607− , line $ "The error arises from the type " ++ prettyPrintType ty ++ " having the kind " ++ prettyPrintKind kind ++ " instead."
604+ , line "The error arises from the type"
605+ , indent $ typeAsBox ty
606+ , line "having the kind"
607+ , indent $ kindAsBox kind
608+ , line "instead."
608609 ]
609− goSimple (IncorrectConstructorArity nm) =
610+ renderSimpleErrorMessage (IncorrectConstructorArity nm) =
610611 line $ "Wrong number of arguments to constructor " ++ showQualified runProperName nm
611− goSimple SubsumptionCheckFailed = line "Unable to check type subsumption"
612− goSimple (ExprDoesNotHaveType expr ty) =
612+ renderSimpleErrorMessage SubsumptionCheckFailed = line "Unable to check type subsumption"
613+ renderSimpleErrorMessage (ExprDoesNotHaveType expr ty) =
613614 paras [ line "Expression"
614− , indent $ line $ prettyPrintValue expr
615+ , indent $ prettyPrintValue expr
615616 , line "does not have type"
616− , indent $ line $ prettyPrintType ty
617+ , indent $ typeAsBox ty
617618 ]
618− goSimple (PropertyIsMissing prop row) =
619− line $ "Row " ++ prettyPrintRow row ++ " lacks required property " ++ show prop
620− goSimple (CannotApplyFunction fn arg) =
619+ renderSimpleErrorMessage (PropertyIsMissing prop row) =
620+ paras [ line "Row"
621+ , indent $ prettyPrintRowWith '(' ')' row
622+ , line $ "lacks required property " ++ show prop
623+ ]
624+ renderSimpleErrorMessage (CannotApplyFunction fn arg) =
621625 paras [ line "Cannot apply function of type"
622− , indent $ line $ prettyPrintType fn
626+ , indent $ typeAsBox fn
623627 , line "to argument"
624− , indent $ line $ prettyPrintValue arg
628+ , indent $ prettyPrintValue arg
625629 ]
626− goSimple TypeSynonymInstance =
630+ renderSimpleErrorMessage TypeSynonymInstance =
627631 line "Type synonym instances are disallowed"
628− goSimple (OrphanInstance nm cnm ts) =
629− paras [ line $ "Instance " ++ showIdent nm ++ " for " ++ showQualified runProperName cnm ++ " " ++ unwords (map prettyPrintTypeAtom ts) ++ " is an orphan instance."
632+ renderSimpleErrorMessage (OrphanInstance nm cnm ts) =
633+ paras [ line $ "Instance " ++ showIdent nm ++ " for "
634+ , indent $ Box.hsep 1 Box.left [ line (showQualified runProperName cnm)
635+ , Box.vcat Box.left (map typeAtomAsBox ts)
636+ ]
637+ , line "is an orphan instance."
630638 , line "An orphan instance is an instance which is defined in neither the class module nor the data type module."
631639 , line "Consider moving the instance, if possible, or using a newtype wrapper."
632640 ]
633− goSimple InvalidNewtype =
641+ renderSimpleErrorMessage InvalidNewtype =
634642 line "Newtypes must define a single constructor with a single argument"
635− goSimple (InvalidInstanceHead ty) =
643+ renderSimpleErrorMessage (InvalidInstanceHead ty) =
636644 paras [ line "Invalid type in class instance head:"
637− , indent $ line $ prettyPrintType ty
645+ , indent $ typeAsBox ty
638646 ]
639− goSimple (TransitiveExportError x ys) =
647+ renderSimpleErrorMessage (TransitiveExportError x ys) =
640648 paras $ line ("An export for " ++ prettyPrintExport x ++ " requires the following to also be exported: ")
641649 : map (line . prettyPrintExport) ys
642− goSimple (ShadowedName nm) =
650+ renderSimpleErrorMessage (ShadowedName nm) =
643651 line $ "Name '" ++ showIdent nm ++ "' was shadowed"
644− goSimple (ShadowedTypeVar tv) =
652+ renderSimpleErrorMessage (ShadowedTypeVar tv) =
645653 line $ "Type variable '" ++ tv ++ "' was shadowed"
646− goSimple (UnusedTypeVar tv) =
654+ renderSimpleErrorMessage (UnusedTypeVar tv) =
647655 line $ "Type variable '" ++ tv ++ "' was declared but not used"
648− goSimple (ClassOperator className opName) =
656+ renderSimpleErrorMessage (ClassOperator className opName) =
649657 paras [ line $ "Class '" ++ runProperName className ++ "' declares operator " ++ showIdent opName ++ "."
650− , indent $ line "This may be disallowed in the future - consider declaring a named member in the class and making the operator an alias:"
651− , indent $ line $ showIdent opName ++ " = someMember"
658+ , line "This may be disallowed in the future - consider declaring a named member in the class and making the operator an alias:"
659+ , indent . line $ showIdent opName ++ " = someMember"
652660 ]
653− goSimple (MisleadingEmptyTypeImport mn name) =
661+ renderSimpleErrorMessage (MisleadingEmptyTypeImport mn name) =
654662 line $ "Importing type " ++ runProperName name ++ "(..) from " ++ runModuleName mn ++ " is misleading as it has no exported data constructors"
655− goSimple (ImportHidingModule name) =
663+ renderSimpleErrorMessage (ImportHidingModule name) =
656664 line $ "Attempted to hide module " ++ runModuleName name ++ " in import expression, this is not permitted"
657− goSimple (WildcardInferredType ty) =
658− line $ "The wildcard type definition has the inferred type " ++ prettyPrintType ty
659− goSimple (NotExhaustivePattern bs b) =
665+ renderSimpleErrorMessage (WildcardInferredType ty) =
666+ paras [ line "The wildcard type definition has the inferred type "
667+ , indent $ typeAsBox ty
668+ ]
669+ renderSimpleErrorMessage (MissingTypeDeclaration ident) =
670+ paras [ line $ "No type declaration was provided for the top-level declaration of " ++ showIdent ident ++ "."
671+ , line "It is good practice to provide type declarations as a form of documentation."
672+ , line "Consider using a type wildcard to display the inferred type:"
673+ , indent $ line $ showIdent ident ++ " :: _"
674+ ]
675+ renderSimpleErrorMessage (NotExhaustivePattern bs b) =
660676 paras $ [ line "A case expression could not be determined to cover all inputs."
661677 , line "The following additional cases are required to cover all inputs:\n"
662678 , Box.hsep 1 Box.left (map (paras . map (line . prettyPrintBinderAtom)) (transpose bs))
663679 ] ++
664680 [ line "..." | not b ]
665− goSimple (OverlappingPattern bs b) =
681+ renderSimpleErrorMessage (OverlappingPattern bs b) =
666682 paras $ [ line "A case expression contains unreachable cases:\n"
667683 , Box.hsep 1 Box.left (map (paras . map (line . prettyPrintBinderAtom)) (transpose bs))
668684 ] ++
669685 [ line "..." | not b ]
670− goSimple IncompleteExhaustivityCheck =
686+ renderSimpleErrorMessage IncompleteExhaustivityCheck =
671687 paras [ line "An exhaustivity check was abandoned due to too many possible cases."
672688 , line "You may want to decomposing your data types into smaller types."
673689 ]
674− go (NotYetDefined names err) =
675− paras [ line $ "The following are not yet defined here: " ++ intercalate ", " (map showIdent names) ++ ":"
676− , indent $ go err
677− ]
678− go (ErrorUnifyingTypes t1 t2 err) =
690+
691+ renderHint :: ErrorMessageHint -> Box.Box
692+ renderHint (NotYetDefined names) =
693+ line $ "The following are not yet defined here: " ++ intercalate ", " (map showIdent names) ++ ":"
694+ renderHint (ErrorUnifyingTypes t1 t2) =
679695 paras [ lineWithLevel "unifying type "
680− , indent $ line $ prettyPrintType t1
696+ , indent $ typeAsBox t1
681697 , line "with type"
682− , indent $ line $ prettyPrintType t2
683− , go err
698+ , indent $ typeAsBox t2
684699 ]
685− go (ErrorInExpression expr err) =
700+ renderHint (ErrorInExpression expr) =
686701 paras [ lineWithLevel "in expression:"
687− , indent $ line $ prettyPrintValue expr
688− , go err
702+ , indent $ prettyPrintValue expr
689703 ]
690− go (ErrorInModule mn err) =
704+ renderHint (ErrorInModule mn) =
691705 paras [ lineWithLevel $ "in module " ++ runModuleName mn ++ ":"
692− , go err
693706 ]
694− go (ErrorInSubsumption t1 t2 err) =
707+ renderHint (ErrorInSubsumption t1 t2) =
695708 paras [ lineWithLevel "checking that type "
696− , indent $ line $ prettyPrintType t1
709+ , indent $ typeAsBox t1
697710 , line "subsumes type"
698− , indent $ line $ prettyPrintType t2
699− , go err
711+ , indent $ typeAsBox t2
700712 ]
701− go (ErrorInInstance name ts err) =
702− paras [ lineWithLevel $ "in type class instance " ++ showQualified runProperName name ++ " " ++ unwords (map prettyPrintTypeAtom ts) ++ ":"
703− , go err
713+ renderHint (ErrorInInstance nm ts) =
714+ paras [ lineWithLevel "in type class instance"
715+ , indent $ Box.hsep 1 Box.left [ line (showQualified runProperName nm)
716+ , Box.vcat Box.left (map typeAtomAsBox ts)
717+ ]
704718 ]
705− go (ErrorCheckingKind ty err) =
719+ renderHint (ErrorCheckingKind ty) =
706720 paras [ lineWithLevel "checking kind of type "
707− , indent $ line $ prettyPrintType ty
708− , go err
721+ , indent $ typeAsBox ty
709722 ]
710− go (ErrorInferringType expr err) =
723+ renderHint (ErrorInferringType expr) =
711724 paras [ lineWithLevel "inferring type of value "
712− , indent $ line $ prettyPrintValue expr
713− , go err
725+ , indent $ prettyPrintValue expr
714726 ]
715− go (ErrorCheckingType expr ty err) =
727+ renderHint (ErrorCheckingType expr ty) =
716728 paras [ lineWithLevel "checking that value "
717− , indent $ line $ prettyPrintValue expr
729+ , indent $ prettyPrintValue expr
718730 , line "has type"
719− , indent $ line $ prettyPrintType ty
720− , go err
731+ , indent $ typeAsBox ty
721732 ]
722− go (ErrorInApplication f t a err) =
733+ renderHint (ErrorInApplication f t a) =
723734 paras [ lineWithLevel "applying function"
724− , indent $ line $ prettyPrintValue f
735+ , indent $ prettyPrintValue f
725736 , line "of type"
726− , indent $ line $ prettyPrintType t
737+ , indent $ typeAsBox t
727738 , line "to argument"
728− , indent $ line $ prettyPrintValue a
729− , go err
730− ]
731− go (ErrorInDataConstructor nm err) =
732− paras [ lineWithLevel $ "in data constructor " ++ runProperName nm ++ ":"
733− , go err
734− ]
735− go (ErrorInTypeConstructor nm err) =
736− paras [ lineWithLevel $ "in type constructor " ++ runProperName nm ++ ":"
737− , go err
738− ]
739− go (ErrorInBindingGroup nms err) =
740− paras [ lineWithLevel $ "in binding group " ++ intercalate ", " (map showIdent nms) ++ ":"
741− , go err
742− ]
743− go (ErrorInDataBindingGroup err) =
744− paras [ lineWithLevel "in data binding group:"
745− , go err
746− ]
747− go (ErrorInTypeSynonym name err) =
748− paras [ lineWithLevel $ "in type synonym " ++ runProperName name ++ ":"
749− , go err
750− ]
751− go (ErrorInValueDeclaration n err) =
752− paras [ lineWithLevel $ "in value declaration " ++ showIdent n ++ ":"
753− , go err
754− ]
755− go (ErrorInTypeDeclaration n err) =
756− paras [ lineWithLevel $ "in type declaration for " ++ showIdent n ++ ":"
757− , go err
758− ]
759− go (ErrorInForeignImport nm err) =
760− paras [ lineWithLevel $ "in foreign import " ++ showIdent nm ++ ":"
761− , go err
762− ]
763− go (PositionedError srcSpan err) =
764− paras [ lineWithLevel $ "at " ++ displaySourceSpan srcSpan ++ ":"
765− , indent $ go err
766− ]
767− go (SimpleErrorWrapper sem) = goSimple sem
739+ , indent $ prettyPrintValue a
740+ ]
741+ renderHint (ErrorInDataConstructor nm) =
742+ lineWithLevel $ "in data constructor " ++ runProperName nm ++ ":"
743+ renderHint (ErrorInTypeConstructor nm) =
744+ lineWithLevel $ "in type constructor " ++ runProperName nm ++ ":"
745+ renderHint (ErrorInBindingGroup nms) =
746+ lineWithLevel $ "in binding group " ++ intercalate ", " (map showIdent nms) ++ ":"
747+ renderHint ErrorInDataBindingGroup =
748+ lineWithLevel "in data binding group:"
749+ renderHint (ErrorInTypeSynonym name) =
750+ lineWithLevel $ "in type synonym " ++ runProperName name ++ ":"
751+ renderHint (ErrorInValueDeclaration n) =
752+ lineWithLevel $ "in value declaration " ++ showIdent n ++ ":"
753+ renderHint (ErrorInTypeDeclaration n) =
754+ lineWithLevel $ "in type declaration for " ++ showIdent n ++ ":"
755+ renderHint (ErrorInForeignImport nm) =
756+ lineWithLevel $ "in foreign import " ++ showIdent nm ++ ":"
757+ renderHint (PositionedError srcSpan) =
758+ lineWithLevel $ "at " ++ displaySourceSpan srcSpan ++ ":"
768759
769760 lineWithLevel :: String -> Box.Box
770761 lineWithLevel text = line $ show level ++ " " ++ text
@@ -774,23 +765,19 @@ prettyPrintSingleError full level e = prettyPrintErrorMessage <$> onTypesInError
774765 Error -> "error"
775766 Warning -> "warning"
776767
777− suggestions :: ErrorMessage -> [Box.Box]
778− suggestions = suggestions' . unwrapErrorMessage
779− where
780− suggestions' (ConflictingImport nm im) = [ line $ "Possible fix: hide " ++ show nm ++ " when importing " ++ runModuleName im ++ ":"
768+ suggestions :: SimpleErrorMessage -> [Box.Box]
769+ suggestions (ConflictingImport nm im) = [ line $ "Possible fix: hide " ++ show nm ++ " when importing " ++ runModuleName im ++ ":"
781770 , indent . line $ "import " ++ runModuleName im ++ " hiding (" ++ nm ++ ")"
782771 ]
783− suggestions' (TypesDoNotUnify t1 t2)
784− | isObject t1 && isFunction t2 = [line "Note that function composition in PureScript is defined using (<<<)"]
785− | otherwise = []
786− suggestions' _ = []
772+ suggestions (TypesDoNotUnify t1 t2)
773+ | isObject t1 && isFunction t2 = [line "Note that function composition in PureScript is defined using (<<<)"]
774+ | otherwise = []
775+ suggestions _ = []
787776
788777 paras :: [Box.Box] -> Box.Box
789778 paras = Box.vcat Box.left
790779
791− -- |
792780 -- Pretty print and export declaration
793− --
794781 prettyPrintExport :: DeclarationRef -> String
795782 prettyPrintExport (TypeRef pn _) = runProperName pn
796783 prettyPrintExport (ValueRef ident) = showIdent ident
@@ -799,38 +786,44 @@ prettyPrintSingleError full level e = prettyPrintErrorMessage <$> onTypesInError
799786 prettyPrintExport (ModuleRef name) = "module " ++ runModuleName name
800787 prettyPrintExport (PositionedDeclarationRef _ _ ref) = prettyPrintExport ref
801788
802− -- |
803− -- Simplify an error message
804− --
805− simplifyErrorMessage :: ErrorMessage -> ErrorMessage
806− simplifyErrorMessage = unwrap Nothing
789+ -- Hints get added at the front, so we need to reverse them before rendering
790+ reverseHints :: ErrorMessage -> ErrorMessage
791+ reverseHints (ErrorMessage hints simple) = ErrorMessage (reverse hints) simple
792+
793+ -- | Put positional hints at the front of the list
794+ positionHintsFirst :: ErrorMessage -> ErrorMessage
795+ positionHintsFirst (ErrorMessage hints simple) = ErrorMessage (uncurry (++) $ partition (isPositionHint . hintCategory) hints) simple
807796 where
808− unwrap :: Maybe SourceSpan -> ErrorMessage -> ErrorMessage
809− unwrap pos (ErrorInExpression _ err) = unwrap pos err
810− unwrap pos (ErrorInInstance name ts err) = ErrorInInstance name ts (unwrap pos err)
811− unwrap pos (ErrorInSubsumption t1 t2 err) = ErrorInSubsumption t1 t2 (unwrap pos err)
812− unwrap pos (ErrorUnifyingTypes _ _ err) = unwrap pos err
813− unwrap pos (ErrorInferringType _ err) = unwrap pos err
814− unwrap pos (ErrorCheckingType _ _ err) = unwrap pos err
815− unwrap pos (ErrorCheckingKind ty err) = ErrorCheckingKind ty (unwrap pos err)
816− unwrap pos (ErrorInModule mn err) = ErrorInModule mn (unwrap pos err)
817− unwrap pos (ErrorInApplication _ _ _ err) = unwrap pos err
818− unwrap pos (ErrorInDataConstructor nm err) = ErrorInDataConstructor nm (unwrap pos err)
819− unwrap pos (ErrorInTypeConstructor nm err) = ErrorInTypeConstructor nm (unwrap pos err)
820− unwrap pos (ErrorInBindingGroup nms err) = ErrorInBindingGroup nms (unwrap pos err)
821− unwrap pos (ErrorInDataBindingGroup err) = ErrorInDataBindingGroup (unwrap pos err)
822− unwrap pos (ErrorInTypeSynonym nm err) = ErrorInTypeSynonym nm (unwrap pos err)
823− unwrap pos (ErrorInValueDeclaration nm err) = ErrorInValueDeclaration nm (unwrap pos err)
824− unwrap pos (ErrorInTypeDeclaration nm err) = ErrorInTypeDeclaration nm (unwrap pos err)
825− unwrap pos (ErrorInForeignImport nm err) = ErrorInForeignImport nm (unwrap pos err)
826− unwrap pos (NotYetDefined ns err) = NotYetDefined ns (unwrap pos err)
827− unwrap _ (PositionedError pos err) = unwrap (Just pos) err
828− unwrap pos other = wrap pos other
829−
830− wrap :: Maybe SourceSpan -> ErrorMessage -> ErrorMessage
831− wrap Nothing = id
832− wrap (Just pos) = PositionedError pos
797+ isPositionHint :: HintCategory -> Bool
798+ isPositionHint PositionHint = True
799+ isPositionHint OtherHint = True
800+ isPositionHint _ = False
833801
802+ -- | Simplify an error message
803+ simplifyErrorMessage :: ErrorMessage -> ErrorMessage
804+ simplifyErrorMessage (ErrorMessage hints simple) = ErrorMessage (simplifyHints hints) simple
805+ where
806+ -- Take the last instance of each "hint category"
807+ simplifyHints :: [ErrorMessageHint] -> [ErrorMessageHint]
808+ simplifyHints = reverse . nubBy categoriesEqual . reverse
809+
810+ -- Don't remove hints in the "other" category
811+ categoriesEqual :: ErrorMessageHint -> ErrorMessageHint -> Bool
812+ categoriesEqual x y =
813+ case (hintCategory x, hintCategory y) of
814+ (OtherHint, _) -> False
815+ (_, OtherHint) -> False
816+ (c1, c2) -> c1 == c2
817+
818+ hintCategory :: ErrorMessageHint -> HintCategory
819+ hintCategory ErrorCheckingType{} = ExprHint
820+ hintCategory ErrorInferringType{} = ExprHint
821+ hintCategory ErrorInExpression{} = ExprHint
822+ hintCategory ErrorUnifyingTypes{} = CheckHint
823+ hintCategory ErrorInSubsumption{} = CheckHint
824+ hintCategory ErrorInApplication{} = CheckHint
825+ hintCategory PositionedError{} = PositionHint
826+ hintCategory _ = OtherHint
834827
835828-- |
836829-- Pretty print multiple errors
@@ -846,11 +839,11 @@ prettyPrintMultipleWarnings full = renderBox . prettyPrintMultipleWarningsBox fu
846839
847840-- | Pretty print warnings as a Box
848841prettyPrintMultipleWarningsBox :: Bool -> MultipleErrors -> Box.Box
849−prettyPrintMultipleWarningsBox full = flip evalState M.empty . prettyPrintMultipleErrorsWith Warning "Warning found:" "Multiple warnings found:" full
842+prettyPrintMultipleWarningsBox full = flip evalState M.empty . prettyPrintMultipleErrorsWith Warning "Warning found:" "Warning" full
850843
851844-- | Pretty print errors as a Box
852845prettyPrintMultipleErrorsBox :: Bool -> MultipleErrors -> Box.Box
853−prettyPrintMultipleErrorsBox full = flip evalState M.empty . prettyPrintMultipleErrorsWith Error "Error found:" "Multiple errors found:" full
846+prettyPrintMultipleErrorsBox full = flip evalState M.empty . prettyPrintMultipleErrorsWith Error "Error found:" "Error" full
854847
855848prettyPrintMultipleErrorsWith :: Level -> String -> String -> Bool -> MultipleErrors -> State UnknownMap Box.Box
856849prettyPrintMultipleErrorsWith level intro _ full (MultipleErrors [e]) = do
@@ -860,11 +853,12 @@ prettyPrintMultipleErrorsWith level intro _ full (MultipleErrors [e]) = do
860853 , result
861854 ]
862855prettyPrintMultipleErrorsWith level _ intro full (MultipleErrors es) = do
863− result <- forM es $ liftM (Box.moveRight 2) . prettyPrintSingleError full level
864− return $
865− Box.vcat Box.left [ Box.text intro
866− , Box.vsep 1 Box.left result
867− ]
856+ result <- forM es $ prettyPrintSingleError full level
857+ return $ Box.vsep 1 Box.left $ concat $ zipWith withIntro [1 :: Int ..] result
858+ where
859+ withIntro i err = [ Box.text (intro ++ " " ++ show i ++ " of " ++ show (length es) ++ ":")
860+ , Box.moveRight 2 err
861+ ]
868862
869863-- | Pretty print a Parsec ParseError as a Box
870864prettyPrintParseError :: P.ParseError -> Box.Box
@@ -914,8 +908,9 @@ prettyPrintParseErrorMessages msgOr msgUnknown msgExpecting msgUnExpected msgEnd
914908
915909 clean = nub . filter (not . null)
916910
911+-- | Indent to the right, and pad on top and bottom.
917912indent :: Box.Box -> Box.Box
918−indent = Box.moveRight 2
913+indent = Box.moveUp 1 . Box.moveDown 1 . Box.moveRight 2
919914
920915line :: String -> Box.Box
921916line = Box.text
@@ -955,8 +950,7 @@ warnAndRethrowWithPosition :: (MonadError MultipleErrors m, MonadWriter Multiple
955950warnAndRethrowWithPosition pos = rethrowWithPosition pos . warnWithPosition pos
956951
957952withPosition :: SourceSpan -> ErrorMessage -> ErrorMessage
958−withPosition _ (PositionedError pos err) = withPosition pos err
959−withPosition pos err = PositionedError pos err
953+withPosition pos (ErrorMessage hints se) = ErrorMessage (PositionedError pos : hints) se
960954
961955-- |
962956-- Collect errors in in parallel