Skip to content

Commit f392aeb

Browse files
committed
new: add error handling
1 parent d5f8ff9 commit f392aeb

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

lambda-buffers-compiler/src/LambdaBuffers/Compiler/KindCheck.hs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,41 @@ runKindCheck = interpret $ \case
140140
either (handleErrClassDef modName classDef) pure $ I.runClassDefCheck ctx modName classDef
141141
where
142142
handleErrClassDef :: forall {b}. PC.ModuleName -> PC.ClassDef -> I.InferErr -> Eff effs b
143-
handleErrClassDef _ _ _err = error "Throw an error"
143+
handleErrClassDef modName classDef = \case
144+
I.InferUnboundTermErr ut ->
145+
case ut of
146+
QualifiedTyRef qtr -> do
147+
if qtr ^. qTyRef'moduleName == modName
148+
then do
149+
-- We're looking at the local module.
150+
let localRef = PC.LocalI . fst . withIso ltrISOqtr (\_ f -> f) $ qtr
151+
let err = PC.UnboundTyRefError classDef localRef modName
152+
throwError . PC.CKC'ClassDefError $ err
153+
else do
154+
-- We're looking at a foreign module.
155+
let foreignRef = PC.ForeignI . withIso ftrISOqtr (\_ f -> f) $ qtr
156+
throwError . PC.CKC'ClassDefError $ PC.UnboundTyRefError classDef foreignRef modName
157+
TyVar tv ->
158+
throwError . PC.CKC'ClassDefError $ PC.UnboundTyVarError classDef (PC.TyVar tv) modName
159+
QualifiedTyClassRef qcr ->
160+
if qcr ^. qTyClass'moduleName == modName
161+
then do
162+
-- We're looking at the local module.
163+
let localClassRef = PC.LocalCI . fst . withIso lcrISOqtcr (\_ f -> f) $ qcr
164+
let err = PC.UnboundTyClassRefError classDef localClassRef modName
165+
throwError . PC.CKC'ClassDefError $ err
166+
else do
167+
-- We're looking at a foreign module.
168+
let foreignRef = PC.ForeignCI . withIso fcrISOqtcr (\_ f -> f) $ qcr
169+
let err = PC.UnboundTyClassRefError classDef foreignRef modName
170+
throwError . PC.CKC'ClassDefError $ err
171+
I.InferUnifyTermErr (I.Constraint (k1, k2)) -> do
172+
err <- PC.IncorrectApplicationError classDef <$> kind2ProtoKind k1 <*> kind2ProtoKind k2 <*> pure modName
173+
throwError $ PC.CKC'ClassDefError err
174+
I.InferRecursiveSubstitutionErr _ ->
175+
throwError . PC.CKC'ClassDefError $ PC.RecursiveKindError classDef modName
176+
I.InferImpossibleErr t ->
177+
throwError $ PC.C'InternalError t
144178

145179
handleErrTyDef :: forall {b}. PC.ModuleName -> PC.TyDef -> I.InferErr -> Eff effs b
146180
handleErrTyDef modName td = \case

lambda-buffers-compiler/src/LambdaBuffers/Compiler/ProtoCompat/Types.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,10 @@ instance (Typeable loc, Show loc) => Exception (KindCheckError loc)
361361

362362
-- | All the compiler errors.
363363
data CompilerError
364-
= CKC'TyDefError (KindCheckError TyDef)
365-
| CKC'ClassDefError (KindCheckError ClassDef)
364+
= -- | Compiler KindChecker Error - within a Type Definition.
365+
CKC'TyDefError (KindCheckError TyDef)
366+
| -- | Compiler KindChecker Error - within a Class Definition.
367+
CKC'ClassDefError (KindCheckError ClassDef)
366368
| C'InternalError Text
367369
deriving stock (Show, Eq, Ord, Generic)
368370
deriving anyclass (SOP.Generic)

lambda-buffers-compiler/test/Test/KindCheck/Errors.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Test.KindCheck.Errors (testGKindCheckErrors) where
22

33
import LambdaBuffers.Compiler.KindCheck (check_)
44
import LambdaBuffers.Compiler.ProtoCompat qualified as PC
5-
import LambdaBuffers.Compiler.ProtoCompat.Types (CompilerError (CompKindCheckError), KindCheckError (UnboundTyRefError, UnboundTyVarError))
5+
import LambdaBuffers.Compiler.ProtoCompat.Types (CompilerError (CKC'TyDefError), KindCheckError (UnboundTyRefError, UnboundTyVarError))
66

77
import Test.Tasty (TestTree, testGroup)
88
import Test.Tasty.HUnit (testCase, (@?=))
@@ -17,19 +17,19 @@ undefinedVariable :: TestTree
1717
undefinedVariable =
1818
testCase "Catch undefined(free) variable in Type Definition." $
1919
check_ compilerInput'undefinedVariable
20-
@?= (Left . CompKindCheckError . withDefModule) (UnboundTyVarError tyDef'undefinedVar tyDef'undefinedVar'var)
20+
@?= (Left . CKC'TyDefError . withDefModule) (UnboundTyVarError tyDef'undefinedVar tyDef'undefinedVar'var)
2121

2222
undefinedLocalTyRef :: TestTree
2323
undefinedLocalTyRef =
2424
testCase "Catch undefined Local TyRef in Type Definition." $
2525
check_ compilerInput'undefinedLocalTyRef
26-
@?= (Left . CompKindCheckError . withDefModule) (UnboundTyRefError tyDef'undefinedLocalTyRef tyDef'undefinedLocalTyRef'TyRef)
26+
@?= (Left . CKC'TyDefError . withDefModule) (UnboundTyRefError tyDef'undefinedLocalTyRef tyDef'undefinedLocalTyRef'TyRef)
2727

2828
undefinedForeignTyRef :: TestTree
2929
undefinedForeignTyRef =
3030
testCase "Catch undefined Foreign TyRef in Type Definition." $
3131
check_ compilerInput'undefinedForeignTyRef
32-
@?= (Left . CompKindCheckError . withDefModule) (UnboundTyRefError tyDef'undefinedForeignTyRef tyDef'undefinedForeignTyRef'TyRef)
32+
@?= (Left . CKC'TyDefError . withDefModule) (UnboundTyRefError tyDef'undefinedForeignTyRef tyDef'undefinedForeignTyRef'TyRef)
3333

3434
withDefModule :: forall a. (PC.ModuleName -> a) -> a
3535
withDefModule f = f (_ModuleName ["Module"])

0 commit comments

Comments
 (0)