Add separate functions for querying DynFlag and ExtensionFlag options
[ghc-hetmet.git] / compiler / typecheck / TcErrors.lhs
index 533520f..db21659 100644 (file)
@@ -1,7 +1,8 @@
 \begin{code}
 module TcErrors( 
        reportUnsolved, reportUnsolvedImplication, reportUnsolvedDeriv,
-       reportUnsolvedWantedEvVars, warnDefaulting, typeExtraInfoMsg,
+       reportUnsolvedWantedEvVars, warnDefaulting, 
+       unifyCtxt, typeExtraInfoMsg, 
        kindErrorTcS, misMatchErrorTcS, flattenForAllErrorTcS,
        occursCheckErrorTcS, solverDepthErrorTcS
   ) where
@@ -81,13 +82,14 @@ reportUnsolvedDeriv unsolved loc
   | null unsolved
   = return ()
   | otherwise
-  = do { env0 <- tcInitTidyEnv
+  = setCtLoc loc $
+    do { env0 <- tcInitTidyEnv
        ; let tidy_env      = tidyFreeTyVars env0 (tyVarsOfTheta unsolved)
              tidy_unsolved = map (tidyPred tidy_env) unsolved
              err_ctxt = CEC { cec_encl  = [] 
                             , cec_extra = alt_fix
                             , cec_tidy  = tidy_env } 
-       ; reportFlat err_ctxt tidy_unsolved loc }
+       ; reportFlat err_ctxt tidy_unsolved (ctLocOrigin loc) }
   where
     alt_fix = vcat [ptext (sLit "Alternatively, use a standalone 'deriving instance' declaration,"),
                     nest 2 $ ptext (sLit "so you can specify the instance context yourself")]
@@ -153,11 +155,11 @@ reportTidyWanteds ctxt unsolved
                  where   
                      pred = wantedEvVarPred d
 
