2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
4 \section[StgLint]{A ``lint'' pass to check for Stg correctness}
8 -- The above warning supression flag is a temporary kludge.
9 -- While working on this module you are encouraged to remove it and fix
10 -- any warnings in the module. See
11 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
14 module StgLint ( lintStgBindings ) where
16 #include "HsVersions.h"
20 import Bag ( Bag, emptyBag, isEmptyBag, snocBag, bagToList )
21 import Id ( Id, idType, isLocalId )
23 import DataCon ( DataCon, dataConInstArgTys, dataConRepType )
24 import CoreSyn ( AltCon(..) )
25 import PrimOp ( primOpType )
26 import Literal ( literalType )
28 import Name ( getSrcLoc )
29 import ErrUtils ( Message, mkLocMessage )
30 import Type ( mkFunTys, splitFunTys, splitTyConApp_maybe,
31 isUnLiftedType, isTyVarTy, dropForAlls, Type
33 import TyCon ( isAlgTyCon, isNewTyCon, tyConDataCons )
34 import Util ( zipEqual, equalLength )
35 import SrcLoc ( srcLocSpan )
41 (a) *some* type errors
42 (b) locally-defined variables used but not defined
45 Note: unless -dverbose-stg is on, display of lint errors will result
46 in "panic: bOGUS_LVs".
51 This module has suffered bit-rot; it is likely to yield lint errors
52 for Stg code that is currently perfectly acceptable for code
53 generation. Solution: don't use it! (KSW 2000-05).
56 %************************************************************************
58 \subsection{``lint'' for various constructs}
60 %************************************************************************
62 @lintStgBindings@ is the top-level interface function.
65 lintStgBindings :: String -> [StgBinding] -> [StgBinding]
67 lintStgBindings whodunnit binds
68 = {-# SCC "StgLint" #-}
69 case (initL (lint_binds binds)) of
71 Just msg -> pprPanic "" (vcat [
72 ptext SLIT("*** Stg Lint ErrMsgs: in") <+>
73 text whodunnit <+> ptext SLIT("***"),
75 ptext SLIT("*** Offending Program ***"),
77 ptext SLIT("*** End of Offense ***")])
79 lint_binds :: [StgBinding] -> LintM ()
81 lint_binds [] = return ()
82 lint_binds (bind:binds) = do
83 binders <- lintStgBinds bind
84 addInScopeVars binders $
90 lintStgArg :: StgArg -> LintM (Maybe Type)
91 lintStgArg (StgLitArg lit) = return (Just (literalType lit))
92 lintStgArg (StgVarArg v) = lintStgVar v
94 lintStgVar v = do checkInScope v
95 return (Just (idType v))
99 lintStgBinds :: StgBinding -> LintM [Id] -- Returns the binders
100 lintStgBinds (StgNonRec binder rhs) = do
101 lint_binds_help (binder,rhs)
104 lintStgBinds (StgRec pairs)
105 = addInScopeVars binders $ do
106 mapM_ lint_binds_help pairs
109 binders = [b | (b,_) <- pairs]
111 lint_binds_help (binder, rhs)
112 = addLoc (RhsOf binder) $ do
114 maybe_rhs_ty <- lintStgRhs rhs
116 -- Check binder doesn't have unlifted type
117 checkL (not (isUnLiftedType binder_ty))
118 (mkUnLiftedTyMsg binder rhs)
120 -- Check match to RHS type
123 Just rhs_ty -> checkTys binder_ty
125 (mkRhsMsg binder rhs_ty)
129 binder_ty = idType binder
133 lintStgRhs :: StgRhs -> LintM (Maybe Type)
135 lintStgRhs (StgRhsClosure _ _ _ _ _ [] expr)
138 lintStgRhs (StgRhsClosure _ _ _ _ _ binders expr)
139 = addLoc (LambdaBodyOf binders) $
140 addInScopeVars binders $ runMaybeT $ do
141 body_ty <- MaybeT $ lintStgExpr expr
142 return (mkFunTys (map idType binders) body_ty)
144 lintStgRhs (StgRhsCon _ con args) = runMaybeT $ do
145 arg_tys <- mapM (MaybeT . lintStgArg) args
146 MaybeT $ checkFunApp con_ty arg_tys (mkRhsConMsg con_ty arg_tys)
148 con_ty = dataConRepType con
152 lintStgExpr :: StgExpr -> LintM (Maybe Type) -- Nothing if error found
154 lintStgExpr (StgLit l) = return (Just (literalType l))
156 lintStgExpr e@(StgApp fun args) = runMaybeT $ do
157 fun_ty <- MaybeT $ lintStgVar fun
158 arg_tys <- mapM (MaybeT . lintStgArg) args
159 MaybeT $ checkFunApp fun_ty arg_tys (mkFunAppMsg fun_ty arg_tys e)
161 lintStgExpr e@(StgConApp con args) = runMaybeT $ do
162 arg_tys <- mapM (MaybeT . lintStgArg) args
163 MaybeT $ checkFunApp con_ty arg_tys (mkFunAppMsg con_ty arg_tys e)
165 con_ty = dataConRepType con
167 lintStgExpr (StgOpApp (StgFCallOp _ _) args res_ty) = runMaybeT $ do
168 -- We don't have enough type information to check
169 -- the application; ToDo
170 maybe_arg_tys <- mapM (MaybeT . lintStgArg) args
173 lintStgExpr e@(StgOpApp (StgPrimOp op) args _) = runMaybeT $ do
174 arg_tys <- mapM (MaybeT . lintStgArg) args
175 MaybeT $ checkFunApp op_ty arg_tys (mkFunAppMsg op_ty arg_tys e)
177 op_ty = primOpType op
179 lintStgExpr (StgLam _ bndrs _) = do
180 addErrL (ptext SLIT("Unexpected StgLam") <+> ppr bndrs)
183 lintStgExpr (StgLet binds body) = do
184 binders <- lintStgBinds binds
185 addLoc (BodyOfLetRec binders) $
186 addInScopeVars binders $
189 lintStgExpr (StgLetNoEscape _ _ binds body) = do
190 binders <- lintStgBinds binds
191 addLoc (BodyOfLetRec binders) $
192 addInScopeVars binders $
195 lintStgExpr (StgSCC _ expr) = lintStgExpr expr
197 lintStgExpr e@(StgCase scrut _ _ bndr _ alts_type alts) = runMaybeT $ do
198 MaybeT $ lintStgExpr scrut
200 MaybeT $ liftM Just $
202 AlgAlt tc -> check_bndr tc
203 PrimAlt tc -> check_bndr tc
204 UbxTupAlt tc -> check_bndr tc
207 MaybeT $ trace (showSDoc (ppr e)) $ do
208 -- we only allow case of tail-call or primop.
210 StgApp _ _ -> return ()
211 StgConApp _ _ -> return ()
212 StgOpApp _ _ _ -> return ()
213 _ -> addErrL (mkCaseOfCaseMsg e)
215 addInScopeVars [bndr] $
216 lintStgAlts alts scrut_ty
218 scrut_ty = idType bndr
219 bad_bndr = mkDefltMsg bndr
220 check_bndr tc = case splitTyConApp_maybe scrut_ty of
221 Just (bndr_tc, _) -> checkL (tc == bndr_tc) bad_bndr
222 Nothing -> addErrL bad_bndr
225 lintStgAlts :: [StgAlt]
226 -> Type -- Type of scrutinee
227 -> LintM (Maybe Type) -- Type of alternatives
229 lintStgAlts alts scrut_ty = do
230 maybe_result_tys <- mapM (lintAlt scrut_ty) alts
232 -- Check the result types
233 case catMaybes (maybe_result_tys) of
236 (first_ty:tys) -> do mapM_ check tys
237 return (Just first_ty)
239 check ty = checkTys first_ty ty (mkCaseAltMsg alts)
241 lintAlt _ (DEFAULT, _, _, rhs)
244 lintAlt scrut_ty (LitAlt lit, _, _, rhs) = do
245 checkTys (literalType lit) scrut_ty (mkAltMsg1 scrut_ty)
248 lintAlt scrut_ty (DataAlt con, args, _, rhs) = do
249 case splitTyConApp_maybe scrut_ty of
250 Just (tycon, tys_applied) | isAlgTyCon tycon &&
251 not (isNewTyCon tycon) -> do
253 cons = tyConDataCons tycon
254 arg_tys = dataConInstArgTys con tys_applied
255 -- This almost certainly does not work for existential constructors
257 checkL (con `elem` cons) (mkAlgAltMsg2 scrut_ty con)
258 checkL (equalLength arg_tys args) (mkAlgAltMsg3 con args)
259 mapM_ check (zipEqual "lintAlgAlt:stg" arg_tys args)
262 addErrL (mkAltMsg1 scrut_ty)
264 addInScopeVars args $
267 check (ty, arg) = checkTys ty (idType arg) (mkAlgAltMsg4 ty arg)
269 -- elem: yes, the elem-list here can sometimes be long-ish,
270 -- but as it's use-once, probably not worth doing anything different
271 -- We give it its own copy, so it isn't overloaded.
273 elem x (y:ys) = x==y || elem x ys
277 %************************************************************************
279 \subsection[lint-monad]{The Lint monad}
281 %************************************************************************
284 newtype LintM a = LintM
285 { unLintM :: [LintLocInfo] -- Locations
286 -> IdSet -- Local vars in scope
287 -> Bag Message -- Error messages so far
288 -> (a, Bag Message) -- 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 (srcLocSpan (getSrcLoc v), ptext SLIT(" [RHS of ") <> pp_binders [v] <> char ']' )
298 dumpLoc (LambdaBodyOf bs) =
299 (srcLocSpan (getSrcLoc (head bs)), ptext SLIT(" [in body of lambda with binders ") <> pp_binders bs <> char ']' )
301 dumpLoc (BodyOfLetRec bs) =
302 (srcLocSpan (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 (vcat (punctuate (text "") (bagToList errs)))
323 instance Monad LintM where
324 return a = LintM $ \_loc _scope errs -> (a, errs)
328 thenL :: LintM a -> (a -> LintM b) -> LintM b
329 thenL m k = LintM $ \loc scope errs
330 -> case unLintM m loc scope errs of
331 (r, errs') -> unLintM (k r) loc scope errs'
333 thenL_ :: LintM a -> LintM b -> LintM b
334 thenL_ m k = LintM $ \loc scope errs
335 -> case unLintM m loc scope errs of
336 (_, errs') -> unLintM k loc scope errs'
340 checkL :: Bool -> Message -> LintM ()
341 checkL True _ = return ()
342 checkL False msg = addErrL msg
344 addErrL :: Message -> LintM ()
345 addErrL msg = LintM $ \loc _scope errs -> ((), addErr errs msg loc)
347 addErr :: Bag Message -> Message -> [LintLocInfo] -> Bag Message
348 addErr errs_so_far msg locs
349 = errs_so_far `snocBag` mk_msg locs
351 mk_msg (loc:_) = let (l,hdr) = dumpLoc loc
352 in mkLocMessage l (hdr $$ msg)
355 addLoc :: LintLocInfo -> LintM a -> LintM a
356 addLoc extra_loc m = LintM $ \loc scope errs
357 -> unLintM m (extra_loc:loc) scope errs
359 addInScopeVars :: [Id] -> LintM a -> LintM a
360 addInScopeVars ids m = LintM $ \loc scope errs
361 -> -- We check if these "new" ids are already
362 -- in scope, i.e., we have *shadowing* going on.
363 -- For now, it's just a "trace"; we may make
364 -- a real error out of it...
366 new_set = mkVarSet ids
368 -- After adding -fliberate-case, Simon decided he likes shadowed
369 -- names after all. WDP 94/07
370 -- (if isEmptyVarSet shadowed
372 -- else pprTrace "Shadowed vars:" (ppr (varSetElems shadowed))) $
373 unLintM m loc (scope `unionVarSet` new_set) errs
376 Checking function applications: we only check that the type has the
377 right *number* of arrows, we don't actually compare the types. This
378 is because we can't expect the types to be equal - the type
379 applications and type lambdas that we use to calculate accurate types
380 have long since disappeared.
383 checkFunApp :: Type -- The function type
384 -> [Type] -- The arg type(s)
385 -> Message -- Error messgae
386 -> LintM (Maybe Type) -- The result type
388 checkFunApp fun_ty arg_tys msg = LintM checkFunApp'
390 checkFunApp' loc _scope errs
391 = cfa res_ty expected_arg_tys arg_tys
393 (expected_arg_tys, res_ty) = splitFunTys (dropForAlls fun_ty)
395 cfa res_ty expected [] -- Args have run out; that's fine
396 = (Just (mkFunTys expected res_ty), errs)
398 cfa res_ty [] arg_tys -- Expected arg tys ran out first;
399 -- first see if res_ty is a tyvar template;
400 -- otherwise, maybe res_ty is a
401 -- dictionary type which is actually a function?
403 = (Just res_ty, errs)
405 = case splitFunTys res_ty of
406 ([], _) -> (Nothing, addErr errs msg loc) -- Too many args
407 (new_expected, new_res) -> cfa new_res new_expected arg_tys
409 cfa res_ty (_:expected_arg_tys) (_:arg_tys)
410 = cfa res_ty expected_arg_tys arg_tys
414 checkInScope :: Id -> LintM ()
415 checkInScope id = LintM $ \loc scope errs
416 -> if isLocalId id && not (id `elemVarSet` scope) then
417 ((), addErr errs (hsep [ppr id, ptext SLIT("is out of scope")]) loc)
421 checkTys :: Type -> Type -> Message -> LintM ()
422 checkTys ty1 ty2 msg = LintM $ \loc scope errs
423 -> -- if (ty1 == ty2) then
425 -- else ((), addErr errs msg loc)
429 mkCaseAltMsg :: [StgAlt] -> Message
431 = ($$) (text "In some case alternatives, type of alternatives not all same:")
432 (empty) -- LATER: ppr alts
434 mkDefltMsg :: Id -> Message
436 = ($$) (ptext SLIT("Binder of a case expression doesn't match type of scrutinee:"))
439 mkFunAppMsg :: Type -> [Type] -> StgExpr -> Message
440 mkFunAppMsg fun_ty arg_tys expr
441 = vcat [text "In a function application, function type doesn't match arg types:",
442 hang (ptext SLIT("Function type:")) 4 (ppr fun_ty),
443 hang (ptext SLIT("Arg types:")) 4 (vcat (map (ppr) arg_tys)),
444 hang (ptext SLIT("Expression:")) 4 (ppr expr)]
446 mkRhsConMsg :: Type -> [Type] -> Message
447 mkRhsConMsg fun_ty arg_tys
448 = vcat [text "In a RHS constructor application, con type doesn't match arg types:",
449 hang (ptext SLIT("Constructor type:")) 4 (ppr fun_ty),
450 hang (ptext SLIT("Arg types:")) 4 (vcat (map (ppr) arg_tys))]
452 mkAltMsg1 :: Type -> Message
454 = ($$) (text "In a case expression, type of scrutinee does not match patterns")
457 mkAlgAltMsg2 :: Type -> DataCon -> Message
460 text "In some algebraic case alternative, constructor is not a constructor of scrutinee type:",
465 mkAlgAltMsg3 :: DataCon -> [Id] -> Message
466 mkAlgAltMsg3 con alts
468 text "In some algebraic case alternative, number of arguments doesn't match constructor:",
473 mkAlgAltMsg4 :: Type -> Id -> Message
476 text "In some algebraic case alternative, type of argument doesn't match data constructor:",
481 mkCaseOfCaseMsg :: StgExpr -> Message
483 = text "Case of non-tail-call:" $$ ppr e
485 mkRhsMsg :: Id -> Type -> Message
487 = vcat [hsep [ptext SLIT("The type of this binder doesn't match the type of its RHS:"),
489 hsep [ptext SLIT("Binder's type:"), ppr (idType binder)],
490 hsep [ptext SLIT("Rhs type:"), ppr ty]
493 mkUnLiftedTyMsg binder rhs
494 = (ptext SLIT("Let(rec) binder") <+> quotes (ppr binder) <+>
495 ptext SLIT("has unlifted type") <+> quotes (ppr (idType binder)))
497 (ptext SLIT("RHS:") <+> ppr rhs)