[project @ 1998-02-02 14:52:08 by simonm]
[ghc-hetmet.git] / ghc / compiler / rename / RnSource.lhs
index e726eb3..4a64569 100644 (file)
@@ -4,53 +4,51 @@
 \section[RnSource]{Main pass of renamer}
 
 \begin{code}
-#include "HsVersions.h"
-
-module RnSource ( rnDecl, rnHsType ) where
+module RnSource ( rnDecl, rnHsType, rnHsSigType ) where
 
-IMP_Ubiq()
-IMPORT_DELOOPER(RnLoop)                -- *check* the RnPass/RnExpr/RnBinds loop-breaking
+#include "HsVersions.h"
 
+import RnExpr
 import HsSyn
-import HsDecls         ( HsIdInfo(..) )
+import HsDecls         ( HsIdInfo(..), HsStrictnessInfo(..) )
 import HsPragmas
-import HsTypes         ( getTyVarName )
+import HsTypes         ( getTyVarName, pprClassAssertion, cmpHsTypes )
 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,
+                         newDfunName, checkDupOrQualNames, checkDupNames,
+                         newLocallyDefinedGlobalName, newImportedGlobalName, ifaceFlavour,
                          listType_RDR, tupleType_RDR )
 import RnMonad
 
-import Name            ( Name, isLocallyDefined, isTvOcc, pprNonSym,
-                         Provenance,
-                         SYN_IE(NameSet), unionNameSets, emptyNameSet, mkNameSet, unitNameSet,
-                         elemNameSet
+import Name            ( Name, isLocallyDefined, 
+                         OccName(..), occNameString, prefixOccName,
+                         ExportFlag(..),
+                         Provenance(..), getNameProvenance,
+                         NameSet, unionNameSets, emptyNameSet, mkNameSet, unitNameSet,
+                         elemNameSet, nameSetToList
                        )
-import ErrUtils                ( addErrLoc, addShortErrLocLine, addShortWarnLocLine )
 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 Pretty
+import Outputable
 import SrcLoc          ( SrcLoc )
--- import TyCon                ( TyCon{-instance NamedThing-} )
 import Unique          ( Unique )
-import UniqSet         ( SYN_IE(UniqSet) )
+import UniqSet         ( UniqSet )
 import UniqFM          ( UniqFM, lookupUFM )
-import Util            ( isIn, isn'tIn, thenCmp, removeDups, cmpPString,
-                         panic, assertPanic{- , pprTrace ToDo:rm-} )
+import Util
+import List            ( partition, nub )
 \end{code}
 
 rnDecl `renames' declarations.
@@ -82,8 +80,14 @@ 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).
+    getModeRn                  `thenRn` \ (InterfaceMode _ print_unqual) ->
+    setModeRn (InterfaceMode Optional print_unqual) $
+       -- 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 +111,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 = text "the data type declaration for" <+> ppr 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 = text "the declaration for type synonym" <+> ppr name
 \end{code}
 
 %*********************************************************
@@ -146,40 +146,86 @@ class declaration in which local names have been replaced by their
 original names, reporting any unknown names.
 
 \begin{code}
-rnDecl (ClD (ClassDecl context cname tyvar sigs mbinds pragmas src_loc))
+rnDecl (ClD (ClassDecl context cname tyvars sigs mbinds pragmas tname dname 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' ->
+
+    lookupBndrRn cname                                 `thenRn` \ cname' ->
+    lookupBndrRn tname                                 `thenRn` \ tname' ->
+    lookupBndrRn dname                                 `thenRn` \ dname' ->
+
+    bindTyVarsRn cls_doc tyvars                                        ( \ tyvars' ->
+       rnContext context                                       `thenRn` \ context' ->
+
+            -- Check the signatures
+       let
+         clas_tyvar_names = map getTyVarName tyvars'
+       in
+       checkDupOrQualNames sig_doc sig_rdr_names_w_locs        `thenRn_` 
+       mapRn (rn_op cname' clas_tyvar_names) sigs              `thenRn` \ sigs' ->
+       returnRn (tyvars', context', sigs')
+    )                                                  `thenRn` \ (tyvars', context', sigs') ->
+
+       -- Check the methods
+    checkDupOrQualNames meth_doc meth_rdr_names_w_locs `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))
+    returnRn (ClD (ClassDecl context' cname' tyvars' sigs' mbinds' NoClassPragmas tname' dname' src_loc))
   where
-    rn_op clas clas_tyvar sig@(ClassOpSig op ty pragmas locn)
+    cls_doc  = text "the declaration for class"        <+> ppr cname
+    sig_doc  = text "the signatures for class"         <+> ppr cname
+    meth_doc = text "the default-methods for class" <+> ppr cname
+
+    sig_rdr_names_w_locs  = [(op,locn) | ClassOpSig op _ _ locn <- sigs]
+    meth_rdr_names_w_locs = bagToList (collectMonoBinders mbinds)
+    meth_rdr_names       = map fst meth_rdr_names_w_locs
+
+    rn_op clas clas_tyvars sig@(ClassOpSig op maybe_dm ty locn)
       = pushSrcLocRn locn $
-       lookupRn op                     `thenRn` \ op_name ->
-       rnHsType ty                     `thenRn` \ new_ty  ->
+       lookupBndrRn op                         `thenRn` \ op_name ->
+       rnHsSigType (quotes (ppr op)) ty        `thenRn` \ new_ty  ->
+
+               -- Make the default-method name
+       let
+           dm_occ = prefixOccName SLIT("$m") (rdrNameOcc op)
+       in
+       getModuleRn                     `thenRn` \ mod_name ->
+       getModeRn                       `thenRn` \ mode ->
+       (case (mode, maybe_dm) of 
+           (SourceMode, _) | op `elem` meth_rdr_names
+               ->      -- There's an explicit method decl
+                  newLocallyDefinedGlobalName mod_name dm_occ 
+                                              (\_ -> Exported) locn    `thenRn` \ dm_name ->
+                  returnRn (Just dm_name)
+
+           (InterfaceMode _ _, Just _) 
+               ->      -- Imported class that has a default method decl
+                   newImportedGlobalName mod_name dm_occ (ifaceFlavour clas)   `thenRn` \ dm_name ->
+                   addOccurrenceName dm_name                                   `thenRn_`
+                   returnRn (Just dm_name)
+
+           other -> returnRn Nothing
+       )                                       `thenRn` \ maybe_dm_name ->
+
+               -- Check that each class tyvar appears in op_ty
        let
            (ctxt, op_ty) = case new_ty of
                                HsForAllTy tvs ctxt op_ty -> (ctxt, op_ty)
                                other                     -> ([], new_ty)
-           ctxt_fvs  = extractCtxtTyNames ctxt
-           op_ty_fvs = extractHsTyNames op_ty          -- Includes tycons/classes but we
-                                                       -- don't care about that
+           ctxt_fvs  = extractHsCtxtTyNames ctxt       -- Includes tycons/classes but we
+           op_ty_fvs = extractHsTyNames op_ty          -- don't care about that
+
+           check_in_op_ty clas_tyvar = checkRn (clas_tyvar `elemNameSet` op_ty_fvs)
+                                               (classTyVarNotInOpTyErr clas_tyvar sig)
        in
-       -- 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)
+        mapRn check_in_op_ty clas_tyvars                `thenRn_`
+
+       returnRn (ClassOpSig op_name maybe_dm_name new_ty locn)
 \end{code}
 
 
@@ -190,40 +236,43 @@ 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 (text "an instance decl") inst_ty      `thenRn` \ inst_ty' ->
+
 
-    returnRn (InstD (InstDecl inst_ty' mbinds' new_uprags dfun_name' src_loc))
+       -- 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 ->
+
+    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) = lookupOptionalOccRn n   `thenRn` \ n' ->
-                      returnRn (Just n')
+    meth_doc = 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 (quotes (ppr 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 ->
-       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
@@ -278,35 +327,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 = text "the fields of constructor" <> ppr 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) 
 
@@ -317,6 +372,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}
 
 
@@ -327,19 +396,56 @@ rnBangTy (Unbanged ty)
 %*********************************************************
 
 \begin{code}
-rnHsType :: RdrNameHsType -> RnMS s RenamedHsType
+rnHsSigType :: SDoc -> 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.
+
+rnHsSigType doc_str full_ty@(HsPreForAllTy ctxt ty)    -- From source code (no kinds on tyvars)
+  = getLocalNameEnv            `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            = extractHsCtxtTyVars ctxt
+       constrained_and_in_scope      = filter in_scope constrained_tyvars
+       constrained_and_not_mentioned = filter (not . (`elem` mentioned_tyvars)) constrained_tyvars
 
-rnHsType (HsForAllTy tvs ctxt ty)
+       -- 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 = text "the type signature for" <+> doc_str
+                            
+
+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)
-  = getNameEnv         `thenRn` \ name_env ->
+rnHsType full_ty@(HsPreForAllTy ctxt ty)       -- A (context => ty) embedded in a type.
+                                               -- Universally quantify over tyvars in context
+  = getLocalNameEnv            `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 = extractHsCtxtTyVars ctxt
     in
     rn_poly_help (map UserTyVar forall_tyvars) ctxt ty
 
@@ -360,27 +466,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)
+rnHsType (MonoDictTy clas tys)
   = lookupOccRn clas           `thenRn` \ clas' ->
-    rnHsType ty                        `thenRn` \ ty' ->
-    returnRn (MonoDictTy clas' ty')
-
+    mapRn rnHsType tys         `thenRn` \ tys' ->
+    returnRn (MonoDictTy clas' tys')
 
 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 = text "a nested for-all type"
 \end{code}
 
 
@@ -391,18 +497,41 @@ rnContext  ctxt
   = mapRn rn_ctxt ctxt `thenRn` \ result ->
     let
        (_, dup_asserts) = removeDups cmp_assert result
+       (alls, theta)    = partition (\(c,_) -> c == allClass_NAME) result
     in
-    -- If this isn't an error, then it ought to be:
-    mapRn (addWarnRn . dupClassAssertWarn result) dup_asserts `thenRn_`
-    returnRn result
-  where
-    rn_ctxt (clas, ty)
-      = lookupOccRn clas       `thenRn` \ clas_name ->
-       rnHsType ty             `thenRn` \ ty' ->
-       returnRn (clas_name, ty')
 
-    cmp_assert (c1,ty1) (c2,ty2)
-      = (c1 `cmp` c2) `thenCmp` (cmpHsType cmp ty1 ty2)
+       -- 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 check_All 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, tys)
+      =                -- 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_`
+       mapRn rnHsType tys      `thenRn` \ tys' ->
+       returnRn (clas_name, tys')
+
+
+    cmp_assert (c1,tys1) (c2,tys2)
+      = (c1 `compare` c2) `thenCmp` (cmpHsTypes compare tys1 tys2)
+
+    check_All (c, [MonoTyVar _]) = returnRn () -- OK!
+    check_All assertion                 = addErrRn (wierdAllErr assertion)
 \end{code}
 
 
@@ -417,35 +546,37 @@ 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 (HsStrictnessInfo 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 (HsStrictnessInfo demands (Just (worker',[])))
 
 -- Boring, but necessary for the type checker.
-rnStrict (StrictnessInfo demands Nothing) = returnRn (StrictnessInfo demands Nothing)
-rnStrict BottomGuaranteed                = returnRn BottomGuaranteed
-rnStrict NoStrictnessInfo                = returnRn NoStrictnessInfo
+rnStrict (HsStrictnessInfo demands Nothing) = returnRn (HsStrictnessInfo demands Nothing)
+rnStrict HsBottom                        = returnRn HsBottom
 \end{code}
 
 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')
 
@@ -504,10 +635,6 @@ rnCoreBndr (UfTyBinder name kind) thing_inside
   = bindLocalsRn "unfolding tyvar" [name] $ \ [name'] ->
     thing_inside (UfTyBinder name' kind)
     
-rnCoreBndr (UfUsageBinder name) thing_inside
-  = bindLocalsRn "unfolding usage" [name] $ \ [name'] ->
-    thing_inside (UfUsageBinder name')
-
 rnCoreBndrs bndrs thing_inside         -- Expect them all to be ValBinders
   = mapRn rnHsType tys                 `thenRn` \ tys' ->
     bindLocalsRn "unfolding value" names $ \ names' ->
@@ -515,12 +642,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 (UfTyArg ty)  = rnHsType ty                  `thenRn` \ ty' -> returnRn (UfTyArg ty')
+rnCoreArg (UfVarArg v)  = lookupOccRn v        `thenRn` \ v' -> returnRn (UfVarArg v')
+rnCoreArg (UfTyArg ty)  = rnHsType ty          `thenRn` \ ty' -> returnRn (UfTyArg ty')
 rnCoreArg (UfLitArg lit) = returnRn (UfLitArg lit)
 
 rnCoreAlts (UfAlgAlts alts deflt)
@@ -528,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)
@@ -542,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)
@@ -566,68 +696,37 @@ rnCorePrim (UfCCallOp str casm gc arg_tys res_ty)
 %*********************************************************
 
 \begin{code}
-derivingNonStdClassErr clas sty
-  = ppCat [ppStr "non-standard class in deriving:", ppr sty clas]
-
-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:"])
-        4 (ppr sty sig)
-
-dupClassAssertWarn ctxt dups sty
-  = ppHang (ppBesides [ppStr "Duplicate class assertion `", ppr sty dups, ppStr "' in context:"])
-        4 (ppr sty ctxt)
+derivingNonStdClassErr clas
+  = hsep [ptext SLIT("non-standard class"), ppr clas, ptext SLIT("in deriving clause")]
+
+classTyVarNotInOpTyErr clas_tyvar sig
+  = hang (hsep [ptext SLIT("Class type variable"),
+                      quotes (ppr clas_tyvar),
+                      ptext SLIT("does not appear in method signature")])
+        4 (ppr sig)
+
+dupClassAssertWarn ctxt (assertion : dups)
+  = sep [hsep [ptext SLIT("Duplicated class assertion"), 
+              quotes (pprClassAssertion assertion),
+              ptext SLIT("in the context:")],
+        nest 4 (pprContext ctxt)]
+
+badDataCon name
+   = hsep [ptext SLIT("Illegal data constructor name"), quotes (ppr name)]
+
+wierdAllErr assertion
+  = ptext SLIT("Mal-formed use of `All':") <+> pprClassAssertion assertion
+
+ctxtErr1 doc tyvars
+  = hsep [ptext SLIT("Context constrains in-scope type variable(s)"), 
+         pprQuotedList tyvars]
+    $$
+    nest 4 (ptext SLIT("in") <+> doc)
+
+ctxtErr2 doc tyvars ty
+  = (ptext SLIT("Context constrains type variable(s)")
+       <+> pprQuotedList tyvars)
+    $$
+    nest 4 (vcat [ptext SLIT("that do not appear in") <+> quotes (ppr ty),
+                 ptext SLIT("in") <+> doc])
 \end{code}
-
-
-
-
-
-===================    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}