-renameSig :: Sig RdrName -> RnM (Sig Name)
--- FixitSig is renamed elsewhere.
-renameSig (TypeSig v ty)
- = lookupLocatedSigOccRn v `thenM` \ new_v ->
- rnHsSigType (quotes (ppr v)) ty `thenM` \ new_ty ->
- returnM (TypeSig new_v new_ty)
-
-renameSig (SpecInstSig ty)
- = rnLHsType (text "A SPECIALISE instance pragma") ty `thenM` \ new_ty ->
- returnM (SpecInstSig new_ty)
-
-renameSig (SpecSig v ty inl)
- = lookupLocatedSigOccRn v `thenM` \ new_v ->
- rnHsSigType (quotes (ppr v)) ty `thenM` \ new_ty ->
- returnM (SpecSig new_v new_ty inl)
-
-renameSig (InlineSig v s)
- = lookupLocatedSigOccRn v `thenM` \ new_v ->
- returnM (InlineSig new_v s)
-
-renameSig (FixSig (FixitySig v f))
- = lookupLocatedSigOccRn v `thenM` \ new_v ->
- returnM (FixSig (FixitySig new_v f))
+renameSig :: Maybe NameSet -> Sig RdrName -> RnM (Sig Name)
+-- FixitySig is renamed elsewhere.
+renameSig mb_names sig@(TypeSig v ty)
+ = do { new_v <- lookupSigOccRn mb_names sig v
+ ; new_ty <- rnHsSigType (quotes (ppr v)) ty
+ ; return (TypeSig new_v new_ty) }
+
+renameSig _ (SpecInstSig ty)
+ = do { new_ty <- rnLHsType (text "A SPECIALISE instance pragma") ty
+ ; return (SpecInstSig new_ty) }
+
+renameSig mb_names sig@(SpecSig v ty inl)
+ = do { new_v <- lookupSigOccRn mb_names sig v
+ ; new_ty <- rnHsSigType (quotes (ppr v)) ty
+ ; return (SpecSig new_v new_ty inl) }
+
+renameSig mb_names sig@(InlineSig v s)
+ = do { new_v <- lookupSigOccRn mb_names sig v
+ ; return (InlineSig new_v s) }
+
+renameSig mb_names sig@(FixSig (FixitySig v f))
+ = do { new_v <- lookupSigOccRn mb_names sig v
+ ; return (FixSig (FixitySig new_v f)) }
+
+-- lookupSigOccRn 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".
+
+lookupSigOccRn :: Maybe NameSet -> Sig RdrName -> Located RdrName -> RnM (Located Name)
+lookupSigOccRn mb_names sig (L loc v)
+ = do { mb_n <- lookupBndrRn_maybe v
+ ; case mb_n of {
+ Just n -> case mb_names of {
+ Nothing -> return (L loc n) ;
+ Just ns | n `elemNameSet` ns -> return (L loc n)
+ | otherwise -> bale_out_with local_msg } ;
+
+ Nothing -> do
+ { mb_n <- lookupGreRn_maybe v
+ ; case mb_n of
+ Just _ -> bale_out_with import_msg
+ Nothing -> bale_out_with empty
+ } }}
+ where
+ bale_out_with msg
+ = do { addErr (sep [ ptext (sLit "The") <+> hsSigDoc sig
+ <+> ptext (sLit "for") <+> quotes (ppr v)
+ , nest 2 $ ptext (sLit "lacks an accompanying binding")]
+ $$ nest 2 msg)
+ ; return (L loc (mkUnboundName v)) }
+
+ local_msg = parens $ ptext (sLit "The") <+> hsSigDoc sig <+> ptext (sLit "must be given where")
+ <+> quotes (ppr v) <+> ptext (sLit "is declared")
+
+ import_msg = parens $ ptext (sLit "You cannot give a") <+> hsSigDoc sig
+ <+> ptext (sLit "an imported value")