Allow type families to use GADT syntax (and be GADTs)
[ghc-hetmet.git] / compiler / rename / RnEnv.lhs
index ae1966c..c6468b4 100644 (file)
@@ -11,7 +11,7 @@ module RnEnv (
        lookupLocatedOccRn, lookupOccRn, 
        lookupLocatedGlobalOccRn, lookupGlobalOccRn,
        lookupLocalDataTcNames, lookupSrcOcc_maybe,
-       lookupFixityRn, lookupTyFixityRn, lookupLocatedSigOccRn, 
+       lookupFixityRn, lookupTyFixityRn, 
        lookupInstDeclBndr, lookupRecordBndr, lookupConstructorFields,
        lookupSyntaxName, lookupSyntaxTable, lookupImportedName,
        lookupGreRn, lookupGreLocalRn, lookupGreRn_maybe,
@@ -30,7 +30,7 @@ module RnEnv (
        mapFvRn, mapFvRnCPS,
        warnUnusedMatches, warnUnusedModules, warnUnusedImports, 
        warnUnusedTopBinds, warnUnusedLocalBinds,
-       dataTcOccs, unknownNameErr
+       dataTcOccs, unknownNameErr, perhapsForallMsg
     ) where
 
 #include "HsVersions.h"
@@ -41,7 +41,7 @@ import HsSyn
 import RdrHsSyn                ( extractHsTyRdrTyVars )
 import RdrName
 import HscTypes                ( availNames, ModIface(..), FixItem(..), lookupFixity)
-import TcEnv           ( tcLookupDataCon )
+import TcEnv           ( tcLookupDataCon, isBrackStage )
 import TcRnMonad
 import Name            ( Name, nameIsLocalOrFrom, mkInternalName, isWiredInName,
                          nameSrcLoc, nameSrcSpan, nameOccName, nameModule, isExternalName )
@@ -140,7 +140,16 @@ newTopSrcBinder this_mod (L loc rdr_name)
                 (addErrAt loc (badQualBndrErr rdr_name))
                -- Binders should not be qualified; if they are, and with a different
                -- module name, we we get a confusing "M.T is not in scope" error later
-       ; newGlobalBinder this_mod (rdrNameOcc rdr_name) loc }
+
+       ; stage <- getStage
+       ; if isBrackStage stage then
+               -- We are inside a TH bracket, so make an *Internal* name
+               -- See Note [Top-level Names in Template Haskell decl quotes] in RnNames
+            do { uniq <- newUnique
+               ; return (mkInternalName uniq (rdrNameOcc rdr_name) loc) } 
+         else  
+               -- Normal case
+            newGlobalBinder this_mod (rdrNameOcc rdr_name) loc }
 \end{code}
 
 %*********************************************************
@@ -215,35 +224,6 @@ lookupTopBndrRn_maybe rdr_name
                Nothing  -> returnM Nothing
                Just gre -> returnM (Just $ gre_name gre) }
              
--- lookupLocatedSigOccRn is used for type signatures and pragmas
--- Is this valid?
---   module A
---     import M( f )
---     f :: Int -> Int
---     f x = x
--- It's clear that the 'f' in the signature must refer to A.f
--- The Haskell98 report does not stipulate this, but it will!
--- So we must treat the 'f' in the signature in the same way
--- as the binding occurrence of 'f', using lookupBndrRn
---
--- However, consider this case:
---     import M( f )
---     f :: Int -> Int
---     g x = x
--- We don't want to say 'f' is out of scope; instead, we want to
--- return the imported 'f', so that later on the reanamer will
--- correctly report "misplaced type sig".
-lookupLocatedSigOccRn :: Located RdrName -> RnM (Located Name)
-lookupLocatedSigOccRn = wrapLocM $ \ rdr_name -> do
-       { local_env <- getLocalRdrEnv
-       ; case lookupLocalRdrEnv local_env rdr_name of {
-               Just n  -> return n ;
-               Nothing -> do
-       { mb_gre <- lookupGreLocalRn rdr_name
-       ; case mb_gre of 
-               Just gre -> return (gre_name gre) 
-               Nothing  -> lookupGlobalOccRn rdr_name
-       }}}
 
 -----------------------------------------------
 lookupInstDeclBndr :: Name -> Located RdrName -> RnM (Located Name)
