git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Ensure explicitly imported types actually exist. Have to sacrifice explicit type class imports for now.
33d72ebPhil Freeman
examples/shouldfail/ImportExplicit2.purs → examples/failing/ImportExplicit2.pursrenamed

The content is the same.

examples/passing/ImportExplicit.purs+2−1
@@ -6,7 +6,8 @@ module M1 where
6module Main where
6module Main where
7
7
8 import M1 (X)
8 import M1 (X)
9−
9+
10+ testX :: X
10 testX = X
11 testX = X
11 testY = Y
12 testY = Y
12
13
src/Language/PureScript/Sugar/Names.hs+27−20
@@ -16,12 +16,16 @@ module Language.PureScript.Sugar.Names (
16 rename
16 rename
17) where
17) where
18
18
19−import Control.Applicative ((<$>), (<*>))
19+import Data.Maybe (fromMaybe)
20−import Control.Monad (forM_, unless, foldM, liftM)
21import Data.Generics (extM, mkM, everywhereM)
20import Data.Generics (extM, mkM, everywhereM)
22import Data.List (intersect)
21import Data.List (intersect)
22+
23+import Control.Applicative ((<$>), (<*>))
24+import Control.Monad (forM_, unless, foldM, liftM)
25+
23import qualified Data.Map as M
26import qualified Data.Map as M
24import qualified Data.Set as S
27import qualified Data.Set as S
28+
25import Language.PureScript.Declarations
29import Language.PureScript.Declarations
26import Language.PureScript.Names
30import Language.PureScript.Names
27import Language.PureScript.Types
31import Language.PureScript.Types
@@ -175,13 +179,12 @@ findImports = foldl findImports' M.empty
175
179
176-- |
180-- |
177-- Constructs a local environment for a module.
181-- Constructs a local environment for a module.
178−-- TODO: check explicit imports actually exist
179--
182--
180resolveImports :: ExportEnvironment -> Module -> Either String ImportEnvironment
183resolveImports :: ExportEnvironment -> Module -> Either String ImportEnvironment
181resolveImports env (Module currentModule decls) = ImportEnvironment
184resolveImports env (Module currentModule decls) = ImportEnvironment
182 <$> resolve (exportedTypes env) (resolveDefs typeImports)
185 <$> resolve (exportedTypes env) (resolveDefs typeImports)
183 <*> resolve (exportedDataConstructors env) resolveDcons
186 <*> resolve (exportedDataConstructors env) resolveDcons
184− <*> resolve (exportedTypeClasses env) (resolveDefs classImports)
187+ <*> resolve (exportedTypeClasses env) (resolveDefs (const []))
185 where
188 where
186 -- A Map from module name to imports from that module, where Nothing indicates everything is to be imported
189 -- A Map from module name to imports from that module, where Nothing indicates everything is to be imported
187 scope :: M.Map ModuleName (Maybe ExplicitImports)
190 scope :: M.Map ModuleName (Maybe ExplicitImports)
@@ -189,9 +192,9 @@ resolveImports env (Module currentModule decls) = ImportEnvironment
189
192
190 -- Resolve all exported names in a set by applying a type-specific resolver function
193 -- Resolve all exported names in a set by applying a type-specific resolver function
191 resolve :: M.Map ModuleName (S.Set id) ->
194 resolve :: M.Map ModuleName (S.Set id) ->
192− (M.Map ProperName (Qualified ProperName) -> (ModuleName, S.Set id) -> Either String (M.Map ProperName (Qualified ProperName))) ->
195+ (M.Map ProperName (Qualified ProperName) -> ModuleName -> S.Set id -> Maybe ExplicitImports -> Either String (M.Map ProperName (Qualified ProperName))) ->
193 Either String (M.Map ProperName (Qualified ProperName))
196 Either String (M.Map ProperName (Qualified ProperName))
194− resolve m f = foldM f M.empty (M.toList m)
197+ resolve m f = foldM (\m' (mn, maybeExpl) -> f m' mn (fromMaybe S.empty $ mn `M.lookup` m) maybeExpl) M.empty (M.toList scope)
195
198
196 -- Resolve a single name by adding it to the Map with a specified ModuleName, checking first
199 -- Resolve a single name by adding it to the Map with a specified ModuleName, checking first
197 -- that the name does not currently exist in scope.
200 -- that the name does not currently exist in scope.
@@ -211,22 +214,26 @@ resolveImports env (Module currentModule decls) = ImportEnvironment
211 -- its first argument
214 -- its first argument
212 resolveDefs :: (ExplicitImports -> [ProperName]) ->
215 resolveDefs :: (ExplicitImports -> [ProperName]) ->
213 M.Map ProperName (Qualified ProperName) ->
216 M.Map ProperName (Qualified ProperName) ->
217+ ModuleName ->
214− (ModuleName, S.Set ProperName) ->
218+ S.Set ProperName ->
219+ Maybe ExplicitImports ->
215 Either String (M.Map ProperName (Qualified ProperName))
220 Either String (M.Map ProperName (Qualified ProperName))
216− resolveDefs filt result (mn, names) = case M.lookup mn scope of
221+ resolveDefs filt result mn names maybeExpl = do
222+ names' <- case maybeExpl of
223+ Just expl -> do
224+ forM_ (filt expl) $ \name ->
225+ unless (name `S.member` names) $ Left $ show name ++ " is explictly imported but is not defined in module " ++ show mn
226+ return $ S.toList names `intersect` filt expl
227+ Nothing -> return $ S.toList names
217− Just maybeExpl -> foldM (resolveDef mn) result $
228+ foldM (resolveDef mn) result names'
218− case maybeExpl of
219− Just expl -> S.toList names `intersect` filt expl
220− Nothing -> S.toList names
221− Nothing -> Right result
222
229
223 -- Resolve a set of data constructors keyed by their type constructor
230 -- Resolve a set of data constructors keyed by their type constructor
224 resolveDcons :: M.Map ProperName (Qualified ProperName) ->
231 resolveDcons :: M.Map ProperName (Qualified ProperName) ->
232+ ModuleName ->
225− (ModuleName, S.Set (ProperName, [ProperName])) ->
233+ S.Set (ProperName, [ProperName]) ->
234+ Maybe ExplicitImports ->
226 Either String (M.Map ProperName (Qualified ProperName))
235 Either String (M.Map ProperName (Qualified ProperName))
227− resolveDcons result (mn, tcons) = case M.lookup mn scope of
228− Just maybeExpl -> foldM (foldM $ resolveDef mn) result $ map snd $
236+ resolveDcons result mn tcons maybeExpl = foldM (foldM $ resolveDef mn) result $ map snd $
229− case maybeExpl of
237+ case maybeExpl of
230− Just expl -> filter ((`elem` typeImports expl) . fst) (S.toList tcons)
238+ Just expl -> filter ((`elem` typeImports expl) . fst) (S.toList tcons)
231− Nothing -> S.toList tcons
239+ Nothing -> S.toList tcons
232− Nothing -> Right result