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"
47 %************************************************************************
51 %************************************************************************
53 @showPass@ and @endPass@ don't really belong here, but it makes a convenient
54 place for them. They print out stuff before and after core passes,
55 and do Core Lint when necessary.
58 endPass :: DynFlags -> String -> DynFlag -> [CoreBind] -> IO [CoreBind]
59 endPass = dumpAndLint dumpIfSet_core
61 endPassIf :: Bool -> DynFlags -> String -> DynFlag -> [CoreBind] -> IO [CoreBind]
62 endPassIf cond = dumpAndLint (dumpIf_core cond)
64 endIteration :: DynFlags -> String -> DynFlag -> [CoreBind] -> IO [CoreBind]
65 endIteration = dumpAndLint dumpIfSet_dyn
67 dumpAndLint :: (DynFlags -> DynFlag -> String -> SDoc -> IO ())
68 -> DynFlags -> String -> DynFlag -> [CoreBind] -> IO [CoreBind]
69 dumpAndLint dump dflags pass_name dump_flag binds
71 -- Report result size if required
72 -- This has the side effect of forcing the intermediate to be evaluated
73 debugTraceMsg dflags 2 $
74 (text " Result size =" <+> int (coreBindsSize binds))
76 -- Report verbosely, if required
77 dump dflags dump_flag pass_name (pprCoreBindings binds)
80 lintCoreBindings dflags pass_name binds
86 %************************************************************************
88 \subsection[lintCoreBindings]{@lintCoreBindings@: Top-level interface}
90 %************************************************************************
92 Checks that a set of core bindings is well-formed. The PprStyle and String
93 just control what we print in the event of an error. The Bool value
94 indicates whether we have done any specialisation yet (in which case we do
99 (b) Out-of-scope type variables
100 (c) Out-of-scope local variables
103 If we have done specialisation the we check that there are
104 (a) No top-level bindings of primitive (unboxed type)
109 -- Things are *not* OK if:
111 -- * Unsaturated type app before specialisation has been done;
113 -- * Oversaturated type app after specialisation (eta reduction
114 -- may well be happening...);
119 In the desugarer, it's very very convenient to be able to say (in effect)
120 let a = Int in <body>
121 That is, use a type let. (See notes just below for why we want this.)
123 We don't have type lets in Core, so the desugarer uses type lambda
125 However, in the lambda form, we'd get lint errors from:
126 (/\a. let x::a = 4 in <body>) Int
127 because (x::a) doesn't look compatible with (4::Int).
129 So (HACK ALERT) the Lint phase does type-beta reduction "on the fly",
130 as it were. It carries a type substitution (in this example [a -> Int])
131 and applies this substitution before comparing types. The functin
132 lintTy :: Type -> LintM Type
133 returns a substituted type; that's the only reason it returns anything.
135 When we encounter a binder (like x::a) we must apply the substitution
136 to the type of the binding variable. lintBinders does this.
138 For Ids, the type-substituted Id is added to the in_scope set (which
139 itself is part of the TvSubst we are carrying down), and when we
140 find an occurence of an Id, we fetch it from the in-scope set.
145 It's needed when dealing with desugarer output for GADTs. Consider
146 data T = forall a. T a (a->Int) Bool
148 f (T x f True) = <e1>
149 f (T y g False) = <e2>
150 After desugaring we get
152 T a (x::a) (f::a->Int) (b:Bool) ->
155 False -> (/\b. let y=x; g=f in <e2>) a
156 And for a reason I now forget, the ...<e2>... can mention a; so
157 we want Lint to know that b=a. Ugh.
159 I tried quite hard to make the necessity for this go away, by changing the
160 desugarer, but the fundamental problem is this:
162 T a (x::a) (y::Int) -> let fail::a = ...
163 in (/\b. ...(case ... of
167 Now the inner case look as though it has incompatible branches.
171 lintCoreBindings :: DynFlags -> String -> [CoreBind] -> IO ()
173 lintCoreBindings dflags _whoDunnit _binds
174 | not (dopt Opt_DoCoreLinting dflags)
177 lintCoreBindings dflags whoDunnit binds
178 = case (initL (lint_binds binds)) of
179 Nothing -> showPass dflags ("Core Linted result of " ++ whoDunnit)
180 Just bad_news -> printDump (display bad_news) >>
183 -- Put all the top-level binders in scope at the start
184 -- This is because transformation rules can bring something
185 -- into use 'unexpectedly'
186 lint_binds binds = addLoc TopLevelBindings $
187 addInScopeVars (bindersOfBinds binds) $
190 lint_bind (Rec prs) = mapM_ (lintSingleBinding TopLevel Recursive) prs
191 lint_bind (NonRec bndr rhs) = lintSingleBinding TopLevel NonRecursive (bndr,rhs)
194 = vcat [ text ("*** Core Lint Errors: in result of " ++ whoDunnit ++ " ***"),
196 ptext (sLit "*** Offending Program ***"),
197 pprCoreBindings binds,
198 ptext (sLit "*** End of Offense ***")
202 %************************************************************************
204 \subsection[lintUnfolding]{lintUnfolding}
206 %************************************************************************
208 We use this to check all unfoldings that come in from interfaces
209 (it is very painful to catch errors otherwise):
212 lintUnfolding :: SrcLoc
213 -> [Var] -- Treat these as in scope
215 -> Maybe Message -- Nothing => OK
217 lintUnfolding locn vars expr
218 = initL (addLoc (ImportedUnfolding locn) $
219 addInScopeVars vars $
223 %************************************************************************
225 \subsection[lintCoreBinding]{lintCoreBinding}
227 %************************************************************************
229 Check a core binding, returning the list of variables bound.
232 lintSingleBinding :: TopLevelFlag -> RecFlag -> (Id, CoreExpr) -> LintM ()
233 lintSingleBinding top_lvl_flag rec_flag (binder,rhs)
234 = addLoc (RhsOf binder) $
236 do { ty <- lintCoreExpr rhs
237 ; lintBinder binder -- Check match to RHS type
238 ; binder_ty <- applySubst binder_ty
239 ; checkTys binder_ty ty (mkRhsMsg binder ty)
240 -- Check (not isUnLiftedType) (also checks for bogus unboxed tuples)
241 ; checkL (not (isUnLiftedType binder_ty)
242 || (isNonRec rec_flag && exprOkForSpeculation rhs))
243 (mkRhsPrimMsg binder rhs)
244 -- Check that if the binder is top-level or recursive, it's not demanded
245 ; checkL (not (isStrictId binder)
246 || (isNonRec rec_flag && not (isTopLevel top_lvl_flag)))
248 -- Check whether binder's specialisations contain any out-of-scope variables
249 ; mapM_ (checkBndrIdInScope binder) bndr_vars
251 -- Check whether arity and demand type are consistent (only if demand analysis
253 ; checkL (case maybeDmdTy of
254 Just (StrictSig dmd_ty) -> idArity binder >= dmdTypeDepth dmd_ty || exprIsTrivial rhs
256 (mkArityMsg binder) }
258 -- We should check the unfolding, if any, but this is tricky because
259 -- the unfolding is a SimplifiableCoreExpr. Give up for now.
261 binder_ty = idType binder
262 maybeDmdTy = idNewStrictness_maybe binder
263 bndr_vars = varSetElems (idFreeVars binder)
264 lintBinder var | isId var = lintIdBndr var $ \_ -> (return ())
265 | otherwise = return ()
268 %************************************************************************
270 \subsection[lintCoreExpr]{lintCoreExpr}
272 %************************************************************************
275 type InType = Type -- Substitution not yet applied
276 type OutType = Type -- Substitution has been applied to this
278 lintCoreExpr :: CoreExpr -> LintM OutType
279 -- The returned type has the substitution from the monad
280 -- already applied to it:
281 -- lintCoreExpr e subst = exprType (subst e)
283 lintCoreExpr (Var var)
284 = do { checkL (not (var == oneTupleDataConId))
285 (ptext (sLit "Illegal one-tuple"))
286 ; var' <- lookupIdInScope var
287 ; return (idType var')
290 lintCoreExpr (Lit lit)
291 = return (literalType lit)
293 --lintCoreExpr (Note (Coerce to_ty from_ty) expr)
294 -- = do { expr_ty <- lintCoreExpr expr
295 -- ; to_ty <- lintTy to_ty
296 -- ; from_ty <- lintTy from_ty
297 -- ; checkTys from_ty expr_ty (mkCoerceErr from_ty expr_ty)
300 lintCoreExpr (Cast expr co)
301 = do { expr_ty <- lintCoreExpr expr
303 ; let (from_ty, to_ty) = coercionKind co'
304 ; checkTys from_ty expr_ty (mkCastErr from_ty expr_ty)
307 lintCoreExpr (Note _ expr)
310 lintCoreExpr (Let (NonRec bndr rhs) body)
311 = do { lintSingleBinding NotTopLevel NonRecursive (bndr,rhs)
312 ; addLoc (BodyOfLetRec [bndr])
313 (lintAndScopeId bndr $ \_ -> (lintCoreExpr body)) }
315 lintCoreExpr (Let (Rec pairs) body)
316 = lintAndScopeIds bndrs $ \_ ->
317 do { mapM (lintSingleBinding NotTopLevel Recursive) pairs
318 ; addLoc (BodyOfLetRec bndrs) (lintCoreExpr body) }
320 bndrs = map fst pairs
322 lintCoreExpr e@(App fun (Type ty))
323 -- See Note [Type let] above
324 = addLoc (AnExpr e) $
327 go (App fun (Type ty)) tys
328 = do { go fun (ty:tys) }
329 go (Lam tv body) (ty:tys)
330 = do { checkL (isTyVar tv) (mkKindErrMsg tv ty) -- Not quite accurate
332 ; let kind = tyVarKind tv
333 ; kind' <- lintTy kind
334 ; let tv' = setTyVarKind tv kind'
336 -- Now extend the substitution so we
337 -- take advantage of it in the body
338 ; addInScopeVars [tv'] $
339 extendSubstL tv' ty' $
342 = do { fun_ty <- lintCoreExpr fun
343 ; lintCoreArgs fun_ty (map Type tys) }
345 lintCoreExpr e@(App fun arg)
346 = do { fun_ty <- lintCoreExpr fun
347 ; addLoc (AnExpr e) $
348 lintCoreArg fun_ty arg }
350 lintCoreExpr (Lam var expr)
351 = addLoc (LambdaBodyOf var) $
352 lintBinders [var] $ \[var'] ->
353 do { body_ty <- lintCoreExpr expr
355 return (mkFunTy (idType var') body_ty)
357 return (mkForAllTy var' body_ty)
359 -- The applySubst is needed to apply the subst to var
361 lintCoreExpr e@(Case scrut var alt_ty alts) =
362 -- Check the scrutinee
363 do { scrut_ty <- lintCoreExpr scrut
364 ; alt_ty <- lintTy alt_ty
365 ; var_ty <- lintTy (idType var)
367 ; let mb_tc_app = splitTyConApp_maybe (idType var)
372 null (tyConDataCons tycon) ->
373 pprTrace "Lint warning: case binder's type has no constructors" (ppr var <+> ppr (idType var))
374 -- This can legitimately happen for type families
376 _otherwise -> return ()
378 -- Don't use lintIdBndr on var, because unboxed tuple is legitimate
380 ; subst <- getTvSubst
381 ; checkTys var_ty scrut_ty (mkScrutMsg var var_ty scrut_ty subst)
383 -- If the binder is an unboxed tuple type, don't put it in scope
384 ; let scope = if (isUnboxedTupleType (idType var)) then
386 else lintAndScopeId var
388 do { -- Check the alternatives
389 mapM (lintCoreAlt scrut_ty alt_ty) alts
390 ; checkCaseAlts e scrut_ty alts
395 lintCoreExpr e@(Type _)
396 = addErrL (mkStrangeTyMsg e)
399 %************************************************************************
401 \subsection[lintCoreArgs]{lintCoreArgs}
403 %************************************************************************
405 The basic version of these functions checks that the argument is a
406 subtype of the required type, as one would expect.
409 lintCoreArgs :: OutType -> [CoreArg] -> LintM OutType
410 lintCoreArg :: OutType -> CoreArg -> LintM OutType
411 -- First argument has already had substitution applied to it
415 lintCoreArgs ty [] = return ty
416 lintCoreArgs ty (a : args) =
417 do { res <- lintCoreArg ty a
418 ; lintCoreArgs res args }
420 lintCoreArg fun_ty (Type arg_ty) =
421 do { arg_ty <- lintTy arg_ty
422 ; lintTyApp fun_ty arg_ty }
424 lintCoreArg fun_ty arg =
425 -- Make sure function type matches argument
426 do { arg_ty <- lintCoreExpr arg
427 ; let err1 = mkAppMsg fun_ty arg_ty arg
428 err2 = mkNonFunAppMsg fun_ty arg_ty arg
429 ; case splitFunTy_maybe fun_ty of
431 do { checkTys arg arg_ty err1
437 -- Both args have had substitution applied
438 lintTyApp :: OutType -> OutType -> LintM OutType
440 = case splitForAllTy_maybe ty of
441 Nothing -> addErrL (mkTyAppMsg ty arg_ty)
444 -> do { checkL (isTyVar tyvar) (mkTyAppMsg ty arg_ty)
445 ; checkKinds tyvar arg_ty
446 ; return (substTyWith [tyvar] [arg_ty] body) }
448 checkKinds :: Var -> Type -> LintM ()
449 checkKinds tyvar arg_ty
450 -- Arg type might be boxed for a function with an uncommitted
451 -- tyvar; notably this is used so that we can give
452 -- error :: forall a:*. String -> a
453 -- and then apply it to both boxed and unboxed types.
454 = checkL (arg_kind `isSubKind` tyvar_kind)
455 (mkKindErrMsg tyvar arg_ty)
457 tyvar_kind = tyVarKind tyvar
458 arg_kind | isCoVar tyvar = coercionKindPredTy arg_ty
459 | otherwise = typeKind arg_ty
463 %************************************************************************
465 \subsection[lintCoreAlts]{lintCoreAlts}
467 %************************************************************************
470 checkCaseAlts :: CoreExpr -> OutType -> [CoreAlt] -> LintM ()
471 -- a) Check that the alts are non-empty
472 -- b1) Check that the DEFAULT comes first, if it exists
473 -- b2) Check that the others are in increasing order
474 -- c) Check that there's a default for infinite types
475 -- NB: Algebraic cases are not necessarily exhaustive, because
476 -- the simplifer correctly eliminates case that can't
480 = addErrL (mkNullAltsMsg e)
482 checkCaseAlts e ty alts =
483 do { checkL (all non_deflt con_alts) (mkNonDefltMsg e)
484 ; checkL (increasing_tag con_alts) (mkNonIncreasingAltsMsg e)
485 ; checkL (isJust maybe_deflt || not is_infinite_ty)
486 (nonExhaustiveAltsMsg e) }
488 (con_alts, maybe_deflt) = findDefault alts
490 -- Check that successive alternatives have increasing tags
491 increasing_tag (alt1 : rest@( alt2 : _)) = alt1 `ltAlt` alt2 && increasing_tag rest
492 increasing_tag _ = True
494 non_deflt (DEFAULT, _, _) = False
497 is_infinite_ty = case splitTyConApp_maybe ty of
499 Just (tycon, _) -> isPrimTyCon tycon
503 checkAltExpr :: CoreExpr -> OutType -> LintM ()
504 checkAltExpr expr ann_ty
505 = do { actual_ty <- lintCoreExpr expr
506 ; checkTys actual_ty ann_ty (mkCaseAltMsg expr actual_ty ann_ty) }
508 lintCoreAlt :: OutType -- Type of scrutinee
509 -> OutType -- Type of the alternative
513 lintCoreAlt _ alt_ty (DEFAULT, args, rhs) =
514 do { checkL (null args) (mkDefaultArgsMsg args)
515 ; checkAltExpr rhs alt_ty }
517 lintCoreAlt scrut_ty alt_ty (LitAlt lit, args, rhs) =
518 do { checkL (null args) (mkDefaultArgsMsg args)
519 ; checkTys lit_ty scrut_ty (mkBadPatMsg lit_ty scrut_ty)
520 ; checkAltExpr rhs alt_ty }
522 lit_ty = literalType lit
524 lintCoreAlt scrut_ty alt_ty alt@(DataAlt con, args, rhs)
525 | isNewTyCon (dataConTyCon con) = addErrL (mkNewTyDataConAltMsg scrut_ty alt)
526 | Just (tycon, tycon_arg_tys) <- splitTyConApp_maybe scrut_ty
527 = addLoc (CaseAlt alt) $ do
528 { -- First instantiate the universally quantified
529 -- type variables of the data constructor
530 -- We've already check
531 checkL (tycon == dataConTyCon con) (mkBadConMsg tycon con)
532 ; let con_payload_ty = applyTys (dataConRepType con) tycon_arg_tys
534 -- And now bring the new binders into scope
535 ; lintBinders args $ \ args -> do
536 { addLoc (CasePat alt) $ do
537 { -- Check the pattern
538 -- Scrutinee type must be a tycon applicn; checked by caller
539 -- This code is remarkably compact considering what it does!
540 -- NB: args must be in scope here so that the lintCoreArgs
542 -- NB: relies on existential type args coming *after*
543 -- ordinary type args
544 ; con_result_ty <- lintCoreArgs con_payload_ty (varsToCoreExprs args)
545 ; checkTys con_result_ty scrut_ty (mkBadPatMsg con_result_ty scrut_ty)
548 ; checkAltExpr rhs alt_ty } }
550 | otherwise -- Scrut-ty is wrong shape
551 = addErrL (mkBadAltMsg scrut_ty alt)
554 %************************************************************************
556 \subsection[lint-types]{Types}
558 %************************************************************************
561 -- When we lint binders, we (one at a time and in order):
562 -- 1. Lint var types or kinds (possibly substituting)
563 -- 2. Add the binder to the in scope set, and if its a coercion var,
564 -- we may extend the substitution to reflect its (possibly) new kind
565 lintBinders :: [Var] -> ([Var] -> LintM a) -> LintM a
566 lintBinders [] linterF = linterF []
567 lintBinders (var:vars) linterF = lintBinder var $ \var' ->
568 lintBinders vars $ \ vars' ->
571 lintBinder :: Var -> (Var -> LintM a) -> LintM a
572 lintBinder var linterF
573 | isTyVar var = lint_ty_bndr
574 | otherwise = lintIdBndr var linterF
576 lint_ty_bndr = do { lintTy (tyVarKind var)
577 ; subst <- getTvSubst
578 ; let (subst', tv') = substTyVarBndr subst var
579 ; updateTvSubst subst' (linterF tv') }
581 lintIdBndr :: Var -> (Var -> LintM a) -> LintM a
582 -- Do substitution on the type of a binder and add the var with this
583 -- new type to the in-scope set of the second argument
584 -- ToDo: lint its rules
585 lintIdBndr id linterF
586 = do { checkL (not (isUnboxedTupleType (idType id)))
587 (mkUnboxedTupleMsg id)
588 -- No variable can be bound to an unboxed tuple.
589 ; lintAndScopeId id $ \id' -> linterF id'
592 lintAndScopeIds :: [Var] -> ([Var] -> LintM a) -> LintM a
593 lintAndScopeIds ids linterF
597 go (id:ids) = do { lintAndScopeId id $ \id ->
598 lintAndScopeIds ids $ \ids ->
601 lintAndScopeId :: Var -> (Var -> LintM a) -> LintM a
602 lintAndScopeId id linterF
603 = do { ty <- lintTy (idType id)
604 ; let id' = Var.setIdType id ty
605 ; addInScopeVars [id'] $ (linterF id')
608 lintTy :: InType -> LintM OutType
609 -- Check the type, and apply the substitution to it
610 -- ToDo: check the kind structure of the type
612 = do { ty' <- applySubst ty
613 ; mapM_ checkTyVarInScope (varSetElems (tyVarsOfType ty'))
618 %************************************************************************
620 \subsection[lint-monad]{The Lint monad}
622 %************************************************************************
627 [LintLocInfo] -> -- Locations
628 TvSubst -> -- Current type substitution; we also use this
629 -- to keep track of all the variables in scope,
630 -- both Ids and TyVars
631 Bag Message -> -- Error messages so far
632 (Maybe a, Bag Message) } -- Result and error messages (if any)
634 {- Note [Type substitution]
635 ~~~~~~~~~~~~~~~~~~~~~~~~
636 Why do we need a type substitution? Consider
637 /\(a:*). \(x:a). /\(a:*). id a x
638 This is ill typed, because (renaming variables) it is really
639 /\(a:*). \(x:a). /\(b:*). id b x
640 Hence, when checking an application, we can't naively compare x's type
641 (at its binding site) with its expected type (at a use site). So we
642 rename type binders as we go, maintaining a substitution.
644 The same substitution also supports let-type, current expressed as
646 Here we substitute 'ty' for 'a' in 'body', on the fly.
649 instance Monad LintM where
650 return x = LintM (\ _ _ errs -> (Just x, errs))
651 fail err = LintM (\ loc subst errs -> (Nothing, addErr subst errs (text err) loc))
652 m >>= k = LintM (\ loc subst errs ->
653 let (res, errs') = unLintM m loc subst errs in
655 Just r -> unLintM (k r) loc subst errs'
656 Nothing -> (Nothing, errs'))
659 = RhsOf Id -- The variable bound
660 | LambdaBodyOf Id -- The lambda-binder
661 | BodyOfLetRec [Id] -- One of the binders
662 | CaseAlt CoreAlt -- Case alternative
663 | CasePat CoreAlt -- *Pattern* of the case alternative
664 | AnExpr CoreExpr -- Some expression
665 | ImportedUnfolding SrcLoc -- Some imported unfolding (ToDo: say which)
671 initL :: LintM a -> Maybe Message {- errors -}
673 = case unLintM m [] emptyTvSubst emptyBag of
674 (_, errs) | isEmptyBag errs -> Nothing
675 | otherwise -> Just (vcat (punctuate (text "") (bagToList errs)))
679 checkL :: Bool -> Message -> LintM ()
680 checkL True _ = return ()
681 checkL False msg = addErrL msg
683 addErrL :: Message -> LintM a
684 addErrL msg = LintM (\ loc subst errs -> (Nothing, addErr subst errs msg loc))
686 addErr :: TvSubst -> Bag Message -> Message -> [LintLocInfo] -> Bag Message
687 addErr subst errs_so_far msg locs
688 = ASSERT( notNull locs )
689 errs_so_far `snocBag` mk_msg msg
691 (loc, cxt1) = dumpLoc (head locs)
692 cxts = [snd (dumpLoc loc) | loc <- locs]
693 context | opt_PprStyle_Debug = vcat (reverse cxts) $$ cxt1 $$
694 ptext (sLit "Substitution:") <+> ppr subst
697 mk_msg msg = mkLocMessage (mkSrcSpan loc loc) (context $$ msg)
699 addLoc :: LintLocInfo -> LintM a -> LintM a
701 LintM (\ loc subst errs -> unLintM m (extra_loc:loc) subst errs)
703 addInScopeVars :: [Var] -> LintM a -> LintM a
704 addInScopeVars vars m
706 = LintM (\ loc subst errs -> unLintM m loc (extendTvInScope subst vars) errs)
708 = addErrL (dupVars dups)
710 (_, dups) = removeDups compare vars
712 updateTvSubst :: TvSubst -> LintM a -> LintM a
713 updateTvSubst subst' m =
714 LintM (\ loc _ errs -> unLintM m loc subst' errs)
716 getTvSubst :: LintM TvSubst
717 getTvSubst = LintM (\ _ subst errs -> (Just subst, errs))
719 applySubst :: Type -> LintM Type
720 applySubst ty = do { subst <- getTvSubst; return (substTy subst ty) }
722 extendSubstL :: TyVar -> Type -> LintM a -> LintM a
724 = LintM (\ loc subst errs -> unLintM m loc (extendTvSubst subst tv ty) errs)
728 lookupIdInScope :: Id -> LintM Id
730 | not (mustHaveLocalBinding id)
731 = return id -- An imported Id
733 = do { subst <- getTvSubst
734 ; case lookupInScope (getTvInScope subst) id of
736 Nothing -> do { addErrL out_of_scope
739 out_of_scope = ppr id <+> ptext (sLit "is out of scope")
742 oneTupleDataConId :: Id -- Should not happen
743 oneTupleDataConId = dataConWorkId (tupleCon Boxed 1)
745 checkBndrIdInScope :: Var -> Var -> LintM ()
746 checkBndrIdInScope binder id
747 = checkInScope msg id
749 msg = ptext (sLit "is out of scope inside info for") <+>
752 checkTyVarInScope :: TyVar -> LintM ()
753 checkTyVarInScope tv = checkInScope (ptext (sLit "is out of scope")) tv
755 checkInScope :: SDoc -> Var -> LintM ()
756 checkInScope loc_msg var =
757 do { subst <- getTvSubst
758 ; checkL (not (mustHaveLocalBinding var) || (var `isInScope` subst))
759 (hsep [ppr var, loc_msg]) }
761 checkTys :: Type -> Type -> Message -> LintM ()
762 -- check ty2 is subtype of ty1 (ie, has same structure but usage
763 -- annotations need only be consistent, not equal)
764 -- Assumes ty1,ty2 are have alrady had the substitution applied
765 checkTys ty1 ty2 msg = checkL (ty1 `coreEqType` ty2) msg
768 %************************************************************************
770 \subsection{Error messages}
772 %************************************************************************
775 dumpLoc :: LintLocInfo -> (SrcLoc, SDoc)
778 = (getSrcLoc v, brackets (ptext (sLit "RHS of") <+> pp_binders [v]))
780 dumpLoc (LambdaBodyOf b)
781 = (getSrcLoc b, brackets (ptext (sLit "in body of lambda with binder") <+> pp_binder b))
783 dumpLoc (BodyOfLetRec [])
784 = (noSrcLoc, brackets (ptext (sLit "In body of a letrec with no binders")))
786 dumpLoc (BodyOfLetRec bs@(_:_))
787 = ( getSrcLoc (head bs), brackets (ptext (sLit "in body of letrec with binders") <+> pp_binders bs))
790 = (noSrcLoc, text "In the expression:" <+> ppr e)
792 dumpLoc (CaseAlt (con, args, _))
793 = (noSrcLoc, text "In a case alternative:" <+> parens (ppr con <+> pp_binders args))
795 dumpLoc (CasePat (con, args, _))
796 = (noSrcLoc, text "In the pattern of a case alternative:" <+> parens (ppr con <+> pp_binders args))
798 dumpLoc (ImportedUnfolding locn)
799 = (locn, brackets (ptext (sLit "in an imported unfolding")))
800 dumpLoc TopLevelBindings
803 pp_binders :: [Var] -> SDoc
804 pp_binders bs = sep (punctuate comma (map pp_binder bs))
806 pp_binder :: Var -> SDoc
807 pp_binder b | isId b = hsep [ppr b, dcolon, ppr (idType b)]
808 | otherwise = hsep [ppr b, dcolon, ppr (tyVarKind b)]
812 ------------------------------------------------------
813 -- Messages for case expressions
815 mkNullAltsMsg :: CoreExpr -> Message
817 = hang (text "Case expression with no alternatives:")
820 mkDefaultArgsMsg :: [Var] -> Message
821 mkDefaultArgsMsg args
822 = hang (text "DEFAULT case with binders")
825 mkCaseAltMsg :: CoreExpr -> Type -> Type -> Message
826 mkCaseAltMsg e ty1 ty2
827 = hang (text "Type of case alternatives not the same as the annotation on case:")
828 4 (vcat [ppr ty1, ppr ty2, ppr e])
830 mkScrutMsg :: Id -> Type -> Type -> TvSubst -> Message
831 mkScrutMsg var var_ty scrut_ty subst
832 = vcat [text "Result binder in case doesn't match scrutinee:" <+> ppr var,
833 text "Result binder type:" <+> ppr var_ty,--(idType var),
834 text "Scrutinee type:" <+> ppr scrut_ty,
835 hsep [ptext (sLit "Current TV subst"), ppr subst]]
837 mkNonDefltMsg, mkNonIncreasingAltsMsg :: CoreExpr -> Message
839 = hang (text "Case expression with DEFAULT not at the beginnning") 4 (ppr e)
840 mkNonIncreasingAltsMsg e
841 = hang (text "Case expression with badly-ordered alternatives") 4 (ppr e)
843 nonExhaustiveAltsMsg :: CoreExpr -> Message
844 nonExhaustiveAltsMsg e
845 = hang (text "Case expression with non-exhaustive alternatives") 4 (ppr e)
847 mkBadConMsg :: TyCon -> DataCon -> Message
848 mkBadConMsg tycon datacon
850 text "In a case alternative, data constructor isn't in scrutinee type:",
851 text "Scrutinee type constructor:" <+> ppr tycon,
852 text "Data con:" <+> ppr datacon
855 mkBadPatMsg :: Type -> Type -> Message
856 mkBadPatMsg con_result_ty scrut_ty
858 text "In a case alternative, pattern result type doesn't match scrutinee type:",
859 text "Pattern result type:" <+> ppr con_result_ty,
860 text "Scrutinee type:" <+> ppr scrut_ty
863 mkBadAltMsg :: Type -> CoreAlt -> Message
864 mkBadAltMsg scrut_ty alt
865 = vcat [ text "Data alternative when scrutinee is not a tycon application",
866 text "Scrutinee type:" <+> ppr scrut_ty,
867 text "Alternative:" <+> pprCoreAlt alt ]
869 mkNewTyDataConAltMsg :: Type -> CoreAlt -> Message
870 mkNewTyDataConAltMsg scrut_ty alt
871 = vcat [ text "Data alternative for newtype datacon",
872 text "Scrutinee type:" <+> ppr scrut_ty,
873 text "Alternative:" <+> pprCoreAlt alt ]
876 ------------------------------------------------------
877 -- Other error messages
879 mkAppMsg :: Type -> Type -> CoreExpr -> Message
880 mkAppMsg fun_ty arg_ty arg
881 = vcat [ptext (sLit "Argument value doesn't match argument type:"),
882 hang (ptext (sLit "Fun type:")) 4 (ppr fun_ty),
883 hang (ptext (sLit "Arg type:")) 4 (ppr arg_ty),
884 hang (ptext (sLit "Arg:")) 4 (ppr arg)]
886 mkNonFunAppMsg :: Type -> Type -> CoreExpr -> Message
887 mkNonFunAppMsg fun_ty arg_ty arg
888 = vcat [ptext (sLit "Non-function type in function position"),
889 hang (ptext (sLit "Fun type:")) 4 (ppr fun_ty),
890 hang (ptext (sLit "Arg type:")) 4 (ppr arg_ty),
891 hang (ptext (sLit "Arg:")) 4 (ppr arg)]
893 mkKindErrMsg :: TyVar -> Type -> Message
894 mkKindErrMsg tyvar arg_ty
895 = vcat [ptext (sLit "Kinds don't match in type application:"),
896 hang (ptext (sLit "Type variable:"))
897 4 (ppr tyvar <+> dcolon <+> ppr (tyVarKind tyvar)),
898 hang (ptext (sLit "Arg type:"))
899 4 (ppr arg_ty <+> dcolon <+> ppr (typeKind arg_ty))]
901 mkTyAppMsg :: Type -> Type -> Message
903 = vcat [text "Illegal type application:",
904 hang (ptext (sLit "Exp type:"))
905 4 (ppr ty <+> dcolon <+> ppr (typeKind ty)),
906 hang (ptext (sLit "Arg type:"))
907 4 (ppr arg_ty <+> dcolon <+> ppr (typeKind arg_ty))]
909 mkRhsMsg :: Id -> Type -> Message
912 [hsep [ptext (sLit "The type of this binder doesn't match the type of its RHS:"),
914 hsep [ptext (sLit "Binder's type:"), ppr (idType binder)],
915 hsep [ptext (sLit "Rhs type:"), ppr ty]]
917 mkRhsPrimMsg :: Id -> CoreExpr -> Message
918 mkRhsPrimMsg binder _rhs
919 = vcat [hsep [ptext (sLit "The type of this binder is primitive:"),
921 hsep [ptext (sLit "Binder's type:"), ppr (idType binder)]
924 mkStrictMsg :: Id -> Message
926 = vcat [hsep [ptext (sLit "Recursive or top-level binder has strict demand info:"),
928 hsep [ptext (sLit "Binder's demand info:"), ppr (idNewDemandInfo binder)]
931 mkArityMsg :: Id -> Message
933 = vcat [hsep [ptext (sLit "Demand type has "),
934 ppr (dmdTypeDepth dmd_ty),
935 ptext (sLit " arguments, rhs has "),
936 ppr (idArity binder),
937 ptext (sLit "arguments, "),
939 hsep [ptext (sLit "Binder's strictness signature:"), ppr dmd_ty]
942 where (StrictSig dmd_ty) = idNewStrictness binder
944 mkUnboxedTupleMsg :: Id -> Message
945 mkUnboxedTupleMsg binder
946 = vcat [hsep [ptext (sLit "A variable has unboxed tuple type:"), ppr binder],
947 hsep [ptext (sLit "Binder's type:"), ppr (idType binder)]]
949 mkCastErr :: Type -> Type -> Message
950 mkCastErr from_ty expr_ty
951 = vcat [ptext (sLit "From-type of Cast differs from type of enclosed expression"),
952 ptext (sLit "From-type:") <+> ppr from_ty,
953 ptext (sLit "Type of enclosed expr:") <+> ppr expr_ty
956 dupVars :: [[Var]] -> Message
958 = hang (ptext (sLit "Duplicate variables brought into scope"))
961 mkStrangeTyMsg :: CoreExpr -> Message
963 = ptext (sLit "Type where expression expected:") <+> ppr e