Add separate functions for querying DynFlag and ExtensionFlag options
[ghc-hetmet.git] / compiler / typecheck / TcErrors.lhs
index b3dfb9c..db21659 100644 (file)
@@ -158,7 +158,7 @@ reportTidyWanteds ctxt unsolved
 reportFlat :: ReportErrCtxt -> [PredType] -> CtOrigin -> TcM ()
 reportFlat ctxt flats origin
   = do { unless (null dicts) $ reportDictErrs ctxt dicts origin
-       ; unless (null eqs)   $ reportEqErrs   ctxt eqs   
+       ; unless (null eqs)   $ reportEqErrs   ctxt eqs   origin
        ; unless (null ips)   $ reportIPErrs   ctxt ips   origin
        ; ASSERT( null others ) return () }
   where
@@ -277,12 +277,18 @@ reportIPErrs ctxt ips orig
 %************************************************************************
 
 \begin{code}
-reportEqErrs :: ReportErrCtxt -> [PredType] -> TcM ()
-reportEqErrs ctxt eqs 
+reportEqErrs :: ReportErrCtxt -> [PredType] -> CtOrigin -> TcM ()
+reportEqErrs ctxt eqs orig
   = mapM_ report_one eqs 
   where
-    report_one (EqPred ty1 ty2) = reportEqErr ctxt ty1 ty2
-    report_one pred             = pprPanic "reportEqErrs" (ppr pred)    
+    env0 = cec_tidy ctxt
+    report_one (EqPred ty1 ty2) 
+      = getWantedEqExtra emptyTvSubst env0 orig ty1 ty2 $ \ env1 extra ->
+        let ctxt' = ctxt { cec_tidy = env1
+                         , cec_extra = cec_extra ctxt $$ extra }
+        in reportEqErr ctxt' ty1 ty2 
+    report_one pred 
+      = pprPanic "reportEqErrs" (ppr pred)    
 
 reportEqErr :: ReportErrCtxt -> TcType -> TcType -> TcM ()
 reportEqErr ctxt ty1 ty2
@@ -554,7 +560,7 @@ monomorphism_fix :: DynFlags -> SDoc
 monomorphism_fix dflags
   = ptext (sLit "Probable fix:") <+> vcat
        [ptext (sLit "give these definition(s) an explicit type signature"),
-        if dopt Opt_MonomorphismRestriction dflags
+        if xopt Opt_MonomorphismRestriction dflags
            then ptext (sLit "or use -XNoMonomorphismRestriction")
            else empty] -- Only suggest adding "-XNoMonomorphismRestriction"
                        -- if it is not already set!
@@ -650,26 +656,21 @@ kindErrorTcS :: CtFlavor -> TcType -> TcType -> TcS ()
 -- in which case that's the error to report.  So we set things
 -- up to call reportEqErr, which does the business properly
 kindErrorTcS fl ty1 ty2
