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 TyCon ( TyCon, isDataTyCon )
26 import Util ( zipEqual )
29 infixr 9 `thenL`, `thenL_`, `thenMaybeL`, `thenMaybeL_`
33 (a) *some* type errors
34 (b) locally-defined variables used but not defined
37 Note: unless -dverbose-stg is on, display of lint errors will result
38 in "panic: bOGUS_LVs".
43 This module has suffered bit-rot; it is likely to yield lint errors
44 for Stg code that is currently perfectly acceptable for code
45 generation. Solution: don't use it! (KSW 2000-05).
48 %************************************************************************
50 \subsection{``lint'' for various constructs}
52 %************************************************************************
54 @lintStgBindings@ is the top-level interface function.
57 lintStgBindings :: String -> [StgBinding] -> [StgBinding]
59 lintStgBindings whodunnit binds
61 case (initL (lint_binds binds)) of
63 Just msg -> pprPanic "" (vcat [
64 ptext SLIT("*** Stg Lint ErrMsgs: in") <+> text whodunnit <+> ptext SLIT("***"),
66 ptext SLIT("*** Offending Program ***"),
68 ptext SLIT("*** End of Offense ***")])
70 lint_binds :: [StgBinding] -> LintM ()
72 lint_binds [] = returnL ()
73 lint_binds (bind:binds)
74 = lintStgBinds bind `thenL` \ binders ->
75 addInScopeVars binders (
82 lintStgArg :: StgArg -> LintM (Maybe Type)
83 lintStgArg (StgLitArg lit) = returnL (Just (literalType lit))
84 lintStgArg (StgVarArg v) = lintStgVar v
86 lintStgVar v = checkInScope v `thenL_`
87 returnL (Just (idType v))
91 lintStgBinds :: StgBinding -> LintM [Id] -- Returns the binders
92 lintStgBinds (StgNonRec binder rhs)
93 = lint_binds_help (binder,rhs) `thenL_`
96 lintStgBinds (StgRec pairs)
97 = addInScopeVars binders (
98 mapL lint_binds_help pairs `thenL_`
102 binders = [b | (b,_) <- pairs]
104 lint_binds_help (binder, rhs)
105 = addLoc (RhsOf binder) (
107 lintStgRhs rhs `thenL` \ maybe_rhs_ty ->
109 -- Check binder doesn't have unlifted type
110 checkL (not (isUnLiftedType binder_ty))
111 (mkUnLiftedTyMsg binder rhs) `thenL_`
113 -- Check match to RHS type
114 (case maybe_rhs_ty of
115 Nothing -> returnL ()
116 Just rhs_ty -> checkTys binder_ty
118 (mkRhsMsg binder rhs_ty)
124 binder_ty = idType binder
128 lintStgRhs :: StgRhs -> LintM (Maybe Type)
130 lintStgRhs (StgRhsClosure _ _ _ _ _ [] expr)
133 lintStgRhs (StgRhsClosure _ _ _ _ _ binders expr)
134 = addLoc (LambdaBodyOf binders) (
135 addInScopeVars binders (
136 lintStgExpr expr `thenMaybeL` \ body_ty ->
137 returnL (Just (mkFunTys (map idType binders) body_ty))
140 lintStgRhs (StgRhsCon _ con args)
141 = mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
142 case maybe_arg_tys of
143 Nothing -> returnL Nothing
144 Just arg_tys -> checkFunApp con_ty arg_tys (mkRhsConMsg con_ty arg_tys)
146 con_ty = dataConRepType con
150 lintStgExpr :: StgExpr -> LintM (Maybe Type) -- Nothing if error found
152 lintStgExpr (StgLit l) = returnL (Just (literalType l))
154 lintStgExpr e@(StgApp fun args)
155 = lintStgVar fun `thenMaybeL` \ fun_ty ->
156 mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
157 case maybe_arg_tys of
158 Nothing -> returnL Nothing
159 Just arg_tys -> checkFunApp fun_ty arg_tys (mkFunAppMsg fun_ty arg_tys e)
161 lintStgExpr e@(StgConApp con args)
162 = mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
163 case maybe_arg_tys of
164 Nothing -> returnL Nothing
165 Just arg_tys -> checkFunApp con_ty arg_tys (mkFunAppMsg con_ty arg_tys e)
167 con_ty = dataConRepType con
169 lintStgExpr e@(StgPrimApp op args _)
170 = mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
171 case maybe_arg_tys of
172 Nothing -> returnL Nothing
173 Just arg_tys -> checkFunApp op_ty arg_tys (mkFunAppMsg op_ty arg_tys e)
175 op_ty = primOpType op
177 lintStgExpr (StgLam _ bndrs _)
178 = addErrL (ptext SLIT("Unexpected StgLam") <+> ppr bndrs) `thenL_`
181 lintStgExpr (StgLet binds body)
182 = lintStgBinds binds `thenL` \ binders ->
183 addLoc (BodyOfLetRec binders) (
184 addInScopeVars binders (
188 lintStgExpr (StgLetNoEscape _ _ binds body)
189 = lintStgBinds binds `thenL` \ binders ->
190 addLoc (BodyOfLetRec binders) (
191 addInScopeVars binders (
195 lintStgExpr (StgSCC _ expr) = lintStgExpr expr
197 lintStgExpr e@(StgCase scrut _ _ bndr _ alts)
198 = lintStgExpr scrut `thenMaybeL` \ _ ->
199 checkTys (idType bndr) scrut_ty (mkDefltMsg bndr) `thenL_`
201 (trace (showSDoc (ppr e)) $
202 -- we only allow case of tail-call or primop.
204 StgApp _ _ -> returnL ()
205 StgConApp _ _ -> returnL ()
206 other -> addErrL (mkCaseOfCaseMsg e)) `thenL_`
208 addInScopeVars [bndr] (lintStgAlts alts scrut_ty)
211 scrut_ty = get_ty alts
213 get_ty (StgAlgAlts ty _ _) = ty
214 get_ty (StgPrimAlts ty _ _) = ty
218 lintStgAlts :: StgCaseAlts
219 -> Type -- Type of scrutinee
220 -> LintM (Maybe Type) -- Type of alternatives
222 lintStgAlts alts scrut_ty
224 StgAlgAlts _ alg_alts deflt ->
225 mapL (lintAlgAlt scrut_ty) alg_alts `thenL` \ maybe_alt_tys ->
226 lintDeflt deflt scrut_ty `thenL` \ maybe_deflt_ty ->
227 returnL (maybe_deflt_ty : maybe_alt_tys)
229 StgPrimAlts _ prim_alts deflt ->
230 mapL (lintPrimAlt scrut_ty) prim_alts `thenL` \ maybe_alt_tys ->
231 lintDeflt deflt scrut_ty `thenL` \ maybe_deflt_ty ->
232 returnL (maybe_deflt_ty : maybe_alt_tys)
233 ) `thenL` \ maybe_result_tys ->
234 -- Check the result types
235 case catMaybes (maybe_result_tys) of
236 [] -> returnL Nothing
238 (first_ty:tys) -> mapL check tys `thenL_`
239 returnL (Just first_ty)
241 check ty = checkTys first_ty ty (mkCaseAltMsg alts)
243 lintAlgAlt scrut_ty (con, args, _, rhs)
244 = (case splitAlgTyConApp_maybe scrut_ty of
246 addErrL (mkAlgAltMsg1 scrut_ty)
247 Just (tycon, tys_applied, cons) ->
249 arg_tys = dataConArgTys con tys_applied
250 -- This almost certainly does not work for existential constructors
252 checkL (con `elem` cons) (mkAlgAltMsg2 scrut_ty con) `thenL_`
253 checkL (length arg_tys == length args) (mkAlgAltMsg3 con args)
255 mapL check (zipEqual "lintAlgAlt:stg" arg_tys args) `thenL_`
258 addInScopeVars args (
262 check (ty, arg) = checkTys ty (idType arg) (mkAlgAltMsg4 ty arg)
264 -- elem: yes, the elem-list here can sometimes be long-ish,
265 -- but as it's use-once, probably not worth doing anything different
266 -- We give it its own copy, so it isn't overloaded.
268 elem x (y:ys) = x==y || elem x ys
270 lintPrimAlt scrut_ty alt@(lit,rhs)
271 = checkTys (literalType lit) scrut_ty (mkPrimAltMsg alt) `thenL_`
274 lintDeflt StgNoDefault scrut_ty = returnL Nothing
275 lintDeflt deflt@(StgBindDefault rhs) scrut_ty = lintStgExpr rhs
279 %************************************************************************
281 \subsection[lint-monad]{The Lint monad}
283 %************************************************************************
286 type LintM a = [LintLocInfo] -- Locations
287 -> IdSet -- Local vars in scope
288 -> Bag ErrMsg -- Error messages so far
289 -> (a, Bag ErrMsg) -- Result and error messages (if any)
292 = RhsOf Id -- The variable bound
293 | LambdaBodyOf [Id] -- The lambda-binder
294 | BodyOfLetRec [Id] -- One of the binders
297 (getSrcLoc v, ptext SLIT(" [RHS of ") <> pp_binders [v] <> char ']' )
298 dumpLoc (LambdaBodyOf bs) =
299 (getSrcLoc (head bs), ptext SLIT(" [in body of lambda with binders ") <> pp_binders bs <> char ']' )
301 dumpLoc (BodyOfLetRec bs) =
302 (getSrcLoc (head bs), ptext SLIT(" [in body of letrec with binders ") <> pp_binders bs <> char ']' )
305 pp_binders :: [Id] -> SDoc
307 = sep (punctuate comma (map pp_binder bs))
310 = hsep [ppr b, dcolon, ppr (idType b)]
314 initL :: LintM a -> Maybe Message
316 = case (m [] emptyVarSet emptyBag) of { (_, errs) ->
317 if isEmptyBag errs then
320 Just (pprBagOfErrors errs)
323 returnL :: a -> LintM a
324 returnL r loc scope errs = (r, errs)
326 thenL :: LintM a -> (a -> LintM b) -> LintM b
327 thenL m k loc scope errs
328 = case m loc scope errs of
329 (r, errs') -> k r loc scope errs'
331 thenL_ :: LintM a -> LintM b -> LintM b
332 thenL_ m k loc scope errs
333 = case m loc scope errs of
334 (_, errs') -> k loc scope errs'
336 thenMaybeL :: LintM (Maybe a) -> (a -> LintM (Maybe b)) -> LintM (Maybe b)
337 thenMaybeL m k loc scope errs
338 = case m loc scope errs of
339 (Nothing, errs2) -> (Nothing, errs2)
340 (Just r, errs2) -> k r loc scope errs2
342 thenMaybeL_ :: LintM (Maybe a) -> LintM (Maybe b) -> LintM (Maybe b)
343 thenMaybeL_ m k loc scope errs
344 = case m loc scope errs of
345 (Nothing, errs2) -> (Nothing, errs2)
346 (Just _, errs2) -> k loc scope errs2
348 mapL :: (a -> LintM b) -> [a] -> LintM [b]
349 mapL f [] = returnL []
352 mapL f xs `thenL` \ rs ->
355 mapMaybeL :: (a -> LintM (Maybe b)) -> [a] -> LintM (Maybe [b])
356 -- Returns Nothing if anything fails
357 mapMaybeL f [] = returnL (Just [])
359 = f x `thenMaybeL` \ r ->
360 mapMaybeL f xs `thenMaybeL` \ rs ->
361 returnL (Just (r:rs))
365 checkL :: Bool -> Message -> LintM ()
366 checkL True msg loc scope errs = ((), errs)
367 checkL False msg loc scope errs = ((), addErr errs msg loc)
369 addErrL :: Message -> LintM ()
370 addErrL msg loc scope errs = ((), addErr errs msg loc)
372 addErr :: Bag ErrMsg -> Message -> [LintLocInfo] -> Bag ErrMsg
374 addErr errs_so_far msg locs
375 = errs_so_far `snocBag` mk_msg locs
377 mk_msg (loc:_) = let (l,hdr) = dumpLoc loc in addErrLocHdrLine l hdr msg
378 mk_msg [] = dontAddErrLoc "" msg
380 addLoc :: LintLocInfo -> LintM a -> LintM a
381 addLoc extra_loc m loc scope errs
382 = m (extra_loc:loc) scope errs
384 addInScopeVars :: [Id] -> LintM a -> LintM a
385 addInScopeVars ids m loc scope errs
386 = -- We check if these "new" ids are already
387 -- in scope, i.e., we have *shadowing* going on.
388 -- For now, it's just a "trace"; we may make
389 -- a real error out of it...
391 new_set = mkVarSet ids
393 shadowed = scope `intersectVarSet` new_set
395 -- After adding -fliberate-case, Simon decided he likes shadowed
396 -- names after all. WDP 94/07
397 -- (if isEmptyVarSet shadowed
399 -- else pprTrace "Shadowed vars:" (ppr (varSetElems shadowed))) $
400 m loc (scope `unionVarSet` new_set) errs
403 Checking function applications: we only check that the type has the
404 right *number* of arrows, we don't actually compare the types. This
405 is because we can't expect the types to be equal - the type
406 applications and type lambdas that we use to calculate accurate types
407 have long since disappeared.
410 checkFunApp :: Type -- The function type
411 -> [Type] -- The arg type(s)
412 -> Message -- Error messgae
413 -> LintM (Maybe Type) -- The result type
415 checkFunApp fun_ty arg_tys msg loc scope errs
416 = cfa res_ty expected_arg_tys arg_tys
418 (_, de_forall_ty) = splitForAllTys fun_ty
419 (expected_arg_tys, res_ty) = splitFunTys de_forall_ty
421 cfa res_ty expected [] -- Args have run out; that's fine
422 = (Just (mkFunTys expected res_ty), errs)
424 cfa res_ty [] arg_tys -- Expected arg tys ran out first;
425 -- first see if res_ty is a tyvar template;
426 -- otherwise, maybe res_ty is a
427 -- dictionary type which is actually a function?
429 = (Just res_ty, errs)
431 = case splitFunTys res_ty of
432 ([], _) -> (Nothing, addErr errs msg loc) -- Too many args
433 (new_expected, new_res) -> cfa new_res new_expected arg_tys
435 cfa res_ty (expected_arg_ty:expected_arg_tys) (arg_ty:arg_tys)
436 = cfa res_ty expected_arg_tys arg_tys
440 checkInScope :: Id -> LintM ()
441 checkInScope id loc scope errs
442 = if isLocallyDefined id && not (id `elemVarSet` scope) then
443 ((), addErr errs (hsep [ppr id, ptext SLIT("is out of scope")]) loc)
447 checkTys :: Type -> Type -> Message -> LintM ()
448 checkTys ty1 ty2 msg loc scope errs
449 = -- if (ty1 == ty2) then
451 -- else ((), addErr errs msg loc)
455 mkCaseAltMsg :: StgCaseAlts -> Message
457 = ($$) (text "In some case alternatives, type of alternatives not all same:")
458 (empty) -- LATER: ppr alts
460 mkCaseAbstractMsg :: TyCon -> Message
461 mkCaseAbstractMsg tycon
462 = ($$) (ptext SLIT("An algebraic case on an abstract type:"))
465 mkDefltMsg :: Id -> Message
467 = ($$) (ptext SLIT("Binder of a case expression doesn't match type of scrutinee:"))
470 mkFunAppMsg :: Type -> [Type] -> StgExpr -> Message
471 mkFunAppMsg fun_ty arg_tys expr
472 = vcat [text "In a function application, function type doesn't match arg types:",
473 hang (ptext SLIT("Function type:")) 4 (ppr fun_ty),
474 hang (ptext SLIT("Arg types:")) 4 (vcat (map (ppr) arg_tys)),
475 hang (ptext SLIT("Expression:")) 4 (ppr expr)]
477 mkRhsConMsg :: Type -> [Type] -> Message
478 mkRhsConMsg fun_ty arg_tys
479 = vcat [text "In a RHS constructor application, con type doesn't match arg types:",
480 hang (ptext SLIT("Constructor type:")) 4 (ppr fun_ty),
481 hang (ptext SLIT("Arg types:")) 4 (vcat (map (ppr) arg_tys))]
483 mkUnappTyMsg :: Id -> Type -> Message
485 = vcat [text "Variable has a for-all type, but isn't applied to any types.",
486 (<>) (ptext SLIT("Var: ")) (ppr var),
487 (<>) (ptext SLIT("Its type: ")) (ppr ty)]
489 mkAlgAltMsg1 :: Type -> Message
491 = ($$) (text "In some case statement, type of scrutinee is not a data type:")
494 mkAlgAltMsg2 :: Type -> DataCon -> Message
497 text "In some algebraic case alternative, constructor is not a constructor of scrutinee type:",
502 mkAlgAltMsg3 :: DataCon -> [Id] -> Message
503 mkAlgAltMsg3 con alts
505 text "In some algebraic case alternative, number of arguments doesn't match constructor:",
510 mkAlgAltMsg4 :: Type -> Id -> Message
513 text "In some algebraic case alternative, type of argument doesn't match data constructor:",
518 mkPrimAltMsg :: (Literal, StgExpr) -> Message
520 = text "In a primitive case alternative, type of literal doesn't match type of scrutinee:"
523 mkCaseOfCaseMsg :: StgExpr -> Message
525 = text "Case of non-tail-call:" $$ ppr e
527 mkRhsMsg :: Id -> Type -> Message
529 = vcat [hsep [ptext SLIT("The type of this binder doesn't match the type of its RHS:"),
531 hsep [ptext SLIT("Binder's type:"), ppr (idType binder)],
532 hsep [ptext SLIT("Rhs type:"), ppr ty]
535 mkUnLiftedTyMsg binder rhs
536 = (ptext SLIT("Let(rec) binder") <+> quotes (ppr binder) <+>
537 ptext SLIT("has unlifted type") <+> quotes (ppr (idType binder)))
539 (ptext SLIT("RHS:") <+> ppr rhs)