@@ -131,12 +131,25 @@ addExport exports name =
132desugarImports :: [Module] -> Either String [Module]
132desugarImports :: [Module] -> Either String [Module]
133desugarImports modules = do
133desugarImports modules = do
134− exports <- findExports modules
134+ unfilteredExports <- findExports modules
135+ exports <- foldM filterModuleExports unfilteredExports modules
135− mapM (renameInModule' exports) modules
136+ mapM (renameInModule' unfilteredExports exports) modules
139+ -- Filters the exports for a module in the global exports environment so that only explicitly
140+ -- exported members remain. If the module does not explicitly export anything, everything is
142+ filterModuleExports :: ExportEnvironment -> Module -> Either String ExportEnvironment
143+ filterModuleExports env (Module mn _ (Just exps)) = filterExports mn exps env
144+ filterModuleExports env _ = return env
146+ -- Rename and check all the names within a module. We tweak the global exports environment so
147+ -- the module has access to an unfiltered list of its own members.
148+ renameInModule' :: ExportEnvironment -> ExportEnvironment -> Module -> Either String Module
137− renameInModule' exports m = rethrowForModule m $ do
149+ renameInModule' unfilteredExports exports m@(Module mn _ _) = rethrowForModule m $ do
150+ let exports' = M.update (\_ -> M.lookup mn unfilteredExports) mn exports
138− imports <- resolveImports exports m
151+ imports <- resolveImports exports' m
139− renameInModule imports exports m
152+ renameInModule imports exports' m
142-- Rethrow an error with the name of the current module in the case of a failure
155-- Rethrow an error with the name of the current module in the case of a failure
@@ -229,10 +242,7 @@ findExports = foldM addModule $ M.singleton (ModuleName [ProperName "Prim"]) pri
230 -- Add all of the exported declarations from a module to the global export environment
243 -- Add all of the exported declarations from a module to the global export environment
231 addModule :: ExportEnvironment -> Module -> Either String ExportEnvironment
244 addModule :: ExportEnvironment -> Module -> Either String ExportEnvironment
232− addModule env m@(Module mn ds Nothing) = rethrowForModule m $ foldM (addDecl mn) (addEmptyModule env mn) ds
233− addModule env m@(Module mn ds (Just exps)) = rethrowForModule m $ do
234− env' <- foldM (addDecl mn) (addEmptyModule env mn) ds
245+ addModule env m@(Module mn ds _) = rethrowForModule m $ foldM (addDecl mn) (addEmptyModule env mn) ds
235− filterExports mn exps env'
237 -- Add a declaration from a module to the global export environment
247 -- Add a declaration from a module to the global export environment
238 addDecl :: ModuleName -> ExportEnvironment -> Declaration -> Either String ExportEnvironment
248 addDecl :: ModuleName -> ExportEnvironment -> Declaration -> Either String ExportEnvironment
@@ -330,9 +340,7 @@ resolveImports env (Module currentModule decls _) =
330-- Extends the local environment for a module by resolving an import of another module.
340-- Extends the local environment for a module by resolving an import of another module.
332resolveImport :: ModuleName -> ModuleName -> Exports -> ImportEnvironment -> Maybe ExplicitImports -> Either String ImportEnvironment
342resolveImport :: ModuleName -> ModuleName -> Exports -> ImportEnvironment -> Maybe ExplicitImports -> Either String ImportEnvironment
333−resolveImport currentModule importModule exps imps i = case i of
343+resolveImport currentModule importModule exps imps = maybe importAll (foldM importExplicit imps)
334− Nothing -> importAll
335− Just expl -> foldM importExplicit imps expl
338 -- Import everything from a module
346 -- Import everything from a module