-  = wrapErrTcS        $ 
-    setCtFlavorLoc fl $ 
-    do { env0 <- tcInitTidyEnv
-       ; let (env1, ty1') = tidyOpenType env0 ty1
+  = wrapEqErrTcS fl ty1 ty2 $ \ env0 extra -> 
+    do { let (env1, ty1') = tidyOpenType env0 ty1
              (env2, ty2') = tidyOpenType env1 ty2
              ctxt = CEC { cec_encl = []
-                        , cec_extra = empty
+                        , cec_extra = extra
                         , cec_tidy = env2 }
        ; reportEqErr ctxt ty1' ty2' }
 
 misMatchErrorTcS :: CtFlavor -> TcType -> TcType -> TcS a
 misMatchErrorTcS fl ty1 ty2
-  = wrapErrTcS            $ 
-    setCtFlavorLocNoEq fl $  -- Don't add the "When matching t1 with t2"
-                            -- part, because it duplciates what we say now
-    do { env0 <- tcInitTidyEnv
-       ; let (env1, ty1') = tidyOpenType env0 ty1
+  = wrapEqErrTcS fl ty1 ty2 $ \ env0 extra -> 
+    do { let (env1, ty1') = tidyOpenType env0 ty1
              (env2, ty2') = tidyOpenType env1 ty2
              (env3, msg)  = misMatchMsgWithExtras env2 ty1' ty2'
-       ; failWithTcM (env3, inaccessible_msg $$ msg) }
+       ; failWithTcM (env3, inaccessible_msg $$ msg $$ extra) }
   where
     inaccessible_msg 
       = case fl of 
@@ -685,13 +686,11 @@ misMatchErrorTcS fl ty1 ty2
 
 occursCheckErrorTcS :: CtFlavor -> TcTyVar -> TcType -> TcS a
 occursCheckErrorTcS fl tv ty
-  = wrapErrTcS           $ 
-    setCtFlavorLoc fl $ 
-    do { env0          <- tcInitTidyEnv
-       ; let (env1, tv') = tidyOpenTyVar env0 tv
+  = wrapEqErrTcS fl (mkTyVarTy tv) ty $ \ env0 extra2 -> 
+    do { let (env1, tv') = tidyOpenTyVar env0 tv
              (env2, ty') = tidyOpenType env1 ty
-             extra = sep [ppr tv', char '=', ppr ty']
-       ; failWithTcM (env2, hang msg 2 extra) }
+             extra1 = sep [ppr tv', char '=', ppr ty']
+       ; failWithTcM (env2, hang msg 2 (extra1 $$ extra2)) }
   where
     msg = text $ "Occurs check: cannot construct the infinite type:"
 
@@ -731,30 +730,52 @@ flattenForAllErrorTcS fl ty _bad_eqs
 %************************************************************************
 
 \begin{code}
-setCtFlavorLocNoEq :: CtFlavor -> TcM a -> TcM a
-setCtFlavorLocNoEq (Wanted  loc) thing = setCtLoc loc thing
-setCtFlavorLocNoEq (Derived loc) thing = setCtLoc loc thing
-setCtFlavorLocNoEq (Given   loc) thing = setCtLoc loc thing
-
 setCtFlavorLoc :: CtFlavor -> TcM a -> TcM a
-setCtFlavorLoc (Wanted  loc) thing = setWantedLoc loc thing
-setCtFlavorLoc (Derived loc) thing = setWantedLoc loc thing
-setCtFlavorLoc (Given   loc) thing = setGivenLoc  loc thing
-
-setWantedLoc :: WantedLoc -> TcM a -> TcM a
-setWantedLoc loc thing_inside 
-  = setCtLoc loc $
-    add_origin (ctLocOrigin loc) $ 
-    thing_inside
-  where
-    add_origin (TypeEqOrigin item) = addErrCtxtM (unifyCtxt item)
-    add_origin orig = addErrCtxt (ptext (sLit "At") <+> ppr orig)
-
-setGivenLoc :: GivenLoc -> TcM a -> TcM a
-setGivenLoc loc thing_inside 
-  = setCtLoc loc $
-    add_origin (ctLocOrigin loc) $ 
-    thing_inside
-  where
-    add_origin skol = addErrCtxt (ptext (sLit "In") <+> pprSkolInfo skol)
+setCtFlavorLoc (Wanted  loc) thing = setCtLoc loc thing
+setCtFlavorLoc (Derived loc) thing = setCtLoc loc thing
+setCtFlavorLoc (Given   loc) thing = setCtLoc loc thing
+
+wrapEqErrTcS :: CtFlavor -> TcType -> TcType
+             -> (TidyEnv -> SDoc -> TcM a)
+             -> TcS a
+wrapEqErrTcS fl ty1 ty2 thing_inside
+  = do { ty_binds_var <- getTcSTyBinds
+       ; wrapErrTcS $ setCtFlavorLoc fl $ 
+    do { env0 <- tcInitTidyEnv 
+       ; ty_binds_bag <- readTcRef ty_binds_var
+       ; let subst = mkOpenTvSubst (mkVarEnv (bagToList ty_binds_bag))
+       ; case fl of
+           Wanted  loc -> getWantedEqExtra subst env0 (ctLocOrigin loc) ty1 ty2 thing_inside
+           Derived loc -> getWantedEqExtra subst env0 (ctLocOrigin loc) ty1 ty2 thing_inside
+           Given {}    -> thing_inside env0 empty  -- We could print more info, but it
+                                                   -- seems to be coming out already
+       } }  
+
+getWantedEqExtra :: TvSubst -> TidyEnv -> CtOrigin -> TcType -> TcType
+                 -> (TidyEnv -> SDoc -> TcM a)
+                 -> TcM a
+getWantedEqExtra subst env0 (TypeEqOrigin item) ty1 ty2 thing_inside
+  -- If the types in the error message are the same 
+  -- as the types we are unifying (remember to zonk the latter)
+  -- don't add the extra expected/actual message
+  --
+  -- The complication is that the types in the TypeEqOrigin must
+  --   (a) be zonked
+  --   (b) have any TcS-monad pending equalities applied to them 
+  --                   (hence the passed-in substitution)
+  = do { act0 <- zonkTcType (uo_actual item)
+       ; exp0 <- zonkTcType (uo_expected item)
+       ; let act1 = substTy subst act0
+             exp1 = substTy subst exp0
+             (env1, exp2) = tidyOpenType env0 exp1
+             (env2, act2) = tidyOpenType env1 act1
+       ; if (act1 `tcEqType` ty1 && exp1 `tcEqType` ty2)
+         || (exp1 `tcEqType` ty1 && act1 `tcEqType` ty2)
+         then  
+            thing_inside env0 empty
+         else 
+            thing_inside env2 (mkExpectedActualMsg act2 exp2) }
+
+getWantedEqExtra _ env0 orig _ _ thing_inside
+  = thing_inside env0 (pprArising orig)
 \end{code}