Skip to content

Commit 4828b5d

Browse files
authored
Improved error messages in the constraint solver (purescript#2230)
Behold: No type class instance was found for Issue1310.Inject (Eff ( console :: CONSOLE | t0 ) ) (Eff ( oops :: Oops | eff1 ) ) The instance head contains unknown type variables. Consider adding a type annotation. while checking that expression inj (log "Oops") has type forall eff. Eff ( oops :: Oops | eff ) Unit while applying a function inj of type forall a g f. (Inject f g) => f a -> g a to argument log "Oops" in value declaration main where eff1 is a rigid type variable bound at line 18, column 1 - line 18, column 23 t0 is an unknown type
1 parent 0824b1f commit 4828b5d

37 files changed

+328
-250
lines changed

src/Language/PureScript/AST/Binders.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ data Binder
6161
-- A binder with a type annotation
6262
--
6363
| TypedBinder Type Binder
64-
deriving (Show, Read, Eq)
64+
deriving (Show, Eq)
6565

6666
-- |
6767
-- Collect all names introduced in binders in an expression

src/Language/PureScript/AST/Declarations.hs

Lines changed: 153 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,153 @@ import Language.PureScript.Kinds
2222
import Language.PureScript.TypeClassDictionaries
2323
import Language.PureScript.Comments
2424
import Language.PureScript.Environment
25+
import qualified Language.PureScript.Bundle as Bundle
26+
27+
import qualified Text.Parsec as P
28+
29+
-- | A type of error messages
30+
data SimpleErrorMessage
31+
= ErrorParsingFFIModule FilePath (Maybe Bundle.ErrorMessage)
32+
| ErrorParsingModule P.ParseError
33+
| MissingFFIModule ModuleName
34+
| MultipleFFIModules ModuleName [FilePath]
35+
| UnnecessaryFFIModule ModuleName FilePath
36+
| MissingFFIImplementations ModuleName [Ident]
37+
| UnusedFFIImplementations ModuleName [Ident]
38+
| InvalidFFIIdentifier ModuleName String
39+
| CannotGetFileInfo FilePath
40+
| CannotReadFile FilePath
41+
| CannotWriteFile FilePath
42+
| InfiniteType Type
43+
| InfiniteKind Kind
44+
| MultipleValueOpFixities (OpName 'ValueOpName)
45+
| MultipleTypeOpFixities (OpName 'TypeOpName)
46+
| OrphanTypeDeclaration Ident
47+
| RedefinedModule ModuleName [SourceSpan]
48+
| RedefinedIdent Ident
49+
| OverlappingNamesInLet
50+
| UnknownName (Qualified Name)
51+
| UnknownImport ModuleName Name
52+
| UnknownImportDataConstructor ModuleName (ProperName 'TypeName) (ProperName 'ConstructorName)
53+
| UnknownExport Name
54+
| UnknownExportDataConstructor (ProperName 'TypeName) (ProperName 'ConstructorName)
55+
| ScopeConflict Name [ModuleName]
56+
| ScopeShadowing Name (Maybe ModuleName) [ModuleName]
57+
| DeclConflict Name Name
58+
| ExportConflict (Qualified Name) (Qualified Name)
59+
| DuplicateModuleName ModuleName
60+
| DuplicateTypeArgument String
61+
| InvalidDoBind
62+
| InvalidDoLet
63+
| CycleInDeclaration Ident
64+
| CycleInTypeSynonym (Maybe (ProperName 'TypeName))
65+
| CycleInModules [ModuleName]
66+
| NameIsUndefined Ident
67+
| UndefinedTypeVariable (ProperName 'TypeName)
68+
| PartiallyAppliedSynonym (Qualified (ProperName 'TypeName))
69+
| EscapedSkolem (Maybe Expr)
70+
| TypesDoNotUnify Type Type
71+
| KindsDoNotUnify Kind Kind
72+
| ConstrainedTypeUnified Type Type
73+
| OverlappingInstances (Qualified (ProperName 'ClassName)) [Type] [Qualified Ident]
74+
| NoInstanceFound Constraint
75+
| PossiblyInfiniteInstance (Qualified (ProperName 'ClassName)) [Type]
76+
| CannotDerive (Qualified (ProperName 'ClassName)) [Type]
77+
| CannotFindDerivingType (ProperName 'TypeName)
78+
| DuplicateLabel String (Maybe Expr)
79+
| DuplicateValueDeclaration Ident
80+
| ArgListLengthsDiffer Ident
81+
| OverlappingArgNames (Maybe Ident)
82+
| MissingClassMember Ident
83+
| ExtraneousClassMember Ident (Qualified (ProperName 'ClassName))
84+
| ExpectedType Type Kind
85+
| IncorrectConstructorArity (Qualified (ProperName 'ConstructorName))
86+
| ExprDoesNotHaveType Expr Type
87+
| PropertyIsMissing String
88+
| AdditionalProperty String
89+
| CannotApplyFunction Type Expr
90+
| TypeSynonymInstance
91+
| OrphanInstance Ident (Qualified (ProperName 'ClassName)) [Type]
92+
| InvalidNewtype (ProperName 'TypeName)
93+
| InvalidInstanceHead Type
94+
| TransitiveExportError DeclarationRef [DeclarationRef]
95+
| TransitiveDctorExportError DeclarationRef (ProperName 'ConstructorName)
96+
| ShadowedName Ident
97+
| ShadowedTypeVar String
98+
| UnusedTypeVar String
99+
| WildcardInferredType Type
100+
| HoleInferredType String Type [(Ident, Type)]
101+
| MissingTypeDeclaration Ident Type
102+
| OverlappingPattern [[Binder]] Bool
103+
| IncompleteExhaustivityCheck
104+
| MisleadingEmptyTypeImport ModuleName (ProperName 'TypeName)
105+
| ImportHidingModule ModuleName
106+
| UnusedImport ModuleName
107+
| UnusedExplicitImport ModuleName [String] (Maybe ModuleName) [DeclarationRef]
108+
| UnusedDctorImport (ProperName 'TypeName)
109+
| UnusedDctorExplicitImport (ProperName 'TypeName) [ProperName 'ConstructorName]
110+
| DuplicateSelectiveImport ModuleName
111+
| DuplicateImport ModuleName ImportDeclarationType (Maybe ModuleName)
112+
| DuplicateImportRef Name
113+
| DuplicateExportRef Name
114+
| IntOutOfRange Integer String Integer Integer
115+
| ImplicitQualifiedImport ModuleName ModuleName [DeclarationRef]
116+
| ImplicitImport ModuleName [DeclarationRef]
117+
| HidingImport ModuleName [DeclarationRef]
118+
| CaseBinderLengthDiffers Int [Binder]
119+
| IncorrectAnonymousArgument
120+
| InvalidOperatorInBinder (Qualified (OpName 'ValueOpName)) (Qualified Ident)
121+
| DeprecatedRequirePath
122+
| CannotGeneralizeRecursiveFunction Ident Type
123+
deriving (Show)
124+
125+
-- | Error message hints, providing more detailed information about failure.
126+
data ErrorMessageHint
127+
= ErrorUnifyingTypes Type Type
128+
| ErrorInExpression Expr
129+
| ErrorInModule ModuleName
130+
| ErrorInInstance (Qualified (ProperName 'ClassName)) [Type]
131+
| ErrorInSubsumption Type Type
132+
| ErrorCheckingAccessor Expr String
133+
| ErrorCheckingType Expr Type
134+
| ErrorCheckingKind Type
135+
| ErrorCheckingGuard
136+
| ErrorInferringType Expr
137+
| ErrorInApplication Expr Type Expr
138+
| ErrorInDataConstructor (ProperName 'ConstructorName)
139+
| ErrorInTypeConstructor (ProperName 'TypeName)
140+
| ErrorInBindingGroup [Ident]
141+
| ErrorInDataBindingGroup
142+
| ErrorInTypeSynonym (ProperName 'TypeName)
143+
| ErrorInValueDeclaration Ident
144+
| ErrorInTypeDeclaration Ident
145+
| ErrorInForeignImport Ident
146+
| ErrorSolvingConstraint Constraint
147+
| PositionedError SourceSpan
148+
deriving (Show)
149+
150+
-- | Categories of hints
151+
data HintCategory
152+
= ExprHint
153+
| KindHint
154+
| CheckHint
155+
| PositionHint
156+
| SolverHint
157+
| OtherHint
158+
deriving (Show, Eq)
159+
160+
data ErrorMessage = ErrorMessage
161+
[ErrorMessageHint]
162+
SimpleErrorMessage
163+
deriving (Show)
25164

26165
-- |
27166
-- A module declaration, consisting of comments about the module, a module name,
28167
-- a list of declarations, and a list of the declarations that are
29168
-- explicitly exported. If the export list is Nothing, everything is exported.
30169
--
31170
data Module = Module SourceSpan [Comment] ModuleName [Declaration] (Maybe [DeclarationRef])
32-
deriving (Show, Read)
171+
deriving (Show)
33172

34173
-- | Return a module's name.
35174
getModuleName :: Module -> ModuleName
@@ -88,7 +227,7 @@ data DeclarationRef
88227
-- A declaration reference with source position information
89228
--
90229
| PositionedDeclarationRef SourceSpan [Comment] DeclarationRef
91-
deriving (Show, Read)
230+
deriving (Show)
92231

93232
instance Eq DeclarationRef where
94233
(TypeRef name dctors) == (TypeRef name' dctors') = name == name' && dctors == dctors'
@@ -149,7 +288,7 @@ data ImportDeclarationType
149288
-- An import with a list of references to hide: `import M hiding (foo)`
150289
--
151290
| Hiding [DeclarationRef]
152-
deriving (Eq, Show, Read)
291+
deriving (Eq, Show)
153292

154293
isImplicit :: ImportDeclarationType -> Bool
155294
isImplicit Implicit = True
@@ -216,15 +355,15 @@ data Declaration
216355
-- A declaration with source position information
217356
--
218357
| PositionedDeclaration SourceSpan [Comment] Declaration
219-
deriving (Show, Read)
358+
deriving (Show)
220359

221360
data ValueFixity = ValueFixity Fixity (Qualified (Either Ident (ProperName 'ConstructorName))) (OpName 'ValueOpName)
222-
deriving (Eq, Ord, Show, Read)
361+
deriving (Eq, Ord, Show)
223362

224363
data TypeFixity = TypeFixity Fixity (Qualified (ProperName 'TypeName)) (OpName 'TypeOpName)
225-
deriving (Eq, Ord, Show, Read)
364+
deriving (Eq, Ord, Show)
226365

227-
pattern ValueFixityDeclaration :: Fixity -> Qualified (Either Ident (ProperName 'ConstructorName)) -> OpName 'ValueOpName -> Declaration
366+
pattern ValueFixityDeclaration :: Fixity -> Qualified (Either Ident (ProperName 'ConstructorName)) -> OpName 'ValueOpName -> Declaration
228367
pattern ValueFixityDeclaration fixity name op = FixityDeclaration (Left (ValueFixity fixity name op))
229368

230369
pattern TypeFixityDeclaration :: Fixity -> Qualified (ProperName 'TypeName) -> OpName 'TypeOpName -> Declaration
@@ -236,7 +375,7 @@ data TypeInstanceBody
236375
= DerivedInstance
237376
-- | This is a regular (explicit) instance
238377
| ExplicitInstance [Declaration]
239-
deriving (Show, Read)
378+
deriving (Show)
240379

241380
mapTypeInstanceBody :: ([Declaration] -> [Declaration]) -> TypeInstanceBody -> TypeInstanceBody
242381
mapTypeInstanceBody f = runIdentity . traverseTypeInstanceBody (Identity . f)
@@ -421,7 +560,9 @@ data Expr
421560
-- at superclass implementations when searching for a dictionary, the type class name and
422561
-- instance type, and the type class dictionaries in scope.
423562
--
424-
| TypeClassDictionary Constraint (M.Map (Maybe ModuleName) (M.Map (Qualified (ProperName 'ClassName)) (M.Map (Qualified Ident) TypeClassDictionaryInScope)))
563+
| TypeClassDictionary Constraint
564+
(M.Map (Maybe ModuleName) (M.Map (Qualified (ProperName 'ClassName)) (M.Map (Qualified Ident) TypeClassDictionaryInScope)))
565+
[ErrorMessageHint]
425566
-- |
426567
-- A typeclass dictionary accessor, the implementation is left unspecified until CoreFn desugaring.
427568
--
@@ -442,7 +583,7 @@ data Expr
442583
-- A value with source position information
443584
--
444585
| PositionedValue SourceSpan [Comment] Expr
445-
deriving (Show, Read)
586+
deriving (Show)
446587

447588
-- |
448589
-- An alternative in a case statement
@@ -456,7 +597,7 @@ data CaseAlternative = CaseAlternative
456597
-- The result expression or a collect of guarded expressions
457598
--
458599
, caseAlternativeResult :: Either [(Guard, Expr)] Expr
459-
} deriving (Show, Read)
600+
} deriving (Show)
460601

461602
-- |
462603
-- A statement in a do-notation block
@@ -478,7 +619,7 @@ data DoNotationElement
478619
-- A do notation element with source position information
479620
--
480621
| PositionedDoNotationElement SourceSpan [Comment] DoNotationElement
481-
deriving (Show, Read)
622+
deriving (Show)
482623

483624
$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''DeclarationRef)
484625
$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''ImportDeclarationType)

src/Language/PureScript/AST/Literals.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ data Literal a
3434
-- An object literal
3535
--
3636
| ObjectLiteral [(String, a)]
37-
deriving (Eq, Ord, Show, Read, Functor)
37+
deriving (Eq, Ord, Show, Functor)

src/Language/PureScript/AST/Operators.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Precedence = Integer
2121
-- Associativity for infix operators
2222
--
2323
data Associativity = Infixl | Infixr | Infix
24-
deriving (Show, Read, Eq, Ord)
24+
deriving (Show, Eq, Ord)
2525

2626
showAssoc :: Associativity -> String
2727
showAssoc Infixl = "infixl"
@@ -44,7 +44,7 @@ instance A.FromJSON Associativity where
4444
-- Fixity data for infix operators
4545
--
4646
data Fixity = Fixity Associativity Precedence
47-
deriving (Show, Read, Eq, Ord)
47+
deriving (Show, Eq, Ord)
4848

4949
instance A.ToJSON Fixity where
5050
toJSON (Fixity associativity precedence) =

src/Language/PureScript/AST/SourcePos.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ data SourcePos = SourcePos
2222
-- Column number
2323
--
2424
, sourcePosColumn :: Int
25-
} deriving (Show, Read, Eq, Ord)
25+
} deriving (Show, Eq, Ord)
2626

2727
displaySourcePos :: SourcePos -> String
2828
displaySourcePos sp =
@@ -50,7 +50,7 @@ data SourceSpan = SourceSpan
5050
-- End of the span
5151
--
5252
, spanEnd :: SourcePos
53-
} deriving (Show, Read, Eq, Ord)
53+
} deriving (Show, Eq, Ord)
5454

5555
displayStartEndPos :: SourceSpan -> String
5656
displayStartEndPos sp =

src/Language/PureScript/AST/Traversals.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ accumTypes f = everythingOnValues mappend forDecls forValues (const mempty) (con
582582
forDecls (TypeDeclaration _ ty) = f ty
583583
forDecls _ = mempty
584584

585-
forValues (TypeClassDictionary c _) = mconcat (map f (constraintArgs c))
585+
forValues (TypeClassDictionary c _ _) = mconcat (map f (constraintArgs c))
586586
forValues (SuperClassDictionary _ tys) = mconcat (map f tys)
587587
forValues (TypedValue _ _ ty) = f ty
588588
forValues _ = mempty

src/Language/PureScript/Bundle.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,20 @@ data ErrorMessage
4343
| UnableToParseModule String
4444
| UnsupportedExport
4545
| ErrorInModule ModuleIdentifier ErrorMessage
46-
deriving (Show, Read)
46+
deriving (Show)
4747

4848
-- | Modules are either "regular modules" (i.e. those generated by psc) or foreign modules.
4949
data ModuleType
5050
= Regular
5151
| Foreign
52-
deriving (Show, Read, Eq, Ord)
52+
deriving (Show, Eq, Ord)
5353

5454
showModuleType :: ModuleType -> String
5555
showModuleType Regular = "Regular"
5656
showModuleType Foreign = "Foreign"
5757

5858
-- | A module is identified by its module name and its type.
59-
data ModuleIdentifier = ModuleIdentifier String ModuleType deriving (Show, Read, Eq, Ord)
59+
data ModuleIdentifier = ModuleIdentifier String ModuleType deriving (Show, Eq, Ord)
6060

6161
moduleName :: ModuleIdentifier -> String
6262
moduleName (ModuleIdentifier name _) = name

src/Language/PureScript/CodeGen/JS.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import Language.PureScript.CodeGen.JS.Common as Common
2727
import Language.PureScript.CodeGen.JS.Optimizer
2828
import Language.PureScript.CoreFn
2929
import Language.PureScript.Crash
30-
import Language.PureScript.Errors
30+
import Language.PureScript.Errors (ErrorMessageHint(..), SimpleErrorMessage(..),
31+
MultipleErrors(..), rethrow,
32+
errorMessage, rethrowWithPosition, addHint)
3133
import Language.PureScript.Names
3234
import Language.PureScript.Options
3335
import Language.PureScript.Traversals (sndM)

src/Language/PureScript/CodeGen/JS/AST.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ data UnaryOperator
3535
-- Constructor
3636
--
3737
| JSNew
38-
deriving (Show, Read, Eq)
38+
deriving (Show, Eq)
3939

4040
-- |
4141
-- Built-in binary operators
@@ -117,7 +117,7 @@ data BinaryOperator
117117
-- Bitwise right shift with zero-fill
118118
--
119119
| ZeroFillShiftRight
120-
deriving (Show, Read, Eq)
120+
deriving (Show, Eq)
121121

122122
-- |
123123
-- Data type for simplified Javascript expressions
@@ -238,7 +238,7 @@ data JS
238238
-- |
239239
-- Commented Javascript
240240
--
241-
| JSComment (Maybe SourceSpan) [Comment] JS deriving (Show, Read, Eq)
241+
| JSComment (Maybe SourceSpan) [Comment] JS deriving (Show, Eq)
242242

243243
withSourceSpan :: SourceSpan -> JS -> JS
244244
withSourceSpan withSpan = go

src/Language/PureScript/Comments.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ import Data.Aeson.TH
1212
data Comment
1313
= LineComment String
1414
| BlockComment String
15-
deriving (Show, Read, Eq, Ord)
15+
deriving (Show, Eq, Ord)
1616

1717
$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''Comment)

0 commit comments

Comments
 (0)