@@ -351,7 +331,7 @@ lookupFamInstDeclBndr :: Module -> Located RdrName -> RnM Name
 lookupFamInstDeclBndr mod lrdr_name@(L _ rdr_name)
   = do { mb_gre <- lookupGreRn_maybe rdr_name
        ; case mb_gre of
-           Just gre -> returnM (gre_name gre) ;
+           Just gre -> returnM (gre_name gre)
           Nothing  -> newTopSrcBinder mod lrdr_name }
 
 --------------------------------------------------
@@ -388,23 +368,27 @@ lookupGlobalOccRn rdr_name
   = lookupImportedName rdr_name        
 
   | otherwise
-  =    -- First look up the name in the normal environment.
-   lookupGreRn_maybe rdr_name          `thenM` \ mb_gre ->
+  = do
+       -- First look up the name in the normal environment.
+   mb_gre <- lookupGreRn_maybe rdr_name
    case mb_gre of {
        Just gre -> returnM (gre_name gre) ;
-       Nothing   -> 
+       Nothing   -> do
 
        -- We allow qualified names on the command line to refer to 
        --  *any* name exported by any module in scope, just as if 
        -- there was an "import qualified M" declaration for every 
        -- module.
-   getModule           `thenM` \ mod ->
-   if isQual rdr_name && mod == iNTERACTIVE then       
-                                       -- This test is not expensive,
-       lookupQualifiedName rdr_name    -- and only happens for failed lookups
-   else        do
+   allow_qual <- doptM Opt_ImplicitImportQualified
+   mod <- getModule
+               -- This test is not expensive,
+               -- and only happens for failed lookups
+   if isQual rdr_name && allow_qual && mod == iNTERACTIVE
+      then lookupQualifiedName rdr_name
+      else do 
         traceRn $ text "lookupGlobalOccRn"
-       unboundName rdr_name }
+        unboundName rdr_name
+  }
 
 lookupImportedName :: RdrName -> TcRnIf m n Name
 -- Lookup the occurrence of an imported name
@@ -495,7 +479,7 @@ lookupQualifiedName rdr_name
   | Just (mod,occ) <- isQual_maybe rdr_name
    -- Note: we want to behave as we would for a source file import here,
    -- and respect hiddenness of modules/packages, hence loadSrcInterface.
-   = loadSrcInterface doc mod False    `thenM` \ iface ->
+   = loadSrcInterface doc mod False Nothing    `thenM` \ iface ->
 
    case  [ (mod,occ) | 
           (mod,avails) <- mi_exports iface,
@@ -893,11 +877,9 @@ checkShadowedNames doc_str (global_env,local_env) loc_rdr_names
 \begin{code}
 -- A useful utility
 mapFvRn :: (a -> RnM (b, FreeVars)) -> [a] -> RnM ([b], FreeVars)
-mapFvRn f xs = mappM f xs      `thenM` \ stuff ->
-              let
-                 (ys, fvs_s) = unzip stuff
-              in
-              returnM (ys, plusFVs fvs_s)
+mapFvRn f xs = do stuff <- mappM f xs
+                  case unzip stuff of
+                      (ys, fvs_s) -> returnM (ys, plusFVs fvs_s)
 
 -- because some of the rename functions are CPSed:
 -- maps the function across the list from left to right; 
@@ -1011,9 +993,13 @@ unknownNameErr rdr_name
                          <+> quotes (ppr rdr_name))
         , extra ]
   where
-    extra | rdr_name == forall_tv_RDR 
-         = ptext (sLit "Perhaps you intended to use -XRankNTypes or similar flag")
-         | otherwise = empty
+    extra | rdr_name == forall_tv_RDR = perhapsForallMsg
+         | otherwise                 = empty
+
+perhapsForallMsg :: SDoc
+perhapsForallMsg 
+  = vcat [ ptext (sLit "Perhaps you intended to use -XRankNTypes or similar flag")
+        , ptext (sLit "to enable explicit-forall syntax: forall <tvs>. <type>")]
 
 unknownSubordinateErr :: SDoc -> RdrName -> SDoc
 unknownSubordinateErr doc op   -- Doc is "method of class" or