2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
6 A ``lint'' pass to check for Core correctness
10 -- The above warning supression flag is a temporary kludge.
11 -- While working on this module you are encouraged to remove it and fix
12 -- any warnings in the module. See
13 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
22 #include "HsVersions.h"
51 %************************************************************************
55 %************************************************************************
57 @showPass@ and @endPass@ don't really belong here, but it makes a convenient
58 place for them. They print out stuff before and after core passes,
59 and do Core Lint when necessary.
62 endPass :: DynFlags -> String -> DynFlag -> [CoreBind] -> IO [CoreBind]
63 endPass dflags pass_name dump_flag binds
65 -- Report result size if required
66 -- This has the side effect of forcing the intermediate to be evaluated
67 debugTraceMsg dflags 2 $
68 (text " Result size =" <+> int (coreBindsSize binds))
70 -- Report verbosely, if required
71 dumpIfSet_core dflags dump_flag pass_name (pprCoreBindings binds)
74 lintCoreBindings dflags pass_name binds
80 %************************************************************************
82 \subsection[lintCoreBindings]{@lintCoreBindings@: Top-level interface}
84 %************************************************************************
86 Checks that a set of core bindings is well-formed. The PprStyle and String
87 just control what we print in the event of an error. The Bool value
88 indicates whether we have done any specialisation yet (in which case we do
93 (b) Out-of-scope type variables
94 (c) Out-of-scope local variables
97 If we have done specialisation the we check that there are
98 (a) No top-level bindings of primitive (unboxed type)
103 -- Things are *not* OK if:
105 -- * Unsaturated type app before specialisation has been done;
107 -- * Oversaturated type app after specialisation (eta reduction
108 -- may well be happening...);
113 In the desugarer, it's very very convenient to be able to say (in effect)
114 let a = Int in <body>
115 That is, use a type let. (See notes just below for why we want this.)
117 We don't have type lets in Core, so the desugarer uses type lambda
119 However, in the lambda form, we'd get lint errors from:
120 (/\a. let x::a = 4 in <body>) Int
121 because (x::a) doesn't look compatible with (4::Int).
123 So (HACK ALERT) the Lint phase does type-beta reduction "on the fly",
124 as it were. It carries a type substitution (in this example [a -> Int])
125 and applies this substitution before comparing types. The functin
126 lintTy :: Type -> LintM Type
127 returns a substituted type; that's the only reason it returns anything.
129 When we encounter a binder (like x::a) we must apply the substitution
130 to the type of the binding variable. lintBinders does this.
132 For Ids, the type-substituted Id is added to the in_scope set (which
133 itself is part of the TvSubst we are carrying down), and when we
134 find an occurence of an Id, we fetch it from the in-scope set.
139 It's needed when dealing with desugarer output for GADTs. Consider
140 data T = forall a. T a (a->Int) Bool
142 f (T x f True) = <e1>
143 f (T y g False) = <e2>
144 After desugaring we get
146 T a (x::a) (f::a->Int) (b:Bool) ->
149 False -> (/\b. let y=x; g=f in <e2>) a
150 And for a reason I now forget, the ...<e2>... can mention a; so
151 we want Lint to know that b=a. Ugh.
153 I tried quite hard to make the necessity for this go away, by changing the
154 desugarer, but the fundamental problem is this:
156 T a (x::a) (y::Int) -> let fail::a = ...
157 in (/\b. ...(case ... of
161 Now the inner case look as though it has incompatible branches.
165 lintCoreBindings :: DynFlags -> String -> [CoreBind] -> IO ()
167 lintCoreBindings dflags whoDunnit binds
168 | not (dopt Opt_DoCoreLinting dflags)
171 lintCoreBindings dflags whoDunnit binds
172 = case (initL (lint_binds binds)) of
173 Nothing -> showPass dflags ("Core Linted result of " ++ whoDunnit)
174 Just bad_news -> printDump (display bad_news) >>
177 -- Put all the top-level binders in scope at the start
178 -- This is because transformation rules can bring something
179 -- into use 'unexpectedly'
180 lint_binds binds = addInScopeVars (bindersOfBinds binds) $
183 lint_bind (Rec prs) = mapM_ (lintSingleBinding TopLevel Recursive) prs
184 lint_bind (NonRec bndr rhs) = lintSingleBinding TopLevel NonRecursive (bndr,rhs)
187 = vcat [ text ("*** Core Lint Errors: in result of " ++ whoDunnit ++ " ***"),
189 ptext SLIT("*** Offending Program ***"),
190 pprCoreBindings binds,
191 ptext SLIT("*** End of Offense ***")
195 %************************************************************************
197 \subsection[lintUnfolding]{lintUnfolding}
199 %************************************************************************
201 We use this to check all unfoldings that come in from interfaces
202 (it is very painful to catch errors otherwise):
205 lintUnfolding :: SrcLoc
206 -> [Var] -- Treat these as in scope
208 -> Maybe Message -- Nothing => OK
210 lintUnfolding locn vars expr
211 = initL (addLoc (ImportedUnfolding locn) $
212 addInScopeVars vars $
216 %************************************************************************
218 \subsection[lintCoreBinding]{lintCoreBinding}
220 %************************************************************************
222 Check a core binding, returning the list of variables bound.
225 lintSingleBinding top_lvl_flag rec_flag (binder,rhs)
226 = addLoc (RhsOf binder) $
228 do { ty <- lintCoreExpr rhs
229 ; lintBinder binder -- Check match to RHS type
230 ; binder_ty <- applySubst binder_ty
231 ; checkTys binder_ty ty (mkRhsMsg binder ty)
232 -- Check (not isUnLiftedType) (also checks for bogus unboxed tuples)
233 ; checkL (not (isUnLiftedType binder_ty)
234 || (isNonRec rec_flag && exprOkForSpeculation rhs))
235 (mkRhsPrimMsg binder rhs)
236 -- Check that if the binder is top-level or recursive, it's not demanded
237 ; checkL (not (isStrictId binder)
238 || (isNonRec rec_flag && not (isTopLevel top_lvl_flag)))
240 -- Check whether binder's specialisations contain any out-of-scope variables
241 ; mapM_ (checkBndrIdInScope binder) bndr_vars
243 -- Check whether arity and demand type are consistent (only if demand analysis
245 ; checkL (case maybeDmdTy of
246 Just (StrictSig dmd_ty) -> idArity binder >= dmdTypeDepth dmd_ty || exprIsTrivial rhs
248 (mkArityMsg binder) }
250 -- We should check the unfolding, if any, but this is tricky because
251 -- the unfolding is a SimplifiableCoreExpr. Give up for now.
253 binder_ty = idType binder
254 maybeDmdTy = idNewStrictness_maybe binder
255 bndr_vars = varSetElems (idFreeVars binder)
256 lintBinder var | isId var = lintIdBndr var $ \_ -> (return ())
257 | otherwise = return ()
260 %************************************************************************
262 \subsection[lintCoreExpr]{lintCoreExpr}
264 %************************************************************************
267 type InType = Type -- Substitution not yet applied
268 type OutType = Type -- Substitution has been applied to this
270 lintCoreExpr :: CoreExpr -> LintM OutType
271 -- The returned type has the substitution from the monad
272 -- already applied to it:
273 -- lintCoreExpr e subst = exprType (subst e)
275 lintCoreExpr (Var var)
276 = do { checkL (not (var == oneTupleDataConId))
277 (ptext SLIT("Illegal one-tuple"))
278 ; var' <- lookupIdInScope var
279 ; return (idType var')
282 lintCoreExpr (Lit lit)
283 = return (literalType lit)
285 --lintCoreExpr (Note (Coerce to_ty from_ty) expr)
286 -- = do { expr_ty <- lintCoreExpr expr
287 -- ; to_ty <- lintTy to_ty
288 -- ; from_ty <- lintTy from_ty
289 -- ; checkTys from_ty expr_ty (mkCoerceErr from_ty expr_ty)
292 lintCoreExpr (Cast expr co)
293 = do { expr_ty <- lintCoreExpr expr
295 ; let (from_ty, to_ty) = coercionKind co'
296 ; checkTys from_ty expr_ty (mkCastErr from_ty expr_ty)
299 lintCoreExpr (Note other_note expr)
302 lintCoreExpr (Let (NonRec bndr rhs) body)
303 = do { lintSingleBinding NotTopLevel NonRecursive (bndr,rhs)
304 ; addLoc (BodyOfLetRec [bndr])
305 (lintAndScopeId bndr $ \_ -> (lintCoreExpr body)) }
307 lintCoreExpr (Let (Rec pairs) body)
308 = lintAndScopeIds bndrs $ \_ ->
309 do { mapM (lintSingleBinding NotTopLevel Recursive) pairs
310 ; addLoc (BodyOfLetRec bndrs) (lintCoreExpr body) }
312 bndrs = map fst pairs
314 lintCoreExpr e@(App fun (Type ty))
315 -- See Note [Type let] above
316 = addLoc (AnExpr e) $
319 go (App fun (Type ty)) tys
320 = do { go fun (ty:tys) }
321 go (Lam tv body) (ty:tys)
322 = do { checkL (isTyVar tv) (mkKindErrMsg tv ty) -- Not quite accurate
324 ; let kind = tyVarKind tv
325 ; kind' <- lintTy kind
326 ; let tv' = setTyVarKind tv kind'
328 -- Now extend the substitution so we
329 -- take advantage of it in the body
330 ; addInScopeVars [tv'] $
331 extendSubstL tv' ty' $
334 = do { fun_ty <- lintCoreExpr fun
335 ; lintCoreArgs fun_ty (map Type tys) }
337 lintCoreExpr e@(App fun arg)
338 = do { fun_ty <- lintCoreExpr fun
339 ; addLoc (AnExpr e) $
340 lintCoreArg fun_ty arg }
342 lintCoreExpr (Lam var expr)
343 = addLoc (LambdaBodyOf var) $
344 lintBinders [var] $ \[var'] ->
345 do { body_ty <- lintCoreExpr expr
347 return (mkFunTy (idType var') body_ty)
349 return (mkForAllTy var' body_ty)
351 -- The applySubst is needed to apply the subst to var
353 lintCoreExpr e@(Case scrut var alt_ty alts) =
354 -- Check the scrutinee
355 do { scrut_ty <- lintCoreExpr scrut
356 ; alt_ty <- lintTy alt_ty
357 ; var_ty <- lintTy (idType var)
358 -- Don't use lintIdBndr on var, because unboxed tuple is legitimate
360 ; subst <- getTvSubst
361 ; checkTys var_ty scrut_ty (mkScrutMsg var var_ty scrut_ty subst)
363 -- If the binder is an unboxed tuple type, don't put it in scope
364 ; let scope = if (isUnboxedTupleType (idType var)) then
366 else lintAndScopeId var
368 do { -- Check the alternatives
369 mapM (lintCoreAlt scrut_ty alt_ty) alts
370 ; checkCaseAlts e scrut_ty alts
375 lintCoreExpr e@(Type ty)
376 = addErrL (mkStrangeTyMsg e)
379 %************************************************************************
381 \subsection[lintCoreArgs]{lintCoreArgs}
383 %************************************************************************
385 The basic version of these functions checks that the argument is a
386 subtype of the required type, as one would expect.
389 lintCoreArgs :: OutType -> [CoreArg] -> LintM OutType
390 lintCoreArg :: OutType -> CoreArg -> LintM OutType
391 -- First argument has already had substitution applied to it
395 lintCoreArgs ty [] = return ty
396 lintCoreArgs ty (a : args) =
397 do { res <- lintCoreArg ty a
398 ; lintCoreArgs res args }
400 lintCoreArg fun_ty a@(Type arg_ty) =
401 do { arg_ty <- lintTy arg_ty
402 ; lintTyApp fun_ty arg_ty }
404 lintCoreArg fun_ty arg =
405 -- Make sure function type matches argument
406 do { arg_ty <- lintCoreExpr arg
407 ; let err1 = mkAppMsg fun_ty arg_ty arg
408 err2 = mkNonFunAppMsg fun_ty arg_ty arg
409 ; case splitFunTy_maybe fun_ty of
411 do { checkTys arg arg_ty err1
417 -- Both args have had substitution applied
418 lintTyApp :: OutType -> OutType -> LintM OutType
420 = case splitForAllTy_maybe ty of
421 Nothing -> addErrL (mkTyAppMsg ty arg_ty)
424 -> do { checkL (isTyVar tyvar) (mkTyAppMsg ty arg_ty)
425 ; checkKinds tyvar arg_ty
426 ; return (substTyWith [tyvar] [arg_ty] body) }
428 checkKinds tyvar arg_ty
429 -- Arg type might be boxed for a function with an uncommitted
430 -- tyvar; notably this is used so that we can give
431 -- error :: forall a:*. String -> a
432 -- and then apply it to both boxed and unboxed types.
433 = checkL (arg_kind `isSubKind` tyvar_kind)
434 (mkKindErrMsg tyvar arg_ty)
436 tyvar_kind = tyVarKind tyvar
437 arg_kind | isCoVar tyvar = coercionKindPredTy arg_ty
438 | otherwise = typeKind arg_ty
442 %************************************************************************
444 \subsection[lintCoreAlts]{lintCoreAlts}
446 %************************************************************************
449 checkCaseAlts :: CoreExpr -> OutType -> [CoreAlt] -> LintM ()
450 -- a) Check that the alts are non-empty
451 -- b1) Check that the DEFAULT comes first, if it exists
452 -- b2) Check that the others are in increasing order
453 -- c) Check that there's a default for infinite types
454 -- NB: Algebraic cases are not necessarily exhaustive, because
455 -- the simplifer correctly eliminates case that can't
458 checkCaseAlts e ty []
459 = addErrL (mkNullAltsMsg e)
461 checkCaseAlts e ty alts =
462 do { checkL (all non_deflt con_alts) (mkNonDefltMsg e)
463 ; checkL (increasing_tag con_alts) (mkNonIncreasingAltsMsg e)
464 ; checkL (isJust maybe_deflt || not is_infinite_ty)
465 (nonExhaustiveAltsMsg e) }
467 (con_alts, maybe_deflt) = findDefault alts
469 -- Check that successive alternatives have increasing tags
470 increasing_tag (alt1 : rest@( alt2 : _)) = alt1 `ltAlt` alt2 && increasing_tag rest
471 increasing_tag other = True
473 non_deflt (DEFAULT, _, _) = False
476 is_infinite_ty = case splitTyConApp_maybe ty of
478 Just (tycon, tycon_arg_tys) -> isPrimTyCon tycon
482 checkAltExpr :: CoreExpr -> OutType -> LintM ()
483 checkAltExpr expr ann_ty
484 = do { actual_ty <- lintCoreExpr expr
485 ; checkTys actual_ty ann_ty (mkCaseAltMsg expr actual_ty ann_ty) }
487 lintCoreAlt :: OutType -- Type of scrutinee
488 -> OutType -- Type of the alternative
492 lintCoreAlt scrut_ty alt_ty alt@(DEFAULT, args, rhs) =
493 do { checkL (null args) (mkDefaultArgsMsg args)
494 ; checkAltExpr rhs alt_ty }
496 lintCoreAlt scrut_ty alt_ty alt@(LitAlt lit, args, rhs) =
497 do { checkL (null args) (mkDefaultArgsMsg args)
498 ; checkTys lit_ty scrut_ty (mkBadPatMsg lit_ty scrut_ty)
499 ; checkAltExpr rhs alt_ty }
501 lit_ty = literalType lit
503 lintCoreAlt scrut_ty alt_ty alt@(DataAlt con, args, rhs)
504 | isNewTyCon (dataConTyCon con) = addErrL (mkNewTyDataConAltMsg scrut_ty alt)
505 | Just (tycon, tycon_arg_tys) <- splitTyConApp_maybe scrut_ty
506 = addLoc (CaseAlt alt) $ do
507 { -- First instantiate the universally quantified
508 -- type variables of the data constructor
509 -- We've already check
510 checkL (tycon == dataConTyCon con) (mkBadConMsg tycon con)
511 ; let con_payload_ty = applyTys (dataConRepType con) tycon_arg_tys
513 -- And now bring the new binders into scope
514 ; lintBinders args $ \ args -> do
515 { addLoc (CasePat alt) $ do
516 { -- Check the pattern
517 -- Scrutinee type must be a tycon applicn; checked by caller
518 -- This code is remarkably compact considering what it does!
519 -- NB: args must be in scope here so that the lintCoreArgs line works.
520 -- NB: relies on existential type args coming *after* ordinary type args
522 ; con_result_ty <- lintCoreArgs con_payload_ty (varsToCoreExprs args)
523 ; checkTys con_result_ty scrut_ty (mkBadPatMsg con_result_ty scrut_ty)
526 ; checkAltExpr rhs alt_ty } }
528 | otherwise -- Scrut-ty is wrong shape
529 = addErrL (mkBadAltMsg scrut_ty alt)
532 %************************************************************************
534 \subsection[lint-types]{Types}
536 %************************************************************************
539 -- When we lint binders, we (one at a time and in order):
540 -- 1. Lint var types or kinds (possibly substituting)
541 -- 2. Add the binder to the in scope set, and if its a coercion var,
542 -- we may extend the substitution to reflect its (possibly) new kind
543 lintBinders :: [Var] -> ([Var] -> LintM a) -> LintM a
544 lintBinders [] linterF = linterF []
545 lintBinders (var:vars) linterF = lintBinder var $ \var' ->
546 lintBinders vars $ \ vars' ->
549 lintBinder :: Var -> (Var -> LintM a) -> LintM a
550 lintBinder var linterF
551 | isTyVar var = lint_ty_bndr
552 | otherwise = lintIdBndr var linterF
554 lint_ty_bndr = do { lintTy (tyVarKind var)
555 ; subst <- getTvSubst
556 ; let (subst', tv') = substTyVarBndr subst var
557 ; updateTvSubst subst' (linterF tv') }
559 lintIdBndr :: Var -> (Var -> LintM a) -> LintM a
560 -- Do substitution on the type of a binder and add the var with this
561 -- new type to the in-scope set of the second argument
562 -- ToDo: lint its rules
563 lintIdBndr id linterF
564 = do { checkL (not (isUnboxedTupleType (idType id)))
565 (mkUnboxedTupleMsg id)
566 -- No variable can be bound to an unboxed tuple.
567 ; lintAndScopeId id $ \id' -> linterF id'
570 lintAndScopeIds :: [Var] -> ([Var] -> LintM a) -> LintM a
571 lintAndScopeIds ids linterF
575 go (id:ids) = do { lintAndScopeId id $ \id ->
576 lintAndScopeIds ids $ \ids ->
579 lintAndScopeId :: Var -> (Var -> LintM a) -> LintM a
580 lintAndScopeId id linterF
581 = do { ty <- lintTy (idType id)
582 ; let id' = Var.setIdType id ty
583 ; addInScopeVars [id'] $ (linterF id')
586 lintTy :: InType -> LintM OutType
587 -- Check the type, and apply the substitution to it
588 -- ToDo: check the kind structure of the type
590 = do { ty' <- applySubst ty
591 ; mapM_ checkTyVarInScope (varSetElems (tyVarsOfType ty'))
596 %************************************************************************
598 \subsection[lint-monad]{The Lint monad}
600 %************************************************************************
605 [LintLocInfo] -> -- Locations
606 TvSubst -> -- Current type substitution; we also use this
607 -- to keep track of all the variables in scope,
608 -- both Ids and TyVars
609 Bag Message -> -- Error messages so far
610 (Maybe a, Bag Message) } -- Result and error messages (if any)
612 {- Note [Type substitution]
613 ~~~~~~~~~~~~~~~~~~~~~~~~
614 Why do we need a type substitution? Consider
615 /\(a:*). \(x:a). /\(a:*). id a x
616 This is ill typed, because (renaming variables) it is really
617 /\(a:*). \(x:a). /\(b:*). id b x
618 Hence, when checking an application, we can't naively compare x's type
619 (at its binding site) with its expected type (at a use site). So we
620 rename type binders as we go, maintaining a substitution.
622 The same substitution also supports let-type, current expressed as
624 Here we substitute 'ty' for 'a' in 'body', on the fly.
627 instance Monad LintM where
628 return x = LintM (\ loc subst errs -> (Just x, errs))
629 fail err = LintM (\ loc subst errs -> (Nothing, addErr subst errs (text err) loc))
630 m >>= k = LintM (\ loc subst errs ->
631 let (res, errs') = unLintM m loc subst errs in
633 Just r -> unLintM (k r) loc subst errs'
634 Nothing -> (Nothing, errs'))
637 = RhsOf Id -- The variable bound
638 | LambdaBodyOf Id -- The lambda-binder
639 | BodyOfLetRec [Id] -- One of the binders
640 | CaseAlt CoreAlt -- Case alternative
641 | CasePat CoreAlt -- *Pattern* of the case alternative
642 | AnExpr CoreExpr -- Some expression
643 | ImportedUnfolding SrcLoc -- Some imported unfolding (ToDo: say which)
648 initL :: LintM a -> Maybe Message {- errors -}
650 = case unLintM m [] emptyTvSubst emptyBag of
651 (_, errs) | isEmptyBag errs -> Nothing
652 | otherwise -> Just (vcat (punctuate (text "") (bagToList errs)))
656 checkL :: Bool -> Message -> LintM ()
657 checkL True msg = return ()
658 checkL False msg = addErrL msg
660 addErrL :: Message -> LintM a
661 addErrL msg = LintM (\ loc subst errs -> (Nothing, addErr subst errs msg loc))
663 addErr :: TvSubst -> Bag Message -> Message -> [LintLocInfo] -> Bag Message
664 addErr subst errs_so_far msg locs
665 = ASSERT( notNull locs )
666 errs_so_far `snocBag` mk_msg msg
668 (loc, cxt1) = dumpLoc (head locs)
669 cxts = [snd (dumpLoc loc) | loc <- locs]
670 context | opt_PprStyle_Debug = vcat (reverse cxts) $$ cxt1 $$
671 ptext SLIT("Substitution:") <+> ppr subst
674 mk_msg msg = mkLocMessage (mkSrcSpan loc loc) (context $$ msg)
676 addLoc :: LintLocInfo -> LintM a -> LintM a
678 LintM (\ loc subst errs -> unLintM m (extra_loc:loc) subst errs)
680 addInScopeVars :: [Var] -> LintM a -> LintM a
681 addInScopeVars vars m =
682 LintM (\ loc subst errs -> unLintM m loc (extendTvInScope subst vars) errs)
684 updateTvSubst :: TvSubst -> LintM a -> LintM a
685 updateTvSubst subst' m =
686 LintM (\ loc subst errs -> unLintM m loc subst' errs)
688 getTvSubst :: LintM TvSubst
689 getTvSubst = LintM (\ loc subst errs -> (Just subst, errs))
691 applySubst :: Type -> LintM Type
692 applySubst ty = do { subst <- getTvSubst; return (substTy subst ty) }
694 extendSubstL :: TyVar -> Type -> LintM a -> LintM a
696 = LintM (\ loc subst errs -> unLintM m loc (extendTvSubst subst tv ty) errs)
700 lookupIdInScope :: Id -> LintM Id
702 | not (mustHaveLocalBinding id)
703 = return id -- An imported Id
705 = do { subst <- getTvSubst
706 ; case lookupInScope (getTvInScope subst) id of
708 Nothing -> do { addErrL out_of_scope
711 out_of_scope = ppr id <+> ptext SLIT("is out of scope")
714 oneTupleDataConId :: Id -- Should not happen
715 oneTupleDataConId = dataConWorkId (tupleCon Boxed 1)
717 checkBndrIdInScope :: Var -> Var -> LintM ()
718 checkBndrIdInScope binder id
719 = checkInScope msg id
721 msg = ptext SLIT("is out of scope inside info for") <+>
724 checkTyVarInScope :: TyVar -> LintM ()
725 checkTyVarInScope tv = checkInScope (ptext SLIT("is out of scope")) tv
727 checkInScope :: SDoc -> Var -> LintM ()
728 checkInScope loc_msg var =
729 do { subst <- getTvSubst
730 ; checkL (not (mustHaveLocalBinding var) || (var `isInScope` subst))
731 (hsep [ppr var, loc_msg]) }
733 checkTys :: Type -> Type -> Message -> LintM ()
734 -- check ty2 is subtype of ty1 (ie, has same structure but usage
735 -- annotations need only be consistent, not equal)
736 -- Assumes ty1,ty2 are have alrady had the substitution applied
737 checkTys ty1 ty2 msg = checkL (ty1 `coreEqType` ty2) msg
740 %************************************************************************
742 \subsection{Error messages}
744 %************************************************************************
748 = (getSrcLoc v, brackets (ptext SLIT("RHS of") <+> pp_binders [v]))
750 dumpLoc (LambdaBodyOf b)
751 = (getSrcLoc b, brackets (ptext SLIT("in body of lambda with binder") <+> pp_binder b))
753 dumpLoc (BodyOfLetRec [])
754 = (noSrcLoc, brackets (ptext SLIT("In body of a letrec with no binders")))
756 dumpLoc (BodyOfLetRec bs@(_:_))
757 = ( getSrcLoc (head bs), brackets (ptext SLIT("in body of letrec with binders") <+> pp_binders bs))
760 = (noSrcLoc, text "In the expression:" <+> ppr e)
762 dumpLoc (CaseAlt (con, args, rhs))
763 = (noSrcLoc, text "In a case alternative:" <+> parens (ppr con <+> pp_binders args))
765 dumpLoc (CasePat (con, args, rhs))
766 = (noSrcLoc, text "In the pattern of a case alternative:" <+> parens (ppr con <+> pp_binders args))
768 dumpLoc (ImportedUnfolding locn)
769 = (locn, brackets (ptext SLIT("in an imported unfolding")))
771 pp_binders :: [Var] -> SDoc
772 pp_binders bs = sep (punctuate comma (map pp_binder bs))
774 pp_binder :: Var -> SDoc
775 pp_binder b | isId b = hsep [ppr b, dcolon, ppr (idType b)]
776 | isTyVar b = hsep [ppr b, dcolon, ppr (tyVarKind b)]
780 ------------------------------------------------------
781 -- Messages for case expressions
783 mkNullAltsMsg :: CoreExpr -> Message
785 = hang (text "Case expression with no alternatives:")
788 mkDefaultArgsMsg :: [Var] -> Message
789 mkDefaultArgsMsg args
790 = hang (text "DEFAULT case with binders")
793 mkCaseAltMsg :: CoreExpr -> Type -> Type -> Message
794 mkCaseAltMsg e ty1 ty2
795 = hang (text "Type of case alternatives not the same as the annotation on case:")
796 4 (vcat [ppr ty1, ppr ty2, ppr e])
798 mkScrutMsg :: Id -> Type -> Type -> TvSubst -> Message
799 mkScrutMsg var var_ty scrut_ty subst
800 = vcat [text "Result binder in case doesn't match scrutinee:" <+> ppr var,
801 text "Result binder type:" <+> ppr var_ty,--(idType var),
802 text "Scrutinee type:" <+> ppr scrut_ty,
803 hsep [ptext SLIT("Current TV subst"), ppr subst]]
806 = hang (text "Case expression with DEFAULT not at the beginnning") 4 (ppr e)
807 mkNonIncreasingAltsMsg e
808 = hang (text "Case expression with badly-ordered alternatives") 4 (ppr e)
810 nonExhaustiveAltsMsg :: CoreExpr -> Message
811 nonExhaustiveAltsMsg e
812 = hang (text "Case expression with non-exhaustive alternatives") 4 (ppr e)
814 mkBadConMsg :: TyCon -> DataCon -> Message
815 mkBadConMsg tycon datacon
817 text "In a case alternative, data constructor isn't in scrutinee type:",
818 text "Scrutinee type constructor:" <+> ppr tycon,
819 text "Data con:" <+> ppr datacon
822 mkBadPatMsg :: Type -> Type -> Message
823 mkBadPatMsg con_result_ty scrut_ty
825 text "In a case alternative, pattern result type doesn't match scrutinee type:",
826 text "Pattern result type:" <+> ppr con_result_ty,
827 text "Scrutinee type:" <+> ppr scrut_ty
830 mkBadAltMsg :: Type -> CoreAlt -> Message
831 mkBadAltMsg scrut_ty alt
832 = vcat [ text "Data alternative when scrutinee is not a tycon application",
833 text "Scrutinee type:" <+> ppr scrut_ty,
834 text "Alternative:" <+> pprCoreAlt alt ]
836 mkNewTyDataConAltMsg :: Type -> CoreAlt -> Message
837 mkNewTyDataConAltMsg scrut_ty alt
838 = vcat [ text "Data alternative for newtype datacon",
839 text "Scrutinee type:" <+> ppr scrut_ty,
840 text "Alternative:" <+> pprCoreAlt alt ]
843 ------------------------------------------------------
844 -- Other error messages
846 mkAppMsg :: Type -> Type -> CoreExpr -> Message
847 mkAppMsg fun_ty arg_ty arg
848 = vcat [ptext SLIT("Argument value doesn't match argument type:"),
849 hang (ptext SLIT("Fun type:")) 4 (ppr fun_ty),
850 hang (ptext SLIT("Arg type:")) 4 (ppr arg_ty),
851 hang (ptext SLIT("Arg:")) 4 (ppr arg)]
853 mkNonFunAppMsg :: Type -> Type -> CoreExpr -> Message
854 mkNonFunAppMsg fun_ty arg_ty arg
855 = vcat [ptext SLIT("Non-function type in function position"),
856 hang (ptext SLIT("Fun type:")) 4 (ppr fun_ty),
857 hang (ptext SLIT("Arg type:")) 4 (ppr arg_ty),
858 hang (ptext SLIT("Arg:")) 4 (ppr arg)]
860 mkKindErrMsg :: TyVar -> Type -> Message
861 mkKindErrMsg tyvar arg_ty
862 = vcat [ptext SLIT("Kinds don't match in type application:"),
863 hang (ptext SLIT("Type variable:"))
864 4 (ppr tyvar <+> dcolon <+> ppr (tyVarKind tyvar)),
865 hang (ptext SLIT("Arg type:"))
866 4 (ppr arg_ty <+> dcolon <+> ppr (typeKind arg_ty))]
868 mkTyAppMsg :: Type -> Type -> Message
870 = vcat [text "Illegal type application:",
871 hang (ptext SLIT("Exp type:"))
872 4 (ppr ty <+> dcolon <+> ppr (typeKind ty)),
873 hang (ptext SLIT("Arg type:"))
874 4 (ppr arg_ty <+> dcolon <+> ppr (typeKind arg_ty))]
876 mkRhsMsg :: Id -> Type -> Message
879 [hsep [ptext SLIT("The type of this binder doesn't match the type of its RHS:"),
881 hsep [ptext SLIT("Binder's type:"), ppr (idType binder)],
882 hsep [ptext SLIT("Rhs type:"), ppr ty]]
884 mkRhsPrimMsg :: Id -> CoreExpr -> Message
885 mkRhsPrimMsg binder rhs
886 = vcat [hsep [ptext SLIT("The type of this binder is primitive:"),
888 hsep [ptext SLIT("Binder's type:"), ppr (idType binder)]
891 mkStrictMsg :: Id -> Message
893 = vcat [hsep [ptext SLIT("Recursive or top-level binder has strict demand info:"),
895 hsep [ptext SLIT("Binder's demand info:"), ppr (idNewDemandInfo binder)]
898 mkArityMsg :: Id -> Message
900 = vcat [hsep [ptext SLIT("Demand type has "),
901 ppr (dmdTypeDepth dmd_ty),
902 ptext SLIT(" arguments, rhs has "),
903 ppr (idArity binder),
904 ptext SLIT("arguments, "),
906 hsep [ptext SLIT("Binder's strictness signature:"), ppr dmd_ty]
909 where (StrictSig dmd_ty) = idNewStrictness binder
911 mkUnboxedTupleMsg :: Id -> Message
912 mkUnboxedTupleMsg binder
913 = vcat [hsep [ptext SLIT("A variable has unboxed tuple type:"), ppr binder],
914 hsep [ptext SLIT("Binder's type:"), ppr (idType binder)]]
916 mkCastErr from_ty expr_ty
917 = vcat [ptext SLIT("From-type of Cast differs from type of enclosed expression"),
918 ptext SLIT("From-type:") <+> ppr from_ty,
919 ptext SLIT("Type of enclosed expr:") <+> ppr expr_ty
923 = ptext SLIT("Type where expression expected:") <+> ppr e