git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Check that specific data constructors do exist
commit3ac606d1cdc8687b00d0c3a882e0536df52741ef authorGary Burgess <gary.burgess@gmail.com> dateMon 3 Mar 2014, 13:24 (13 years ago) parent0d705d1
4 files changed+22−7
examples/failing/ImportExplicit.purs+1−1 examples/failing/ImportExplicit2.purs+1−1 examples/failing/ImportExplicit3.purs+7 src/Language/PureScript/Sugar/Names.hs+13−5
examples/failing/ImportExplicit.purs+1−1
@@ -5,7 +5,7 @@ module M1 where
55
66module Main where
77
8− import M1 (X)
8+ import M1 (X(..))
99
1010 testX = X
1111 testY = Y
examples/failing/ImportExplicit2.purs+1−1
@@ -4,4 +4,4 @@ module M1 where
44
55module Main where
66
7− import M1 (X)
7+ import M1 (X(..))
examples/failing/ImportExplicit3.pursnew file+7
@@ -0,0 +1,7 @@
1+module M1 where
2+
3+ data X = Y
4+
5+module Main where
6+
7+ import M1 (X(Z, Q))
src/Language/PureScript/Sugar/Names.hs+13−5
@@ -16,8 +16,8 @@ module Language.PureScript.Sugar.Names (
1616 desugarImports
1717) where
1818
19−import Data.Maybe (fromMaybe)
20−import Data.List (intersect)
19+import Data.Maybe (fromMaybe, isJust)
20+import Data.List (intersect, intercalate, (\\))
2121import Data.Data
2222import Data.Generics (extM, mkM, everywhereM)
2323import Data.Generics.Extras (mkS, extS, everywhereWithContextM')
@@ -116,7 +116,7 @@ desugarImports modules = do
116116 exports <- findExports modules
117117 mapM (renameInModule' exports) modules
118118 where
119− renameInModule' exports m = do
119+ renameInModule' exports m@(Module mn _) = do
120120 imports <- resolveImports exports m
121121 rethrowForModule m (renameInModule imports m)
122122
@@ -225,12 +225,20 @@ resolveImports env (Module currentModule decls) =
225225 importExplicit mn imp (TypeImport name dctors) = do
226226 allDctors <- allExportedDataConstructors env mn name
227227 types' <- updateImports mn (importedTypes imp) name
228− dctors' <- foldM (updateImports mn) (importedDataConstructors imp) (fromMaybe allDctors dctors)
229− return $ imp { importedTypes = types', importedDataConstructors = dctors' }
228+ dctors' <- maybe (return allDctors) (check "data constructor" allDctors) dctors
229+ dctors'' <- foldM (updateImports mn) (importedDataConstructors imp) dctors'
230+ return $ imp { importedTypes = types', importedDataConstructors = dctors'' }
230231 importExplicit mn imp (TypeClassImport name) = do
231232 typeClasses' <- updateImports mn (importedTypeClasses imp) name
232233 return $ imp { importedTypeClasses = typeClasses' }
233234
235+ -- Check that each item in a list of explicit imports does actually exist
236+ check :: (Show a, Eq a) => String -> [a] -> [a] -> Either String [a]
237+ check t all selected = case selected \\ all of
238+ [] -> return selected
239+ (unknown:[]) -> throwError $ "Unknown " ++ t ++ " '" ++ show unknown ++ "'"
240+ unknowns -> throwError $ "Unknown " ++ t ++ "s '" ++ (intercalate "', '" (show `map` unknowns)) ++ "'"
241+
234242 -- Import everything from a module
235243 importAll :: ImportEnvironment -> ModuleName -> ExportEnvironment -> Either String ImportEnvironment
236244 importAll imp mn env = do