git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix #2379, add error message for unknown classes (#2396)
b8bb12bPhil Freeman
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
56 , examples/failing/*.purs
56 , examples/failing/*.purs
57 , examples/failing/1733/*.purs
57 , examples/failing/1733/*.purs
58 , examples/failing/2378/*.purs
58 , examples/failing/2378/*.purs
59+ , examples/failing/2379/*.purs
59 , examples/failing/ConflictingExports/*.purs
60 , examples/failing/ConflictingExports/*.purs
60 , examples/failing/ConflictingImports/*.purs
61 , examples/failing/ConflictingImports/*.purs
61 , examples/failing/ConflictingImports2/*.purs
62 , examples/failing/ConflictingImports2/*.purs
src/Language/PureScript/AST/Declarations.hs+1
@@ -83,6 +83,7 @@ data SimpleErrorMessage
83 | ConstrainedTypeUnified Type Type
83 | ConstrainedTypeUnified Type Type
84 | OverlappingInstances (Qualified (ProperName 'ClassName)) [Type] [Qualified Ident]
84 | OverlappingInstances (Qualified (ProperName 'ClassName)) [Type] [Qualified Ident]
85 | NoInstanceFound Constraint
85 | NoInstanceFound Constraint
86+ | UnknownClass (Qualified (ProperName 'ClassName))
86 | PossiblyInfiniteInstance (Qualified (ProperName 'ClassName)) [Type]
87 | PossiblyInfiniteInstance (Qualified (ProperName 'ClassName)) [Type]
87 | CannotDerive (Qualified (ProperName 'ClassName)) [Type]
88 | CannotDerive (Qualified (ProperName 'ClassName)) [Type]
88 | InvalidNewtypeInstance (Qualified (ProperName 'ClassName)) [Type]
89 | InvalidNewtypeInstance (Qualified (ProperName 'ClassName)) [Type]
src/Language/PureScript/Errors.hs+6
@@ -118,6 +118,7 @@ errorCode em = case unwrapErrorMessage em of
118 ConstrainedTypeUnified{} -> "ConstrainedTypeUnified"
118 ConstrainedTypeUnified{} -> "ConstrainedTypeUnified"
119 OverlappingInstances{} -> "OverlappingInstances"
119 OverlappingInstances{} -> "OverlappingInstances"
120 NoInstanceFound{} -> "NoInstanceFound"
120 NoInstanceFound{} -> "NoInstanceFound"
121+ UnknownClass{} -> "UnknownClass"
121 PossiblyInfiniteInstance{} -> "PossiblyInfiniteInstance"
122 PossiblyInfiniteInstance{} -> "PossiblyInfiniteInstance"
122 CannotDerive{} -> "CannotDerive"
123 CannotDerive{} -> "CannotDerive"
123 InvalidNewtypeInstance{} -> "InvalidNewtypeInstance"
124 InvalidNewtypeInstance{} -> "InvalidNewtypeInstance"
@@ -599,6 +600,11 @@ prettyPrintSingleError (PPEOptions codeColor full level showWiki) e = flip evalS
599 , line "They may be disallowed completely in a future version of the compiler."
600 , line "They may be disallowed completely in a future version of the compiler."
600 ]
601 ]
601 renderSimpleErrorMessage OverlappingInstances{} = internalError "OverlappingInstances: empty instance list"
602 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+ ]
602 renderSimpleErrorMessage (NoInstanceFound (Constraint C.Fail [ ty ] _)) | Just box <- toTypelevelString ty =
608 renderSimpleErrorMessage (NoInstanceFound (Constraint C.Fail [ ty ] _)) | Just box <- toTypelevelString ty =
603 paras [ line "A custom type error occurred while solving type class constraints:"
609 paras [ line "A custom type error occurred while solving type class constraints:"
604 , indent box
610 , indent box
src/Language/PureScript/TypeChecker/Entailment.hs+4−2
@@ -150,8 +150,10 @@ entails SolverOptions{..} constraint context hints =
150 inferred <- lift get
150 inferred <- lift get
151 -- We need information about functional dependencies, so we have to look up the class
151 -- We need information about functional dependencies, so we have to look up the class
152 -- name in the environment:
152 -- name in the environment:
153− let findClass = fromMaybe (internalError "entails: type class not found in environment") . M.lookup className'
153+ classesInScope <- lift . lift $ gets (typeClasses . checkEnv)
154− TypeClassData{ typeClassDependencies } <- lift . lift $ gets (findClass . typeClasses . checkEnv)
154+ TypeClassData{ typeClassDependencies } <- case M.lookup className' classesInScope of
155+ Nothing -> throwError . errorMessage $ UnknownClass className'
156+ Just tcd -> pure tcd
155 let instances =
157 let instances =
156 [ (substs, tcd)
158 [ (substs, tcd)
157 | tcd <- forClassName (combineContexts context inferred) className' tys''
159 | tcd <- forClassName (combineContexts context inferred) className' tys''