Check that exported modules were actually imported; fixes #1384
[ghc-hetmet.git] / compiler / typecheck / TcRnTypes.lhs
index dc23308..d11ee27 100644 (file)
@@ -13,7 +13,7 @@ module TcRnTypes(
        IfGblEnv(..), IfLclEnv(..), 
 
        -- Ranamer types
-       ErrCtxt,
+       ErrCtxt, RecFieldEnv,
        ImportAvails(..), emptyImportAvails, plusImportAvails, 
        WhereFrom(..), mkModDeps,
 
@@ -30,7 +30,7 @@ module TcRnTypes(
        -- Insts
        Inst(..), InstOrigin(..), InstLoc(..), 
        pprInstLoc, pprInstArising, instLocSpan, instLocOrigin,
-       LIE, emptyLIE, unitLIE, plusLIE, consLIE, 
+       LIE, emptyLIE, unitLIE, plusLIE, consLIE, instLoc, instSpan,
        plusLIEs, mkLIE, isEmptyLIE, lieToList, listToLIE,
 
        -- Misc other types
@@ -133,7 +133,8 @@ data TcGblEnv
        tcg_default :: Maybe [Type],    -- Types used for defaulting
                                        -- Nothing => no 'default' decl
 
-       tcg_fix_env  :: FixityEnv,      -- Just for things in this module
+       tcg_fix_env   :: FixityEnv,     -- Just for things in this module
+       tcg_field_env :: RecFieldEnv,   -- Just for things in this module
 
        tcg_type_env :: TypeEnv,        -- Global type env for the module we are compiling now
                -- All TyCons and Classes (for this module) end up in here right away,
@@ -225,8 +226,17 @@ data TcGblEnv
        tcg_fords     :: [LForeignDecl Id], -- ...Foreign import & exports
 
        tcg_doc :: Maybe (HsDoc Name), -- Maybe Haddock documentation
-        tcg_hmi :: HaddockModInfo Name -- Haddock module information
+        tcg_hmi :: HaddockModInfo Name, -- Haddock module information
+        tcg_hpc :: AnyHpcUsage -- True if any part of the prog uses hpc instrumentation.
     }
+
+type RecFieldEnv = NameEnv [Name]      -- Maps a constructor name *in this module*
+                                       -- to the fields for that constructor
+       -- This is used when dealing with ".." notation in record 
+       -- construction and pattern matching.
+       -- The FieldEnv deals *only* with constructors defined in
+       -- *thie* module.  For imported modules, we get the same info
+       -- from the TypeEnv
 \end{code}
 
 %************************************************************************
@@ -306,11 +316,15 @@ data TcLclEnv             -- Changes as we move inside an expression
                -- Maintained during renaming, of course, but also during
                -- type checking, solely so that when renaming a Template-Haskell
                -- splice we have the right environment for the renamer.
+               --
+               -- Used only for names bound within a value binding (bound by
+               -- lambda, case, where, let etc), but *not* for top-level names.
+               -- 
+               -- Does *not* include global name envt; may shadow it
+               -- Includes both ordinary variables and type variables;
+               -- they are kept distinct because tyvar have a different
+               -- occurrence contructor (Name.TvOcc)
                -- 
-               --   Does *not* include global name envt; may shadow it
-               --   Includes both ordinary variables and type variables;
-               --   they are kept distinct because tyvar have a different
-               --   occurrence contructor (Name.TvOcc)
                -- We still need the unsullied global name env so that
                --   we can look up record field names
 
@@ -477,8 +491,11 @@ It is used         * when processing the export list
 \begin{code}
 data ImportAvails 
    = ImportAvails {
-       imp_mods :: ModuleEnv (Module, Bool, SrcSpan),
+       imp_mods :: ModuleEnv (Module, [(ModuleName, Bool, SrcSpan)]),
                -- Domain is all directly-imported modules
+        -- The ModuleName is what the module was imported as, e.g. in
+        --     import Foo as Bar
+        -- it is Bar.
                -- Bool means:
                --   True => import was "import Foo ()"
                --   False  => import was some other form
@@ -541,12 +558,13 @@ plusImportAvails
   (ImportAvails { imp_mods = mods2,
                  imp_dep_mods = dmods2, imp_dep_pkgs = dpkgs2,
                   imp_orphs = orphs2, imp_finsts = finsts2 })
-  = ImportAvails { imp_mods     = mods1  `plusModuleEnv` mods2,        
+  = ImportAvails { imp_mods     = plusModuleEnv_C plus_mod mods1 mods2,        
                   imp_dep_mods = plusUFM_C plus_mod_dep dmods1 dmods2, 
                   imp_dep_pkgs = dpkgs1 `unionLists` dpkgs2,
                   imp_orphs    = orphs1 `unionLists` orphs2,
                   imp_finsts   = finsts1 `unionLists` finsts2 }
   where
+    plus_mod (m1, xs1) (_, xs2) = (m1, xs1 ++ xs2)
     plus_mod_dep (m1, boot1) (m2, boot2) 
        = WARN( not (m1 == m2), (ppr m1 <+> ppr m2) $$ (ppr boot1 <+> ppr boot2) )
                -- Check mod-names match
@@ -598,14 +616,20 @@ data Inst
   | ImplicInst {       -- An implication constraint
                        -- forall tvs. (reft, given) => wanted
        tci_name   :: Name,
+       tci_tyvars :: [TcTyVar],    -- Quantified type variables
+                                   -- Includes coercion variables
+                                   --   mentioned in tci_reft
        tci_reft   :: Refinement,
-       tci_tyvars :: [TcTyVar],
        tci_given  :: [Inst],       -- Only Dicts
                                    --   (no Methods, LitInsts, ImplicInsts)
        tci_wanted :: [Inst],       -- Only Dicts and ImplicInsts
                                    --   (no Methods or LitInsts)
+
        tci_loc    :: InstLoc
     }
+       -- NB: the tci_given are not necessarily rigid,
+       --     although they will be if the tci_reft is non-trivial
+       -- NB: the tci_reft is already applied to tci_given and tci_wanted
 
   | Method {
        tci_id :: TcId,         -- The Id for the Inst
@@ -701,10 +725,20 @@ emptyLIE          = emptyBag
 unitLIE inst     = unitBag inst
 mkLIE insts      = listToBag insts
 plusLIE lie1 lie2 = lie1 `unionBags` lie2
-consLIE inst lie  = inst `consBag` lie
 plusLIEs lies    = unionManyBags lies
 lieToList        = bagToList
 listToLIE        = listToBag
+
+consLIE inst lie  = lie `snocBag` inst
+-- Putting the new Inst at the *end* of the bag is a half-hearted attempt
+-- to ensure that we tend to report the *leftmost* type-constraint error
+-- E.g.        f :: [a]
+--             f = [1,2,3]
+-- we'd like to complain about the '1', not the '3'.
+--
+-- "Half-hearted" because the rest of the type checker makes no great
+-- claims for retaining order in the constraint set.  Still, this 
+-- seems to improve matters slightly.  Exampes: mdofail001, tcfail015
 \end{code}
 
 
@@ -725,6 +759,12 @@ functions that deal with it.
 -------------------------------------------
 data InstLoc = InstLoc InstOrigin SrcSpan ErrCtxt
 
+instLoc :: Inst -> InstLoc
+instLoc inst = tci_loc inst
+
+instSpan :: Inst -> SrcSpan
+instSpan wanted = instLocSpan (instLoc wanted)
+
 instLocSpan :: InstLoc -> SrcSpan
 instLocSpan (InstLoc _ s _) = s