lookupFixityRn,
- getTyClDeclBinders,
- removeContext -- removeContext probably belongs somewhere else
+ getTyClDeclBinders
) where
#include "HsVersions.h"
+import DriverState ( GhcMode(..), v_GhcMode )
import DriverUtil ( splitFilename )
import CmdLineOpts ( opt_IgnoreIfacePragmas )
import HscTypes ( ModuleLocation(..),
AvailInfo, GenAvailInfo(..), Avails, Deprecations(..)
)
import HsSyn ( TyClDecl(..), InstDecl(..),
- HsType(..), FixitySig(..), RuleDecl(..),
- tyClDeclNames, tyClDeclSysNames
- )
-import RdrHsSyn ( RdrNameTyClDecl, RdrNameInstDecl, RdrNameRuleDecl,
- extractHsTyRdrNames
+ HsType(..), HsPred(..), FixitySig(..), RuleDecl(..),
+ tyClDeclNames, tyClDeclSysNames, hsTyVarNames, getHsInstHead,
)
+import RdrHsSyn ( RdrNameTyClDecl, RdrNameInstDecl, RdrNameRuleDecl )
+import RnHsSyn ( extractHsTyNames_s )
import BasicTypes ( Version, defaultFixity )
+import RnTypes ( rnHsType )
import RnEnv
import RnMonad
import ParseIface ( parseIface )
nameModule, isLocalName, nameIsLocalOrFrom
)
import NameEnv
-import Module ( Module,
- moduleName, isHomeModule,
- ModuleName, WhereFrom(..),
- extendModuleEnv, mkVanillaModule
- )
+import NameSet
+import Module
import RdrName ( rdrNameOcc )
import SrcLoc ( mkSrcLoc )
import Maybes ( maybeToBool, orElse )
import StringBuffer ( hGetStringBuffer )
import FastString ( mkFastString )
import ErrUtils ( Message )
-import Finder ( findModule )
+import Finder ( findModule, findPackageModule )
import Lex
import FiniteMap
+import ListSetOps ( minusList )
import Outputable
import Bag
import Config
+import IOExts
import Directory
\end{code}
ImportByUser -> not (mi_boot iface)
ImportByUserSource -> mi_boot iface
ImportBySystem -> True
- ImportByCmdLine -> True
-> returnRn (iface, Nothing) ; -- Already loaded
-- The not (mi_boot iface) test checks that the already-loaded
-- interface isn't a boot iface. This can conceivably happen,
= case (from, mod_info) of
(ImportByUser, _) -> False -- Not hi-boot
(ImportByUserSource, _) -> True -- hi-boot
- (ImportByCmdLine, _) -> False
(ImportBySystem, Just (_, is_boot)) -> is_boot
(ImportBySystem, Nothing) -> False
-- We're importing a module we know absolutely
= case (from, mod_info) of
(ImportByUserSource, Just (_,False)) -> True
other -> False
-
- home_allowed | ImportByCmdLine <- from = True
- | otherwise = False
in
-- Issue a warning for a redundant {- SOURCE -} import
(warnSelfImport this_mod) `thenRn_`
-- READ THE MODULE IN
- findAndReadIface doc_str mod_name hi_boot_file home_allowed
+ findAndReadIface doc_str mod_name hi_boot_file
`thenRn` \ read_result ->
case read_result of {
Left err -> -- Not found, so add an empty export env to the Ifaces map
new_version_map = extendNameEnv version_map main_name version
in
+ traceRn (text "Loading" <+> ppr full_avail) `thenRn_`
returnRn (new_version_map, new_decls_map)
-----------------------------------------------------
-- instance Foo a => Baz (T a) where ...
--
-- Here the gates are Baz and T, but *not* Foo.
+ --
+ -- HOWEVER: functional dependencies make things more complicated
+ -- class C a b | a->b where ...
+ -- instance C Foo Baz where ...
+ -- Here, the gates are really only C and Foo, *not* Baz.
+ -- That is, if C and Foo are visible, even if Baz isn't, we must
+ -- slurp the decl.
+ --
+ -- Rather than take fundeps into account "properly", we just slurp
+ -- if C is visible and *any one* of the Names in the types
+ -- This is a slightly brutal approximation, but most instance decls
+ -- are regular H98 ones and it's perfect for them.
+ --
+ -- NOTICE that we rename the type before extracting its free
+ -- variables. The free-variable finder for a renamed HsType
+ -- does the Right Thing for built-in syntax like [] and (,).
+ initIfaceRnMS mod (
+ rnHsType (text "In an interface instance decl") inst_ty
+ ) `thenRn` \ inst_ty' ->
let
- munged_inst_ty = removeContext inst_ty
- free_names = extractHsTyRdrNames munged_inst_ty
- in
- mapRn lookupIfaceName free_names `thenRn` \ gate_names ->
- returnRn ((gate_names, (mod, decl)) `consBag` insts)
-
+ (tvs,(cls,tys)) = getHsInstHead inst_ty'
+ free_tcs = nameSetToList (extractHsTyNames_s tys) `minusList` hsTyVarNames tvs
--- In interface files, the instance decls now look like
--- forall a. Foo a -> Baz (T a)
--- so we have to strip off function argument types as well
--- as the bit before the '=>' (which is always empty in interface files)
-removeContext (HsForAllTy tvs cxt ty) = HsForAllTy tvs [] (removeFuns ty)
-removeContext ty = removeFuns ty
+ gate_fn vis_fn = vis_fn cls && (null free_tcs || any vis_fn free_tcs)
+ -- Here is the implementation of HOWEVER above
+ -- (Note that we do let the inst decl in if it mentions
+ -- no tycons at all. Hence the null free_ty_names.)
+ in
+ returnRn ((gate_fn, (mod, decl)) `consBag` insts)
-removeFuns (HsFunTy _ ty) = removeFuns ty
-removeFuns ty = ty
-----------------------------------------------------
loadRule :: Module -> RdrNameRuleDecl -> RnM d (GatedDecl RdrNameRuleDecl)
-- "Gate" the rule simply by whether the rule variable is
-- needed. We can refine this later.
-loadRule mod decl@(IfaceRule _ _ var _ _ src_loc)
+loadRule mod decl@(IfaceRule _ _ _ var _ _ src_loc)
= lookupIfaceName var `thenRn` \ var_name ->
- returnRn ([var_name], (mod, decl))
+ returnRn (\vis_fn -> vis_fn var_name, (mod, decl))
-----------------------------------------------------
findAndReadIface :: SDoc -> ModuleName
-> IsBootInterface -- True <=> Look for a .hi-boot file
-- False <=> Look for .hi file
- -> Bool -- True <=> can read home interface
-> RnM d (Either Message (Module, ParsedIface))
-- Nothing <=> file not found, or unreadable, or illegible
-- Just x <=> successfully found and parsed
-findAndReadIface doc_str mod_name hi_boot_file home_allowed
+findAndReadIface doc_str mod_name hi_boot_file
= traceRn trace_msg `thenRn_`
- ioToRnM (findModule mod_name) `thenRn` \ maybe_found ->
+ -- In interactive or --make mode, we are *not allowed* to demand-load
+ -- a home package .hi file. So don't even look for them.
+ -- This helps in the case where you are sitting in eg. ghc/lib/std
+ -- and start up GHCi - it won't complain that all the modules it tries
+ -- to load are found in the home location.
+ ioToRnM_no_fail (readIORef v_GhcMode) `thenRn` \ mode ->
+ let home_allowed = hi_boot_file ||
+ mode `notElem` [ DoInteractive, DoMake ]
+ in
+
+ ioToRnM (if home_allowed
+ then findModule mod_name
+ else findPackageModule mod_name) `thenRn` \ maybe_found ->
+
case maybe_found of
Right (Just (wanted_mod,locn))
- -> -- in CmdLineMode, we cannot demand-load home interfaces
- -- because the corresponding code won't be loaded, so we
- -- check for this here and emit an error message.
- if (home_allowed && isHomeModule wanted_mod)
- then returnRn (Left (notLoaded wanted_mod))
- else
-
- mkHiPath hi_boot_file locn `thenRn` \ file ->
+ -> mkHiPath hi_boot_file locn `thenRn` \ file ->
readIface file `thenRn` \ read_result ->
case read_result of
Left bad -> returnRn (Left bad)
Right iface
-> let read_mod = pi_mod iface
- in warnCheckRn (wanted_mod == read_mod)
- (hiModuleNameMismatchWarn wanted_mod
- read_mod) `thenRn_`
+ in -- check that the module names agree
+ checkRn
+ (wanted_mod == read_mod)
+ (hiModuleNameMismatchWarn wanted_mod read_mod)
+ `thenRn_`
+ -- check that the package names agree
+ warnCheckRn
+ (modulePackage wanted_mod == modulePackage read_mod)
+ (packageNameMismatchWarn wanted_mod read_mod)
+ `thenRn_`
returnRn (Right (wanted_mod, iface))
-- Can't find it
other -> traceRn (ptext SLIT("...not found")) `thenRn_`
if b then returnRn hi_boot_ver_path
else returnRn hi_boot_path
| otherwise = returnRn hi_path
- where (Just hi_path) = ml_hi_file locn
+ where hi_path = ml_hi_file locn
(hi_base, _hi_suf) = splitFilename hi_path
hi_boot_path = hi_base ++ ".hi-boot"
hi_boot_ver_path = hi_base ++ ".hi-boot-" ++ cHscIfaceFileVersion
bale_out err = returnRn (Left (badIfaceFile file_path err))
\end{code}
-
%*********************************************************
%* *
\subsection{Looking up fixities}
%* *
%*********************************************************
-@lookupFixityRn@ has to be in RnIfaces (or RnHiFiles) because
-it calls @loadHomeInterface@.
+@lookupFixityRn@ has to be in RnIfaces (or RnHiFiles), instead of
+its obvious home in RnEnv, because it calls @loadHomeInterface@.
lookupFixity is a bit strange.
returnRn (lookupLocalFixity local_fix_env name)
else -- It's imported
- -- For imported names, we have to get their fixities by doing a loadHomeInterface,
- -- and consulting the Ifaces that comes back from that, because the interface
- -- file for the Name might not have been loaded yet. Why not? Suppose you import module A,
- -- which exports a function 'f', which is defined in module B. Then B isn't loaded
- -- right away (after all, it's possible that nothing from B will be used).
- -- When we come across a use of 'f', we need to know its fixity, and it's then,
- -- and only then, that we load B.hi. That is what's happening here.
+ -- For imported names, we have to get their fixities by doing a
+ -- loadHomeInterface, and consulting the Ifaces that comes back
+ -- from that, because the interface file for the Name might not
+ -- have been loaded yet. Why not? Suppose you import module A,
+ -- which exports a function 'f', which is defined in module B.
+ -- Then B isn't loaded right away (after all, it's possible that
+ -- nothing from B will be used). When we come across a use of
+ -- 'f', we need to know its fixity, and it's then, and only
+ -- then, that we load B.hi. That is what's happening here.
loadHomeInterface doc name `thenRn` \ iface ->
returnRn (lookupNameEnv (mi_fixities iface) name `orElse` defaultFixity)
where
, ppr read_mod
]
+packageNameMismatchWarn :: Module -> Module -> Message
+packageNameMismatchWarn requested_mod read_mod =
+ fsep [ ptext SLIT("Module"), quotes (ppr requested_mod),
+ ptext SLIT("is located in package"),
+ quotes (ptext (modulePackage requested_mod)),
+ ptext SLIT("but its interface file claims it is part of package"),
+ quotes (ptext (modulePackage read_mod))
+ ]
+
warnRedundantSourceImport mod_name
= ptext SLIT("Unnecessary {- SOURCE -} in the import of module")
<+> quotes (ppr mod_name)
-notLoaded mod
- = ptext SLIT("Module") <+> quotes (ppr mod) <+> ptext SLIT("is not loaded")
-
warnSelfImport mod
= ptext SLIT("Importing my own interface: module") <+> ppr mod
\end{code}
-