@@ -364,14 +364,16 @@ filterExports mn exps env = do
364364--
365365type ExplicitImports = [DeclarationRef]
366366
367+type HiddenImports = [DeclarationRef]
368+
367369-- |
368370-- Finds the imports within a module, mapping the imported module name to an optional set of
369371-- explicitly imported declarations.
370372--
371−findImports :: [Declaration] -> M.Map ModuleName (Maybe SourcePos, Maybe ExplicitImports, Maybe ModuleName)
373+findImports :: [Declaration] -> M.Map ModuleName (Maybe SourcePos, Maybe ExplicitImports, Maybe HiddenImports, Maybe ModuleName)
372374findImports = foldl (findImports' Nothing) M.empty
373375 where
374− findImports' pos result (ImportDeclaration mn expl qual) = M.insert mn (pos, expl, qual) result
376+ findImports' pos result (ImportDeclaration mn expl hidden qual) = M.insert mn (pos, expl, hidden, qual) result
375377 findImports' _ result (PositionedDeclaration pos d) = findImports' (Just pos) result d
376378 findImports' _ result _ = result
377379
@@ -386,13 +388,13 @@ resolveImports env (Module currentModule decls _) =
386388 -- A Map from module name to the source position for the import, the list of imports from that
387389 -- module (where Nothing indicates everything is to be imported), and optionally a qualified name
388390 -- for the module
389− scope :: M.Map ModuleName (Maybe SourcePos, Maybe ExplicitImports, Maybe ModuleName)
390− scope = M.insert currentModule (Nothing, Nothing, Nothing) (findImports decls)
391+ scope :: M.Map ModuleName (Maybe SourcePos, Maybe ExplicitImports, Maybe HiddenImports, Maybe ModuleName)
392+ scope = M.insert currentModule (Nothing, Nothing, Nothing, Nothing) (findImports decls)
391393
392− resolveImport' :: ImportEnvironment -> (ModuleName, (Maybe SourcePos, Maybe ExplicitImports, Maybe ModuleName)) -> Either ErrorStack ImportEnvironment
393− resolveImport' imp (mn, (pos, explImports, impQual)) = do
394+ resolveImport' :: ImportEnvironment -> (ModuleName, (Maybe SourcePos, Maybe ExplicitImports, Maybe HiddenImports, Maybe ModuleName)) -> Either ErrorStack ImportEnvironment
395+ resolveImport' imp (mn, (pos, explImports, hiddenImports, impQual)) = do
394396 modExports <- positioned $ maybe (throwError $ mkErrorStack ("Cannot import unknown module '" ++ show mn ++ "'") Nothing) return $ mn `M.lookup` env
395− positioned $ resolveImport currentModule mn modExports imp impQual explImports
397+ positioned $ resolveImport currentModule mn modExports imp impQual explImports hiddenImports
396398 where
397399 positioned err = case pos of
398400 Nothing -> err
@@ -401,37 +403,61 @@ resolveImports env (Module currentModule decls _) =
401403-- |
402404-- Extends the local environment for a module by resolving an import of another module.
403405--
404−resolveImport :: ModuleName -> ModuleName -> Exports -> ImportEnvironment -> Maybe ModuleName -> Maybe ExplicitImports-> Either ErrorStack ImportEnvironment
405−resolveImport currentModule importModule exps imps impQual = maybe importAll (foldM importExplicit imps)
406+resolveImport :: ModuleName -> ModuleName -> Exports -> ImportEnvironment -> Maybe ModuleName -> Maybe ExplicitImports -> Maybe HiddenImports -> Either ErrorStack ImportEnvironment
407+resolveImport currentModule importModule exps imps impQual explImports hiddenImports =
408+ maybe importAll (foldM importExplicit imps) explImports
406409 where
407410
408411 -- Import everything from a module
409412 importAll :: Either ErrorStack ImportEnvironment
410413 importAll = do
411− imp' <- foldM (\m (name, dctors) -> importExplicit m (TypeRef name (Just dctors))) imps (exportedTypes exps)
412− imp'' <- foldM (\m name -> importExplicit m (ValueRef name)) imp' (exportedValues exps)
413− foldM (\m name -> importExplicit m (TypeClassRef name)) imp'' (exportedTypeClasses exps)
414+ imp' <- foldM (\m (name, dctors) -> importExplicitNotHidden m (TypeRef name (Just dctors))) imps (exportedTypes exps)
415+ imp'' <- foldM (\m name -> importExplicitNotHidden m (ValueRef name)) imp' (exportedValues exps)
416+ foldM (\m name -> importExplicitNotHidden m (TypeClassRef name)) imp'' (exportedTypeClasses exps)
417+
418+ -- Import something explicitly only if it's not hidden
419+ importExplicitNotHidden :: ImportEnvironment -> DeclarationRef -> Either ErrorStack ImportEnvironment
420+ importExplicitNotHidden m value = do
421+ hidden <- hiddenImports'
422+ if elem value hidden then (Right m) else importExplicit m value
414423
415424 -- Import something explicitly
416425 importExplicit :: ImportEnvironment -> DeclarationRef -> Either ErrorStack ImportEnvironment
417426 importExplicit imp (PositionedDeclarationRef pos r) = rethrowWithPosition pos $ importExplicit imp r
418− importExplicit imp (ValueRef name) = do
419− _ <- checkImportExists "value" values name
427+ importExplicit imp ref@(ValueRef name) = do
428+ _ <- checkRefIsValid ref
420429 values' <- updateImports (importedValues imp) name
421430 return $ imp { importedValues = values' }
422− importExplicit imp (TypeRef name dctors) = do
423− _ <- checkImportExists "type" availableTypes name
431+ importExplicit imp ref@(TypeRef name dctors) = do
432+ _ <- checkRefIsValid ref
424433 types' <- updateImports (importedTypes imp) name
425434 let allDctors = allExportedDataConstructors name
426435 dctors' <- maybe (return allDctors) (mapM $ checkDctorExists allDctors) dctors
427436 dctors'' <- foldM updateImports (importedDataConstructors imp) dctors'
428437 return $ imp { importedTypes = types', importedDataConstructors = dctors'' }
429− importExplicit imp (TypeClassRef name) = do
430− _ <- checkImportExists "type class" classes name
438+ importExplicit imp ref@(TypeClassRef name) = do
439+ _ <- checkRefIsValid ref
431440 typeClasses' <- updateImports (importedTypeClasses imp) name
432441 return $ imp { importedTypeClasses = typeClasses' }
433442 importExplicit _ _ = error "Invalid argument to importExplicit"
434443
444+ -- hiddenImports checked for validness
445+ hiddenImports' =
446+ mapM checkRefIsValid (fromMaybe [] hiddenImports)
447+
448+ -- Check if DeclarationRef points to a existent symbol
449+ checkRefIsValid :: DeclarationRef -> Either ErrorStack DeclarationRef
450+ checkRefIsValid ref@(PositionedDeclarationRef pos r) = do
451+ _ <- rethrowWithPosition pos $ checkRefIsValid r
452+ return ref
453+ checkRefIsValid ref@(ValueRef name) =
454+ checkImportExists "value" values name >> return ref
455+ checkRefIsValid ref@(TypeRef name _) =
456+ checkImportExists "type" availableTypes name >> return ref
457+ checkRefIsValid ref@(TypeClassRef name) =
458+ checkImportExists "type class" classes name >> return ref
459+ checkRefIsValid _ = error "Invalid argument to checkRefIsValid"
460+
435461 -- Find all exported data constructors for a given type
436462 allExportedDataConstructors :: ProperName -> [ProperName]
437463 allExportedDataConstructors name = fromMaybe [] $ name `lookup` exportedTypes exps