3 % (c) The University of Glasgow 2006
4 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
7 A ``lint'' pass to check for Core correctness
11 -- The above warning supression flag is a temporary kludge.
12 -- While working on this module you are encouraged to remove it and fix
13 -- any warnings in the module. See
14 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
23 #include "HsVersions.h"
53 %************************************************************************
57 %************************************************************************
59 @showPass@ and @endPass@ don't really belong here, but it makes a convenient
60 place for them. They print out stuff before and after core passes,
61 and do Core Lint when necessary.
64 endPass :: DynFlags -> String -> DynFlag -> [CoreBind] -> IO [CoreBind]
65 endPass dflags pass_name dump_flag binds
67 -- Report result size if required
68 -- This has the side effect of forcing the intermediate to be evaluated
69 debugTraceMsg dflags 2 $
70 (text " Result size =" <+> int (coreBindsSize binds))
72 -- Report verbosely, if required
73 dumpIfSet_core dflags dump_flag pass_name (pprCoreBindings binds)
76 lintCoreBindings dflags pass_name binds
82 %************************************************************************
84 \subsection[lintCoreBindings]{@lintCoreBindings@: Top-level interface}
86 %************************************************************************
88 Checks that a set of core bindings is well-formed. The PprStyle and String
89 just control what we print in the event of an error. The Bool value
90 indicates whether we have done any specialisation yet (in which case we do
95 (b) Out-of-scope type variables
96 (c) Out-of-scope local variables
99 If we have done specialisation the we check that there are
100 (a) No top-level bindings of primitive (unboxed type)
105 -- Things are *not* OK if:
107 -- * Unsaturated type app before specialisation has been done;
109 -- * Oversaturated type app after specialisation (eta reduction
110 -- may well be happening...);
115 In the desugarer, it's very very convenient to be able to say (in effect)
116 let a = Int in <body>
117 That is, use a type let. (See notes just below for why we want this.)
119 We don't have type lets in Core, so the desugarer uses type lambda
121 However, in the lambda form, we'd get lint errors from:
122 (/\a. let x::a = 4 in <body>) Int
123 because (x::a) doesn't look compatible with (4::Int).
125 So (HACK ALERT) the Lint phase does type-beta reduction "on the fly",
126 as it were. It carries a type substitution (in this example [a -> Int])
127 and applies this substitution before comparing types. The functin
128 lintTy :: Type -> LintM Type
129 returns a substituted type; that's the only reason it returns anything.
131 When we encounter a binder (like x::a) we must apply the substitution
132 to the type of the binding variable. lintBinders does this.
134 For Ids, the type-substituted Id is added to the in_scope set (which
135 itself is part of the TvSubst we are carrying down), and when we
136 find an occurence of an Id, we fetch it from the in-scope set.
141 It's needed when dealing with desugarer output for GADTs. Consider
142 data T = forall a. T a (a->Int) Bool
144 f (T x f True) = <e1>
145 f (T y g False) = <e2>
146 After desugaring we get
148 T a (x::a) (f::a->Int) (b:Bool) ->
151 False -> (/\b. let y=x; g=f in <e2>) a
152 And for a reason I now forget, the ...<e2>... can mention a; so
153 we want Lint to know that b=a. Ugh.
155 I tried quite hard to make the necessity for this go away, by changing the
156 desugarer, but the fundamental problem is this:
158 T a (x::a) (y::Int) -> let fail::a = ...
159 in (/\b. ...(case ... of
163 Now the inner case look as though it has incompatible branches.
167 lintCoreBindings :: DynFlags -> String -> [CoreBind] -> IO ()
169 lintCoreBindings dflags whoDunnit binds
170 | not (dopt Opt_DoCoreLinting dflags)
173 lintCoreBindings dflags whoDunnit binds
174 = case (initL (lint_binds binds)) of
175 Nothing -> showPass dflags ("Core Linted result of " ++ whoDunnit)
176 Just bad_news -> printDump (display bad_news) >>
179 -- Put all the top-level binders in scope at the start
180 -- This is because transformation rules can bring something
181 -- into use 'unexpectedly'
182 lint_binds binds = addLoc TopLevelBindings $
183 addInScopeVars (bindersOfBinds binds) $
186 lint_bind (Rec prs) = mapM_ (lintSingleBinding TopLevel Recursive) prs
187 lint_bind (NonRec bndr rhs) = lintSingleBinding TopLevel NonRecursive (bndr,rhs)
190 = vcat [ text ("*** Core Lint Errors: in result of " ++ whoDunnit ++ " ***"),
192 ptext SLIT("*** Offending Program ***"),
193 pprCoreBindings binds,
194 ptext SLIT("*** End of Offense ***")
198 %************************************************************************
200 \subsection[lintUnfolding]{lintUnfolding}
202 %************************************************************************
204 We use this to check all unfoldings that come in from interfaces
205 (it is very painful to catch errors otherwise):
208 lintUnfolding :: SrcLoc
209 -> [Var] -- Treat these as in scope
211 -> Maybe Message -- Nothing => OK
213 lintUnfolding locn vars expr
214 = initL (addLoc (ImportedUnfolding locn) $
215 addInScopeVars vars $
219 %************************************************************************
221 \subsection[lintCoreBinding]{lintCoreBinding}
223 %************************************************************************
225 Check a core binding, returning the list of variables bound.
228 lintSingleBinding top_lvl_flag rec_flag (binder,rhs)
229 = addLoc (RhsOf binder) $
231 do { ty <- lintCoreExpr rhs
232 ; lintBinder binder -- Check match to RHS type
233 ; binder_ty <- applySubst binder_ty
234 ; checkTys binder_ty ty (mkRhsMsg binder ty)
235 -- Check (not isUnLiftedType) (also checks for bogus unboxed tuples)
236 ; checkL (not (isUnLiftedType binder_ty)
237 || (isNonRec rec_flag && exprOkForSpeculation rhs))
238 (mkRhsPrimMsg binder rhs)
239 -- Check that if the binder is top-level or recursive, it's not demanded
240 ; checkL (not (isStrictId binder)
241 || (isNonRec rec_flag && not (isTopLevel top_lvl_flag)))
243 -- Check whether binder's specialisations contain any out-of-scope variables
244 ; mapM_ (checkBndrIdInScope binder) bndr_vars
246 -- Check whether arity and demand type are consistent (only if demand analysis
248 ; checkL (case maybeDmdTy of
249 Just (StrictSig dmd_ty) -> idArity binder >= dmdTypeDepth dmd_ty || exprIsTrivial rhs
251 (mkArityMsg binder) }
253 -- We should check the unfolding, if any, but this is tricky because
254 -- the unfolding is a SimplifiableCoreExpr. Give up for now.
256 binder_ty = idType binder
257 maybeDmdTy = idNewStrictness_maybe binder
258 bndr_vars = varSetElems (idFreeVars binder)
259 lintBinder var | isId var = lintIdBndr var $ \_ -> (return ())
260 | otherwise = return ()
263 %************************************************************************
265 \subsection[lintCoreExpr]{lintCoreExpr}
267 %************************************************************************
270 type InType = Type -- Substitution not yet applied
271 type OutType = Type -- Substitution has been applied to this
273 lintCoreExpr :: CoreExpr -> LintM OutType
274 -- The returned type has the substitution from the monad
275 -- already applied to it:
276 -- lintCoreExpr e subst = exprType (subst e)
278 lintCoreExpr (Var var)
279 = do { checkL (not (var == oneTupleDataConId))
280 (ptext SLIT("Illegal one-tuple"))
281 ; var' <- lookupIdInScope var
282 ; return (idType var')
285 lintCoreExpr (Lit lit)
286 = return (literalType lit)
288 --lintCoreExpr (Note (Coerce to_ty from_ty) expr)
289 -- = do { expr_ty <- lintCoreExpr expr
290 -- ; to_ty <- lintTy to_ty
291 -- ; from_ty <- lintTy from_ty
292 -- ; checkTys from_ty expr_ty (mkCoerceErr from_ty expr_ty)
295 lintCoreExpr (Cast expr co)
296 = do { expr_ty <- lintCoreExpr expr
298 ; let (from_ty, to_ty) = coercionKind co'
299 ; checkTys from_ty expr_ty (mkCastErr from_ty expr_ty)
302 lintCoreExpr (Note other_note expr)
305 lintCoreExpr (Let (NonRec bndr rhs) body)
306 = do { lintSingleBinding NotTopLevel NonRecursive (bndr,rhs)
307 ; addLoc (BodyOfLetRec [bndr])
308 (lintAndScopeId bndr $ \_ -> (lintCoreExpr body)) }
310 lintCoreExpr (Let (Rec pairs) body)
311 = lintAndScopeIds bndrs $ \_ ->
312 do { mapM (lintSingleBinding NotTopLevel Recursive) pairs
313 ; addLoc (BodyOfLetRec bndrs) (lintCoreExpr body) }
315 bndrs = map fst pairs
317 lintCoreExpr e@(App fun (Type ty))
318 -- See Note [Type let] above
319 = addLoc (AnExpr e) $
322 go (App fun (Type ty)) tys
323 = do { go fun (ty:tys) }
324 go (Lam tv body) (ty:tys)
325 = do { checkL (isTyVar tv) (mkKindErrMsg tv ty) -- Not quite accurate
327 ; let kind = tyVarKind tv
328 ; kind' <- lintTy kind
329 ; let tv' = setTyVarKind tv kind'
331 -- Now extend the substitution so we
332 -- take advantage of it in the body
333 ; addInScopeVars [tv'] $
334 extendSubstL tv' ty' $
337 = do { fun_ty <- lintCoreExpr fun
338 ; lintCoreArgs fun_ty (map Type tys) }
340 lintCoreExpr e@(App fun arg)
341 = do { fun_ty <- lintCoreExpr fun
342 ; addLoc (AnExpr e) $
343 lintCoreArg fun_ty arg }
345 lintCoreExpr (Lam var expr)
346 = addLoc (LambdaBodyOf var) $
347 lintBinders [var] $ \[var'] ->
348 do { body_ty <- lintCoreExpr expr
350 return (mkFunTy (idType var') body_ty)
352 return (mkForAllTy var' body_ty)
354 -- The applySubst is needed to apply the subst to var
356 lintCoreExpr e@(Case scrut var alt_ty alts) =
357 -- Check the scrutinee
358 do { scrut_ty <- lintCoreExpr scrut
359 ; alt_ty <- lintTy alt_ty
360 ; var_ty <- lintTy (idType var)
361 -- Don't use lintIdBndr on var, because unboxed tuple is legitimate
363 ; subst <- getTvSubst
364 ; checkTys var_ty scrut_ty (mkScrutMsg var var_ty scrut_ty subst)
366 -- If the binder is an unboxed tuple type, don't put it in scope
367 ; let scope = if (isUnboxedTupleType (idType var)) then
369 else lintAndScopeId var
371 do { -- Check the alternatives
372 mapM (lintCoreAlt scrut_ty alt_ty) alts
373 ; checkCaseAlts e scrut_ty alts
378 lintCoreExpr e@(Type ty)
379 = addErrL (mkStrangeTyMsg e)
382 %************************************************************************
384 \subsection[lintCoreArgs]{lintCoreArgs}
386 %************************************************************************
388 The basic version of these functions checks that the argument is a
389 subtype of the required type, as one would expect.
392 lintCoreArgs :: OutType -> [CoreArg] -> LintM OutType
393 lintCoreArg :: OutType -> CoreArg -> LintM OutType
394 -- First argument has already had substitution applied to it
398 lintCoreArgs ty [] = return ty
399 lintCoreArgs ty (a : args) =
400 do { res <- lintCoreArg ty a
401 ; lintCoreArgs res args }
403 lintCoreArg fun_ty a@(Type arg_ty) =
404 do { arg_ty <- lintTy arg_ty
405 ; lintTyApp fun_ty arg_ty }
407 lintCoreArg fun_ty arg =
408 -- Make sure function type matches argument
409 do { arg_ty <- lintCoreExpr arg
410 ; let err1 = mkAppMsg fun_ty arg_ty arg
411 err2 = mkNonFunAppMsg fun_ty arg_ty arg
412 ; case splitFunTy_maybe fun_ty of
414 do { checkTys arg arg_ty err1
420 -- Both args have had substitution applied
421 lintTyApp :: OutType -> OutType -> LintM OutType
423 = case splitForAllTy_maybe ty of
424 Nothing -> addErrL (mkTyAppMsg ty arg_ty)
427 -> do { checkL (isTyVar tyvar) (mkTyAppMsg ty arg_ty)
428 ; checkKinds tyvar arg_ty
429 ; return (substTyWith [tyvar] [arg_ty] body) }
431 checkKinds tyvar arg_ty
432 -- Arg type might be boxed for a function with an uncommitted
433 -- tyvar; notably this is used so that we can give
434 -- error :: forall a:*. String -> a
435 -- and then apply it to both boxed and unboxed types.
436 = checkL (arg_kind `isSubKind` tyvar_kind)
437 (mkKindErrMsg tyvar arg_ty)
439 tyvar_kind = tyVarKind tyvar
440 arg_kind | isCoVar tyvar = coercionKindPredTy arg_ty
441 | otherwise = typeKind arg_ty
445 %************************************************************************
447 \subsection[lintCoreAlts]{lintCoreAlts}
449 %************************************************************************
452 checkCaseAlts :: CoreExpr -> OutType -> [CoreAlt] -> LintM ()
453 -- a) Check that the alts are non-empty
454 -- b1) Check that the DEFAULT comes first, if it exists
455 -- b2) Check that the others are in increasing order
456 -- c) Check that there's a default for infinite types
457 -- NB: Algebraic cases are not necessarily exhaustive, because
458 -- the simplifer correctly eliminates case that can't
461 checkCaseAlts e ty []
462 = addErrL (mkNullAltsMsg e)
464 checkCaseAlts e ty alts =
465 do { checkL (all non_deflt con_alts) (mkNonDefltMsg e)
466 ; checkL (increasing_tag con_alts) (mkNonIncreasingAltsMsg e)
467 ; checkL (isJust maybe_deflt || not is_infinite_ty)
468 (nonExhaustiveAltsMsg e) }
470 (con_alts, maybe_deflt) = findDefault alts
472 -- Check that successive alternatives have increasing tags
473 increasing_tag (alt1 : rest@( alt2 : _)) = alt1 `ltAlt` alt2 && increasing_tag rest
474 increasing_tag other = True
476 non_deflt (DEFAULT, _, _) = False
479 is_infinite_ty = case splitTyConApp_maybe ty of
481 Just (tycon, tycon_arg_tys) -> isPrimTyCon tycon
485 checkAltExpr :: CoreExpr -> OutType -> LintM ()
486 checkAltExpr expr ann_ty
487 = do { actual_ty <- lintCoreExpr expr
488 ; checkTys actual_ty ann_ty (mkCaseAltMsg expr actual_ty ann_ty) }
490 lintCoreAlt :: OutType -- Type of scrutinee
491 -> OutType -- Type of the alternative
495 lintCoreAlt scrut_ty alt_ty alt@(DEFAULT, args, rhs) =
496 do { checkL (null args) (mkDefaultArgsMsg args)
497 ; checkAltExpr rhs alt_ty }
499 lintCoreAlt scrut_ty alt_ty alt@(LitAlt lit, args, rhs) =
500 do { checkL (null args) (mkDefaultArgsMsg args)
501 ; checkTys lit_ty scrut_ty (mkBadPatMsg lit_ty scrut_ty)
502 ; checkAltExpr rhs alt_ty }
504 lit_ty = literalType lit
506 lintCoreAlt scrut_ty alt_ty alt@(DataAlt con, args, rhs)
507 | isNewTyCon (dataConTyCon con) = addErrL (mkNewTyDataConAltMsg scrut_ty alt)
508 | Just (tycon, tycon_arg_tys) <- splitTyConApp_maybe scrut_ty
509 = addLoc (CaseAlt alt) $ do
510 { -- First instantiate the universally quantified
511 -- type variables of the data constructor
512 -- We've already check
513 checkL (tycon == dataConTyCon con) (mkBadConMsg tycon con)
514 ; let con_payload_ty = applyTys (dataConRepType con) tycon_arg_tys
516 -- And now bring the new binders into scope
517 ; lintBinders args $ \ args -> do
518 { addLoc (CasePat alt) $ do
519 { -- Check the pattern
520 -- Scrutinee type must be a tycon applicn; checked by caller
521 -- This code is remarkably compact considering what it does!
522 -- NB: args must be in scope here so that the lintCoreArgs line works.
523 -- NB: relies on existential type args coming *after* ordinary type args
525 ; con_result_ty <- lintCoreArgs con_payload_ty (varsToCoreExprs args)
526 ; checkTys con_result_ty scrut_ty (mkBadPatMsg con_result_ty scrut_ty)
529 ; checkAltExpr rhs alt_ty } }
531 | otherwise -- Scrut-ty is wrong shape
532 = addErrL (mkBadAltMsg scrut_ty alt)
535 %************************************************************************
537 \subsection[lint-types]{Types}
539 %************************************************************************
542 -- When we lint binders, we (one at a time and in order):
543 -- 1. Lint var types or kinds (possibly substituting)
544 -- 2. Add the binder to the in scope set, and if its a coercion var,
545 -- we may extend the substitution to reflect its (possibly) new kind
546 lintBinders :: [Var] -> ([Var] -> LintM a) -> LintM a
547 lintBinders [] linterF = linterF []
548 lintBinders (var:vars) linterF = lintBinder var $ \var' ->
549 lintBinders vars $ \ vars' ->
552 lintBinder :: Var -> (Var -> LintM a) -> LintM a
553 lintBinder var linterF
554 | isTyVar var = lint_ty_bndr
555 | otherwise = lintIdBndr var linterF
557 lint_ty_bndr = do { lintTy (tyVarKind var)
558 ; subst <- getTvSubst
559 ; let (subst', tv') = substTyVarBndr subst var
560 ; updateTvSubst subst' (linterF tv') }
562 lintIdBndr :: Var -> (Var -> LintM a) -> LintM a
563 -- Do substitution on the type of a binder and add the var with this
564 -- new type to the in-scope set of the second argument
565 -- ToDo: lint its rules
566 lintIdBndr id linterF
567 = do { checkL (not (isUnboxedTupleType (idType id)))
568 (mkUnboxedTupleMsg id)
569 -- No variable can be bound to an unboxed tuple.
570 ; lintAndScopeId id $ \id' -> linterF id'
573 lintAndScopeIds :: [Var] -> ([Var] -> LintM a) -> LintM a
574 lintAndScopeIds ids linterF
578 go (id:ids) = do { lintAndScopeId id $ \id ->
579 lintAndScopeIds ids $ \ids ->
582 lintAndScopeId :: Var -> (Var -> LintM a) -> LintM a
583 lintAndScopeId id linterF
584 = do { ty <- lintTy (idType id)
585 ; let id' = Var.setIdType id ty
586 ; addInScopeVars [id'] $ (linterF id')
589 lintTy :: InType -> LintM OutType
590 -- Check the type, and apply the substitution to it
591 -- ToDo: check the kind structure of the type
593 = do { ty' <- applySubst ty
594 ; mapM_ checkTyVarInScope (varSetElems (tyVarsOfType ty'))
599 %************************************************************************
601 \subsection[lint-monad]{The Lint monad}
603 %************************************************************************
608 [LintLocInfo] -> -- Locations
609 TvSubst -> -- Current type substitution; we also use this
610 -- to keep track of all the variables in scope,
611 -- both Ids and TyVars
612 Bag Message -> -- Error messages so far
613 (Maybe a, Bag Message) } -- Result and error messages (if any)
615 {- Note [Type substitution]
616 ~~~~~~~~~~~~~~~~~~~~~~~~
617 Why do we need a type substitution? Consider
618 /\(a:*). \(x:a). /\(a:*). id a x
619 This is ill typed, because (renaming variables) it is really
620 /\(a:*). \(x:a). /\(b:*). id b x
621 Hence, when checking an application, we can't naively compare x's type
622 (at its binding site) with its expected type (at a use site). So we
623 rename type binders as we go, maintaining a substitution.
625 The same substitution also supports let-type, current expressed as
627 Here we substitute 'ty' for 'a' in 'body', on the fly.
630 instance Monad LintM where
631 return x = LintM (\ loc subst errs -> (Just x, errs))
632 fail err = LintM (\ loc subst errs -> (Nothing, addErr subst errs (text err) loc))
633 m >>= k = LintM (\ loc subst errs ->
634 let (res, errs') = unLintM m loc subst errs in
636 Just r -> unLintM (k r) loc subst errs'
637 Nothing -> (Nothing, errs'))
640 = RhsOf Id -- The variable bound
641 | LambdaBodyOf Id -- The lambda-binder
642 | BodyOfLetRec [Id] -- One of the binders
643 | CaseAlt CoreAlt -- Case alternative
644 | CasePat CoreAlt -- *Pattern* of the case alternative
645 | AnExpr CoreExpr -- Some expression
646 | ImportedUnfolding SrcLoc -- Some imported unfolding (ToDo: say which)
652 initL :: LintM a -> Maybe Message {- errors -}
654 = case unLintM m [] emptyTvSubst emptyBag of
655 (_, errs) | isEmptyBag errs -> Nothing
656 | otherwise -> Just (vcat (punctuate (text "") (bagToList errs)))
660 checkL :: Bool -> Message -> LintM ()
661 checkL True msg = return ()
662 checkL False msg = addErrL msg
664 addErrL :: Message -> LintM a
665 addErrL msg = LintM (\ loc subst errs -> (Nothing, addErr subst errs msg loc))
667 addErr :: TvSubst -> Bag Message -> Message -> [LintLocInfo] -> Bag Message
668 addErr subst errs_so_far msg locs
669 = ASSERT( notNull locs )
670 errs_so_far `snocBag` mk_msg msg
672 (loc, cxt1) = dumpLoc (head locs)
673 cxts = [snd (dumpLoc loc) | loc <- locs]
674 context | opt_PprStyle_Debug = vcat (reverse cxts) $$ cxt1 $$
675 ptext SLIT("Substitution:") <+> ppr subst
678 mk_msg msg = mkLocMessage (mkSrcSpan loc loc) (context $$ msg)
680 addLoc :: LintLocInfo -> LintM a -> LintM a
682 LintM (\ loc subst errs -> unLintM m (extra_loc:loc) subst errs)
684 addInScopeVars :: [Var] -> LintM a -> LintM a
685 addInScopeVars vars m
687 = LintM (\ loc subst errs -> unLintM m loc (extendTvInScope subst vars) errs)
689 = addErrL (dupVars dups)
691 (_, dups) = removeDups compare vars
693 updateTvSubst :: TvSubst -> LintM a -> LintM a
694 updateTvSubst subst' m =
695 LintM (\ loc subst errs -> unLintM m loc subst' errs)
697 getTvSubst :: LintM TvSubst
698 getTvSubst = LintM (\ loc subst errs -> (Just subst, errs))
700 applySubst :: Type -> LintM Type
701 applySubst ty = do { subst <- getTvSubst; return (substTy subst ty) }
703 extendSubstL :: TyVar -> Type -> LintM a -> LintM a
705 = LintM (\ loc subst errs -> unLintM m loc (extendTvSubst subst tv ty) errs)
709 lookupIdInScope :: Id -> LintM Id
711 | not (mustHaveLocalBinding id)
712 = return id -- An imported Id
714 = do { subst <- getTvSubst
715 ; case lookupInScope (getTvInScope subst) id of
717 Nothing -> do { addErrL out_of_scope
720 out_of_scope = ppr id <+> ptext SLIT("is out of scope")
723 oneTupleDataConId :: Id -- Should not happen
724 oneTupleDataConId = dataConWorkId (tupleCon Boxed 1)
726 checkBndrIdInScope :: Var -> Var -> LintM ()
727 checkBndrIdInScope binder id
728 = checkInScope msg id
730 msg = ptext SLIT("is out of scope inside info for") <+>
733 checkTyVarInScope :: TyVar -> LintM ()
734 checkTyVarInScope tv = checkInScope (ptext SLIT("is out of scope")) tv
736 checkInScope :: SDoc -> Var -> LintM ()
737 checkInScope loc_msg var =
738 do { subst <- getTvSubst
739 ; checkL (not (mustHaveLocalBinding var) || (var `isInScope` subst))
740 (hsep [ppr var, loc_msg]) }
742 checkTys :: Type -> Type -> Message -> LintM ()
743 -- check ty2 is subtype of ty1 (ie, has same structure but usage
744 -- annotations need only be consistent, not equal)
745 -- Assumes ty1,ty2 are have alrady had the substitution applied
746 checkTys ty1 ty2 msg = checkL (ty1 `coreEqType` ty2) msg
749 %************************************************************************
751 \subsection{Error messages}
753 %************************************************************************
757 = (getSrcLoc v, brackets (ptext SLIT("RHS of") <+> pp_binders [v]))
759 dumpLoc (LambdaBodyOf b)
760 = (getSrcLoc b, brackets (ptext SLIT("in body of lambda with binder") <+> pp_binder b))
762 dumpLoc (BodyOfLetRec [])
763 = (noSrcLoc, brackets (ptext SLIT("In body of a letrec with no binders")))
765 dumpLoc (BodyOfLetRec bs@(_:_))
766 = ( getSrcLoc (head bs), brackets (ptext SLIT("in body of letrec with binders") <+> pp_binders bs))
769 = (noSrcLoc, text "In the expression:" <+> ppr e)
771 dumpLoc (CaseAlt (con, args, rhs))
772 = (noSrcLoc, text "In a case alternative:" <+> parens (ppr con <+> pp_binders args))
774 dumpLoc (CasePat (con, args, rhs))
775 = (noSrcLoc, text "In the pattern of a case alternative:" <+> parens (ppr con <+> pp_binders args))
777 dumpLoc (ImportedUnfolding locn)
778 = (locn, brackets (ptext SLIT("in an imported unfolding")))
779 dumpLoc TopLevelBindings
782 pp_binders :: [Var] -> SDoc
783 pp_binders bs = sep (punctuate comma (map pp_binder bs))
785 pp_binder :: Var -> SDoc
786 pp_binder b | isId b = hsep [ppr b, dcolon, ppr (idType b)]
787 | isTyVar b = hsep [ppr b, dcolon, ppr (tyVarKind b)]
791 ------------------------------------------------------
792 -- Messages for case expressions
794 mkNullAltsMsg :: CoreExpr -> Message
796 = hang (text "Case expression with no alternatives:")
799 mkDefaultArgsMsg :: [Var] -> Message
800 mkDefaultArgsMsg args
801 = hang (text "DEFAULT case with binders")
804 mkCaseAltMsg :: CoreExpr -> Type -> Type -> Message
805 mkCaseAltMsg e ty1 ty2
806 = hang (text "Type of case alternatives not the same as the annotation on case:")
807 4 (vcat [ppr ty1, ppr ty2, ppr e])
809 mkScrutMsg :: Id -> Type -> Type -> TvSubst -> Message
810 mkScrutMsg var var_ty scrut_ty subst
811 = vcat [text "Result binder in case doesn't match scrutinee:" <+> ppr var,
812 text "Result binder type:" <+> ppr var_ty,--(idType var),
813 text "Scrutinee type:" <+> ppr scrut_ty,
814 hsep [ptext SLIT("Current TV subst"), ppr subst]]
817 = hang (text "Case expression with DEFAULT not at the beginnning") 4 (ppr e)
818 mkNonIncreasingAltsMsg e
819 = hang (text "Case expression with badly-ordered alternatives") 4 (ppr e)
821 nonExhaustiveAltsMsg :: CoreExpr -> Message
822 nonExhaustiveAltsMsg e
823 = hang (text "Case expression with non-exhaustive alternatives") 4 (ppr e)
825 mkBadConMsg :: TyCon -> DataCon -> Message
826 mkBadConMsg tycon datacon
828 text "In a case alternative, data constructor isn't in scrutinee type:",
829 text "Scrutinee type constructor:" <+> ppr tycon,
830 text "Data con:" <+> ppr datacon
833 mkBadPatMsg :: Type -> Type -> Message
834 mkBadPatMsg con_result_ty scrut_ty
836 text "In a case alternative, pattern result type doesn't match scrutinee type:",
837 text "Pattern result type:" <+> ppr con_result_ty,
838 text "Scrutinee type:" <+> ppr scrut_ty
841 mkBadAltMsg :: Type -> CoreAlt -> Message
842 mkBadAltMsg scrut_ty alt
843 = vcat [ text "Data alternative when scrutinee is not a tycon application",
844 text "Scrutinee type:" <+> ppr scrut_ty,
845 text "Alternative:" <+> pprCoreAlt alt ]
847 mkNewTyDataConAltMsg :: Type -> CoreAlt -> Message
848 mkNewTyDataConAltMsg scrut_ty alt
849 = vcat [ text "Data alternative for newtype datacon",
850 text "Scrutinee type:" <+> ppr scrut_ty,
851 text "Alternative:" <+> pprCoreAlt alt ]
854 ------------------------------------------------------
855 -- Other error messages
857 mkAppMsg :: Type -> Type -> CoreExpr -> Message
858 mkAppMsg fun_ty arg_ty arg
859 = vcat [ptext SLIT("Argument value doesn't match argument type:"),
860 hang (ptext SLIT("Fun type:")) 4 (ppr fun_ty),
861 hang (ptext SLIT("Arg type:")) 4 (ppr arg_ty),
862 hang (ptext SLIT("Arg:")) 4 (ppr arg)]
864 mkNonFunAppMsg :: Type -> Type -> CoreExpr -> Message
865 mkNonFunAppMsg fun_ty arg_ty arg
866 = vcat [ptext SLIT("Non-function type in function position"),
867 hang (ptext SLIT("Fun type:")) 4 (ppr fun_ty),
868 hang (ptext SLIT("Arg type:")) 4 (ppr arg_ty),
869 hang (ptext SLIT("Arg:")) 4 (ppr arg)]
871 mkKindErrMsg :: TyVar -> Type -> Message
872 mkKindErrMsg tyvar arg_ty
873 = vcat [ptext SLIT("Kinds don't match in type application:"),
874 hang (ptext SLIT("Type variable:"))
875 4 (ppr tyvar <+> dcolon <+> ppr (tyVarKind tyvar)),
876 hang (ptext SLIT("Arg type:"))
877 4 (ppr arg_ty <+> dcolon <+> ppr (typeKind arg_ty))]
879 mkTyAppMsg :: Type -> Type -> Message
881 = vcat [text "Illegal type application:",
882 hang (ptext SLIT("Exp type:"))
883 4 (ppr ty <+> dcolon <+> ppr (typeKind ty)),
884 hang (ptext SLIT("Arg type:"))
885 4 (ppr arg_ty <+> dcolon <+> ppr (typeKind arg_ty))]
887 mkRhsMsg :: Id -> Type -> Message
890 [hsep [ptext SLIT("The type of this binder doesn't match the type of its RHS:"),
892 hsep [ptext SLIT("Binder's type:"), ppr (idType binder)],
893 hsep [ptext SLIT("Rhs type:"), ppr ty]]
895 mkRhsPrimMsg :: Id -> CoreExpr -> Message
896 mkRhsPrimMsg binder rhs
897 = vcat [hsep [ptext SLIT("The type of this binder is primitive:"),
899 hsep [ptext SLIT("Binder's type:"), ppr (idType binder)]
902 mkStrictMsg :: Id -> Message
904 = vcat [hsep [ptext SLIT("Recursive or top-level binder has strict demand info:"),
906 hsep [ptext SLIT("Binder's demand info:"), ppr (idNewDemandInfo binder)]
909 mkArityMsg :: Id -> Message
911 = vcat [hsep [ptext SLIT("Demand type has "),
912 ppr (dmdTypeDepth dmd_ty),
913 ptext SLIT(" arguments, rhs has "),
914 ppr (idArity binder),
915 ptext SLIT("arguments, "),
917 hsep [ptext SLIT("Binder's strictness signature:"), ppr dmd_ty]
920 where (StrictSig dmd_ty) = idNewStrictness binder
922 mkUnboxedTupleMsg :: Id -> Message
923 mkUnboxedTupleMsg binder
924 = vcat [hsep [ptext SLIT("A variable has unboxed tuple type:"), ppr binder],
925 hsep [ptext SLIT("Binder's type:"), ppr (idType binder)]]
927 mkCastErr from_ty expr_ty
928 = vcat [ptext SLIT("From-type of Cast differs from type of enclosed expression"),
929 ptext SLIT("From-type:") <+> ppr from_ty,
930 ptext SLIT("Type of enclosed expr:") <+> ppr expr_ty
934 = hang (ptext SLIT("Duplicate variables brought into scope"))
938 = ptext SLIT("Type where expression expected:") <+> ppr e