-reportFlat :: ReportErrCtxt -> [PredType] -> WantedLoc -> TcM ()
-reportFlat ctxt flats loc
-  = do { unless (null dicts) $ reportDictErrs ctxt dicts loc
-       ; unless (null eqs)   $ reportEqErrs   ctxt eqs   loc
-       ; unless (null ips)   $ reportIPErrs   ctxt ips   loc
+reportFlat :: ReportErrCtxt -> [PredType] -> CtOrigin -> TcM ()
+reportFlat ctxt flats origin
+  = do { unless (null dicts) $ reportDictErrs ctxt dicts origin
+       ; unless (null eqs)   $ reportEqErrs   ctxt eqs   origin
+       ; unless (null ips)   $ reportIPErrs   ctxt ips   origin
        ; ASSERT( null others ) return () }
   where
     (dicts, non_dicts) = partition isClassPred flats
@@ -168,8 +170,8 @@ reportFlat ctxt flats loc
 --      Support code 
 --------------------------------------------
 
-groupErrs :: ([PredType] -> WantedLoc -> TcM ()) -- Deal with one group
-         -> [WantedEvVar]                       -- Unsolved wanteds
+groupErrs :: ([PredType] -> CtOrigin -> TcM ()) -- Deal with one group
+         -> [WantedEvVar]                      -- Unsolved wanteds
           -> TcM ()
 -- Group together insts with the same origin
 -- We want to report them together in error messages
@@ -177,7 +179,8 @@ groupErrs :: ([PredType] -> WantedLoc -> TcM ()) -- Deal with one group
 groupErrs _ [] 
   = return ()
 groupErrs report_err (wanted : wanteds)
-  = do { setCtLoc the_loc $ report_err the_vars the_loc
+  = do { setCtLoc the_loc $ 
+          report_err the_vars (ctLocOrigin the_loc)
        ; groupErrs report_err others }
   where
    the_loc           = wantedEvVarLoc wanted
@@ -193,8 +196,8 @@ groupErrs report_err (wanted : wanteds)
        -- and it avoids need equality on InstLocs.
 
 -- Add the "arising from..." part to a message about bunch of dicts
-addArising :: WantedLoc -> SDoc -> SDoc
-addArising loc msg = msg $$ nest 2 (pprArising loc)
+addArising :: CtOrigin -> SDoc -> SDoc
+addArising orig msg = msg $$ nest 2 (pprArising orig)
 
 pprWithArising :: [WantedEvVar] -> (WantedLoc, SDoc)
 -- Print something like
@@ -204,7 +207,7 @@ pprWithArising :: [WantedEvVar] -> (WantedLoc, SDoc)
 pprWithArising [] 
   = panic "pprWithArising"
 pprWithArising [WantedEvVar ev loc] 
-  = (loc, pprEvVarTheta [ev] <+> pprArising loc)
+  = (loc, pprEvVarTheta [ev] <+> pprArising (ctLocOrigin loc))
 pprWithArising ev_vars
   = (first_loc, vcat (map ppr_one ev_vars))
   where
@@ -255,9 +258,9 @@ getUserGivens (CEC {cec_encl = ctxt})
 %************************************************************************
 
 \begin{code}
-reportIPErrs :: ReportErrCtxt -> [PredType] -> WantedLoc -> TcM ()
-reportIPErrs ctxt ips loc
-  = addErrorReport ctxt $ addArising loc msg
+reportIPErrs :: ReportErrCtxt -> [PredType] -> CtOrigin -> TcM ()
+reportIPErrs ctxt ips orig
+  = addErrorReport ctxt $ addArising orig msg
   where
     msg | Just givens <- getUserGivens ctxt
         = couldNotDeduce givens ips
@@ -274,32 +277,39 @@ reportIPErrs ctxt ips loc
 %************************************************************************
 
 \begin{code}
-reportEqErrs :: ReportErrCtxt -> [PredType] -> WantedLoc -> TcM ()
-reportEqErrs ctxt eqs loc = mapM_ (reportEqErr ctxt loc) eqs 
-
-reportEqErr :: ReportErrCtxt -> WantedLoc -> PredType -> TcM ()
-reportEqErr ctxt loc pred@(EqPred ty1 ty2)
-  | Just tv1 <- tcGetTyVar_maybe ty1 = reportTyVarEqErr ctxt loc tv1 ty2
-  | Just tv2 <- tcGetTyVar_maybe ty2 = reportTyVarEqErr ctxt loc tv2 ty1
+reportEqErrs :: ReportErrCtxt -> [PredType] -> CtOrigin -> TcM ()
+reportEqErrs ctxt eqs orig
+  = mapM_ report_one eqs 
+  where
+    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
+  | Just tv1 <- tcGetTyVar_maybe ty1 = reportTyVarEqErr ctxt tv1 ty2
+  | Just tv2 <- tcGetTyVar_maybe ty2 = reportTyVarEqErr ctxt tv2 ty1
   | otherwise  -- Neither side is a type variable
                -- Since the unsolved constraint is canonical, 
                -- it must therefore be of form (F tys ~ ty)
   = addErrorReport ctxt (msg $$ mkTyFunInfoMsg ty1 ty2)
   where
     msg = case getUserGivens ctxt of
-            Just givens -> couldNotDeduce givens [pred]
+            Just givens -> couldNotDeduce givens [EqPred ty1 ty2]
             Nothing     -> misMatchMsg ty1 ty2
 
-reportEqErr _ _ _ = panic "reportEqErr"          -- Must be equality pred
-
-reportTyVarEqErr :: ReportErrCtxt -> WantedLoc
-                 -> TcTyVar -> TcType -> TcM ()
-reportTyVarEqErr ctxt loc tv1 ty2
+reportTyVarEqErr :: ReportErrCtxt -> TcTyVar -> TcType -> TcM ()
+reportTyVarEqErr ctxt tv1 ty2
   | not is_meta1
   , Just tv2 <- tcGetTyVar_maybe ty2
   , isMetaTyVar tv2
   = -- sk ~ alpha: swap
-    reportTyVarEqErr ctxt loc tv2 ty1
+    reportTyVarEqErr ctxt tv2 ty1
 
   | not is_meta1
   = -- sk ~ ty, where ty isn't a meta-tyvar: mis-match
@@ -398,6 +408,20 @@ typeExtraInfoMsg env ty
   = (env1, pprSkolTvBinding tv1)
   where
 typeExtraInfoMsg env _ty = (env, empty)                -- Normal case
+
+--------------------
+unifyCtxt :: EqOrigin -> TidyEnv -> TcM (TidyEnv, SDoc)
+unifyCtxt (UnifyOrigin { uo_actual = act_ty, uo_expected = exp_ty }) tidy_env
+  = do  { act_ty' <- zonkTcType act_ty
+        ; exp_ty' <- zonkTcType exp_ty
+        ; let (env1, exp_ty'') = tidyOpenType tidy_env exp_ty'
+              (env2, act_ty'') = tidyOpenType env1     act_ty'
+        ; return (env2, mkExpectedActualMsg act_ty'' exp_ty'') }
+
+mkExpectedActualMsg :: Type -> Type -> SDoc
+mkExpectedActualMsg act_ty exp_ty
+  = vcat [ text "Expected type" <> colon <+> ppr exp_ty
+         , text "  Actual type" <> colon <+> ppr act_ty ]
 \end{code}
 
 Note [Non-injective type functions]
@@ -418,8 +442,8 @@ Warn of loopy local equalities that were dropped.
 %************************************************************************
 
 \begin{code}
-reportDictErrs :: ReportErrCtxt -> [PredType] -> WantedLoc -> TcM ()   
-reportDictErrs ctxt wanteds loc
+reportDictErrs :: ReportErrCtxt -> [PredType] -> CtOrigin -> TcM ()    
+reportDictErrs ctxt wanteds orig
   = do { inst_envs <- tcGetInstEnvs
        ; let (others, overlaps) = partitionWith (check_overlap inst_envs) wanteds
        ; unless (null others) $
@@ -442,7 +466,7 @@ reportDictErrs ctxt wanteds loc
 
     mk_overlap_msg pred (matches, unifiers)
       = ASSERT( not (null matches) )
-        vcat [ addArising loc (ptext (sLit "Overlapping instances for") 
+        vcat [ addArising orig (ptext (sLit "Overlapping instances for") 
                                <+> pprPred pred)
             ,  sep [ptext (sLit "Matching instances") <> colon,
                     nest 2 (vcat [pprInstances ispecs, pprInstances unifiers])]
@@ -461,11 +485,11 @@ reportDictErrs ctxt wanteds loc
     mk_no_inst_err :: [PredType] -> SDoc
     mk_no_inst_err wanteds
       | Just givens <- getUserGivens ctxt
-      = vcat [ addArising loc $ couldNotDeduce givens wanteds
+      = vcat [ addArising orig $ couldNotDeduce givens wanteds
             , show_fixes (fix1 : fixes2) ]
 
       | otherwise      -- Top level 
-      = vcat [ addArising loc $
+      = vcat [ addArising orig $
               ptext (sLit "No instance") <> plural wanteds
                    <+> ptext (sLit "for") <+> pprTheta wanteds
             , show_fixes fixes2 ]
@@ -536,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!
@@ -626,24 +650,27 @@ warnDefaulting wanteds default_ty
 %************************************************************************
 
 \begin{code}
-kindErrorTcS :: CtFlavor -> TcType -> TcType -> TcS a
+kindErrorTcS :: CtFlavor -> TcType -> TcType -> TcS ()
+-- If there's a kind error, we don't want to blindly say "kind error"
+-- We might, say, be unifying a skolem 'a' with a type 'Int', 
+-- 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
-       ; failWithTcM (env2, kindErrorMsg ty1' ty2') }
+             ctxt = CEC { cec_encl = []
+                        , cec_extra = extra
+                        , cec_tidy = env2 }
+       ; reportEqErr ctxt ty1' ty2' }
 
 misMatchErrorTcS :: CtFlavor -> TcType -> TcType -> TcS a
 misMatchErrorTcS 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
              (env3, msg)  = misMatchMsgWithExtras env2 ty1' ty2'
-       ; failWithTcM (env3, inaccessible_msg $$ msg) }
+       ; failWithTcM (env3, inaccessible_msg $$ msg $$ extra) }
   where
     inaccessible_msg 
       = case fl of 
@@ -659,21 +686,14 @@ 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:"
 
-setCtFlavorLoc :: CtFlavor -> TcM a -> TcM a
-setCtFlavorLoc (Wanted  loc) thing = setCtLoc loc thing
-setCtFlavorLoc (Derived loc) thing = setCtLoc loc thing
-setCtFlavorLoc (Given loc)   thing = setCtLoc loc thing
-
 solverDepthErrorTcS :: Int -> [CanonicalCt] -> TcS a
 solverDepthErrorTcS depth stack
   | null stack     -- Shouldn't happen unless you say -fcontext-stack=0
@@ -694,7 +714,7 @@ solverDepthErrorTcS depth stack
 
 flattenForAllErrorTcS :: CtFlavor -> TcType -> Bag CanonicalCt -> TcS a
 flattenForAllErrorTcS fl ty _bad_eqs
-  = wrapErrTcS           $ 
+  = wrapErrTcS        $ 
     setCtFlavorLoc fl $ 
     do { env0 <- tcInitTidyEnv
        ; let (env1, ty') = tidyOpenType env0 ty 
@@ -702,3 +722,60 @@ flattenForAllErrorTcS fl ty _bad_eqs
                        , ppr ty' ]
        ; failWithTcM (env1, msg) }
 \end{code}
+
+%************************************************************************
+%*                                                                     *
+                 Setting the context
+%*                                                                     *
+%************************************************************************
+
+\begin{code}
+setCtFlavorLoc :: CtFlavor -> TcM a -> TcM a
+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}