git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #1759 from purescript/virtual-imports
896bbcdGary Burgess
examples/failing/1733.pursnew file+13
@@ -0,0 +1,13 @@
1+-- @shouldFailWith UnknownValue
2+
3+module Main where
4+
5+import Thingy as Thing
6+
7+main = Thing.doesntExist "hi"
8+
9+module Thingy where
10+
11+foo :: Int
12+foo = 1
13+
src/Language/PureScript/Sugar/Names.hs+8−3
@@ -16,6 +16,7 @@ import Control.Monad.Writer (MonadWriter(..), censor)
16import Control.Monad.State.Lazy
16import Control.Monad.State.Lazy
17
17
18import qualified Data.Map as M
18import qualified Data.Map as M
19+import qualified Data.Set as S
19
20
20import Language.PureScript.Crash
21import Language.PureScript.Crash
21import Language.PureScript.AST
22import Language.PureScript.AST
@@ -257,17 +258,21 @@ renameInModule env imports (Module ss coms mn decls exps) =
257 -- by qualified importing). If that's not the case, then we just need to
258 -- by qualified importing). If that's not the case, then we just need to
258 -- check it refers to a symbol in another module.
259 -- check it refers to a symbol in another module.
259 (Nothing, Just mn'') -> do
260 (Nothing, Just mn'') -> do
260− modExports <- getExports env mn''
261+ case M.lookup mn'' env of
262+ Nothing
263+ | mn'' `S.member` importedVirtualModules imports -> throwUnknown
261− maybe (throwError . errorMessage $ unknown qname) return (getE modExports name)
264+ | otherwise -> throwError . errorMessage $ UnknownModule mn''
265+ Just env' -> maybe throwUnknown return (getE (envModuleExports env') name)
262
266
263 -- If neither of the above cases are true then it's an undefined or
267 -- If neither of the above cases are true then it's an undefined or
264 -- unimported symbol.
268 -- unimported symbol.
265− _ -> throwError . errorMessage $ unknown qname
269+ _ -> throwUnknown
266
270
267 where
271 where
268 positioned err = case pos of
272 positioned err = case pos of
269 Nothing -> err
273 Nothing -> err
270 Just pos' -> rethrowWithPosition pos' err
274 Just pos' -> rethrowWithPosition pos' err
275+ throwUnknown = throwError . errorMessage $ unknown qname
271
276
272-- |
277-- |
273-- Replaces `ProperRef` export values with a `TypeRef` or `TypeClassRef`
278-- Replaces `ProperRef` export values with a `TypeRef` or `TypeClassRef`
src/Language/PureScript/Sugar/Names/Env.hs+9−3
@@ -22,6 +22,7 @@ import Data.Function (on)
22import Data.List (groupBy, sortBy, nub)
22import Data.List (groupBy, sortBy, nub)
23import Data.Maybe (fromJust)
23import Data.Maybe (fromJust)
24import qualified Data.Map as M
24import qualified Data.Map as M
25+import qualified Data.Set as S
25
26
26import Control.Monad
27import Control.Monad
27import Control.Monad.Error.Class (MonadError(..))
28import Control.Monad.Error.Class (MonadError(..))
@@ -54,16 +55,21 @@ data Imports = Imports
54 --
55 --
55 , importedValues :: M.Map (Qualified Ident) [(Qualified Ident, ModuleName)]
56 , importedValues :: M.Map (Qualified Ident) [(Qualified Ident, ModuleName)]
56 -- |
57 -- |
57− -- The list of modules that have been imported into the current scope.
58+ -- The modules that have been imported into the current scope.
58 --
59 --
59− , importedModules :: [ModuleName]
60+ , importedModules :: S.Set ModuleName
61+ -- |
62+ -- The names of "virtual" modules that come into existence when "import as"
63+ -- is used.
64+ --
65+ , importedVirtualModules :: S.Set ModuleName
60 } deriving (Show, Read)
66 } deriving (Show, Read)
61
67
62-- |
68-- |
63-- An empty 'Imports' value.
69-- An empty 'Imports' value.
64--
70--
65nullImports :: Imports
71nullImports :: Imports
66−nullImports = Imports M.empty M.empty M.empty M.empty []
72+nullImports = Imports M.empty M.empty M.empty M.empty S.empty S.empty
67
73
68-- |
74-- |
69-- The exported declarations from a module.
75-- The exported declarations from a module.
src/Language/PureScript/Sugar/Names/Imports.hs+5−1
@@ -23,6 +23,7 @@ import Control.Monad.Error.Class (MonadError(..))
23import Control.Monad.Writer (MonadWriter(..))
23import Control.Monad.Writer (MonadWriter(..))
24
24
25import qualified Data.Map as M
25import qualified Data.Map as M
26+import qualified Data.Set as S
26
27
27import Language.PureScript.Crash
28import Language.PureScript.Crash
28import Language.PureScript.AST
29import Language.PureScript.AST
@@ -168,7 +169,10 @@ resolveModuleImport env ie (mn, imps) = foldM go ie imps
168 go :: Imports -> (Maybe SourceSpan, ImportDeclarationType, Maybe ModuleName) -> m Imports
169 go :: Imports -> (Maybe SourceSpan, ImportDeclarationType, Maybe ModuleName) -> m Imports
169 go ie' (pos, typ, impQual) = do
170 go ie' (pos, typ, impQual) = do
170 modExports <- positioned $ maybe (throwError . errorMessage $ UnknownModule mn) (return . envModuleExports) $ mn `M.lookup` env
171 modExports <- positioned $ maybe (throwError . errorMessage $ UnknownModule mn) (return . envModuleExports) $ mn `M.lookup` env
172+ let virtualModules = importedVirtualModules ie'
171− let ie'' = ie' { importedModules = mn : importedModules ie' }
173+ ie'' = ie' { importedModules = S.insert mn (importedModules ie')
174+ , importedVirtualModules = maybe virtualModules (`S.insert` virtualModules) impQual
175+ }
172 positioned $ resolveImport mn modExports ie'' impQual typ
176 positioned $ resolveImport mn modExports ie'' impQual typ
173 where
177 where
174 positioned err = case pos of
178 positioned err = case pos of