- returnM (mod:mods, occs', avails')
-
- exports_from_item acc@(mods, occs, avails) ie
- = lookupGRE (ieName ie) `thenM` \ mb_gre ->
- case mb_gre of {
- Nothing -> addErr (unknownNameErr (ieName ie)) `thenM_`
- returnM acc ;
- Just gre ->
-
- -- Get the AvailInfo for the parent of the specified name
- case lookupAvailEnv entity_avail_env (gre_parent gre) of {
- Nothing -> pprPanic "exportsFromAvail"
- ((ppr (ieName ie)) <+> ppr gre) ;
- Just avail ->
-
- -- Filter out the bits we want
- case filterAvail ie avail of {
- Nothing -> -- Not enough availability
- addErr (exportItemErr ie) `thenM_`
- returnM acc ;
-
- Just export_avail ->
-
- -- Phew! It's OK! Now to check the occurrence stuff!
- warnIf (not (ok_item ie avail)) (dodgyExportWarn ie) `thenM_`
- check_occs warn_dup_exports ie occs export_avail `thenM` \ occs' ->
- returnM (mods, occs', addAvail avails export_avail)
- }}}
-
-
-
-ok_item (IEThingAll _) (AvailTC _ [n]) = False
- -- This occurs when you import T(..), but
- -- only export T abstractly. The single [n]
- -- in the AvailTC is the type or class itself
-ok_item _ _ = True
-
-check_occs :: Bool -> RdrNameIE -> ExportOccMap -> AvailInfo -> TcRn m ExportOccMap
-check_occs warn_dup_exports ie occs avail
- = foldlM check occs (availNames avail)
+
+ -- This check_occs not only finds conflicts between this item
+ -- and others, but also internally within this item. That is,
+ -- if 'M.x' is in scope in several ways, we'll have several
+ -- members of mod_avails with the same OccName.
+ check_occs ie occs (nameSetToList new_exports) `thenM` \ occs' ->
+ returnM (mod:mods, occs', exports `unionNameSets` new_exports)
+
+ exports_from_item acc@(mods, occs, exports) ie
+ = lookupGlobalOccRn (ieName ie) `thenM` \ name ->
+ if isUnboundName name then
+ returnM acc -- Avoid error cascade
+ else let
+ new_exports = filterAvail ie name sub_env
+ in
+ checkErr (not (null new_exports)) (exportItemErr ie) `thenM_`
+ checkForDodgyExport ie new_exports `thenM_`
+ check_occs ie occs new_exports `thenM` \ occs' ->
+ returnM (mods, occs', addListToNameSet exports new_exports)
+
+-------------------------------
+filterAvail :: IE RdrName -- Wanted
+ -> Name -- The Name of the ieName of the item
+ -> NameEnv [Name] -- Maps type/class names to their sub-names
+ -> [Name] -- Empty if even one thing reqd is missing
+
+filterAvail (IEVar _) n subs = [n]
+filterAvail (IEThingAbs _) n subs = [n]
+filterAvail (IEThingAll _) n subs = n : subNames subs n
+filterAvail (IEThingWith _ rdrs) n subs
+ | any isNothing mb_names = []
+ | otherwise = n : catMaybes mb_names
+ where
+ env = mkOccEnv [(nameOccName s, s) | s <- subNames subs n]
+ mb_names = map (lookupOccEnv env . rdrNameOcc) rdrs
+
+subNames :: NameEnv [Name] -> Name -> [Name]
+subNames env n = lookupNameEnv env n `orElse` []
+
+mkSubNameEnv :: NameSet -> NameEnv [Name]
+-- Maps types and classes to their constructors/classops respectively
+-- This mapping just makes it easier to deal with A(..) export items
+mkSubNameEnv names
+ = foldNameSet add_name emptyNameEnv names
+ where
+ add_name name env
+ | Just parent <- nameParent_maybe name
+ = extendNameEnv_C (\ns _ -> name:ns) env parent [name]
+ | otherwise = env
+
+-------------------------------
+inScopeUnqual :: GlobalRdrEnv -> Name -> Bool
+-- Checks whether the Name is in scope unqualified,
+-- regardless of whether it's ambiguous or not
+inScopeUnqual env n = any unQualOK (lookupGRE_Name env n)
+
+-------------------------------
+checkForDodgyExport :: IE RdrName -> [Name] -> RnM ()
+checkForDodgyExport ie@(IEThingAll tc) [n]
+ | isTcOcc (nameOccName n) = addWarn (dodgyExportWarn tc)
+ -- This occurs when you export T(..), but
+ -- only import T abstractly, or T is a synonym.
+ -- The single [n] is the type or class itself
+ | otherwise = addErr (exportItemErr ie)
+ -- This happes if you export x(..), which is bogus
+checkForDodgyExport _ _ = return ()
+
+-------------------------------
+check_occs :: IE RdrName -> ExportOccMap -> [Name] -> RnM ExportOccMap
+check_occs ie occs names
+ = foldlM check occs names