@@ -69,9 +69,13 @@ runRepM
69
69
∷ Context
70
70
→ RepM a
71
71
→ Either CoreFnError (Tagged " needsRuntimeLazy" Bool , a )
72
- runRepM ctx (RepM m) =
73
- runStateT m ctx <&> \ (a, ctx') →
74
- (Tagged . getAny $ needsRuntimeLazy ctx', a)
72
+ runRepM ctx (RepM m) = do
73
+ (a, ctx') ← runStateT m ctx
74
+ let remainingAnnotations = annotations ctx'
75
+ unless (Map. null remainingAnnotations) do
76
+ Left . CoreFnError (Cfn. moduleName (contextModule ctx)) $
77
+ UnusedAnnotations remainingAnnotations
78
+ pure (Tagged . getAny $ needsRuntimeLazy ctx', a)
75
79
76
80
mkModule
77
81
∷ Cfn. Module Cfn. Ann
@@ -119,6 +123,15 @@ parseAnnotations currentModule =
119
123
& first
120
124
(CoreFnError (Cfn. moduleName currentModule) . AnnotationParsingError )
121
125
126
+ useAnnotation ∷ Name → RepM (Maybe Annotation )
127
+ useAnnotation name = do
128
+ ctx ← get
129
+ let (ann, annotations') =
130
+ -- delete the annotation from the map returning the value
131
+ Map. updateLookupWithKey (\ _ _ → Nothing ) name (annotations ctx)
132
+ put $ ctx {annotations = annotations'}
133
+ pure ann
134
+
122
135
mkImports ∷ RepM [ModuleName ]
123
136
mkImports = do
124
137
Cfn. Module {moduleName, moduleImports} ← gets contextModule
@@ -135,8 +148,13 @@ mkReExports =
135
148
Map. fromAscList . fmap (identToName <<$>> ) . Map. toAscList
136
149
<$> gets (contextModule >>> Cfn. moduleReExports)
137
150
138
- mkForeign ∷ RepM [Name ]
139
- mkForeign = identToName <<$>> gets (contextModule >>> Cfn. moduleForeign)
151
+ mkForeign ∷ RepM [(Ann , Name )]
152
+ mkForeign = do
153
+ idents ← gets (contextModule >>> Cfn. moduleForeign)
154
+ forM idents \ ident → do
155
+ let name = identToName ident
156
+ ann ← useAnnotation name
157
+ pure (ann, name)
140
158
141
159
collectDataDeclarations
142
160
∷ Map ModuleName (Cfn. Module Cfn. Ann )
@@ -179,7 +197,7 @@ mkBinding ∷ Cfn.Bind Cfn.Ann → RepM Binding
179
197
mkBinding = \ case
180
198
Cfn. NonRec _ann ident cfnExpr → do
181
199
let name = identToName ident
182
- ann ← gets $ annotations >>> Map. lookup name
200
+ ann ← useAnnotation name
183
201
expr ← makeExprAnnotated ann cfnExpr
184
202
pure $ Standalone (noAnn, name, expr)
185
203
Cfn. Rec bindingGroup → do
@@ -324,7 +342,7 @@ mkLet ann binds expr = do
324
342
-- The algorithm is based on this document: ------------------------------------
325
343
-- https://julesjacobs.com/notes/patternmatching/patternmatching.pdf -----------
326
344
327
- mkCase ∷ Ann -> [CfnExp ] → NonEmpty (Cfn. CaseAlternative Cfn. Ann ) → RepM Exp
345
+ mkCase ∷ Ann → [CfnExp ] → NonEmpty (Cfn. CaseAlternative Cfn. Ann ) → RepM Exp
328
346
mkCase ann cfnExpressions alternatives = do
329
347
expressions ← traverse makeExpr cfnExpressions
330
348
-- Before making clauses, we need to prepare bindings
@@ -699,7 +717,10 @@ algebraicTy modName tyName = do
699
717
--------------------------------------------------------------------------------
700
718
-- Errors ----------------------------------------------------------------------
701
719
702
- throwContextualError ∷ CoreFnErrorReason → RepM a
720
+ throwContextualError
721
+ ∷ (MonadState Context m , MonadError CoreFnError m )
722
+ ⇒ CoreFnErrorReason
723
+ → m a
703
724
throwContextualError e = do
704
725
currentModule ← gets (contextModule >>> Cfn. moduleName)
705
726
throwError $ CoreFnError currentModule e
@@ -730,6 +751,7 @@ data CoreFnErrorReason
730
751
TyName
731
752
| UnicodeDecodeError UnicodeException
732
753
| AnnotationParsingError (Megaparsec. ParseErrorBundle Text Void )
754
+ | UnusedAnnotations (Map Name Annotation )
733
755
734
756
instance Show CoreFnErrorReason where
735
757
show = \ case
@@ -762,3 +784,5 @@ instance Show CoreFnErrorReason where
762
784
" Unicode decode error: " <> displayException e
763
785
AnnotationParsingError bundle →
764
786
" Annotation parsing error: " <> Megaparsec. errorBundlePretty bundle
787
+ UnusedAnnotations anns →
788
+ " Unused annotations: " <> toString (pShow anns)
0 commit comments