Skip to content

Commit a118d14

Browse files
committed
Merge pull request purescript#319 from garyb/name-decl-errors
Name decl errors
2 parents c902818 + 04bc640 commit a118d14

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

src/Language/PureScript/Sugar/Names.hs

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Language.PureScript.Sugar.Names (
1616
desugarImports
1717
) where
1818

19+
import Data.Data
1920
import Data.Maybe (fromMaybe, isJust)
2021
import Data.Generics (extM, mkM, everywhereM)
2122
import Data.Generics.Extras (mkS, extS, everywhereWithContextM')
@@ -155,10 +156,16 @@ desugarImports modules = do
155156
renameInModule imports env (elaborateExports exps m)
156157

157158
-- |
158-
-- Rethrow an error with the name of the current module in the case of a failure
159+
-- Rethrow an error with an extra message line prepended
160+
--
161+
rethrow :: String -> Either String a -> Either String a
162+
rethrow msg = flip catchError $ \e -> throwError (msg ++ ":\n" ++ e)
163+
164+
-- |
165+
-- Rethrow an error with details of the current module prepended to the message
159166
--
160167
rethrowForModule :: Module -> Either String a -> Either String a
161-
rethrowForModule (Module mn _ _) = flip catchError $ \e -> throwError ("Error in module '" ++ show mn ++ "':\n" ++ e)
168+
rethrowForModule (Module mn _ _) = rethrow $ "Error in module '" ++ show mn ++ "'"
162169

163170
-- |
164171
-- Make all exports for a module explicit. This may still effect modules that have an exports list,
@@ -176,17 +183,21 @@ elaborateExports exps (Module mn decls _) = Module mn decls (Just $
176183
--
177184
renameInModule :: ImportEnvironment -> ExportEnvironment -> Module -> Either String Module
178185
renameInModule imports exports (Module mn decls exps) =
179-
Module mn <$> (mapM updateDecl decls >>= (mapM updateVars >=> everywhereM (mkM updateType `extM` updateValue `extM` updateBinder))) <*> pure exps
186+
Module mn <$> mapM go decls <*> pure exps
180187
where
181-
updateDecl (TypeInstanceDeclaration name cs cn ts ds) =
182-
TypeInstanceDeclaration name <$> updateConstraints cs <*> updateClassName cn <*> pure ts <*> pure ds
183-
updateDecl (ExternInstanceDeclaration name cs cn ts) =
184-
ExternInstanceDeclaration name <$> updateConstraints cs <*> updateClassName cn <*> pure ts
185-
updateDecl d = return d
186-
187-
updateVars :: Declaration -> Either String Declaration
188-
updateVars (ValueDeclaration name nameKind [] Nothing val) =
189-
ValueDeclaration name nameKind [] Nothing <$> everywhereWithContextM' [] (mkS bindFunctionArgs `extS` bindBinders) val
188+
go (DataDeclaration name args dctors) =
189+
rethrowFor "data declaration" name $ DataDeclaration <$> pure name <*> pure args <*> updateAll dctors
190+
go (DataBindingGroupDeclaration decls') =
191+
DataBindingGroupDeclaration <$> mapM go decls'
192+
go (TypeSynonymDeclaration name ps ty) =
193+
rethrowFor "type synonym" name $ TypeSynonymDeclaration <$> pure name <*> pure ps <*> updateType' ty
194+
go (TypeInstanceDeclaration name cs cn ts ds) =
195+
TypeInstanceDeclaration name <$> updateConstraints cs <*> updateClassName cn <*> updateType' ts <*> mapM go ds
196+
go (ExternInstanceDeclaration name cs cn ts) =
197+
ExternInstanceDeclaration name <$> updateConstraints cs <*> updateClassName cn <*> updateType' ts
198+
go (ValueDeclaration name nameKind [] Nothing val) = do
199+
val' <- everywhereWithContextM' [] (mkS bindFunctionArgs `extS` bindBinders) val
200+
rethrowFor "declaration" name $ ValueDeclaration name nameKind [] Nothing <$> updateAll val'
190201
where
191202
bindFunctionArgs bound (Abs (Left arg) val') = return (arg : bound, Abs (Left arg) val')
192203
bindFunctionArgs bound (Let ds val') = let args = map letBoundVariable ds in
@@ -206,17 +217,33 @@ renameInModule imports exports (Module mn decls exps) =
206217
letBoundVariable :: Declaration -> Ident
207218
letBoundVariable (ValueDeclaration ident _ _ _ _) = ident
208219
letBoundVariable _ = error "Invalid argument to letBoundVariable"
209-
updateVars (ValueDeclaration name _ _ _ _) = error $ "Binders should have been desugared in " ++ show name
210-
updateVars (TypeInstanceDeclaration name deps className tys ds) = TypeInstanceDeclaration name deps className tys <$> mapM updateVars ds
211-
updateVars other = return other
220+
go (ValueDeclaration name _ _ _ _) = error $ "Binders should have been desugared in " ++ show name
221+
go (ExternDeclaration fit name js ty) =
222+
rethrowFor "declaration" name $ ExternDeclaration <$> pure fit <*> pure name <*> pure js <*> updateType' ty
223+
go (BindingGroupDeclaration decls') = do
224+
BindingGroupDeclaration <$> mapM go' decls'
225+
where go' = \(name, nk, value) -> rethrowFor "declaration" name $ (,,) <$> pure name <*> pure nk <*> updateAll value
226+
go d = updateAll d
227+
228+
rethrowFor :: (Show a) => String -> a -> Either String b -> Either String b
229+
rethrowFor what name = rethrow $ "Error in " ++ what ++ " '" ++ show name ++ "'"
230+
231+
updateAll :: Data d => d -> Either String d
232+
updateAll = everywhereM (mkM updateType `extM` updateValue `extM` updateBinder)
233+
212234
updateValue (Constructor name) = Constructor <$> updateDataConstructorName name
213235
updateValue v = return v
236+
214237
updateBinder (ConstructorBinder name b) = ConstructorBinder <$> updateDataConstructorName name <*> pure b
215238
updateBinder v = return v
239+
216240
updateType (TypeConstructor name) = TypeConstructor <$> updateTypeName name
217241
updateType (SaturatedTypeSynonym name tys) = SaturatedTypeSynonym <$> updateTypeName name <*> mapM updateType tys
218242
updateType (ConstrainedType cs t) = ConstrainedType <$> updateConstraints cs <*> pure t
219243
updateType t = return t
244+
updateType' :: Data d => d -> Either String d
245+
updateType' = everywhereM (mkM updateType)
246+
220247
updateConstraints = mapM (\(name, ts) -> (,) <$> updateClassName name <*> pure ts)
221248

222249
updateTypeName = update "type" importedTypes (\mes -> isJust . (`lookup` (exportedTypes mes)))

0 commit comments

Comments
 (0)