2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
4 \section[StgLint]{A ``lint'' pass to check for Stg correctness}
7 module StgLint ( lintStgBindings ) where
9 #include "HsVersions.h"
13 import Bag ( Bag, emptyBag, isEmptyBag, snocBag, foldBag )
14 import Id ( Id, idType )
16 import DataCon ( DataCon, dataConArgTys, dataConRepType )
17 import PrimOp ( primOpType )
18 import Literal ( literalType, Literal )
19 import Maybes ( catMaybes )
20 import Name ( isLocallyDefined, getSrcLoc )
21 import ErrUtils ( ErrMsg, Message, addErrLocHdrLine, pprBagOfErrors, dontAddErrLoc )
22 import Type ( mkFunTys, splitFunTys, splitAlgTyConApp_maybe,
23 isUnLiftedType, isTyVarTy, splitForAllTys, Type
25 import PprType ( {- instance Outputable Type -} )
26 import TyCon ( TyCon, isDataTyCon )
27 import Util ( zipEqual )
30 infixr 9 `thenL`, `thenL_`, `thenMaybeL`, `thenMaybeL_`
34 (a) *some* type errors
35 (b) locally-defined variables used but not defined
37 %************************************************************************
39 \subsection{``lint'' for various constructs}
41 %************************************************************************
43 @lintStgBindings@ is the top-level interface function.
46 lintStgBindings :: String -> [StgBinding] -> [StgBinding]
48 lintStgBindings whodunnit binds
50 case (initL (lint_binds binds)) of
52 Just msg -> pprPanic "" (vcat [
53 ptext SLIT("*** Stg Lint ErrMsgs: in") <+> text whodunnit <+> ptext SLIT("***"),
55 ptext SLIT("*** Offending Program ***"),
57 ptext SLIT("*** End of Offense ***")])
59 lint_binds :: [StgBinding] -> LintM ()
61 lint_binds [] = returnL ()
62 lint_binds (bind:binds)
63 = lintStgBinds bind `thenL` \ binders ->
64 addInScopeVars binders (
71 lintStgArg :: StgArg -> LintM (Maybe Type)
72 lintStgArg (StgLitArg lit) = returnL (Just (literalType lit))
73 lintStgArg (StgVarArg v) = lintStgVar v
75 lintStgVar v = checkInScope v `thenL_`
76 returnL (Just (idType v))
80 lintStgBinds :: StgBinding -> LintM [Id] -- Returns the binders
81 lintStgBinds (StgNonRec binder rhs)
82 = lint_binds_help (binder,rhs) `thenL_`
85 lintStgBinds (StgRec pairs)
86 = addInScopeVars binders (
87 mapL lint_binds_help pairs `thenL_`
91 binders = [b | (b,_) <- pairs]
93 lint_binds_help (binder, rhs)
94 = addLoc (RhsOf binder) (
96 lintStgRhs rhs `thenL` \ maybe_rhs_ty ->
98 -- Check binder doesn't have unlifted type
99 checkL (not (isUnLiftedType binder_ty))
100 (mkUnLiftedTyMsg binder rhs) `thenL_`
102 -- Check match to RHS type
103 (case maybe_rhs_ty of
104 Nothing -> returnL ()
105 Just rhs_ty -> checkTys binder_ty
107 (mkRhsMsg binder rhs_ty)
113 binder_ty = idType binder
117 lintStgRhs :: StgRhs -> LintM (Maybe Type)
119 lintStgRhs (StgRhsClosure _ _ _ _ _ [] expr)
122 lintStgRhs (StgRhsClosure _ _ _ _ _ binders expr)
123 = addLoc (LambdaBodyOf binders) (
124 addInScopeVars binders (
125 lintStgExpr expr `thenMaybeL` \ body_ty ->
126 returnL (Just (mkFunTys (map idType binders) body_ty))
129 lintStgRhs (StgRhsCon _ con args)
130 = mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
131 case maybe_arg_tys of
132 Nothing -> returnL Nothing
133 Just arg_tys -> checkFunApp con_ty arg_tys (mkRhsConMsg con_ty arg_tys)
135 con_ty = dataConRepType con
139 lintStgExpr :: StgExpr -> LintM (Maybe Type) -- Nothing if error found
141 lintStgExpr (StgLit l) = returnL (Just (literalType l))
143 lintStgExpr e@(StgApp fun args)
144 = lintStgVar fun `thenMaybeL` \ fun_ty ->
145 mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
146 case maybe_arg_tys of
147 Nothing -> returnL Nothing
148 Just arg_tys -> checkFunApp fun_ty arg_tys (mkFunAppMsg fun_ty arg_tys e)
150 lintStgExpr e@(StgConApp con args)
151 = mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
152 case maybe_arg_tys of
153 Nothing -> returnL Nothing
154 Just arg_tys -> checkFunApp con_ty arg_tys (mkFunAppMsg con_ty arg_tys e)
156 con_ty = dataConRepType con
158 lintStgExpr e@(StgPrimApp op args _)
159 = mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
160 case maybe_arg_tys of
161 Nothing -> returnL Nothing
162 Just arg_tys -> checkFunApp op_ty arg_tys (mkFunAppMsg op_ty arg_tys e)
164 op_ty = primOpType op
166 lintStgExpr (StgLam _ bndrs _)
167 = addErrL (ptext SLIT("Unexpected StgLam") <+> ppr bndrs) `thenL_`
170 lintStgExpr (StgLet binds body)
171 = lintStgBinds binds `thenL` \ binders ->
172 addLoc (BodyOfLetRec binders) (
173 addInScopeVars binders (
177 lintStgExpr (StgLetNoEscape _ _ binds body)
178 = lintStgBinds binds `thenL` \ binders ->
179 addLoc (BodyOfLetRec binders) (
180 addInScopeVars binders (
184 lintStgExpr (StgSCC _ expr) = lintStgExpr expr
186 lintStgExpr e@(StgCase scrut _ _ bndr _ alts)
187 = lintStgExpr scrut `thenMaybeL` \ _ ->
188 checkTys (idType bndr) scrut_ty (mkDefltMsg bndr) `thenL_`
190 (trace (showSDoc (ppr e)) $
191 -- we only allow case of tail-call or primop.
193 StgApp _ _ -> returnL ()
194 StgConApp _ _ -> returnL ()
195 other -> addErrL (mkCaseOfCaseMsg e)) `thenL_`
197 addInScopeVars [bndr] (lintStgAlts alts scrut_ty)
200 scrut_ty = get_ty alts
202 get_ty (StgAlgAlts ty _ _) = ty
203 get_ty (StgPrimAlts ty _ _) = ty
207 lintStgAlts :: StgCaseAlts
208 -> Type -- Type of scrutinee
209 -> LintM (Maybe Type) -- Type of alternatives
211 lintStgAlts alts scrut_ty
213 StgAlgAlts _ alg_alts deflt ->
214 mapL (lintAlgAlt scrut_ty) alg_alts `thenL` \ maybe_alt_tys ->
215 lintDeflt deflt scrut_ty `thenL` \ maybe_deflt_ty ->
216 returnL (maybe_deflt_ty : maybe_alt_tys)
218 StgPrimAlts _ prim_alts deflt ->
219 mapL (lintPrimAlt scrut_ty) prim_alts `thenL` \ maybe_alt_tys ->
220 lintDeflt deflt scrut_ty `thenL` \ maybe_deflt_ty ->
221 returnL (maybe_deflt_ty : maybe_alt_tys)
222 ) `thenL` \ maybe_result_tys ->
223 -- Check the result types
224 case catMaybes (maybe_result_tys) of
225 [] -> returnL Nothing
227 (first_ty:tys) -> mapL check tys `thenL_`
228 returnL (Just first_ty)
230 check ty = checkTys first_ty ty (mkCaseAltMsg alts)
232 lintAlgAlt scrut_ty (con, args, _, rhs)
233 = (case splitAlgTyConApp_maybe scrut_ty of
235 addErrL (mkAlgAltMsg1 scrut_ty)
236 Just (tycon, tys_applied, cons) ->
238 arg_tys = dataConArgTys con tys_applied
239 -- This almost certainly does not work for existential constructors
241 checkL (con `elem` cons) (mkAlgAltMsg2 scrut_ty con) `thenL_`
242 checkL (length arg_tys == length args) (mkAlgAltMsg3 con args)
244 mapL check (zipEqual "lintAlgAlt:stg" arg_tys args) `thenL_`
247 addInScopeVars args (
251 check (ty, arg) = checkTys ty (idType arg) (mkAlgAltMsg4 ty arg)
253 -- elem: yes, the elem-list here can sometimes be long-ish,
254 -- but as it's use-once, probably not worth doing anything different
255 -- We give it its own copy, so it isn't overloaded.
257 elem x (y:ys) = x==y || elem x ys
259 lintPrimAlt scrut_ty alt@(lit,rhs)
260 = checkTys (literalType lit) scrut_ty (mkPrimAltMsg alt) `thenL_`
263 lintDeflt StgNoDefault scrut_ty = returnL Nothing
264 lintDeflt deflt@(StgBindDefault rhs) scrut_ty = lintStgExpr rhs
268 %************************************************************************
270 \subsection[lint-monad]{The Lint monad}
272 %************************************************************************
275 type LintM a = [LintLocInfo] -- Locations
276 -> IdSet -- Local vars in scope
277 -> Bag ErrMsg -- Error messages so far
278 -> (a, Bag ErrMsg) -- Result and error messages (if any)
281 = RhsOf Id -- The variable bound
282 | LambdaBodyOf [Id] -- The lambda-binder
283 | BodyOfLetRec [Id] -- One of the binders
286 (getSrcLoc v, ptext SLIT(" [RHS of ") <> pp_binders [v] <> char ']' )
287 dumpLoc (LambdaBodyOf bs) =
288 (getSrcLoc (head bs), ptext SLIT(" [in body of lambda with binders ") <> pp_binders bs <> char ']' )
290 dumpLoc (BodyOfLetRec bs) =
291 (getSrcLoc (head bs), ptext SLIT(" [in body of letrec with binders ") <> pp_binders bs <> char ']' )
294 pp_binders :: [Id] -> SDoc
296 = sep (punctuate comma (map pp_binder bs))
299 = hsep [ppr b, dcolon, ppr (idType b)]
303 initL :: LintM a -> Maybe Message
305 = case (m [] emptyVarSet emptyBag) of { (_, errs) ->
306 if isEmptyBag errs then
309 Just (pprBagOfErrors errs)
312 returnL :: a -> LintM a
313 returnL r loc scope errs = (r, errs)
315 thenL :: LintM a -> (a -> LintM b) -> LintM b
316 thenL m k loc scope errs
317 = case m loc scope errs of
318 (r, errs') -> k r loc scope errs'
320 thenL_ :: LintM a -> LintM b -> LintM b
321 thenL_ m k loc scope errs
322 = case m loc scope errs of
323 (_, errs') -> k loc scope errs'
325 thenMaybeL :: LintM (Maybe a) -> (a -> LintM (Maybe b)) -> LintM (Maybe b)
326 thenMaybeL m k loc scope errs
327 = case m loc scope errs of
328 (Nothing, errs2) -> (Nothing, errs2)
329 (Just r, errs2) -> k r loc scope errs2
331 thenMaybeL_ :: LintM (Maybe a) -> LintM (Maybe b) -> LintM (Maybe b)
332 thenMaybeL_ m k loc scope errs
333 = case m loc scope errs of
334 (Nothing, errs2) -> (Nothing, errs2)
335 (Just _, errs2) -> k loc scope errs2
337 mapL :: (a -> LintM b) -> [a] -> LintM [b]
338 mapL f [] = returnL []
341 mapL f xs `thenL` \ rs ->
344 mapMaybeL :: (a -> LintM (Maybe b)) -> [a] -> LintM (Maybe [b])
345 -- Returns Nothing if anything fails
346 mapMaybeL f [] = returnL (Just [])
348 = f x `thenMaybeL` \ r ->
349 mapMaybeL f xs `thenMaybeL` \ rs ->
350 returnL (Just (r:rs))
354 checkL :: Bool -> Message -> LintM ()
355 checkL True msg loc scope errs = ((), errs)
356 checkL False msg loc scope errs = ((), addErr errs msg loc)
358 addErrL :: Message -> LintM ()
359 addErrL msg loc scope errs = ((), addErr errs msg loc)
361 addErr :: Bag ErrMsg -> Message -> [LintLocInfo] -> Bag ErrMsg
363 addErr errs_so_far msg locs
364 = errs_so_far `snocBag` mk_msg locs
366 mk_msg (loc:_) = let (l,hdr) = dumpLoc loc in addErrLocHdrLine l hdr msg
367 mk_msg [] = dontAddErrLoc "" msg
369 addLoc :: LintLocInfo -> LintM a -> LintM a
370 addLoc extra_loc m loc scope errs
371 = m (extra_loc:loc) scope errs
373 addInScopeVars :: [Id] -> LintM a -> LintM a
374 addInScopeVars ids m loc scope errs
375 = -- We check if these "new" ids are already
376 -- in scope, i.e., we have *shadowing* going on.
377 -- For now, it's just a "trace"; we may make
378 -- a real error out of it...
380 new_set = mkVarSet ids
382 shadowed = scope `intersectVarSet` new_set
384 -- After adding -fliberate-case, Simon decided he likes shadowed
385 -- names after all. WDP 94/07
386 -- (if isEmptyVarSet shadowed
388 -- else pprTrace "Shadowed vars:" (ppr (varSetElems shadowed))) $
389 m loc (scope `unionVarSet` new_set) errs
392 Checking function applications: we only check that the type has the
393 right *number* of arrows, we don't actually compare the types. This
394 is because we can't expect the types to be equal - the type
395 applications and type lambdas that we use to calculate accurate types
396 have long since disappeared.
399 checkFunApp :: Type -- The function type
400 -> [Type] -- The arg type(s)
401 -> Message -- Error messgae
402 -> LintM (Maybe Type) -- The result type
404 checkFunApp fun_ty arg_tys msg loc scope errs
405 = cfa res_ty expected_arg_tys arg_tys
407 (_, de_forall_ty) = splitForAllTys fun_ty
408 (expected_arg_tys, res_ty) = splitFunTys de_forall_ty
410 cfa res_ty expected [] -- Args have run out; that's fine
411 = (Just (mkFunTys expected res_ty), errs)
413 cfa res_ty [] arg_tys -- Expected arg tys ran out first;
414 -- first see if res_ty is a tyvar template;
415 -- otherwise, maybe res_ty is a
416 -- dictionary type which is actually a function?
418 = (Just res_ty, errs)
420 = case splitFunTys res_ty of
421 ([], _) -> (Nothing, addErr errs msg loc) -- Too many args
422 (new_expected, new_res) -> cfa new_res new_expected arg_tys
424 cfa res_ty (expected_arg_ty:expected_arg_tys) (arg_ty:arg_tys)
425 = cfa res_ty expected_arg_tys arg_tys
429 checkInScope :: Id -> LintM ()
430 checkInScope id loc scope errs
431 = if isLocallyDefined id && not (id `elemVarSet` scope) then
432 ((), addErr errs (hsep [ppr id, ptext SLIT("is out of scope")]) loc)
436 checkTys :: Type -> Type -> Message -> LintM ()
437 checkTys ty1 ty2 msg loc scope errs
438 = -- if (ty1 == ty2) then
440 -- else ((), addErr errs msg loc)
444 mkCaseAltMsg :: StgCaseAlts -> Message
446 = ($$) (text "In some case alternatives, type of alternatives not all same:")
447 (empty) -- LATER: ppr alts
449 mkCaseAbstractMsg :: TyCon -> Message
450 mkCaseAbstractMsg tycon
451 = ($$) (ptext SLIT("An algebraic case on an abstract type:"))
454 mkDefltMsg :: Id -> Message
456 = ($$) (ptext SLIT("Binder of a case expression doesn't match type of scrutinee:"))
459 mkFunAppMsg :: Type -> [Type] -> StgExpr -> Message
460 mkFunAppMsg fun_ty arg_tys expr
461 = vcat [text "In a function application, function type doesn't match arg types:",
462 hang (ptext SLIT("Function type:")) 4 (ppr fun_ty),
463 hang (ptext SLIT("Arg types:")) 4 (vcat (map (ppr) arg_tys)),
464 hang (ptext SLIT("Expression:")) 4 (ppr expr)]
466 mkRhsConMsg :: Type -> [Type] -> Message
467 mkRhsConMsg fun_ty arg_tys
468 = vcat [text "In a RHS constructor application, con type doesn't match arg types:",
469 hang (ptext SLIT("Constructor type:")) 4 (ppr fun_ty),
470 hang (ptext SLIT("Arg types:")) 4 (vcat (map (ppr) arg_tys))]
472 mkUnappTyMsg :: Id -> Type -> Message
474 = vcat [text "Variable has a for-all type, but isn't applied to any types.",
475 (<>) (ptext SLIT("Var: ")) (ppr var),
476 (<>) (ptext SLIT("Its type: ")) (ppr ty)]
478 mkAlgAltMsg1 :: Type -> Message
480 = ($$) (text "In some case statement, type of scrutinee is not a data type:")
483 mkAlgAltMsg2 :: Type -> DataCon -> Message
486 text "In some algebraic case alternative, constructor is not a constructor of scrutinee type:",
491 mkAlgAltMsg3 :: DataCon -> [Id] -> Message
492 mkAlgAltMsg3 con alts
494 text "In some algebraic case alternative, number of arguments doesn't match constructor:",
499 mkAlgAltMsg4 :: Type -> Id -> Message
502 text "In some algebraic case alternative, type of argument doesn't match data constructor:",
507 mkPrimAltMsg :: (Literal, StgExpr) -> Message
509 = text "In a primitive case alternative, type of literal doesn't match type of scrutinee:"
512 mkCaseOfCaseMsg :: StgExpr -> Message
514 = text "Case of non-tail-call:" $$ ppr e
516 mkRhsMsg :: Id -> Type -> Message
518 = vcat [hsep [ptext SLIT("The type of this binder doesn't match the type of its RHS:"),
520 hsep [ptext SLIT("Binder's type:"), ppr (idType binder)],
521 hsep [ptext SLIT("Rhs type:"), ppr ty]
524 mkUnLiftedTyMsg binder rhs
525 = (ptext SLIT("Let(rec) binder") <+> quotes (ppr binder) <+>
526 ptext SLIT("has unlifted type") <+> quotes (ppr (idType binder)))
528 (ptext SLIT("RHS:") <+> ppr rhs)