Skip to content

Commit c9f0839

Browse files
committed
Programmable type errors (purescript#2155)
* Programmable type errors * update unifiesWith and typeHeadsAreEqual
1 parent de8ab59 commit c9f0839

File tree

13 files changed

+60
-77
lines changed

13 files changed

+60
-77
lines changed

src/Language/PureScript/Constants.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ partial = "Partial"
327327
pattern Partial :: Qualified (ProperName 'ClassName)
328328
pattern Partial = Qualified (Just (ModuleName [ProperName "Prim"])) (ProperName "Partial")
329329

330+
pattern Fail :: Qualified (ProperName 'ClassName)
331+
pattern Fail = Qualified (Just (ModuleName [ProperName "Prim"])) (ProperName "Fail")
332+
330333
-- Code Generation
331334

332335
__superclass_ :: String

src/Language/PureScript/Environment.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ primTypes =
240240
, (primName "Int", (Star, ExternData))
241241
, (primName "Boolean", (Star, ExternData))
242242
, (primName "Partial", (Star, ExternData))
243+
, (primName "Fail", (FunKind Symbol Star, ExternData))
243244
]
244245

245246
-- |
@@ -249,7 +250,9 @@ primTypes =
249250
primClasses :: M.Map (Qualified (ProperName 'ClassName)) ([(String, Maybe Kind)], [(Ident, Type)], [Constraint])
250251
primClasses =
251252
M.fromList
252-
[ (primName "Partial", ([], [], [])) ]
253+
[ (primName "Partial", ([], [], []))
254+
, (primName "Fail", ([("message", Just Symbol)], [], []))
255+
]
253256

254257
-- |
255258
-- Finds information about data constructors from the current environment.

src/Language/PureScript/Errors.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,10 @@ prettyPrintSingleError (PPEOptions codeColor full level showWiki) e = flip evalS
713713
, line "They may be disallowed completely in a future version of the compiler."
714714
]
715715
renderSimpleErrorMessage OverlappingInstances{} = internalError "OverlappingInstances: empty instance list"
716+
renderSimpleErrorMessage (NoInstanceFound (Constraint C.Fail [ TypeLevelString message ] _)) =
717+
paras [ line "A custom type error occurred while solving type class constraints:"
718+
, indent . paras . map line . lines $ message
719+
]
716720
renderSimpleErrorMessage (NoInstanceFound (Constraint C.Partial
717721
_
718722
(Just (PartialConstraintData bs b)))) =

src/Language/PureScript/Kinds.hs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,20 @@ import Prelude.Compat
66

77
import qualified Data.Aeson.TH as A
88

9-
-- |
10-
-- The data type of kinds
11-
--
9+
-- | The data type of kinds
1210
data Kind
13-
-- |
14-
-- Unification variable of type Kind
15-
--
11+
-- | Unification variable of type Kind
1612
= KUnknown Int
17-
-- |
18-
-- The kind of types
19-
--
13+
-- | The kind of types
2014
| Star
21-
-- |
22-
-- The kind of effects
23-
--
15+
-- | The kind of effects
2416
| Bang
25-
-- |
26-
-- Kinds for labelled, unordered rows without duplicates
27-
--
17+
-- | Kinds for labelled, unordered rows without duplicates
2818
| Row Kind
29-
-- |
30-
-- Function kinds
31-
--
19+
-- | Function kinds
3220
| FunKind Kind Kind
21+
-- | Type-level strings
22+
| Symbol
3323
deriving (Show, Read, Eq, Ord)
3424

3525
$(A.deriveJSON A.defaultOptions ''Kind)

src/Language/PureScript/Parser/Kinds.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ parseStar = const Star <$> symbol' "*"
1818
parseBang :: TokenParser Kind
1919
parseBang = const Bang <$> symbol' "!"
2020

21+
parseSymbol :: TokenParser Kind
22+
parseSymbol = const Symbol <$> uname' "Symbol"
23+
2124
parseTypeAtom :: TokenParser Kind
2225
parseTypeAtom = indented *> P.choice
2326
[ parseStar
2427
, parseBang
28+
, parseSymbol
2529
, parens parseKind
2630
]
2731
-- |

src/Language/PureScript/Parser/Types.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ parseFunction = parens rarrow >> return tyFunction
2626
parseObject :: TokenParser Type
2727
parseObject = braces $ TypeApp tyRecord <$> parseRow
2828

29+
parseTypeLevelString :: TokenParser Type
30+
parseTypeLevelString = TypeLevelString <$> stringLiteral
31+
2932
parseTypeWildcard :: TokenParser Type
3033
parseTypeWildcard = do
3134
start <- P.getPosition
@@ -53,6 +56,7 @@ parseTypeAtom :: TokenParser Type
5356
parseTypeAtom = indented *> P.choice
5457
[ P.try parseConstrainedType
5558
, P.try parseFunction
59+
, parseTypeLevelString
5660
, parseObject
5761
, parseTypeWildcard
5862
, parseForAll

src/Language/PureScript/Pretty/Kinds.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ typeLiterals = mkPattern match
2121
where
2222
match Star = Just "*"
2323
match Bang = Just "!"
24+
match Symbol = Just "Symbol"
2425
match (KUnknown u) = Just $ 'u' : show u
2526
match _ = Nothing
2627

src/Language/PureScript/Pretty/Types.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ typeLiterals = mkPattern match
3232
where
3333
match TypeWildcard{} = Just $ text "_"
3434
match (TypeVar var) = Just $ text var
35+
match (TypeLevelString s) = Just . text $ show s
3536
match (PrettyPrintObject row) = Just $ prettyPrintRowWith '{' '}' row
3637
match (TypeConstructor ctor) = Just $ text $ runProperName $ disqualify ctor
3738
match (TUnknown u) = Just $ text $ 't' : show u

src/Language/PureScript/TypeChecker.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ checkTypeClassInstance
147147
-> Type
148148
-> m ()
149149
checkTypeClassInstance _ (TypeVar _) = return ()
150+
checkTypeClassInstance _ (TypeLevelString _) = return ()
150151
checkTypeClassInstance _ (TypeConstructor ctor) = do
151152
env <- getEnv
152153
when (ctor `M.member` typeSynonyms env) . throwError . errorMessage $ TypeSynonymInstance

src/Language/PureScript/TypeChecker/Entailment.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ typeHeadsAreEqual _ (TUnknown u1) (TUnknown u2) | u1 == u2 = Just
184184
typeHeadsAreEqual _ (Skolem _ s1 _ _) (Skolem _ s2 _ _) | s1 == s2 = Just []
185185
typeHeadsAreEqual _ t (TypeVar v) = Just [(v, t)]
186186
typeHeadsAreEqual _ (TypeConstructor c1) (TypeConstructor c2) | c1 == c2 = Just []
187+
typeHeadsAreEqual _ (TypeLevelString s1) (TypeLevelString s2) | s1 == s2 = Just []
187188
typeHeadsAreEqual m (TypeApp h1 t1) (TypeApp h2 t2) = (++) <$> typeHeadsAreEqual m h1 h2
188189
<*> typeHeadsAreEqual m t1 t2
189190
typeHeadsAreEqual _ REmpty REmpty = Just []

0 commit comments

Comments
 (0)