git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix #2379, add error message for unknown classes (#2396)

* Fix #2379, add error message for unknown classes * Typo, fix cabal file

commitb8bb12b5fec663509040413ca39816bab02ddc4c authorPhil Freeman <paf31@cantab.net> dateSat 22 Oct 2016, 12:36 (10 years ago) parenta8756f0
6 files changed+27−2
examples/failing/2379.purs+6 examples/failing/2379/Lib.purs+9 purescript.cabal+1 src/Language/PureScript/AST/Declarations.hs+1 src/Language/PureScript/Errors.hs+6 src/Language/PureScript/TypeChecker/Entailment.hs+4−2
examples/failing/2379.pursnew file+6
@@ -0,0 +1,6 @@
1+-- @shouldFailWith UnknownClass
2+module Main where
3+
4+import Lib
5+
6+test = x [1, 2, 3]
examples/failing/2379/Lib.pursnew file+9
@@ -0,0 +1,9 @@
1+module Lib (class X, x) where
2+
3+class X a where
4+ x :: a -> String
5+
6+class Y a
7+
8+instance xArray :: Y a => X (Array a) where
9+ x _ = "[]"
purescript.cabal+1
@@ -56,6 +56,7 @@ extra-source-files: examples/passing/*.purs
5656 , examples/failing/*.purs
5757 , examples/failing/1733/*.purs
5858 , examples/failing/2378/*.purs
59+ , examples/failing/2379/*.purs
5960 , examples/failing/ConflictingExports/*.purs
6061 , examples/failing/ConflictingImports/*.purs
6162 , examples/failing/ConflictingImports2/*.purs
src/Language/PureScript/AST/Declarations.hs+1
@@ -83,6 +83,7 @@ data SimpleErrorMessage
8383 | ConstrainedTypeUnified Type Type
8484 | OverlappingInstances (Qualified (ProperName 'ClassName)) [Type] [Qualified Ident]
8585 | NoInstanceFound Constraint
86+ | UnknownClass (Qualified (ProperName 'ClassName))
8687 | PossiblyInfiniteInstance (Qualified (ProperName 'ClassName)) [Type]
8788 | CannotDerive (Qualified (ProperName 'ClassName)) [Type]
8889 | InvalidNewtypeInstance (Qualified (ProperName 'ClassName)) [Type]
src/Language/PureScript/Errors.hs+6
@@ -118,6 +118,7 @@ errorCode em = case unwrapErrorMessage em of
118118 ConstrainedTypeUnified{} -> "ConstrainedTypeUnified"
119119 OverlappingInstances{} -> "OverlappingInstances"
120120 NoInstanceFound{} -> "NoInstanceFound"
121+ UnknownClass{} -> "UnknownClass"
121122 PossiblyInfiniteInstance{} -> "PossiblyInfiniteInstance"
122123 CannotDerive{} -> "CannotDerive"
123124 InvalidNewtypeInstance{} -> "InvalidNewtypeInstance"
@@ -599,6 +600,11 @@ prettyPrintSingleError (PPEOptions codeColor full level showWiki) e = flip evalS
599600 , line "They may be disallowed completely in a future version of the compiler."
600601 ]
601602 renderSimpleErrorMessage OverlappingInstances{} = internalError "OverlappingInstances: empty instance list"
603+ renderSimpleErrorMessage (UnknownClass nm) =
604+ paras [ line "No type class instance was found for class"
605+ , markCodeBox $ indent $ line (showQualified runProperName nm)
606+ , line "because the class was not in scope. Perhaps it was not exported."
607+ ]
602608 renderSimpleErrorMessage (NoInstanceFound (Constraint C.Fail [ ty ] _)) | Just box <- toTypelevelString ty =
603609 paras [ line "A custom type error occurred while solving type class constraints:"
604610 , indent box
src/Language/PureScript/TypeChecker/Entailment.hs+4−2
@@ -150,8 +150,10 @@ entails SolverOptions{..} constraint context hints =
150150 inferred <- lift get
151151 -- We need information about functional dependencies, so we have to look up the class
152152 -- name in the environment:
153− let findClass = fromMaybe (internalError "entails: type class not found in environment") . M.lookup className'
154− TypeClassData{ typeClassDependencies } <- lift . lift $ gets (findClass . typeClasses . checkEnv)
153+ classesInScope <- lift . lift $ gets (typeClasses . checkEnv)
154+ TypeClassData{ typeClassDependencies } <- case M.lookup className' classesInScope of
155+ Nothing -> throwError . errorMessage $ UnknownClass className'
156+ Just tcd -> pure tcd
155157 let instances =
156158 [ (substs, tcd)
157159 | tcd <- forClassName (combineContexts context inferred) className' tys''