Repository : ssh://darcs.haskell.org//srv/darcs/ghc

On branch  : master

http://hackage.haskell.org/trac/ghc/changeset/6d46439c1c0e3765827dde26077ff45f85e210bd

>---------------------------------------------------------------

commit 6d46439c1c0e3765827dde26077ff45f85e210bd
Author: Simon Peyton Jones <[email protected]>
Date:   Thu Apr 5 11:37:02 2012 +0100

    Improve error reporting for out-of-scope variables
    
    Sometimes when a variable is out of scope in the type-checker,
    it's not a GHC bug, but rather a TH staging error.  See
    Note [Out of scope might be a staging error] in TcEnv.
    
    This showed up as Trac #5795.

>---------------------------------------------------------------

 compiler/rename/RnEnv.lhs    |    4 +++-
 compiler/typecheck/TcEnv.lhs |   36 ++++++++++++++++++++++++++++--------
 2 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/compiler/rename/RnEnv.lhs b/compiler/rename/RnEnv.lhs
index f45354d..2834a78 100644
--- a/compiler/rename/RnEnv.lhs
+++ b/compiler/rename/RnEnv.lhs
@@ -251,7 +251,9 @@ lookupExactOcc name
 
   where
     exact_nm_err = hang (ptext (sLit "The exact Name") <+> quotes (ppr name) 
<+> ptext (sLit "is not in scope"))
-                      2 (ptext (sLit "Probable cause: you used a unique name 
(NameU) in Template Haskell but did not bind it"))
+                      2 (vcat [ ptext (sLit "Probable cause: you used a unique 
name (NameU), perhaps via newName,")
+                              , ptext (sLit "in Template Haskell, but did not 
bind it")
+                              , ptext (sLit "If that's it, then -ddump-splices 
might be useful") ])
 
 -----------------------------------------------
 lookupInstDeclBndr :: Name -> SDoc -> RdrName -> RnM Name
diff --git a/compiler/typecheck/TcEnv.lhs b/compiler/typecheck/TcEnv.lhs
index 604db4d..0cf0580 100644
--- a/compiler/typecheck/TcEnv.lhs
+++ b/compiler/typecheck/TcEnv.lhs
@@ -520,10 +520,7 @@ checkWellStaged pp_thing bind_lvl use_lvl
   = return ()                   -- E.g.  \x -> [| $(f x) |]
 
   | bind_lvl == outerLevel      -- GHC restriction on top level splices
-  = failWithTc $ 
-    sep [ptext (sLit "GHC stage restriction:") <+>  pp_thing,
-         nest 2 (vcat [ ptext (sLit "is used in a top-level splice or 
annotation,")
-                      , ptext (sLit "and must be imported, not defined 
locally")])]
+  = stageRestrictionError pp_thing
 
   | otherwise                   -- Badly staged
   = failWithTc $                -- E.g.  \x -> $(f x)
@@ -531,6 +528,13 @@ checkWellStaged pp_thing bind_lvl use_lvl
         hsep   [ptext (sLit "is bound at stage") <+> ppr bind_lvl,
                 ptext (sLit "but used at stage") <+> ppr use_lvl]
 
+stageRestrictionError :: SDoc -> TcM a
+stageRestrictionError pp_thing
+  = failWithTc $ 
+    sep [ ptext (sLit "GHC stage restriction:")
+        , nest 2 (vcat [ pp_thing <+> ptext (sLit "is used in a top-level 
splice or annotation,")
+                       , ptext (sLit "and must be imported, not defined 
locally")])]
+
 topIdLvl :: Id -> ThLevel
 -- Globals may either be imported, or may be from an earlier "chunk" 
 -- (separated by declaration splices) of this module.  The former
@@ -765,19 +769,35 @@ pprBinders bndrs  = pprWithCommas ppr bndrs
 
 notFound :: Name -> TcM TyThing
 notFound name 
-  = do { (_gbl,lcl) <- getEnvs
-       ; failWithTc (vcat[ptext (sLit "GHC internal error:") <+> quotes (ppr 
name) <+> 
+  = do { lcl_env <- getLclEnv
+       ; let stage = tcl_th_ctxt lcl_env
+       ; case stage of   -- See Note [Out of scope might be a staging error]
+           Splice -> stageRestrictionError (quotes (ppr name))
+           _ -> failWithTc $
+                vcat[ptext (sLit "GHC internal error:") <+> quotes (ppr name) 
<+> 
                      ptext (sLit "is not in scope during type checking, but it 
passed the renamer"),
-                     ptext (sLit "tcl_env of environment:") <+> ppr (tcl_env 
lcl)]
+                     ptext (sLit "tcl_env of environment:") <+> ppr (tcl_env 
lcl_env)]
                        -- Take case: printing the whole gbl env can
                        -- cause an infnite loop, in the case where we
                        -- are in the middle of a recursive TyCon/Class group;
                        -- so let's just not print it!  Getting a loop here is
                        -- very unhelpful, because it hides one compiler bug 
with another
-                    ) }
+       }
 
 wrongThingErr :: String -> TcTyThing -> Name -> TcM a
 wrongThingErr expected thing name
   = failWithTc (pprTcTyThingCategory thing <+> quotes (ppr name) <+> 
                 ptext (sLit "used as a") <+> text expected)
 \end{code}
+
+Note [Out of scope might be a staging error]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Consider
+  x = 3
+  data T = MkT $(foo x)
+
+This is really a staging error, because we can't run code involving 'x'.
+But in fact the type checker processes types first, so 'x' won't even be
+in the type envt when we look for it in $(foo x).  So inside splices we
+report something missing from the type env as a staging error.
+See Trac #5752 and #5795.
\ No newline at end of file



_______________________________________________
Cvs-ghc mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/cvs-ghc

Reply via email to