git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #1389 from purescript/817-1112-multiple-imports

Merge multiple imports of the same module

commitc56f0ac1b86f0766c7fc95fcd0cffd614fb706a2 authorGary Burgess <gary.burgess@gmail.com> dateMon 17 Aug 2015, 17:23 (11 years ago) parent282ae382c8ea37
2 files changed+26−17
src/Language/PureScript/Errors.hs+1−1 src/Language/PureScript/Sugar/Names.hs+25−16
src/Language/PureScript/Errors.hs+1−1
@@ -454,7 +454,7 @@ prettyPrintSingleError full level e = prettyPrintErrorMessage <$> onTypesInError
454454 goSimple (ConflictingImport nm mn) =
455455 line $ "Cannot declare " ++ show nm ++ " since another declaration of that name was imported from " ++ show mn
456456 goSimple (ConflictingImports nm m1 m2) =
457− line $ "Conflicting imports for " ++ show nm ++ " from modules " ++ show m1 ++ " and " ++ show m2
457+ line $ "Conflicting imports for " ++ nm ++ " from modules " ++ show m1 ++ " and " ++ show m2
458458 goSimple (ConflictingTypeDecls nm) =
459459 line $ "Conflicting type declarations for " ++ show nm
460460 goSimple (ConflictingCtorDecls nm) =
src/Language/PureScript/Sugar/Names.hs+25−16
@@ -468,10 +468,13 @@ filterExports mn exps env = do
468468-- Finds the imports within a module, mapping the imported module name to an optional set of
469469-- explicitly imported declarations.
470470--
471−findImports :: [Declaration] -> M.Map ModuleName (Maybe SourceSpan, ImportDeclarationType, Maybe ModuleName)
471+findImports :: [Declaration] -> M.Map ModuleName [(Maybe SourceSpan, ImportDeclarationType, Maybe ModuleName)]
472472findImports = foldl (findImports' Nothing) M.empty
473473 where
474− findImports' pos result (ImportDeclaration mn typ qual) = M.insert mn (pos, typ, qual) result
474+ findImports' pos result (ImportDeclaration mn typ qual) =
475+ case mn `M.lookup` result of
476+ Just is -> M.insert mn ((pos, typ, qual):is) result
477+ Nothing -> M.insert mn [(pos, typ, qual)] result
475478 findImports' _ result (PositionedDeclaration pos _ d) = findImports' (Just pos) result d
476479 findImports' _ result _ = result
477480
@@ -486,17 +489,20 @@ resolveImports env (Module _ _ currentModule decls _) =
486489 -- A Map from module name to the source position for the import, the list of imports from that
487490 -- module (where Nothing indicates everything is to be imported), and optionally a qualified name
488491 -- for the module
489− scope :: M.Map ModuleName (Maybe SourceSpan, ImportDeclarationType, Maybe ModuleName)
490− scope = M.insert currentModule (Nothing, Implicit, Nothing) (findImports decls)
492+ scope :: M.Map ModuleName [(Maybe SourceSpan, ImportDeclarationType, Maybe ModuleName)]
493+ scope = M.insert currentModule [(Nothing, Implicit, Nothing)] (findImports decls)
491494
492− resolveImport' :: ImportEnvironment -> (ModuleName, (Maybe SourceSpan, ImportDeclarationType, Maybe ModuleName)) -> m ImportEnvironment
493− resolveImport' imp (mn, (pos, typ, impQual)) = do
494− modExports <- positioned $ maybe (throwError . errorMessage $ UnknownModule mn) return $ mn `M.lookup` env
495− positioned $ resolveImport currentModule mn modExports imp impQual typ
495+ resolveImport' :: ImportEnvironment -> (ModuleName, [(Maybe SourceSpan, ImportDeclarationType, Maybe ModuleName)]) -> m ImportEnvironment
496+ resolveImport' ie (mn, imps) = foldM go ie imps
496497 where
497− positioned err = case pos of
498− Nothing -> err
499− Just pos' -> rethrowWithPosition pos' err
498+ go :: ImportEnvironment -> (Maybe SourceSpan, ImportDeclarationType, Maybe ModuleName) -> m ImportEnvironment
499+ go ie' (pos, typ, impQual) = do
500+ modExports <- positioned $ maybe (throwError . errorMessage $ UnknownModule mn) return $ mn `M.lookup` env
501+ positioned $ resolveImport currentModule mn modExports ie' impQual typ
502+ where
503+ positioned err = case pos of
504+ Nothing -> err
505+ Just pos' -> rethrowWithPosition pos' err
500506
501507-- |
502508-- Extends the local environment for a module by resolving an import of another module.
@@ -583,11 +589,14 @@ resolveImport currentModule importModule exps imps impQual =
583589 updateImports m name = case M.lookup (Qualified impQual name) m of
584590 Nothing -> return $ M.insert (Qualified impQual name) (Qualified (Just importModule) name) m
585591 Just (Qualified Nothing _) -> error "Invalid state in updateImports"
586− Just (Qualified (Just mn) _) -> throwError . errorMessage $ err
587− where
588− err = if currentModule `elem` [mn, importModule]
589− then ConflictingImport (show name) importModule
590− else ConflictingImports (show name) mn importModule
592+ Just (Qualified (Just mn) _)
593+ | mn == importModule -> return m
594+ | otherwise -> throwError . errorMessage $ err
595+ where
596+ err = if currentModule `elem` [mn, importModule]
597+ then ConflictingImport (show name) importModule
598+ else ConflictingImports (show name) mn importModule
599+
591600
592601 -- The available values, types, and classes in the module being imported
593602 values = exportedValues exps