@@ -28,6 +28,7 @@ import Control.Applicative (Applicative(..), (<$>), (<*>))
28import Control.Monad
28import Control.Monad
29import Control.Monad.Error.Class (MonadError(..))
29import Control.Monad.Error.Class (MonadError(..))
30import Control.Monad.Writer (MonadWriter(..), censor)
30import Control.Monad.Writer (MonadWriter(..), censor)
31+import Control.Monad.State.Lazy
32import qualified Data.Map as M
33import qualified Data.Map as M
@@ -41,6 +42,7 @@ import Language.PureScript.Externs
41import Language.PureScript.Sugar.Names.Env
42import Language.PureScript.Sugar.Names.Env
42import Language.PureScript.Sugar.Names.Imports
43import Language.PureScript.Sugar.Names.Imports
43import Language.PureScript.Sugar.Names.Exports
44import Language.PureScript.Sugar.Names.Exports
45+import Language.PureScript.Linter.Imports
46-- Replaces all local names with qualified names within a list of modules. The
48-- Replaces all local names with qualified names within a list of modules. The
@@ -103,9 +105,11 @@ desugarImports externs modules = do
104 renameInModule' :: Env -> Module -> m Module
106 renameInModule' :: Env -> Module -> m Module
105 renameInModule' env m@(Module _ _ mn _ _) =
107 renameInModule' env m@(Module _ _ mn _ _) =
106− rethrow (addHint (ErrorInModule mn)) $ do
108+ warnAndRethrow (addHint (ErrorInModule mn)) $ do
107 let (_, imps, exps) = fromMaybe (internalError "Module is missing in renameInModule'") $ M.lookup mn env
109 let (_, imps, exps) = fromMaybe (internalError "Module is missing in renameInModule'") $ M.lookup mn env
108− elaborateImports imps <$> renameInModule env imps (elaborateExports exps m)
110+ (m', used) <- flip runStateT M.empty $ renameInModule env imps (elaborateExports exps m)
111+ findUnusedImports m imps used
112+ return $ elaborateImports imps m'
111-- Make all exports for a module explicit. This may still effect modules that
115-- Make all exports for a module explicit. This may still effect modules that
@@ -146,10 +150,11 @@ elaborateImports imps (Module ss coms mn decls exps) = Module ss coms mn decls'
146-- Replaces all local names with qualified names within a module and checks that all existing
150-- Replaces all local names with qualified names within a module and checks that all existing
147-- qualified names are valid.
151-- qualified names are valid.
149−renameInModule :: forall m. (Applicative m, MonadError MultipleErrors m) => Env -> Imports -> Module -> m Module
153+renameInModule :: forall m. (Applicative m, MonadError MultipleErrors m, MonadWriter MultipleErrors m, MonadState UsedImports m) => Env -> Imports -> Module -> m Module
150renameInModule env imports (Module ss coms mn decls exps) =
154renameInModule env imports (Module ss coms mn decls exps) =
151 Module ss coms mn <$> parU decls go <*> pure exps
155 Module ss coms mn <$> parU decls go <*> pure exps
153 (go, _, _, _, _) = everywhereWithContextOnValuesM (Nothing, []) updateDecl updateValue updateBinder updateCase defS
158 (go, _, _, _, _) = everywhereWithContextOnValuesM (Nothing, []) updateDecl updateValue updateBinder updateCase defS
155 updateDecl :: (Maybe SourceSpan, [Ident]) -> Declaration -> m ((Maybe SourceSpan, [Ident]), Declaration)
160 updateDecl :: (Maybe SourceSpan, [Ident]) -> Declaration -> m ((Maybe SourceSpan, [Ident]), Declaration)
@@ -168,7 +173,7 @@ renameInModule env imports (Module ss coms mn decls exps) =
168 updateDecl (pos, bound) (ExternDeclaration name ty) =
173 updateDecl (pos, bound) (ExternDeclaration name ty) =
169 (,) (pos, name : bound) <$> (ExternDeclaration name <$> updateTypesEverywhere pos ty)
174 (,) (pos, name : bound) <$> (ExternDeclaration name <$> updateTypesEverywhere pos ty)
170 updateDecl s d = return (s, d)
175 updateDecl s d = return (s, d)
172 updateValue :: (Maybe SourceSpan, [Ident]) -> Expr -> m ((Maybe SourceSpan, [Ident]), Expr)
177 updateValue :: (Maybe SourceSpan, [Ident]) -> Expr -> m ((Maybe SourceSpan, [Ident]), Expr)
173 updateValue (_, bound) v@(PositionedValue pos' _ _) =
178 updateValue (_, bound) v@(PositionedValue pos' _ _) =
174 return ((Just pos', bound), v)
179 return ((Just pos', bound), v)
@@ -189,7 +194,7 @@ renameInModule env imports (Module ss coms mn decls exps) =
189 updateValue s@(pos, _) (TypedValue check val ty) =
194 updateValue s@(pos, _) (TypedValue check val ty) =
190 (,) s <$> (TypedValue check val <$> updateTypesEverywhere pos ty)
195 (,) s <$> (TypedValue check val <$> updateTypesEverywhere pos ty)
191 updateValue s v = return (s, v)
196 updateValue s v = return (s, v)
193 updateBinder :: (Maybe SourceSpan, [Ident]) -> Binder -> m ((Maybe SourceSpan, [Ident]), Binder)
198 updateBinder :: (Maybe SourceSpan, [Ident]) -> Binder -> m ((Maybe SourceSpan, [Ident]), Binder)
194 updateBinder (_, bound) v@(PositionedBinder pos _ _) =
199 updateBinder (_, bound) v@(PositionedBinder pos _ _) =
195 return ((Just pos, bound), v)
200 return ((Just pos, bound), v)
@@ -201,8 +206,8 @@ renameInModule env imports (Module ss coms mn decls exps) =
201 return (s', TypedBinder t' b')
206 return (s', TypedBinder t' b')
202 updateBinder s v =
207 updateBinder s v =
203 return (s, v)
208 return (s, v)
205− updateCase :: (Maybe SourceSpan, [Ident]) -> CaseAlternative -> m ((Maybe SourceSpan, [Ident]), CaseAlternative)
210+ updateCase :: (Maybe SourceSpan, [Ident]) -> CaseAlternative -> m ((Maybe SourceSpan, [Ident]), CaseAlternative)
206 updateCase (pos, bound) c@(CaseAlternative bs _) =
211 updateCase (pos, bound) c@(CaseAlternative bs _) =
207 return ((pos, concatMap binderNames bs ++ bound), c)
212 return ((pos, concatMap binderNames bs ++ bound), c)
@@ -223,16 +228,16 @@ renameInModule env imports (Module ss coms mn decls exps) =
223 updateConstraints pos = mapM (\(name, ts) -> (,) <$> updateClassName name pos <*> mapM (updateTypesEverywhere pos) ts)
228 updateConstraints pos = mapM (\(name, ts) -> (,) <$> updateClassName name pos <*> mapM (updateTypesEverywhere pos) ts)
225 updateTypeName :: Qualified ProperName -> Maybe SourceSpan -> m (Qualified ProperName)
230 updateTypeName :: Qualified ProperName -> Maybe SourceSpan -> m (Qualified ProperName)
226− updateTypeName = update UnknownType (importedTypes imports) (resolveType . exportedTypes)
231+ updateTypeName = update UnknownType (importedTypes imports) (resolveType . exportedTypes) IsProperName
228 updateDataConstructorName :: Qualified ProperName -> Maybe SourceSpan -> m (Qualified ProperName)
233 updateDataConstructorName :: Qualified ProperName -> Maybe SourceSpan -> m (Qualified ProperName)
229− updateDataConstructorName = update (flip UnknownDataConstructor Nothing) (importedDataConstructors imports) (resolveDctor . exportedTypes)
234+ updateDataConstructorName = update (flip UnknownDataConstructor Nothing) (importedDataConstructors imports) (resolveDctor . exportedTypes) IsProperName
231 updateClassName :: Qualified ProperName -> Maybe SourceSpan -> m (Qualified ProperName)
236 updateClassName :: Qualified ProperName -> Maybe SourceSpan -> m (Qualified ProperName)
232− updateClassName = update UnknownTypeClass (importedTypeClasses imports) (resolve . exportedTypeClasses)
237+ updateClassName = update UnknownTypeClass (importedTypeClasses imports) (resolve . exportedTypeClasses) IsProperName
234 updateValueName :: Qualified Ident -> Maybe SourceSpan -> m (Qualified Ident)
239 updateValueName :: Qualified Ident -> Maybe SourceSpan -> m (Qualified Ident)
235− updateValueName = update UnknownValue (importedValues imports) (resolve . exportedValues)
240+ updateValueName = update UnknownValue (importedValues imports) (resolve . exportedValues) IdentName
237 -- Used when performing an update to qualify values and classes with their
242 -- Used when performing an update to qualify values and classes with their
238 -- module of original definition.
243 -- module of original definition.
@@ -255,16 +260,22 @@ renameInModule env imports (Module ss coms mn decls exps) =
255 update :: (Ord a) => (Qualified a -> SimpleErrorMessage)
260 update :: (Ord a) => (Qualified a -> SimpleErrorMessage)
256 -> M.Map (Qualified a) (Qualified a, ModuleName)
261 -> M.Map (Qualified a) (Qualified a, ModuleName)
257 -> (Exports -> a -> Maybe (Qualified a))
262 -> (Exports -> a -> Maybe (Qualified a))
263+ -> (Qualified a -> Name)
258 -> Qualified a
264 -> Qualified a
259 -> Maybe SourceSpan
265 -> Maybe SourceSpan
260 -> m (Qualified a)
266 -> m (Qualified a)
261− update unknown imps getE qname@(Qualified mn' name) pos = positioned $
267+ update unknown imps getE toName qname@(Qualified mn' name) pos = positioned $
262 case (M.lookup qname imps, mn') of
268 case (M.lookup qname imps, mn') of
263 -- We found the name in our imports, so we return the name for it,
269 -- We found the name in our imports, so we return the name for it,
264 -- qualifying with the name of the module it was originally defined in
270 -- qualifying with the name of the module it was originally defined in
265 -- rather than the module we're importing from, to handle the case of
271 -- rather than the module we're importing from, to handle the case of
266 -- re-exports.
272 -- re-exports.
273+ (Just (qn, mnOrig), _) -> do
275+ Qualified (Just mnNew) _ ->
276+ modify $ \result -> M.insert mnNew (maybe [toName qname] (toName qname :) (mnNew `M.lookup` result)) result
267− (Just (_, mnOrig), _) -> return $ Qualified (Just mnOrig) name
278+ return $ Qualified (Just mnOrig) name
268 -- If the name wasn't found in our imports but was qualified then we need
279 -- If the name wasn't found in our imports but was qualified then we need
269 -- to check whether it's a failed import from a "pseudo" module (created
280 -- to check whether it's a failed import from a "pseudo" module (created
270 -- by qualified importing). If that's not the case, then we just need to
281 -- by qualified importing). If that's not the case, then we just need to