[project @ 1997-06-18 23:52:36 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / RnSource.lhs
index 15acf55..ff3620e 100644 (file)
@@ -6,10 +6,16 @@
 \begin{code}
 #include "HsVersions.h"
 
-module RnSource ( rnDecl, rnHsType ) where
+module RnSource ( rnDecl, rnHsType, rnHsSigType ) where
 
+IMPORT_1_3(List(partition))
 IMP_Ubiq()
+
+#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
 IMPORT_DELOOPER(RnLoop)                -- *check* the RnPass/RnExpr/RnBinds loop-breaking
+#else
+import {-# SOURCE #-} RnExpr
+#endif
 
 import HsSyn
 import HsDecls         ( HsIdInfo(..) )
@@ -18,14 +24,17 @@ import HsTypes              ( getTyVarName )
 import RdrHsSyn
 import RnHsSyn
 import HsCore
+import CmdLineOpts     ( opt_IgnoreIfacePragmas )
 
 import RnBinds         ( rnTopBinds, rnMethodBinds )
-import RnEnv           ( bindTyVarsRn, lookupRn, lookupOccRn, lookupImplicitOccRn, bindLocalsRn,
-                         lookupOptionalOccRn, newDfunName, 
+import RnEnv           ( bindTyVarsRn, lookupBndrRn, lookupOccRn, lookupImplicitOccRn, bindLocalsRn,
+                         newSysName, newDfunName, checkDupOrQualNames, checkDupNames,
                          listType_RDR, tupleType_RDR )
 import RnMonad
 
-import Name            ( Name, isLocallyDefined, isTvOcc, pprNonSym,
+import Name            ( Name, isLocallyDefined, 
+                         OccName(..), occNameString, prefixOccName,
+                         ExportFlag(..),
                          Provenance,
                          SYN_IE(NameSet), unionNameSets, emptyNameSet, mkNameSet, unitNameSet,
                          elemNameSet
@@ -35,22 +44,21 @@ import FiniteMap    ( emptyFM, lookupFM, addListToFM_C )
 import Id              ( GenId{-instance NamedThing-} )
 import IdInfo          ( IdInfo, StrictnessInfo(..), FBTypeInfo, DemandInfo, ArgUsageInfo )
 import SpecEnv         ( SpecEnv )
+import Lex             ( isLexCon )
 import CoreUnfold      ( Unfolding(..), SimpleUnfolding )
 import MagicUFs                ( MagicUnfoldingFun )
-import PrelInfo                ( derivingOccurrences, evalClass_RDR, numClass_RDR )
+import PrelInfo                ( derivingOccurrences, evalClass_RDR, numClass_RDR, allClass_NAME )
 import ListSetOps      ( unionLists, minusList )
 import Maybes          ( maybeToBool, catMaybes )
 import Bag             ( emptyBag, unitBag, consBag, unionManyBags, unionBags, listToBag, bagToList )
-import Outputable      ( Outputable(..){-instances-} )
---import PprStyle      -- ToDo:rm 
+import Outputable      ( PprStyle(..), Outputable(..){-instances-} )
 import Pretty
 import SrcLoc          ( SrcLoc )
--- import TyCon                ( TyCon{-instance NamedThing-} )
 import Unique          ( Unique )
 import UniqSet         ( SYN_IE(UniqSet) )
 import UniqFM          ( UniqFM, lookupUFM )
-import Util            ( isIn, isn'tIn, thenCmp, removeDups, cmpPString,
-                         panic, assertPanic{- , pprTrace ToDo:rm-} )
+import Util
+IMPORT_1_3(List(nub))
 \end{code}
 
 rnDecl `renames' declarations.
@@ -82,8 +90,12 @@ rnDecl (ValD binds) = rnTopBinds binds       `thenRn` \ new_binds ->
 
 rnDecl (SigD (IfaceSig name ty id_infos loc))
   = pushSrcLocRn loc $
-    lookupRn name              `thenRn` \ name' ->
+    lookupBndrRn name          `thenRn` \ name' ->
     rnHsType ty                        `thenRn` \ ty' ->
+       -- Get the pragma info (if any).
+    setModeRn (InterfaceMode Optional) $
+       -- In all the rest of the signature we read in optional mode,
+       -- so that (a) we don't die
     mapRn rnIdInfo id_infos    `thenRn` \ id_infos' -> 
     returnRn (SigD (IfaceSig name' ty' id_infos' loc))
 \end{code}
@@ -107,32 +119,28 @@ it again to rename the tyvars! However, we can also do some scoping
 checks at the same time.
 
 \begin{code}
-rnDecl (TyD (TyData context tycon tyvars condecls derivings pragmas src_loc))
+rnDecl (TyD (TyData new_or_data context tycon tyvars condecls derivings pragmas src_loc))
   = pushSrcLocRn src_loc $
-    lookupRn tycon                             `thenRn` \ tycon' ->
-    bindTyVarsRn "data declaration" tyvars     $ \ tyvars' ->
-    rnContext context                          `thenRn` \ context' ->
-    mapRn rnConDecl condecls                   `thenRn` \ condecls' ->
-    rnDerivs derivings                         `thenRn` \ derivings' ->
+    lookupBndrRn tycon                                 `thenRn` \ tycon' ->
+    bindTyVarsRn data_doc tyvars                       $ \ tyvars' ->
+    rnContext context                                  `thenRn` \ context' ->
+    checkDupOrQualNames data_doc con_names             `thenRn_`
+    mapRn rnConDecl condecls                           `thenRn` \ condecls' ->
+    rnDerivs derivings                                 `thenRn` \ derivings' ->
     ASSERT(isNoDataPragmas pragmas)
-    returnRn (TyD (TyData context' tycon' tyvars' condecls' derivings' noDataPragmas src_loc))
-
-rnDecl (TyD (TyNew context tycon tyvars condecl derivings pragmas src_loc))
-  = pushSrcLocRn src_loc $
-    lookupRn tycon                             `thenRn` \ tycon' ->
-    bindTyVarsRn "newtype declaration" tyvars  $ \ tyvars' ->
-    rnContext context                          `thenRn` \ context' ->
-    rnConDecl condecl                          `thenRn` \ condecl' ->
-    rnDerivs derivings                         `thenRn` \ derivings' ->
-    ASSERT(isNoDataPragmas pragmas)
-    returnRn (TyD (TyNew context' tycon' tyvars' condecl' derivings' noDataPragmas src_loc))
+    returnRn (TyD (TyData new_or_data context' tycon' tyvars' condecls' derivings' noDataPragmas src_loc))
+  where
+    data_doc sty = text "the data type declaration for" <+> ppr sty tycon
+    con_names = map conDeclName condecls
 
 rnDecl (TyD (TySynonym name tyvars ty src_loc))
   = pushSrcLocRn src_loc $
-    lookupRn name                              `thenRn` \ name' ->
-    bindTyVarsRn "type declaration" tyvars     $ \ tyvars' ->
+    lookupBndrRn name                          `thenRn` \ name' ->
+    bindTyVarsRn syn_doc tyvars                $ \ tyvars' ->
     rnHsType ty                                        `thenRn` \ ty' ->
     returnRn (TyD (TySynonym name' tyvars' ty' src_loc))
+  where
+    syn_doc sty = text "the declaration for type synonym" <+> ppr sty name
 \end{code}
 
 %*********************************************************
@@ -148,18 +156,50 @@ original names, reporting any unknown names.
 \begin{code}
 rnDecl (ClD (ClassDecl context cname tyvar sigs mbinds pragmas src_loc))
   = pushSrcLocRn src_loc $
-    bindTyVarsRn "class declaration" [tyvar]           $ \ [tyvar'] ->
-    rnContext context                                  `thenRn` \ context' ->
-    lookupRn cname                                     `thenRn` \ cname' ->
-    mapRn (rn_op cname' (getTyVarName tyvar')) sigs    `thenRn` \ sigs' ->
+    bindTyVarsRn cls_doc [tyvar]                       ( \ [tyvar'] ->
+       rnContext context                                       `thenRn` \ context' ->
+       lookupBndrRn cname                                      `thenRn` \ cname' ->
+
+            -- Check the signatures
+       checkDupOrQualNames sig_doc sig_names           `thenRn_` 
+       mapRn (rn_op cname' (getTyVarName tyvar')) sigs `thenRn` \ sigs' ->
+       returnRn (tyvar', context', cname', sigs')
+    )                                                  `thenRn` \ (tyvar', context', cname', sigs') ->
+
+       -- Check the methods
+    checkDupOrQualNames meth_doc meth_names            `thenRn_`
     rnMethodBinds mbinds                               `thenRn` \ mbinds' ->
+
+       -- Typechecker is responsible for checking that we only
+       -- give default-method bindings for things in this class.
+       -- The renamer *could* check this for class decls, but can't
+       -- for instance decls.
+
     ASSERT(isNoClassPragmas pragmas)
     returnRn (ClD (ClassDecl context' cname' tyvar' sigs' mbinds' NoClassPragmas src_loc))
   where
-    rn_op clas clas_tyvar sig@(ClassOpSig op ty pragmas locn)
+    cls_doc sty  = text "the declaration for class"    <+> ppr sty cname
+    sig_doc sty  = text "the signatures for class"     <+> ppr sty cname
+    meth_doc sty = text "the default-methods for class" <+> ppr sty cname
+
+    sig_names   = [(op,locn) | ClassOpSig op _ _ locn <- sigs]
+    meth_names   = bagToList (collectMonoBinders mbinds)
+
+    rn_op clas clas_tyvar sig@(ClassOpSig op _ ty locn)
       = pushSrcLocRn locn $
-       lookupRn op                     `thenRn` \ op_name ->
-       rnHsType ty                     `thenRn` \ new_ty  ->
+       lookupBndrRn op                         `thenRn` \ op_name ->
+       rnHsSigType (\sty -> ppr sty op) ty     `thenRn` \ new_ty  ->
+
+               -- Call up interface info for default method, if such info exists
+       let
+           dm_occ = prefixOccName SLIT("$m") (rdrNameOcc op)
+       in
+        newSysName dm_occ Exported locn                `thenRn` \ dm_name ->
+       setModeRn (InterfaceMode Optional) (
+            addOccurrenceName dm_name
+       )                                               `thenRn_`
+
+               -- Checks.....
        let
            (ctxt, op_ty) = case new_ty of
                                HsForAllTy tvs ctxt op_ty -> (ctxt, op_ty)
@@ -168,18 +208,12 @@ rnDecl (ClD (ClassDecl context cname tyvar sigs mbinds pragmas src_loc))
            op_ty_fvs = extractHsTyNames op_ty          -- Includes tycons/classes but we
                                                        -- don't care about that
        in
-       -- check that class tyvar appears in op_ty
+               -- Check that class tyvar appears in op_ty
         checkRn (clas_tyvar `elemNameSet` op_ty_fvs)
                (classTyVarNotInOpTyErr clas_tyvar sig)
                                                         `thenRn_`
 
-       -- check that class tyvar *doesn't* appear in the sig's context
-        checkRn (not (clas_tyvar `elemNameSet` ctxt_fvs))
-               (classTyVarInOpCtxtErr clas_tyvar sig)
-                                                        `thenRn_`
-
-       ASSERT(isNoClassOpPragmas pragmas)
-       returnRn (ClassOpSig op_name new_ty noClassOpPragmas locn)
+       returnRn (ClassOpSig op_name dm_name new_ty locn)
 \end{code}
 
 
@@ -190,42 +224,48 @@ rnDecl (ClD (ClassDecl context cname tyvar sigs mbinds pragmas src_loc))
 %*********************************************************
 
 \begin{code}
-rnDecl (InstD (InstDecl inst_ty mbinds uprags maybe_dfun_name src_loc))
+rnDecl (InstD (InstDecl inst_ty mbinds uprags maybe_dfun src_loc))
   = pushSrcLocRn src_loc $
-    rnHsType inst_ty                   `thenRn` \ inst_ty' ->
-    rnMethodBinds mbinds               `thenRn` \ mbinds' ->
-    mapRn rn_uprag uprags              `thenRn` \ new_uprags ->
-    rn_dfun maybe_dfun_name            `thenRn` \ dfun_name' ->
+    rnHsSigType (\sty -> text "an instance decl") inst_ty      `thenRn` \ inst_ty' ->
+
+
+       -- Rename the bindings
+       -- NB meth_names can be qualified!
+    checkDupNames meth_doc meth_names          `thenRn_`
+    rnMethodBinds mbinds                       `thenRn` \ mbinds' ->
+    mapRn rn_uprag uprags                      `thenRn` \ new_uprags ->
 
-    returnRn (InstD (InstDecl inst_ty' mbinds' new_uprags dfun_name' src_loc))
+    newDfunName maybe_dfun src_loc             `thenRn` \ dfun_name ->
+    addOccurrenceName dfun_name                        `thenRn_`
+                       -- The dfun is not optional, because we use its version number
+                       -- to identify the version of the instance declaration
+
+       -- The typechecker checks that all the bindings are for the right class.
+    returnRn (InstD (InstDecl inst_ty' mbinds' new_uprags (Just dfun_name) src_loc))
   where
-    rn_dfun Nothing  = newDfunName src_loc     `thenRn` \ n' ->
-                      returnRn (Just n')
-    rn_dfun (Just n) = lookupOccRn n           `thenRn` \ n' ->
-                               -- The dfun is not optional, because we use its version number
-                               -- to identify the version of the instance declaration
-                      returnRn (Just n')
+    meth_doc sty = text "the bindings in an instance declaration"
+    meth_names   = bagToList (collectMonoBinders mbinds)
 
     rn_uprag (SpecSig op ty using locn)
       = pushSrcLocRn src_loc $
-       lookupRn op                     `thenRn` \ op_name ->
-       rnHsType ty                     `thenRn` \ new_ty ->
-       rn_using using                  `thenRn` \ new_using ->
+       lookupBndrRn op                         `thenRn` \ op_name ->
+       rnHsSigType (\sty -> ppr sty op) ty     `thenRn` \ new_ty ->
+       rn_using using                          `thenRn` \ new_using ->
        returnRn (SpecSig op_name new_ty new_using locn)
 
     rn_uprag (InlineSig op locn)
       = pushSrcLocRn locn $
-       lookupRn op                     `thenRn` \ op_name ->
+       lookupBndrRn op                 `thenRn` \ op_name ->
        returnRn (InlineSig op_name locn)
 
     rn_uprag (DeforestSig op locn)
       = pushSrcLocRn locn $
-       lookupRn op                     `thenRn` \ op_name ->
+       lookupBndrRn op                 `thenRn` \ op_name ->
        returnRn (DeforestSig op_name locn)
 
     rn_uprag (MagicUnfoldingSig op str locn)
       = pushSrcLocRn locn $
-       lookupRn op                     `thenRn` \ op_name ->
+       lookupBndrRn op                 `thenRn` \ op_name ->
        returnRn (MagicUnfoldingSig op_name str locn)
 
     rn_using Nothing  = returnRn Nothing
@@ -280,35 +320,41 @@ rnDerivs (Just ds)
 \end{code}
 
 \begin{code}
-rnConDecl :: RdrNameConDecl -> RnMS s RenamedConDecl
+conDeclName :: RdrNameConDecl -> (RdrName, SrcLoc)
+conDeclName (ConDecl n _ _ l)     = (n,l)
 
-rnConDecl (ConDecl name tys src_loc)
-  = pushSrcLocRn src_loc $
-    lookupRn name              `thenRn` \ new_name ->
-    mapRn rnBangTy tys         `thenRn` \ new_tys  ->
-    returnRn (ConDecl new_name new_tys src_loc)
-
-rnConDecl (ConOpDecl ty1 op ty2 src_loc)
-  = pushSrcLocRn src_loc $
-    lookupRn op                        `thenRn` \ new_op  ->
-    rnBangTy ty1               `thenRn` \ new_ty1 ->
+rnConDecl :: RdrNameConDecl -> RnMS s RenamedConDecl
+rnConDecl (ConDecl name cxt details locn)
+  = pushSrcLocRn locn $
+    checkConName name                  `thenRn_` 
+    lookupBndrRn name                  `thenRn` \ new_name ->
+    rnConDetails name locn details     `thenRn` \ new_details -> 
+    rnContext cxt                      `thenRn` \ new_context ->
+    returnRn (ConDecl new_name new_context new_details locn)
+
+rnConDetails con locn (VanillaCon tys)
+  = mapRn rnBangTy tys         `thenRn` \ new_tys  ->
+    returnRn (VanillaCon new_tys)
+
+rnConDetails con locn (InfixCon ty1 ty2)
+  = rnBangTy ty1               `thenRn` \ new_ty1 ->
     rnBangTy ty2               `thenRn` \ new_ty2 ->
-    returnRn (ConOpDecl new_ty1 new_op new_ty2 src_loc)
+    returnRn (InfixCon new_ty1 new_ty2)
 
-rnConDecl (NewConDecl name ty src_loc)
-  = pushSrcLocRn src_loc $
-    lookupRn name              `thenRn` \ new_name ->
-    rnHsType ty                        `thenRn` \ new_ty  ->
-    returnRn (NewConDecl new_name new_ty src_loc)
+rnConDetails con locn (NewCon ty)
+  = rnHsType ty                        `thenRn` \ new_ty  ->
+    returnRn (NewCon new_ty)
 
-rnConDecl (RecConDecl name fields src_loc)
-  = pushSrcLocRn src_loc $
-    lookupRn name              `thenRn` \ new_name ->
-    mapRn rnField fields       `thenRn` \ new_fields ->
-    returnRn (RecConDecl new_name new_fields src_loc)
+rnConDetails con locn (RecCon fields)
+  = checkDupOrQualNames fld_doc field_names    `thenRn_`
+    mapRn rnField fields                       `thenRn` \ new_fields ->
+    returnRn (RecCon new_fields)
+  where
+    fld_doc sty = text "the fields of constructor" <> ppr sty con
+    field_names = [(fld, locn) | (flds, _) <- fields, fld <- flds]
 
 rnField (names, ty)
-  = mapRn lookupRn names       `thenRn` \ new_names ->
+  = mapRn lookupBndrRn names   `thenRn` \ new_names ->
     rnBangTy ty                        `thenRn` \ new_ty ->
     returnRn (new_names, new_ty) 
 
@@ -319,6 +365,20 @@ rnBangTy (Banged ty)
 rnBangTy (Unbanged ty)
   = rnHsType ty `thenRn` \ new_ty ->
     returnRn (Unbanged new_ty)
+
+-- This data decl will parse OK
+--     data T = a Int
+-- treating "a" as the constructor.
+-- It is really hard to make the parser spot this malformation.
+-- So the renamer has to check that the constructor is legal
+--
+-- We can get an operator as the constructor, even in the prefix form:
+--     data T = :% Int Int
+-- from interface files, which always print in prefix form
+
+checkConName name
+  = checkRn (isLexCon (occNameString (rdrNameOcc name)))
+           (badDataCon name)
 \end{code}
 
 
@@ -329,19 +389,56 @@ rnBangTy (Unbanged ty)
 %*********************************************************
 
 \begin{code}
-rnHsType :: RdrNameHsType -> RnMS s RenamedHsType
+rnHsSigType :: (PprStyle -> Doc) -> RdrNameHsType -> RnMS s RenamedHsType 
+       -- rnHsSigType is used for source-language type signatures,
+       -- which use *implicit* universal quantification.
+
+-- Given the signature  C => T  we universally quantify over FV(T) \ {in-scope-tyvars} 
+-- 
+-- We insist that the universally quantified type vars is a superset of FV(C)
+-- It follows that FV(T) is a superset of FV(C), so that the context constrains
+-- no type variables that don't appear free in the tau-type part.
 
-rnHsType (HsForAllTy tvs ctxt ty)
+rnHsSigType doc_str full_ty@(HsPreForAllTy ctxt ty)    -- From source code (no kinds on tyvars)
+  = getNameEnv         `thenRn` \ name_env ->
+    let
+       mentioned_tyvars = extractHsTyVars ty
+       forall_tyvars    = filter (not . in_scope) mentioned_tyvars
+       in_scope tv      = maybeToBool (lookupFM name_env tv)
+
+       constrained_tyvars            = nub (concat (map (extractHsTyVars . snd) ctxt))
+       constrained_and_in_scope      = filter in_scope constrained_tyvars
+       constrained_and_not_mentioned = filter (not . (`elem` mentioned_tyvars)) constrained_tyvars
+
+       -- Zap the context if there's a problem, to avoid duplicate error message.
+       ctxt' | null constrained_and_in_scope && null constrained_and_not_mentioned = ctxt
+             | otherwise = []
+    in
+    checkRn (null constrained_and_in_scope)
+           (ctxtErr1 sig_doc constrained_and_in_scope) `thenRn_`
+    checkRn (null constrained_and_not_mentioned)
+           (ctxtErr2 sig_doc constrained_and_not_mentioned ty) `thenRn_`
+
+    (bindTyVarsRn sig_doc (map UserTyVar forall_tyvars)        $ \ new_tyvars ->
+     rnContext ctxt'                                   `thenRn` \ new_ctxt ->
+     rnHsType ty                                       `thenRn` \ new_ty ->
+     returnRn (HsForAllTy new_tyvars new_ctxt new_ty)
+    )
+  where
+    sig_doc sty = text "the type signature for" <+> doc_str sty
+                            
+
+rnHsSigType doc_str other_ty = rnHsType other_ty
+
+rnHsType :: RdrNameHsType -> RnMS s RenamedHsType
+rnHsType (HsForAllTy tvs ctxt ty)              -- From an interface file (tyvars may be kinded)
   = rn_poly_help tvs ctxt ty
 
-rnHsType full_ty@(HsPreForAllTy ctxt ty)
+rnHsType full_ty@(HsPreForAllTy ctxt ty)       -- A (context => ty) embedded in a type.
+                                               -- Universally quantify over tyvars in context
   = getNameEnv         `thenRn` \ name_env ->
     let
-       mentioned_tyvars = extractHsTyVars full_ty
-       forall_tyvars    = filter not_in_scope mentioned_tyvars
-       not_in_scope tv  = case lookupFM name_env tv of
-                                   Nothing -> True
-                                   Just _  -> False
+       forall_tyvars = foldr unionLists [] (map (extractHsTyVars . snd) ctxt)
     in
     rn_poly_help (map UserTyVar forall_tyvars) ctxt ty
 
@@ -362,27 +459,27 @@ rnHsType (MonoTupleTy _ tys)
     mapRn rnHsType tys                                 `thenRn` \ tys' ->
     returnRn (MonoTupleTy tycon_name tys')
 
-rnHsType (MonoTyApp name tys)
-  = lookupOccRn name           `thenRn` \ name' ->
-    mapRn rnHsType tys         `thenRn` \ tys' ->
-    returnRn (MonoTyApp name' tys')
+rnHsType (MonoTyApp ty1 ty2)
+  = rnHsType ty1               `thenRn` \ ty1' ->
+    rnHsType ty2               `thenRn` \ ty2' ->
+    returnRn (MonoTyApp ty1' ty2')
 
 rnHsType (MonoDictTy clas ty)
   = lookupOccRn clas           `thenRn` \ clas' ->
     rnHsType ty                        `thenRn` \ ty' ->
     returnRn (MonoDictTy clas' ty')
 
-
 rn_poly_help :: [HsTyVar RdrName]              -- Universally quantified tyvars
             -> RdrNameContext
             -> RdrNameHsType
             -> RnMS s RenamedHsType
-
 rn_poly_help tyvars ctxt ty
-  = bindTyVarsRn "type signature" tyvars               $ \ new_tyvars ->
+  = bindTyVarsRn sig_doc tyvars                                $ \ new_tyvars ->
     rnContext ctxt                                     `thenRn` \ new_ctxt ->
     rnHsType ty                                                `thenRn` \ new_ty ->
     returnRn (HsForAllTy new_tyvars new_ctxt new_ty)
+  where
+    sig_doc sty = text "a nested for-all type"
 \end{code}
 
 
@@ -393,18 +490,41 @@ rnContext  ctxt
   = mapRn rn_ctxt ctxt `thenRn` \ result ->
     let
        (_, dup_asserts) = removeDups cmp_assert result
+       (alls, theta)    = partition (\(c,_) -> c == allClass_NAME) result
+       non_tyvar_alls   = [(c,t) | (c,t) <- alls, not (is_tyvar t)]
     in
-    -- If this isn't an error, then it ought to be:
-    mapRn (addWarnRn . dupClassAssertWarn result) dup_asserts `thenRn_`
-    returnRn result
+
+       -- Check for duplicate assertions
+       -- If this isn't an error, then it ought to be:
+    mapRn (addWarnRn . dupClassAssertWarn theta) dup_asserts `thenRn_`
+
+       -- Check for All constraining a non-type-variable
+    mapRn (addWarnRn . allOfNonTyVar) non_tyvar_alls   `thenRn_`
+    
+       -- Done.  Return a theta omitting all the "All" constraints.
+       -- They have done done their work by ensuring that we universally
+       -- quantify over their tyvar.
+    returnRn theta
   where
     rn_ctxt (clas, ty)
-      = lookupOccRn clas       `thenRn` \ clas_name ->
+      =                -- Mini hack here.  If the class is our pseudo-class "All",
+               -- then we don't want to record it as an occurrence, otherwise
+               -- we try to slurp it in later and it doesn't really exist at all.
+               -- Easiest thing is simply not to put it in the occurrence set.
+       lookupBndrRn clas       `thenRn` \ clas_name ->
+       (if clas_name /= allClass_NAME then
+               addOccurrenceName clas_name
+        else
+               returnRn clas_name
+       )                       `thenRn_`
        rnHsType ty             `thenRn` \ ty' ->
        returnRn (clas_name, ty')
 
     cmp_assert (c1,ty1) (c2,ty2)
       = (c1 `cmp` c2) `thenCmp` (cmpHsType cmp ty1 ty2)
+
+    is_tyvar (MonoTyVar _) = True
+    is_tyvar other         = False
 \end{code}
 
 
@@ -419,17 +539,21 @@ rnIdInfo (HsStrictness strict)
   = rnStrict strict    `thenRn` \ strict' ->
     returnRn (HsStrictness strict')
 
-rnIdInfo (HsUnfold expr)       = rnCoreExpr expr       `thenRn` \ expr' ->
-                                 returnRn (HsUnfold expr')
+rnIdInfo (HsUnfold inline expr)        = rnCoreExpr expr       `thenRn` \ expr' ->
+                                 returnRn (HsUnfold inline expr')
 rnIdInfo (HsArity arity)       = returnRn (HsArity arity)
 rnIdInfo (HsUpdate update)     = returnRn (HsUpdate update)
 rnIdInfo (HsFBType fb)         = returnRn (HsFBType fb)
 rnIdInfo (HsArgUsage au)       = returnRn (HsArgUsage au)
 rnIdInfo (HsDeforest df)       = returnRn (HsDeforest df)
 
-rnStrict (StrictnessInfo demands (Just worker))
-  = lookupOptionalOccRn worker         `thenRn` \ worker' ->
-    returnRn (StrictnessInfo demands (Just worker'))
+rnStrict (StrictnessInfo demands (Just (worker,cons)))
+       -- The sole purpose of the "cons" field is so that we can mark the constructors
+       -- needed to build the wrapper as "needed", so that their data type decl will be
+       -- slurped in. After that their usefulness is o'er, so we just put in the empty list.
+  = lookupOccRn worker                 `thenRn` \ worker' ->
+    mapRn lookupOccRn cons             `thenRn_` 
+    returnRn (StrictnessInfo demands (Just (worker',[])))
 
 -- Boring, but necessary for the type checker.
 rnStrict (StrictnessInfo demands Nothing) = returnRn (StrictnessInfo demands Nothing)
@@ -441,13 +565,13 @@ UfCore expressions.
 
 \begin{code}
 rnCoreExpr (UfVar v)
-  = lookupOptionalOccRn v      `thenRn` \ v' ->
+  = lookupOccRn v      `thenRn` \ v' ->
     returnRn (UfVar v')
 
 rnCoreExpr (UfLit lit) = returnRn (UfLit lit)
 
 rnCoreExpr (UfCon con args) 
-  = lookupOptionalOccRn con            `thenRn` \ con' ->
+  = lookupOccRn con            `thenRn` \ con' ->
     mapRn rnCoreArg args       `thenRn` \ args' ->
     returnRn (UfCon con' args')
 
@@ -517,11 +641,15 @@ rnCoreBndrs bndrs thing_inside            -- Expect them all to be ValBinders
   where
     names = map (\ (UfValBinder name _) -> name) bndrs
     tys   = map (\ (UfValBinder _   ty) -> ty)   bndrs
+
+rnCoreBndrNamess names thing_inside
+  = bindLocalsRn "unfolding value" names $ \ names' ->
+    thing_inside names'
 \end{code}    
 
 \begin{code}
-rnCoreArg (UfVarArg v)  = lookupOptionalOccRn v        `thenRn` \ v' -> returnRn (UfVarArg v')
-rnCoreArg (UfUsageArg u) = lookupOptionalOccRn u       `thenRn` \ u' -> returnRn (UfUsageArg u')
+rnCoreArg (UfVarArg v)  = lookupOccRn v        `thenRn` \ v' -> returnRn (UfVarArg v')
+rnCoreArg (UfUsageArg u) = lookupOccRn u       `thenRn` \ u' -> returnRn (UfUsageArg u')
 rnCoreArg (UfTyArg ty)  = rnHsType ty                  `thenRn` \ ty' -> returnRn (UfTyArg ty')
 rnCoreArg (UfLitArg lit) = returnRn (UfLitArg lit)
 
@@ -530,9 +658,9 @@ rnCoreAlts (UfAlgAlts alts deflt)
     rnCoreDefault deflt                `thenRn` \ deflt' ->
     returnRn (UfAlgAlts alts' deflt')
   where
-    rn_alt (con, bndrs, rhs) = lookupOptionalOccRn con `thenRn` \ con' ->
-                               rnCoreBndrs bndrs       $ \ bndrs' ->
-                               rnCoreExpr rhs          `thenRn` \ rhs' ->
+    rn_alt (con, bndrs, rhs) = lookupOccRn con                 `thenRn` \ con' ->
+                               bindLocalsRn "unfolding alt" bndrs      $ \ bndrs' ->
+                               rnCoreExpr rhs                          `thenRn` \ rhs' ->
                                returnRn (con', bndrs', rhs')
 
 rnCoreAlts (UfPrimAlts alts deflt)
@@ -544,15 +672,15 @@ rnCoreAlts (UfPrimAlts alts deflt)
                        returnRn (lit, rhs')
 
 rnCoreDefault UfNoDefault = returnRn UfNoDefault
-rnCoreDefault (UfBindDefault bndr rhs) = rnCoreBndr bndr       $ \ bndr' ->
-                                        rnCoreExpr rhs         `thenRn` \ rhs' ->
+rnCoreDefault (UfBindDefault bndr rhs) = bindLocalsRn "unfolding default" [bndr]       $ \ [bndr'] ->
+                                        rnCoreExpr rhs                                 `thenRn` \ rhs' ->
                                         returnRn (UfBindDefault bndr' rhs')
 
-rnCoercion (UfIn  n) = lookupOptionalOccRn n `thenRn` \ n' -> returnRn (UfIn  n')
-rnCoercion (UfOut n) = lookupOptionalOccRn n `thenRn` \ n' -> returnRn (UfOut n')
+rnCoercion (UfIn  n) = lookupOccRn n `thenRn` \ n' -> returnRn (UfIn  n')
+rnCoercion (UfOut n) = lookupOccRn n `thenRn` \ n' -> returnRn (UfOut n')
 
 rnCorePrim (UfOtherOp op) 
-  = lookupOptionalOccRn op     `thenRn` \ op' ->
+  = lookupOccRn op     `thenRn` \ op' ->
     returnRn (UfOtherOp op')
 
 rnCorePrim (UfCCallOp str casm gc arg_tys res_ty)
@@ -569,67 +697,36 @@ rnCorePrim (UfCCallOp str casm gc arg_tys res_ty)
 
 \begin{code}
 derivingNonStdClassErr clas sty
-  = ppCat [ppStr "non-standard class in deriving:", ppr sty clas]
+  = hsep [ptext SLIT("non-standard class"), ppr sty clas, ptext SLIT("in deriving clause")]
 
 classTyVarNotInOpTyErr clas_tyvar sig sty
-  = ppHang (ppBesides [ppStr "Class type variable `", ppr sty clas_tyvar, ppStr "' does not appear in method signature:"])
-        4 (ppr sty sig)
-
-classTyVarInOpCtxtErr clas_tyvar sig sty
-  = ppHang (ppBesides [ ppStr "Class type variable `", ppr sty clas_tyvar, 
-                       ppStr "' present in method's local overloading context:"])
+  = hang (hsep [ptext SLIT("Class type variable"), 
+                      ppr sty clas_tyvar, 
+                      ptext SLIT("does not appear in method signature")])
         4 (ppr sty sig)
 
 dupClassAssertWarn ctxt dups sty
-  = ppHang (ppBesides [ppStr "Duplicate class assertion `", ppr sty dups, ppStr "' in context:"])
+  = hang (hcat [ptext SLIT("Duplicate class assertion `"), 
+                      ppr sty dups, 
+                      ptext SLIT("' in context:")])
         4 (ppr sty ctxt)
-\end{code}
 
+badDataCon name sty
+   = hsep [ptext SLIT("Illegal data constructor name:"), ppr sty name]
 
+allOfNonTyVar ty sty
+  = hsep [ptext SLIT("`All' applied to a non-type variable:"), ppr sty ty]
 
+ctxtErr1 doc tyvars sty
+  = hsep [ptext SLIT("Context constrains in-scope type variable(s)"), 
+         hsep (punctuate comma (map (ppr sty) tyvars))]
+    $$
+    nest 4 (ptext SLIT("in") <+> doc sty)
 
-
-===================    OLD STUFF    ======================
-
-%*********************************************************
-%*                                                      *
-\subsection{SPECIALIZE data pragmas}
-%*                                                      *
-%*********************************************************
-
-\begin{pseudocode}
-rnSpecDataSig :: RdrNameSpecDataSig
-             -> RnMS s RenamedSpecDataSig
-
-rnSpecDataSig (SpecDataSig tycon ty src_loc)
-  = pushSrcLocRn src_loc $
-    let
-       tyvars = filter extractHsTyNames ty
-    in
-    mkTyVarNamesEnv src_loc tyvars             `thenRn` \ (tv_env,_) ->
-    lookupOccRn tycon                  `thenRn` \ tycon' ->
-    rnHsType tv_env ty         `thenRn` \ ty' ->
-    returnRn (SpecDataSig tycon' ty' src_loc)
-
-\end{pseudocode}
-
-%*********************************************************
-%*                                                     *
-\subsection{@SPECIALIZE instance@ user-pragmas}
-%*                                                     *
-%*********************************************************
-
-\begin{pseudocode}
-rnSpecInstSig :: RdrNameSpecInstSig
-             -> RnMS s RenamedSpecInstSig
-
-rnSpecInstSig (SpecInstSig clas ty src_loc)
-  = pushSrcLocRn src_loc $
-    let
-       tyvars = extractHsTyNames is_tyvar_name ty
-    in
-    mkTyVarNamesEnv src_loc tyvars             `thenRn` \ (tv_env,_) ->
-    lookupOccRn clas                   `thenRn` \ new_clas ->
-    rnHsType tv_env ty         `thenRn` \ new_ty ->
-    returnRn (SpecInstSig new_clas new_ty src_loc)
-\end{pseudocode}
+ctxtErr2 doc tyvars ty sty
+  = (ptext SLIT("Context constrains type variable(s)")
+       <+> hsep (punctuate comma (map (ppr sty) tyvars)))
+    $$
+    nest 4 (vcat [ptext SLIT("that do not appear in") <+> ppr sty ty,
+                 ptext SLIT("in") <+> doc sty])
+\end{code}