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
13 showPass, endPass, endPassIf, endIteration
16 #include "HsVersions.h"
46 %************************************************************************
50 %************************************************************************
52 @showPass@ and @endPass@ don't really belong here, but it makes a convenient
53 place for them. They print out stuff before and after core passes,
54 and do Core Lint when necessary.
57 endPass :: DynFlags -> String -> DynFlag -> [CoreBind] -> IO [CoreBind]
58 endPass = dumpAndLint dumpIfSet_core
60 endPassIf :: Bool -> DynFlags -> String -> DynFlag -> [CoreBind] -> IO [CoreBind]
61 endPassIf cond = dumpAndLint (dumpIf_core cond)
63 endIteration :: DynFlags -> String -> DynFlag -> [CoreBind] -> IO [CoreBind]
64 endIteration = dumpAndLint dumpIfSet_dyn
66 dumpAndLint :: (DynFlags -> DynFlag -> String -> SDoc -> IO ())
67 -> DynFlags -> String -> DynFlag -> [CoreBind] -> IO [CoreBind]
68 dumpAndLint dump dflags pass_name dump_flag binds
70 -- Report result size if required
71 -- This has the side effect of forcing the intermediate to be evaluated
72 debugTraceMsg dflags 2 $
73 (text " Result size =" <+> int (coreBindsSize binds))
75 -- Report verbosely, if required
76 dump dflags dump_flag pass_name (pprCoreBindings binds)
79 lintCoreBindings dflags pass_name binds
85 %************************************************************************
87 \subsection[lintCoreBindings]{@lintCoreBindings@: Top-level interface}
89 %************************************************************************
91 Checks that a set of core bindings is well-formed. The PprStyle and String
92 just control what we print in the event of an error. The Bool value
93 indicates whether we have done any specialisation yet (in which case we do
98 (b) Out-of-scope type variables
99 (c) Out-of-scope local variables
102 If we have done specialisation the we check that there are
103 (a) No top-level bindings of primitive (unboxed type)
108 -- Things are *not* OK if:
110 -- * Unsaturated type app before specialisation has been done;
112 -- * Oversaturated type app after specialisation (eta reduction
113 -- may well be happening...);
118 In the desugarer, it's very very convenient to be able to say (in effect)
119 let a = Int in <body>
120 That is, use a type let. (See notes just below for why we want this.)
122 We don't have type lets in Core, so the desugarer uses type lambda
124 However, in the lambda form, we'd get lint errors from:
125 (/\a. let x::a = 4 in <body>) Int
126 because (x::a) doesn't look compatible with (4::Int).
128 So (HACK ALERT) the Lint phase does type-beta reduction "on the fly",
129 as it were. It carries a type substitution (in this example [a -> Int])
130 and applies this substitution before comparing types. The functin
131 lintTy :: Type -> LintM Type
132 returns a substituted type; that's the only reason it returns anything.
134 When we encounter a binder (like x::a) we must apply the substitution
135 to the type of the binding variable. lintBinders does this.
137 For Ids, the type-substituted Id is added to the in_scope set (which
138 itself is part of the TvSubst we are carrying down), and when we
139 find an occurence of an Id, we fetch it from the in-scope set.
144 It's needed when dealing with desugarer output for GADTs. Consider
145 data T = forall a. T a (a->Int) Bool
147 f (T x f True) = <e1>
148 f (T y g False) = <e2>
149 After desugaring we get
151 T a (x::a) (f::a->Int) (b:Bool) ->
154 False -> (/\b. let y=x; g=f in <e2>) a
155 And for a reason I now forget, the ...<e2>... can mention a; so
156 we want Lint to know that b=a. Ugh.
158 I tried quite hard to make the necessity for this go away, by changing the
159 desugarer, but the fundamental problem is this:
161 T a (x::a) (y::Int) -> let fail::a = ...
162 in (/\b. ...(case ... of
166 Now the inner case look as though it has incompatible branches.
170 lintCoreBindings :: DynFlags -> String -> [CoreBind] -> IO ()
172 lintCoreBindings dflags _whoDunnit _binds
173 | not (dopt Opt_DoCoreLinting dflags)
176 lintCoreBindings dflags whoDunnit binds
177 = case (initL (lint_binds binds)) of
178 Nothing -> showPass dflags ("Core Linted result of " ++ whoDunnit)
179 Just bad_news -> printDump (display bad_news) >>
182 -- Put all the top-level binders in scope at the start
183 -- This is because transformation rules can bring something
184 -- into use 'unexpectedly'
185 lint_binds binds = addLoc TopLevelBindings $
186 addInScopeVars (bindersOfBinds binds) $
189 lint_bind (Rec prs) = mapM_ (lintSingleBinding TopLevel Recursive) prs
190 lint_bind (NonRec bndr rhs) = lintSingleBinding TopLevel NonRecursive (bndr,rhs)
193 = vcat [ text ("*** Core Lint Errors: in result of " ++ whoDunnit ++ " ***"),
195 ptext SLIT("*** Offending Program ***"),
196 pprCoreBindings binds,
197 ptext SLIT("*** End of Offense ***")
201 %************************************************************************
203 \subsection[lintUnfolding]{lintUnfolding}
205 %************************************************************************
207 We use this to check all unfoldings that come in from interfaces
208 (it is very painful to catch errors otherwise):
211 lintUnfolding :: SrcLoc
212 -> [Var] -- Treat these as in scope
214 -> Maybe Message -- Nothing => OK
216 lintUnfolding locn vars expr
217 = initL (addLoc (ImportedUnfolding locn) $
218 addInScopeVars vars $
222 %************************************************************************
224 \subsection[lintCoreBinding]{lintCoreBinding}
226 %************************************************************************
228 Check a core binding, returning the list of variables bound.
231 lintSingleBinding :: TopLevelFlag -> RecFlag -> (Id, CoreExpr) -> LintM ()
232 lintSingleBinding top_lvl_flag rec_flag (binder,rhs)
233 = addLoc (RhsOf binder) $
235 do { ty <- lintCoreExpr rhs
236 ; lintBinder binder -- Check match to RHS type
237 ; binder_ty <- applySubst binder_ty
238 ; checkTys binder_ty ty (mkRhsMsg binder ty)
239 -- Check (not isUnLiftedType) (also checks for bogus unboxed tuples)
240 ; checkL (not (isUnLiftedType binder_ty)
241 || (isNonRec rec_flag && exprOkForSpeculation rhs))
242 (mkRhsPrimMsg binder rhs)
243 -- Check that if the binder is top-level or recursive, it's not demanded
244 ; checkL (not (isStrictId binder)
245 || (isNonRec rec_flag && not (isTopLevel top_lvl_flag)))
247 -- Check whether binder's specialisations contain any out-of-scope variables
248 ; mapM_ (checkBndrIdInScope binder) bndr_vars
250 -- Check whether arity and demand type are consistent (only if demand analysis
252 ; checkL (case maybeDmdTy of
253 Just (StrictSig dmd_ty) -> idArity binder >= dmdTypeDepth dmd_ty || exprIsTrivial rhs
255 (mkArityMsg binder) }
257 -- We should check the unfolding, if any, but this is tricky because
258 -- the unfolding is a SimplifiableCoreExpr. Give up for now.
260 binder_ty = idType binder
261 maybeDmdTy = idNewStrictness_maybe binder
262 bndr_vars = varSetElems (idFreeVars binder)
263 lintBinder var | isId var = lintIdBndr var $ \_ -> (return ())
264 | otherwise = return ()
267 %************************************************************************
269 \subsection[lintCoreExpr]{lintCoreExpr}
271 %************************************************************************
274 type InType = Type -- Substitution not yet applied
275 type OutType = Type -- Substitution has been applied to this
277 lintCoreExpr :: CoreExpr -> LintM OutType
278 -- The returned type has the substitution from the monad
279 -- already applied to it:
280 -- lintCoreExpr e subst = exprType (subst e)
282 lintCoreExpr (Var var)
283 = do { checkL (not (var == oneTupleDataConId))
284 (ptext SLIT("Illegal one-tuple"))
285 ; var' <- lookupIdInScope var
286 ; return (idType var')
289 lintCoreExpr (Lit lit)
290 = return (literalType lit)
292 --lintCoreExpr (Note (Coerce to_ty from_ty) expr)
293 -- = do { expr_ty <- lintCoreExpr expr
294 -- ; to_ty <- lintTy to_ty
295 -- ; from_ty <- lintTy from_ty
296 -- ; checkTys from_ty expr_ty (mkCoerceErr from_ty expr_ty)
299 lintCoreExpr (Cast expr co)
300 = do { expr_ty <- lintCoreExpr expr
302 ; let (from_ty, to_ty) = coercionKind co'
303 ; checkTys from_ty expr_ty (mkCastErr from_ty expr_ty)
306 lintCoreExpr (Note _ expr)
309 lintCoreExpr (Let (NonRec bndr rhs) body)
310 = do { lintSingleBinding NotTopLevel NonRecursive (bndr,rhs)
311 ; addLoc (BodyOfLetRec [bndr])
312 (lintAndScopeId bndr $ \_ -> (lintCoreExpr body)) }
314 lintCoreExpr (Let (Rec pairs) body)
315 = lintAndScopeIds bndrs $ \_ ->
316 do { mapM (lintSingleBinding NotTopLevel Recursive) pairs
317 ; addLoc (BodyOfLetRec bndrs) (lintCoreExpr body) }
319 bndrs = map fst pairs
321 lintCoreExpr e@(App fun (Type ty))
322 -- See Note [Type let] above
323 = addLoc (AnExpr e) $
326 go (App fun (Type ty)) tys
327 = do { go fun (ty:tys) }
328 go (Lam tv body) (ty:tys)
329 = do { checkL (isTyVar tv) (mkKindErrMsg tv ty) -- Not quite accurate
331 ; let kind = tyVarKind tv
332 ; kind' <- lintTy kind
333 ; let tv' = setTyVarKind tv kind'
335 -- Now extend the substitution so we
336 -- take advantage of it in the body
337 ; addInScopeVars [tv'] $
338 extendSubstL tv' ty' $
341 = do { fun_ty <- lintCoreExpr fun
342 ; lintCoreArgs fun_ty (map Type tys) }
344 lintCoreExpr e@(App fun arg)
345 = do { fun_ty <- lintCoreExpr fun
346 ; addLoc (AnExpr e) $
347 lintCoreArg fun_ty arg }
349 lintCoreExpr (Lam var expr)
350 = addLoc (LambdaBodyOf var) $
351 lintBinders [var] $ \[var'] ->
352 do { body_ty <- lintCoreExpr expr
354 return (mkFunTy (idType var') body_ty)
356 return (mkForAllTy var' body_ty)
358 -- The applySubst is needed to apply the subst to var
360 lintCoreExpr e@(Case scrut var alt_ty alts) =
361 -- Check the scrutinee
362 do { scrut_ty <- lintCoreExpr scrut
363 ; alt_ty <- lintTy alt_ty
364 ; var_ty <- lintTy (idType var)
365 -- Don't use lintIdBndr on var, because unboxed tuple is legitimate
367 ; subst <- getTvSubst
368 ; checkTys var_ty scrut_ty (mkScrutMsg var var_ty scrut_ty subst)
370 -- If the binder is an unboxed tuple type, don't put it in scope
371 ; let scope = if (isUnboxedTupleType (idType var)) then
373 else lintAndScopeId var
375 do { -- Check the alternatives
376 mapM (lintCoreAlt scrut_ty alt_ty) alts
377 ; checkCaseAlts e scrut_ty alts
382 lintCoreExpr e@(Type _)
383 = addErrL (mkStrangeTyMsg e)
386 %************************************************************************
388 \subsection[lintCoreArgs]{lintCoreArgs}
390 %************************************************************************
392 The basic version of these functions checks that the argument is a
393 subtype of the required type, as one would expect.
396 lintCoreArgs :: OutType -> [CoreArg] -> LintM OutType
397 lintCoreArg :: OutType -> CoreArg -> LintM OutType
398 -- First argument has already had substitution applied to it
402 lintCoreArgs ty [] = return ty
403 lintCoreArgs ty (a : args) =
404 do { res <- lintCoreArg ty a
405 ; lintCoreArgs res args }
407 lintCoreArg fun_ty (Type arg_ty) =
408 do { arg_ty <- lintTy arg_ty
409 ; lintTyApp fun_ty arg_ty }
411 lintCoreArg fun_ty arg =
412 -- Make sure function type matches argument
413 do { arg_ty <- lintCoreExpr arg
414 ; let err1 = mkAppMsg fun_ty arg_ty arg
415 err2 = mkNonFunAppMsg fun_ty arg_ty arg
416 ; case splitFunTy_maybe fun_ty of
418 do { checkTys arg arg_ty err1
424 -- Both args have had substitution applied
425 lintTyApp :: OutType -> OutType -> LintM OutType
427 = case splitForAllTy_maybe ty of
428 Nothing -> addErrL (mkTyAppMsg ty arg_ty)
431 -> do { checkL (isTyVar tyvar) (mkTyAppMsg ty arg_ty)
432 ; checkKinds tyvar arg_ty
433 ; return (substTyWith [tyvar] [arg_ty] body) }
435 checkKinds :: Var -> Type -> LintM ()
436 checkKinds tyvar arg_ty
437 -- Arg type might be boxed for a function with an uncommitted
438 -- tyvar; notably this is used so that we can give
439 -- error :: forall a:*. String -> a
440 -- and then apply it to both boxed and unboxed types.
441 = checkL (arg_kind `isSubKind` tyvar_kind)
442 (mkKindErrMsg tyvar arg_ty)
444 tyvar_kind = tyVarKind tyvar
445 arg_kind | isCoVar tyvar = coercionKindPredTy arg_ty
446 | otherwise = typeKind arg_ty
450 %************************************************************************
452 \subsection[lintCoreAlts]{lintCoreAlts}
454 %************************************************************************
457 checkCaseAlts :: CoreExpr -> OutType -> [CoreAlt] -> LintM ()
458 -- a) Check that the alts are non-empty
459 -- b1) Check that the DEFAULT comes first, if it exists
460 -- b2) Check that the others are in increasing order
461 -- c) Check that there's a default for infinite types
462 -- NB: Algebraic cases are not necessarily exhaustive, because
463 -- the simplifer correctly eliminates case that can't
467 = addErrL (mkNullAltsMsg e)
469 checkCaseAlts e ty alts =
470 do { checkL (all non_deflt con_alts) (mkNonDefltMsg e)
471 ; checkL (increasing_tag con_alts) (mkNonIncreasingAltsMsg e)
472 ; checkL (isJust maybe_deflt || not is_infinite_ty)
473 (nonExhaustiveAltsMsg e) }
475 (con_alts, maybe_deflt) = findDefault alts
477 -- Check that successive alternatives have increasing tags
478 increasing_tag (alt1 : rest@( alt2 : _)) = alt1 `ltAlt` alt2 && increasing_tag rest
479 increasing_tag _ = True
481 non_deflt (DEFAULT, _, _) = False
484 is_infinite_ty = case splitTyConApp_maybe ty of
486 Just (tycon, _) -> isPrimTyCon tycon
490 checkAltExpr :: CoreExpr -> OutType -> LintM ()
491 checkAltExpr expr ann_ty
492 = do { actual_ty <- lintCoreExpr expr
493 ; checkTys actual_ty ann_ty (mkCaseAltMsg expr actual_ty ann_ty) }
495 lintCoreAlt :: OutType -- Type of scrutinee
496 -> OutType -- Type of the alternative
500 lintCoreAlt _ alt_ty (DEFAULT, args, rhs) =
501 do { checkL (null args) (mkDefaultArgsMsg args)
502 ; checkAltExpr rhs alt_ty }
504 lintCoreAlt scrut_ty alt_ty (LitAlt lit, args, rhs) =
505 do { checkL (null args) (mkDefaultArgsMsg args)
506 ; checkTys lit_ty scrut_ty (mkBadPatMsg lit_ty scrut_ty)
507 ; checkAltExpr rhs alt_ty }
509 lit_ty = literalType lit
511 lintCoreAlt scrut_ty alt_ty alt@(DataAlt con, args, rhs)
512 | isNewTyCon (dataConTyCon con) = addErrL (mkNewTyDataConAltMsg scrut_ty alt)
513 | Just (tycon, tycon_arg_tys) <- splitTyConApp_maybe scrut_ty
514 = addLoc (CaseAlt alt) $ do
515 { -- First instantiate the universally quantified
516 -- type variables of the data constructor
517 -- We've already check
518 checkL (tycon == dataConTyCon con) (mkBadConMsg tycon con)
519 ; let con_payload_ty = applyTys (dataConRepType con) tycon_arg_tys
521 -- And now bring the new binders into scope
522 ; lintBinders args $ \ args -> do
523 { addLoc (CasePat alt) $ do
524 { -- Check the pattern
525 -- Scrutinee type must be a tycon applicn; checked by caller
526 -- This code is remarkably compact considering what it does!
527 -- NB: args must be in scope here so that the lintCoreArgs
529 -- NB: relies on existential type args coming *after*
530 -- ordinary type args
531 ; con_result_ty <- lintCoreArgs con_payload_ty (varsToCoreExprs args)
532 ; checkTys con_result_ty scrut_ty (mkBadPatMsg con_result_ty scrut_ty)
535 ; checkAltExpr rhs alt_ty } }
537 | otherwise -- Scrut-ty is wrong shape
538 = addErrL (mkBadAltMsg scrut_ty alt)
541 %************************************************************************
543 \subsection[lint-types]{Types}
545 %************************************************************************
548 -- When we lint binders, we (one at a time and in order):
549 -- 1. Lint var types or kinds (possibly substituting)
550 -- 2. Add the binder to the in scope set, and if its a coercion var,
551 -- we may extend the substitution to reflect its (possibly) new kind
552 lintBinders :: [Var] -> ([Var] -> LintM a) -> LintM a
553 lintBinders [] linterF = linterF []
554 lintBinders (var:vars) linterF = lintBinder var $ \var' ->
555 lintBinders vars $ \ vars' ->
558 lintBinder :: Var -> (Var -> LintM a) -> LintM a
559 lintBinder var linterF
560 | isTyVar var = lint_ty_bndr
561 | otherwise = lintIdBndr var linterF
563 lint_ty_bndr = do { lintTy (tyVarKind var)
564 ; subst <- getTvSubst
565 ; let (subst', tv') = substTyVarBndr subst var
566 ; updateTvSubst subst' (linterF tv') }
568 lintIdBndr :: Var -> (Var -> LintM a) -> LintM a
569 -- Do substitution on the type of a binder and add the var with this
570 -- new type to the in-scope set of the second argument
571 -- ToDo: lint its rules
572 lintIdBndr id linterF
573 = do { checkL (not (isUnboxedTupleType (idType id)))
574 (mkUnboxedTupleMsg id)
575 -- No variable can be bound to an unboxed tuple.
576 ; lintAndScopeId id $ \id' -> linterF id'
579 lintAndScopeIds :: [Var] -> ([Var] -> LintM a) -> LintM a
580 lintAndScopeIds ids linterF
584 go (id:ids) = do { lintAndScopeId id $ \id ->
585 lintAndScopeIds ids $ \ids ->
588 lintAndScopeId :: Var -> (Var -> LintM a) -> LintM a
589 lintAndScopeId id linterF
590 = do { ty <- lintTy (idType id)
591 ; let id' = Var.setIdType id ty
592 ; addInScopeVars [id'] $ (linterF id')
595 lintTy :: InType -> LintM OutType
596 -- Check the type, and apply the substitution to it
597 -- ToDo: check the kind structure of the type
599 = do { ty' <- applySubst ty
600 ; mapM_ checkTyVarInScope (varSetElems (tyVarsOfType ty'))
605 %************************************************************************
607 \subsection[lint-monad]{The Lint monad}
609 %************************************************************************
614 [LintLocInfo] -> -- Locations
615 TvSubst -> -- Current type substitution; we also use this
616 -- to keep track of all the variables in scope,
617 -- both Ids and TyVars
618 Bag Message -> -- Error messages so far
619 (Maybe a, Bag Message) } -- Result and error messages (if any)
621 {- Note [Type substitution]
622 ~~~~~~~~~~~~~~~~~~~~~~~~
623 Why do we need a type substitution? Consider
624 /\(a:*). \(x:a). /\(a:*). id a x
625 This is ill typed, because (renaming variables) it is really
626 /\(a:*). \(x:a). /\(b:*). id b x
627 Hence, when checking an application, we can't naively compare x's type
628 (at its binding site) with its expected type (at a use site). So we
629 rename type binders as we go, maintaining a substitution.
631 The same substitution also supports let-type, current expressed as
633 Here we substitute 'ty' for 'a' in 'body', on the fly.
636 instance Monad LintM where
637 return x = LintM (\ _ _ errs -> (Just x, errs))
638 fail err = LintM (\ loc subst errs -> (Nothing, addErr subst errs (text err) loc))
639 m >>= k = LintM (\ loc subst errs ->
640 let (res, errs') = unLintM m loc subst errs in
642 Just r -> unLintM (k r) loc subst errs'
643 Nothing -> (Nothing, errs'))
646 = RhsOf Id -- The variable bound
647 | LambdaBodyOf Id -- The lambda-binder
648 | BodyOfLetRec [Id] -- One of the binders
649 | CaseAlt CoreAlt -- Case alternative
650 | CasePat CoreAlt -- *Pattern* of the case alternative
651 | AnExpr CoreExpr -- Some expression
652 | ImportedUnfolding SrcLoc -- Some imported unfolding (ToDo: say which)
658 initL :: LintM a -> Maybe Message {- errors -}
660 = case unLintM m [] emptyTvSubst emptyBag of
661 (_, errs) | isEmptyBag errs -> Nothing
662 | otherwise -> Just (vcat (punctuate (text "") (bagToList errs)))
666 checkL :: Bool -> Message -> LintM ()
667 checkL True _ = return ()
668 checkL False msg = addErrL msg
670 addErrL :: Message -> LintM a
671 addErrL msg = LintM (\ loc subst errs -> (Nothing, addErr subst errs msg loc))
673 addErr :: TvSubst -> Bag Message -> Message -> [LintLocInfo] -> Bag Message
674 addErr subst errs_so_far msg locs
675 = ASSERT( notNull locs )
676 errs_so_far `snocBag` mk_msg msg
678 (loc, cxt1) = dumpLoc (head locs)
679 cxts = [snd (dumpLoc loc) | loc <- locs]
680 context | opt_PprStyle_Debug = vcat (reverse cxts) $$ cxt1 $$
681 ptext SLIT("Substitution:") <+> ppr subst
684 mk_msg msg = mkLocMessage (mkSrcSpan loc loc) (context $$ msg)
686 addLoc :: LintLocInfo -> LintM a -> LintM a
688 LintM (\ loc subst errs -> unLintM m (extra_loc:loc) subst errs)
690 addInScopeVars :: [Var] -> LintM a -> LintM a
691 addInScopeVars vars m
693 = LintM (\ loc subst errs -> unLintM m loc (extendTvInScope subst vars) errs)
695 = addErrL (dupVars dups)
697 (_, dups) = removeDups compare vars
699 updateTvSubst :: TvSubst -> LintM a -> LintM a
700 updateTvSubst subst' m =
701 LintM (\ loc _ errs -> unLintM m loc subst' errs)
703 getTvSubst :: LintM TvSubst
704 getTvSubst = LintM (\ _ subst errs -> (Just subst, errs))
706 applySubst :: Type -> LintM Type
707 applySubst ty = do { subst <- getTvSubst; return (substTy subst ty) }
709 extendSubstL :: TyVar -> Type -> LintM a -> LintM a
711 = LintM (\ loc subst errs -> unLintM m loc (extendTvSubst subst tv ty) errs)
715 lookupIdInScope :: Id -> LintM Id
717 | not (mustHaveLocalBinding id)
718 = return id -- An imported Id
720 = do { subst <- getTvSubst
721 ; case lookupInScope (getTvInScope subst) id of
723 Nothing -> do { addErrL out_of_scope
726 out_of_scope = ppr id <+> ptext SLIT("is out of scope")
729 oneTupleDataConId :: Id -- Should not happen
730 oneTupleDataConId = dataConWorkId (tupleCon Boxed 1)
732 checkBndrIdInScope :: Var -> Var -> LintM ()
733 checkBndrIdInScope binder id
734 = checkInScope msg id
736 msg = ptext SLIT("is out of scope inside info for") <+>
739 checkTyVarInScope :: TyVar -> LintM ()
740 checkTyVarInScope tv = checkInScope (ptext SLIT("is out of scope")) tv
742 checkInScope :: SDoc -> Var -> LintM ()
743 checkInScope loc_msg var =
744 do { subst <- getTvSubst
745 ; checkL (not (mustHaveLocalBinding var) || (var `isInScope` subst))
746 (hsep [ppr var, loc_msg]) }
748 checkTys :: Type -> Type -> Message -> LintM ()
749 -- check ty2 is subtype of ty1 (ie, has same structure but usage
750 -- annotations need only be consistent, not equal)
751 -- Assumes ty1,ty2 are have alrady had the substitution applied
752 checkTys ty1 ty2 msg = checkL (ty1 `coreEqType` ty2) msg
755 %************************************************************************
757 \subsection{Error messages}
759 %************************************************************************
762 dumpLoc :: LintLocInfo -> (SrcLoc, SDoc)
765 = (getSrcLoc v, brackets (ptext SLIT("RHS of") <+> pp_binders [v]))
767 dumpLoc (LambdaBodyOf b)
768 = (getSrcLoc b, brackets (ptext SLIT("in body of lambda with binder") <+> pp_binder b))
770 dumpLoc (BodyOfLetRec [])
771 = (noSrcLoc, brackets (ptext SLIT("In body of a letrec with no binders")))
773 dumpLoc (BodyOfLetRec bs@(_:_))
774 = ( getSrcLoc (head bs), brackets (ptext SLIT("in body of letrec with binders") <+> pp_binders bs))
777 = (noSrcLoc, text "In the expression:" <+> ppr e)
779 dumpLoc (CaseAlt (con, args, _))
780 = (noSrcLoc, text "In a case alternative:" <+> parens (ppr con <+> pp_binders args))
782 dumpLoc (CasePat (con, args, _))
783 = (noSrcLoc, text "In the pattern of a case alternative:" <+> parens (ppr con <+> pp_binders args))
785 dumpLoc (ImportedUnfolding locn)
786 = (locn, brackets (ptext SLIT("in an imported unfolding")))
787 dumpLoc TopLevelBindings
790 pp_binders :: [Var] -> SDoc
791 pp_binders bs = sep (punctuate comma (map pp_binder bs))
793 pp_binder :: Var -> SDoc
794 pp_binder b | isId b = hsep [ppr b, dcolon, ppr (idType b)]
795 | otherwise = hsep [ppr b, dcolon, ppr (tyVarKind b)]
799 ------------------------------------------------------
800 -- Messages for case expressions
802 mkNullAltsMsg :: CoreExpr -> Message
804 = hang (text "Case expression with no alternatives:")
807 mkDefaultArgsMsg :: [Var] -> Message
808 mkDefaultArgsMsg args
809 = hang (text "DEFAULT case with binders")
812 mkCaseAltMsg :: CoreExpr -> Type -> Type -> Message
813 mkCaseAltMsg e ty1 ty2
814 = hang (text "Type of case alternatives not the same as the annotation on case:")
815 4 (vcat [ppr ty1, ppr ty2, ppr e])
817 mkScrutMsg :: Id -> Type -> Type -> TvSubst -> Message
818 mkScrutMsg var var_ty scrut_ty subst
819 = vcat [text "Result binder in case doesn't match scrutinee:" <+> ppr var,
820 text "Result binder type:" <+> ppr var_ty,--(idType var),
821 text "Scrutinee type:" <+> ppr scrut_ty,
822 hsep [ptext SLIT("Current TV subst"), ppr subst]]
824 mkNonDefltMsg, mkNonIncreasingAltsMsg :: CoreExpr -> Message
826 = hang (text "Case expression with DEFAULT not at the beginnning") 4 (ppr e)
827 mkNonIncreasingAltsMsg e
828 = hang (text "Case expression with badly-ordered alternatives") 4 (ppr e)
830 nonExhaustiveAltsMsg :: CoreExpr -> Message
831 nonExhaustiveAltsMsg e
832 = hang (text "Case expression with non-exhaustive alternatives") 4 (ppr e)
834 mkBadConMsg :: TyCon -> DataCon -> Message
835 mkBadConMsg tycon datacon
837 text "In a case alternative, data constructor isn't in scrutinee type:",
838 text "Scrutinee type constructor:" <+> ppr tycon,
839 text "Data con:" <+> ppr datacon
842 mkBadPatMsg :: Type -> Type -> Message
843 mkBadPatMsg con_result_ty scrut_ty
845 text "In a case alternative, pattern result type doesn't match scrutinee type:",
846 text "Pattern result type:" <+> ppr con_result_ty,
847 text "Scrutinee type:" <+> ppr scrut_ty
850 mkBadAltMsg :: Type -> CoreAlt -> Message
851 mkBadAltMsg scrut_ty alt
852 = vcat [ text "Data alternative when scrutinee is not a tycon application",
853 text "Scrutinee type:" <+> ppr scrut_ty,
854 text "Alternative:" <+> pprCoreAlt alt ]
856 mkNewTyDataConAltMsg :: Type -> CoreAlt -> Message
857 mkNewTyDataConAltMsg scrut_ty alt
858 = vcat [ text "Data alternative for newtype datacon",
859 text "Scrutinee type:" <+> ppr scrut_ty,
860 text "Alternative:" <+> pprCoreAlt alt ]
863 ------------------------------------------------------
864 -- Other error messages
866 mkAppMsg :: Type -> Type -> CoreExpr -> Message
867 mkAppMsg fun_ty arg_ty arg
868 = vcat [ptext SLIT("Argument value doesn't match argument type:"),
869 hang (ptext SLIT("Fun type:")) 4 (ppr fun_ty),
870 hang (ptext SLIT("Arg type:")) 4 (ppr arg_ty),
871 hang (ptext SLIT("Arg:")) 4 (ppr arg)]
873 mkNonFunAppMsg :: Type -> Type -> CoreExpr -> Message
874 mkNonFunAppMsg fun_ty arg_ty arg
875 = vcat [ptext SLIT("Non-function type in function position"),
876 hang (ptext SLIT("Fun type:")) 4 (ppr fun_ty),
877 hang (ptext SLIT("Arg type:")) 4 (ppr arg_ty),
878 hang (ptext SLIT("Arg:")) 4 (ppr arg)]
880 mkKindErrMsg :: TyVar -> Type -> Message
881 mkKindErrMsg tyvar arg_ty
882 = vcat [ptext SLIT("Kinds don't match in type application:"),
883 hang (ptext SLIT("Type variable:"))
884 4 (ppr tyvar <+> dcolon <+> ppr (tyVarKind tyvar)),
885 hang (ptext SLIT("Arg type:"))
886 4 (ppr arg_ty <+> dcolon <+> ppr (typeKind arg_ty))]
888 mkTyAppMsg :: Type -> Type -> Message
890 = vcat [text "Illegal type application:",
891 hang (ptext SLIT("Exp type:"))
892 4 (ppr ty <+> dcolon <+> ppr (typeKind ty)),
893 hang (ptext SLIT("Arg type:"))
894 4 (ppr arg_ty <+> dcolon <+> ppr (typeKind arg_ty))]
896 mkRhsMsg :: Id -> Type -> Message
899 [hsep [ptext SLIT("The type of this binder doesn't match the type of its RHS:"),
901 hsep [ptext SLIT("Binder's type:"), ppr (idType binder)],
902 hsep [ptext SLIT("Rhs type:"), ppr ty]]
904 mkRhsPrimMsg :: Id -> CoreExpr -> Message
905 mkRhsPrimMsg binder _rhs
906 = vcat [hsep [ptext SLIT("The type of this binder is primitive:"),
908 hsep [ptext SLIT("Binder's type:"), ppr (idType binder)]
911 mkStrictMsg :: Id -> Message
913 = vcat [hsep [ptext SLIT("Recursive or top-level binder has strict demand info:"),
915 hsep [ptext SLIT("Binder's demand info:"), ppr (idNewDemandInfo binder)]
918 mkArityMsg :: Id -> Message
920 = vcat [hsep [ptext SLIT("Demand type has "),
921 ppr (dmdTypeDepth dmd_ty),
922 ptext SLIT(" arguments, rhs has "),
923 ppr (idArity binder),
924 ptext SLIT("arguments, "),
926 hsep [ptext SLIT("Binder's strictness signature:"), ppr dmd_ty]
929 where (StrictSig dmd_ty) = idNewStrictness binder
931 mkUnboxedTupleMsg :: Id -> Message
932 mkUnboxedTupleMsg binder
933 = vcat [hsep [ptext SLIT("A variable has unboxed tuple type:"), ppr binder],
934 hsep [ptext SLIT("Binder's type:"), ppr (idType binder)]]
936 mkCastErr :: Type -> Type -> Message
937 mkCastErr from_ty expr_ty
938 = vcat [ptext SLIT("From-type of Cast differs from type of enclosed expression"),
939 ptext SLIT("From-type:") <+> ppr from_ty,
940 ptext SLIT("Type of enclosed expr:") <+> ppr expr_ty
943 dupVars :: [[Var]] -> Message
945 = hang (ptext SLIT("Duplicate variables brought into scope"))
948 mkStrangeTyMsg :: CoreExpr -> Message
950 = ptext SLIT("Type where expression expected:") <+> ppr e