Skip to content

Commit 81d7f95

Browse files
committed
update: round 2 of comments
1 parent d7381a3 commit 81d7f95

File tree

7 files changed

+73
-43
lines changed

7 files changed

+73
-43
lines changed

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ globalStrategy = reinterpret $ \case
117117

118118
moduleStrategy :: Transform GlobalCheck ModuleCheck
119119
moduleStrategy = reinterpret $ \case
120-
CreateContext ci -> evalState (mempty @(M.Map Variable P.TyName)) . resolveCreateContext $ ci
120+
CreateContext ci -> evalState (mempty @(M.Map Variable P.TyDef)) . resolveCreateContext $ ci
121121
ValidateModule cx md -> do
122122
traverse_ (kCTypeDefinition (module2ModuleName md) cx) (md ^. #typeDefs)
123123
traverse_ (kCClassInstance cx) (md ^. #instances)
@@ -190,35 +190,50 @@ tyDef2TyName (P.TyDef n _ _) = n
190190
-}
191191
resolveCreateContext ::
192192
forall effs.
193-
Member (State (M.Map Variable P.TyName)) effs =>
193+
Member (State (M.Map Variable P.TyDef)) effs =>
194194
Member Err effs =>
195195
P.CompilerInput ->
196196
Eff effs Context
197197
resolveCreateContext ci = do
198198
ctxs <- traverse module2Context (ci ^. #modules)
199199
pure $ mconcat ctxs
200200

201-
module2Context :: forall effs. Member (State (M.Map Variable P.TyName)) effs => Member Err effs => P.Module -> Eff effs Context
201+
module2Context ::
202+
forall effs.
203+
Member (State (M.Map Variable P.TyDef)) effs =>
204+
Member Err effs =>
205+
P.Module ->
206+
Eff effs Context
202207
module2Context m = do
203208
let typeDefinitions = m ^. #typeDefs
204-
ctxs <- traverse (tyDef2Context (moduleName2ModName (m ^. #moduleName))) typeDefinitions
209+
ctxs <- runReader (m ^. #moduleName) $ do
210+
traverse (tyDef2Context (moduleName2ModName (m ^. #moduleName))) typeDefinitions
205211
pure $ mconcat ctxs
206212

207213
-- | Creates a Context entry from one type definition.
208-
tyDef2Context :: forall effs. Member (State (M.Map Variable P.TyName)) effs => Member Err effs => ModName -> P.TyDef -> Eff effs Context
209-
tyDef2Context curModName tyDef@(P.TyDef tyName _ _) = do
214+
tyDef2Context ::
215+
forall effs.
216+
Member (Reader P.ModuleName) effs =>
217+
Member (State (M.Map Variable P.TyDef)) effs =>
218+
Member Err effs =>
219+
ModName ->
220+
P.TyDef ->
221+
Eff effs Context
222+
tyDef2Context curModName tyDef = do
210223
r@(v, _) <- tyDef2NameAndKind curModName tyDef
211-
associateName v tyName
224+
associateName v tyDef
212225
pure $ mempty & context .~ uncurry M.singleton r
213226
where
214227
-- Ads the name to our map - we can use its SourceLocation in the case of a
215228
-- double use. If it's already in our map - that means we've double declared it.
216-
associateName :: Variable -> P.TyName -> Eff effs ()
217-
associateName v t = do
218-
maps <- get @(M.Map Variable P.TyName)
229+
associateName :: Variable -> P.TyDef -> Eff effs ()
230+
associateName v curTyDef = do
231+
modName <- ask
232+
maps <- get @(M.Map Variable P.TyDef)
219233
case maps M.!? v of
220-
Just otherTyName -> throwError . PT.CompKindCheckError $ PT.MultipleTyDefError otherTyName t
221-
Nothing -> modify (M.insert v t)
234+
Just otherTyDef ->
235+
throwError . PT.CompKindCheckError $ PT.MultipleTyDefError modName [otherTyDef, curTyDef]
236+
Nothing -> modify (M.insert v curTyDef)
222237

223238
{- | Converts the Proto Module name to a local modname - dropping the
224239
information.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,8 @@ instance IsMessage P.KindCheckError KindCheckError where
568568
<*> fromProto (err ^. P.definedKind)
569569
P.KindCheckError'MultipleTydefError err ->
570570
MultipleTyDefError
571-
<$> fromProto (err ^. P.declaration1)
572-
<*> fromProto (err ^. P.declaration2)
571+
<$> fromProto (err ^. P.moduleName)
572+
<*> traverse fromProto (err ^. P.tyDefs)
573573
Nothing -> throwProtoError EmptyField
574574

575575
toProto = \case
@@ -590,10 +590,10 @@ instance IsMessage P.KindCheckError KindCheckError where
590590
& (P.inconsistentTypeError . P.tyName) .~ toProto name
591591
& (P.inconsistentTypeError . P.inferredKind) .~ toProto ki
592592
& (P.inconsistentTypeError . P.definedKind) .~ toProto kd
593-
MultipleTyDefError d1 d2 ->
593+
MultipleTyDefError m ds ->
594594
defMessage
595-
& (P.multipleTydefError . P.declaration1) .~ toProto d1
596-
& (P.multipleTydefError . P.declaration2) .~ toProto d2
595+
& (P.multipleTydefError . P.moduleName) .~ toProto m
596+
& (P.multipleTydefError . P.tyDefs) .~ (toProto <$> ds)
597597

598598
instance IsMessage P.CompilerError CompilerError where
599599
fromProto cErr = case cErr ^. P.maybe'compilerError of

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -305,16 +305,11 @@ instance Arbitrary CompilerInput where
305305
fn n = CompilerInput <$> resize n arbitrary
306306

307307
data KindCheckError
308-
= -- | The following term is unbound in the following type definition.
309-
UnboundTermError TyName VarName
310-
| -- | Failed unifying TyRef with TyRef in TyName. This is the TyDef.
311-
IncorrectApplicationError TyName Kind Kind
312-
| -- | Kind recurses forever - not permitted.
313-
RecursiveKindError TyName
314-
| -- | The following type has the wrong.
315-
InconsistentTypeError TyName Kind Kind
316-
| -- | The following type@(TyName) was redeclared here@(TyName).
317-
MultipleTyDefError TyName TyName
308+
= UnboundTermError TyName VarName
309+
| IncorrectApplicationError TyName Kind Kind
310+
| RecursiveKindError TyName
311+
| InconsistentTypeError TyName Kind Kind
312+
| MultipleTyDefError ModuleName [TyDef]
318313
deriving stock (Show, Eq, Ord, Generic)
319314
deriving (Arbitrary) via GenericArbitrary KindCheckError
320315
instance Exception KindCheckError

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import Test.Utils.CompilerInput (
3232
compilerInput'incoherent,
3333
compilerInput'maybe,
3434
)
35-
import Test.Utils.Constructors (_tyName)
35+
import Test.Utils.Constructors (_ModuleName)
36+
import Test.Utils.TyDef (tyDef'maybe)
3637

3738
--------------------------------------------------------------------------------
3839
-- Top Level tests
@@ -49,7 +50,10 @@ testMultipleDec = testGroup "Multiple declaration tests." [doubleDeclaration, pa
4950
doubleDeclaration :: TestTree
5051
doubleDeclaration =
5152
testCase "Two declarations of Maybe within the same module are caught." $
52-
check_ compilerInput'doubleDeclaration @?= Left (P.CompKindCheckError $ P.MultipleTyDefError (_tyName "Maybe") (_tyName "Maybe"))
53+
check_ compilerInput'doubleDeclaration
54+
@?= Left (P.CompKindCheckError (P.MultipleTyDefError moduleName [tyDef'maybe, tyDef'maybe]))
55+
where
56+
moduleName = _ModuleName ["Module"]
5357

5458
passingDoubleDeclaration :: TestTree
5559
passingDoubleDeclaration =

lambda-buffers-compiler/test/Test/Utils/Constructors.hs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,37 @@ module Test.Utils.Constructors (
1212
_Type,
1313
_TyDef,
1414
_TyRefILocal,
15+
_Module,
16+
_ModuleName,
17+
_ModuleNamePart,
1518
) where
1619

1720
import Data.List.NonEmpty (fromList)
1821
import Data.Text (Text)
1922
import LambdaBuffers.Compiler.ProtoCompat qualified as P
2023
import Test.Utils.SourceInfo (sourceInfo'empty)
2124

25+
_Module :: P.ModuleName -> [P.TyDef] -> [P.ClassDef] -> [P.InstanceClause] -> P.Module
26+
_Module mn tds cds ins =
27+
P.Module
28+
{ P.moduleName = mn
29+
, P.typeDefs = tds
30+
, P.classDefs = cds
31+
, P.instances = ins
32+
, P.imports = mempty
33+
, P.sourceInfo = sourceInfo'empty
34+
}
35+
36+
_ModuleName :: [Text] -> P.ModuleName
37+
_ModuleName ps =
38+
P.ModuleName
39+
{ P.parts = _ModuleNamePart <$> ps
40+
, P.sourceInfo = sourceInfo'empty
41+
}
42+
43+
_ModuleNamePart :: Text -> P.ModuleNamePart
44+
_ModuleNamePart n = P.ModuleNamePart n sourceInfo'empty
45+
2246
_tyName :: Text -> P.TyName
2347
_tyName x = P.TyName x sourceInfo'empty
2448

lambda-buffers-compiler/test/Test/Utils/Module.hs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,6 @@ import LambdaBuffers.Compiler.ProtoCompat qualified as P
55
import Test.Utils.SourceInfo (sourceInfo'empty)
66
import Test.Utils.TyDef (tyDef'incoherent, tyDef'maybe)
77

8-
_Module :: P.ModuleName -> [P.TyDef] -> [P.ClassDef] -> [P.InstanceClause] -> P.Module
9-
_Module mn tds cds ins =
10-
P.Module
11-
{ P.moduleName = mn
12-
, P.typeDefs = tds
13-
, P.classDefs = cds
14-
, P.instances = ins
15-
, P.imports = mempty
16-
, P.sourceInfo = sourceInfo'empty
17-
}
18-
198
module'maybe :: P.Module
209
module'maybe =
2110
P.Module

lambda-buffers-proto/compiler.proto

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,10 +566,13 @@ message KindCheckError {
566566
Kind defined_kind = 3;
567567
}
568568

569-
// A tydef was defined multiple times.
569+
// Error reads:
570+
// Multiple TyDefs with the same TyName were found in ModuleName.
570571
message MultipleTyDefError {
571-
TyName declaration_1 = 1;
572-
TyName declaration_2 = 2;
572+
// Module in which the error was found.
573+
ModuleName module_name = 1;
574+
// Conflicting type definitions.
575+
repeated TyDef ty_defs = 2;
573576
}
574577

575578
// The types of inference errors.

0 commit comments

Comments
 (0)