git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #1657 from purescript/warn-duplicate-imports

Warn on duplicate imports

commitc709c2e76be771b99c3dfcc0cb1e7f34c38171cc authorGary Burgess <gary.burgess@gmail.com> dateMon 23 Nov 2015, 08:23 (11 years ago) parent8e33d05ed29a59
3 files changed+65−10
src/Language/PureScript/AST/Declarations.hs+1−1 src/Language/PureScript/Errors.hs+33−5 src/Language/PureScript/Sugar/Names/Imports.hs+31−4
src/Language/PureScript/AST/Declarations.hs+1−1
@@ -109,7 +109,7 @@ data ImportDeclarationType
109109 -- An import with a list of references to hide: `import M hiding (foo)`
110110 --
111111 | Hiding [DeclarationRef]
112− deriving (Show, Read, D.Data, D.Typeable)
112+ deriving (Eq, Show, Read, D.Data, D.Typeable)
113113
114114-- |
115115-- The data type of declarations
src/Language/PureScript/Errors.hs+33−5
@@ -143,6 +143,9 @@ data SimpleErrorMessage
143143 | UnusedDctorImport ProperName
144144 | UnusedDctorExplicitImport ProperName [ProperName]
145145 | DeprecatedQualifiedSyntax ModuleName ModuleName
146+ | RedundantUnqualifiedImport ModuleName ImportDeclarationType
147+ | DuplicateSelectiveImport ModuleName
148+ | DuplicateImport ModuleName ImportDeclarationType (Maybe ModuleName)
146149 deriving (Show)
147150
148151-- | Error message hints, providing more detailed information about failure.
@@ -278,6 +281,9 @@ errorCode em = case unwrapErrorMessage em of
278281 UnusedDctorImport{} -> "UnusedDctorImport"
279282 UnusedDctorExplicitImport{} -> "UnusedDctorExplicitImport"
280283 DeprecatedQualifiedSyntax{} -> "DeprecatedQualifiedSyntax"
284+ RedundantUnqualifiedImport{} -> "RedundantUnqualifiedImport"
285+ DuplicateSelectiveImport{} -> "DuplicateSelectiveImport"
286+ DuplicateImport{} -> "DuplicateImport"
281287
282288
283289-- |
@@ -747,6 +753,15 @@ prettyPrintSingleError full level e = do
747753 paras [ line $ "The import of type " ++ runModuleName name ++ " as " ++ runModuleName qualName ++ " uses the deprecated 'import qualified' syntax."
748754 , line $ "This syntax form will be removed in PureScript 0.9." ]
749755
756+ renderSimpleErrorMessage (RedundantUnqualifiedImport name imp) =
757+ line $ "Import of " ++ prettyPrintImport name imp Nothing ++ " is redundant due to a whole-module import"
758+
759+ renderSimpleErrorMessage (DuplicateSelectiveImport name) =
760+ line $ "There is an existing import of " ++ runModuleName name ++ ", consider merging the import lists"
761+
762+ renderSimpleErrorMessage (DuplicateImport name imp qual) =
763+ line $ "Duplicate import of " ++ prettyPrintImport name imp qual
764+
750765 renderHint :: ErrorMessageHint -> Box.Box -> Box.Box
751766 renderHint (ErrorUnifyingTypes t1 t2) detail =
752767 paras [ detail
@@ -878,11 +893,24 @@ prettyPrintSingleError full level e = do
878893 -- Pretty print and export declaration
879894 prettyPrintExport :: DeclarationRef -> String
880895 prettyPrintExport (TypeRef pn _) = runProperName pn
881− prettyPrintExport (ValueRef ident) = showIdent ident
882− prettyPrintExport (TypeClassRef pn) = runProperName pn
883− prettyPrintExport (TypeInstanceRef ident) = showIdent ident
884− prettyPrintExport (ModuleRef name) = "module " ++ runModuleName name
885− prettyPrintExport (PositionedDeclarationRef _ _ ref) = prettyPrintExport ref
896+ prettyPrintExport ref = prettyPrintRef ref
897+
898+ prettyPrintRef :: DeclarationRef -> String
899+ prettyPrintRef (TypeRef pn Nothing) = runProperName pn ++ "(..)"
900+ prettyPrintRef (TypeRef pn (Just dctors)) = runProperName pn ++ "(" ++ intercalate ", " (map runProperName dctors) ++ ")"
901+ prettyPrintRef (ValueRef ident) = showIdent ident
902+ prettyPrintRef (TypeClassRef pn) = runProperName pn
903+ prettyPrintRef (TypeInstanceRef ident) = showIdent ident
904+ prettyPrintRef (ModuleRef name) = "module " ++ runModuleName name
905+ prettyPrintRef (PositionedDeclarationRef _ _ ref) = prettyPrintExport ref
906+
907+ prettyPrintImport :: ModuleName -> ImportDeclarationType -> Maybe ModuleName -> String
908+ prettyPrintImport mn idt qual =
909+ let i = case idt of
910+ Implicit -> runModuleName mn
911+ Explicit refs -> runModuleName mn ++ " (" ++ intercalate ", " (map prettyPrintRef refs) ++ ")"
912+ Hiding refs -> runModuleName mn ++ " hiding (" ++ intercalate "," (map prettyPrintRef refs) ++ ")"
913+ in i ++ maybe "" (\q -> " as " ++ runModuleName q) qual
886914
887915 -- | Simplify an error message
888916 simplifyErrorMessage :: ErrorMessage -> ErrorMessage
src/Language/PureScript/Sugar/Names/Imports.hs+31−4
@@ -24,9 +24,9 @@ module Language.PureScript.Sugar.Names.Imports
2424import Prelude ()
2525import Prelude.Compat
2626
27−import Data.List (find)
28−import Data.Maybe (fromMaybe, isNothing, fromJust)
29−import Data.Foldable (traverse_)
27+import Data.List (find, delete)
28+import Data.Maybe (fromMaybe, isJust, isNothing, fromJust)
29+import Data.Foldable (traverse_, for_)
3030
3131import Control.Arrow (first)
3232import Control.Monad
@@ -70,9 +70,36 @@ findImports = foldM (go Nothing) M.empty
7070resolveImports :: (Applicative m, MonadError MultipleErrors m, MonadWriter MultipleErrors m) => Env -> Module -> m Imports
7171resolveImports env (Module _ _ currentModule decls _) =
7272 censor (addHint (ErrorInModule currentModule)) $ do
73− scope <- M.insert currentModule [(Nothing, Implicit, Nothing)] <$> findImports decls
73+ imports <- findImports decls
74+
75+ for_ (M.toList imports) $ \(mn, imps) -> do
76+ let unqual = filter (\(_, _, q) -> isJust q) (reverse imps)
77+
78+ when (length imps > 1) $ for_ (selfCartesianSubset imps) $
79+ \((_, t1, q1), (pos, t2, q2)) ->
80+ when (t1 == t2 && q1 == q2) $
81+ maybe id warnWithPosition pos $
82+ tell . errorMessage $ DuplicateImport mn t2 q2
83+
84+ when (length unqual > 1) $
85+ case find (\(_, typ, _) -> typ == Implicit) unqual of
86+ Just i ->
87+ for_ (delete i unqual) $ \(pos, typ, _) ->
88+ maybe id warnWithPosition pos $
89+ tell $ errorMessage $ RedundantUnqualifiedImport mn typ
90+ Nothing ->
91+ for_ (tail unqual) $ \(pos, _, _) ->
92+ maybe id warnWithPosition pos $
93+ tell $ errorMessage $ DuplicateSelectiveImport mn
94+
95+ let scope = M.insert currentModule [(Nothing, Implicit, Nothing)] imports
7496 foldM (resolveModuleImport currentModule env) nullImports (M.toList scope)
7597
98+ where
99+ selfCartesianSubset :: [a] -> [(a, a)]
100+ selfCartesianSubset (x : xs) = [(x, y) | y <- xs] ++ selfCartesianSubset xs
101+ selfCartesianSubset [] = []
102+
76103-- | Constructs a set of imports for a single module import.
77104resolveModuleImport ::
78105 forall m. (Applicative m, MonadError MultipleErrors m, MonadWriter MultipleErrors m) =>