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") <+> text whodunnit <+> ptext SLIT("***"),
74 ptext SLIT("*** Offending Program ***"),
76 ptext SLIT("*** End of Offense ***")])
78 lint_binds :: [StgBinding] -> LintM ()
80 lint_binds [] = return ()
81 lint_binds (bind:binds) = do
82 binders <- lintStgBinds bind
83 addInScopeVars binders $
89 lintStgArg :: StgArg -> LintM (Maybe Type)
90 lintStgArg (StgLitArg lit) = return (Just (literalType lit))
91 lintStgArg (StgVarArg v) = lintStgVar v
93 lintStgVar v = do checkInScope v
94 return (Just (idType v))
98 lintStgBinds :: StgBinding -> LintM [Id] -- Returns the binders
99 lintStgBinds (StgNonRec binder rhs) = do
100 lint_binds_help (binder,rhs)
103 lintStgBinds (StgRec pairs)
104 = addInScopeVars binders $ do
105 mapM_ lint_binds_help pairs
108 binders = [b | (b,_) <- pairs]
110 lint_binds_help (binder, rhs)
111 = addLoc (RhsOf binder) $ do
113 maybe_rhs_ty <- lintStgRhs rhs
115 -- Check binder doesn't have unlifted type
116 checkL (not (isUnLiftedType binder_ty))
117 (mkUnLiftedTyMsg binder rhs)
119 -- Check match to RHS type
122 Just rhs_ty -> checkTys binder_ty
124 (mkRhsMsg binder rhs_ty)
128 binder_ty = idType binder
132 lintStgRhs :: StgRhs -> LintM (Maybe Type)
134 lintStgRhs (StgRhsClosure _ _ _ _ _ [] expr)
137 lintStgRhs (StgRhsClosure _ _ _ _ _ binders expr)
138 = addLoc (LambdaBodyOf binders) $
139 addInScopeVars binders $ runMaybeT $ do
140 body_ty <- MaybeT $ lintStgExpr expr
141 return (mkFunTys (map idType binders) body_ty)
143 lintStgRhs (StgRhsCon _ con args) = runMaybeT $ do
144 arg_tys <- mapM (MaybeT . lintStgArg) args
145 MaybeT $ checkFunApp con_ty arg_tys (mkRhsConMsg con_ty arg_tys)
147 con_ty = dataConRepType con
151 lintStgExpr :: StgExpr -> LintM (Maybe Type) -- Nothing if error found
153 lintStgExpr (StgLit l) = return (Just (literalType l))
155 lintStgExpr e@(StgApp fun args) = runMaybeT $ do
156 fun_ty <- MaybeT $ lintStgVar fun
157 arg_tys <- mapM (MaybeT . lintStgArg) args
158 MaybeT $ checkFunApp fun_ty arg_tys (mkFunAppMsg fun_ty arg_tys e)
160 lintStgExpr e@(StgConApp con args) = runMaybeT $ do
161 arg_tys <- mapM (MaybeT . lintStgArg) args
162 MaybeT $ checkFunApp con_ty arg_tys (mkFunAppMsg con_ty arg_tys e)
164 con_ty = dataConRepType con
166 lintStgExpr e@(StgOpApp (StgFCallOp _ _) args res_ty) = runMaybeT $ do
167 -- We don't have enough type information to check
168 -- the application; ToDo
169 maybe_arg_tys <- mapM (MaybeT . lintStgArg) args
172 lintStgExpr e@(StgOpApp (StgPrimOp op) args _) = runMaybeT $ do
173 arg_tys <- mapM (MaybeT . lintStgArg) args
174 MaybeT $ checkFunApp op_ty arg_tys (mkFunAppMsg op_ty arg_tys e)
176 op_ty = primOpType op
178 lintStgExpr (StgLam _ bndrs _) = do
179 addErrL (ptext SLIT("Unexpected StgLam") <+> ppr bndrs)
182 lintStgExpr (StgLet binds body) = do
183 binders <- lintStgBinds binds
184 addLoc (BodyOfLetRec binders) $
185 addInScopeVars binders $
188 lintStgExpr (StgLetNoEscape _ _ binds body) = do
189 binders <- lintStgBinds binds
190 addLoc (BodyOfLetRec binders) $
191 addInScopeVars binders $
194 lintStgExpr (StgSCC _ expr) = lintStgExpr expr
196 lintStgExpr e@(StgCase scrut _ _ bndr _ alts_type alts) = runMaybeT $ do
197 MaybeT $ lintStgExpr scrut
199 MaybeT $ liftM Just $
201 AlgAlt tc -> check_bndr tc
202 PrimAlt tc -> check_bndr tc
203 UbxTupAlt tc -> check_bndr tc
206 MaybeT $ trace (showSDoc (ppr e)) $ do
207 -- we only allow case of tail-call or primop.
209 StgApp _ _ -> return ()
210 StgConApp _ _ -> return ()
211 StgOpApp _ _ _ -> return ()
212 other -> addErrL (mkCaseOfCaseMsg e)
214 addInScopeVars [bndr] $
215 lintStgAlts alts scrut_ty
217 scrut_ty = idType bndr
218 bad_bndr = mkDefltMsg bndr
219 check_bndr tc = case splitTyConApp_maybe scrut_ty of
220 Just (bndr_tc, _) -> checkL (tc == bndr_tc) bad_bndr
221 Nothing -> addErrL bad_bndr
224 lintStgAlts :: [StgAlt]
225 -> Type -- Type of scrutinee
226 -> LintM (Maybe Type) -- Type of alternatives
228 lintStgAlts alts scrut_ty = do
229 maybe_result_tys <- mapM (lintAlt scrut_ty) alts
231 -- Check the result types
232 case catMaybes (maybe_result_tys) of
235 (first_ty:tys) -> do mapM_ check tys
236 return (Just first_ty)
238 check ty = checkTys first_ty ty (mkCaseAltMsg alts)
240 lintAlt scrut_ty (DEFAULT, _, _, rhs)
243 lintAlt scrut_ty (LitAlt lit, _, _, rhs) = do
244 checkTys (literalType lit) scrut_ty (mkAltMsg1 scrut_ty)
247 lintAlt scrut_ty (DataAlt con, args, _, rhs) = do
248 case splitTyConApp_maybe scrut_ty of
249 Just (tycon, tys_applied) | isAlgTyCon tycon &&
250 not (isNewTyCon tycon) -> do
252 cons = tyConDataCons tycon
253 arg_tys = dataConInstArgTys con tys_applied
254 -- This almost certainly does not work for existential constructors
256 checkL (con `elem` cons) (mkAlgAltMsg2 scrut_ty con)
257 checkL (equalLength arg_tys args) (mkAlgAltMsg3 con args)
258 mapM_ check (zipEqual "lintAlgAlt:stg" arg_tys args)
261 addErrL (mkAltMsg1 scrut_ty)
263 addInScopeVars args $
266 check (ty, arg) = checkTys ty (idType arg) (mkAlgAltMsg4 ty arg)
268 -- elem: yes, the elem-list here can sometimes be long-ish,
269 -- but as it's use-once, probably not worth doing anything different
270 -- We give it its own copy, so it isn't overloaded.
272 elem x (y:ys) = x==y || elem x ys
276 %************************************************************************
278 \subsection[lint-monad]{The Lint monad}
280 %************************************************************************
283 newtype LintM a = LintM
284 { unLintM :: [LintLocInfo] -- Locations
285 -> IdSet -- Local vars in scope
286 -> Bag Message -- Error messages so far
287 -> (a, Bag Message) -- Result and error messages (if any)
291 = RhsOf Id -- The variable bound
292 | LambdaBodyOf [Id] -- The lambda-binder
293 | BodyOfLetRec [Id] -- One of the binders
296 (srcLocSpan (getSrcLoc v), ptext SLIT(" [RHS of ") <> pp_binders [v] <> char ']' )
297 dumpLoc (LambdaBodyOf bs) =
298 (srcLocSpan (getSrcLoc (head bs)), ptext SLIT(" [in body of lambda with binders ") <> pp_binders bs <> char ']' )
300 dumpLoc (BodyOfLetRec bs) =
301 (srcLocSpan (getSrcLoc (head bs)), ptext SLIT(" [in body of letrec with binders ") <> pp_binders bs <> char ']' )
304 pp_binders :: [Id] -> SDoc
306 = sep (punctuate comma (map pp_binder bs))
309 = hsep [ppr b, dcolon, ppr (idType b)]
313 initL :: LintM a -> Maybe Message
315 = case (m [] emptyVarSet emptyBag) of { (_, errs) ->
316 if isEmptyBag errs then
319 Just (vcat (punctuate (text "") (bagToList errs)))
322 instance Monad LintM where
323 return a = LintM $ \loc scope errs -> (a, errs)
327 thenL :: LintM a -> (a -> LintM b) -> LintM b
328 thenL m k = LintM $ \loc scope errs
329 -> case unLintM m loc scope errs of
330 (r, errs') -> unLintM (k r) loc scope errs'
332 thenL_ :: LintM a -> LintM b -> LintM b
333 thenL_ m k = LintM $ \loc scope errs
334 -> case unLintM m loc scope errs of
335 (_, errs') -> unLintM k loc scope errs'
339 checkL :: Bool -> Message -> LintM ()
340 checkL True msg = return ()
341 checkL False msg = addErrL msg
343 addErrL :: Message -> LintM ()
344 addErrL msg = LintM $ \loc scope errs -> ((), addErr errs msg loc)
346 addErr :: Bag Message -> Message -> [LintLocInfo] -> Bag Message
347 addErr errs_so_far msg locs
348 = errs_so_far `snocBag` mk_msg locs
350 mk_msg (loc:_) = let (l,hdr) = dumpLoc loc
351 in mkLocMessage l (hdr $$ msg)
354 addLoc :: LintLocInfo -> LintM a -> LintM a
355 addLoc extra_loc m = LintM $ \loc scope errs
356 -> unLintM m (extra_loc:loc) scope errs
358 addInScopeVars :: [Id] -> LintM a -> LintM a
359 addInScopeVars ids m = LintM $ \loc scope errs
360 -> -- We check if these "new" ids are already
361 -- in scope, i.e., we have *shadowing* going on.
362 -- For now, it's just a "trace"; we may make
363 -- a real error out of it...
365 new_set = mkVarSet ids
367 -- After adding -fliberate-case, Simon decided he likes shadowed
368 -- names after all. WDP 94/07
369 -- (if isEmptyVarSet shadowed
371 -- else pprTrace "Shadowed vars:" (ppr (varSetElems shadowed))) $
372 unLintM m loc (scope `unionVarSet` new_set) errs
375 Checking function applications: we only check that the type has the
376 right *number* of arrows, we don't actually compare the types. This
377 is because we can't expect the types to be equal - the type
378 applications and type lambdas that we use to calculate accurate types
379 have long since disappeared.
382 checkFunApp :: Type -- The function type
383 -> [Type] -- The arg type(s)
384 -> Message -- Error messgae
385 -> LintM (Maybe Type) -- The result type
387 checkFunApp fun_ty arg_tys msg = LintM checkFunApp'
389 checkFunApp' loc scope errs
390 = cfa res_ty expected_arg_tys arg_tys
392 (expected_arg_tys, res_ty) = splitFunTys (dropForAlls fun_ty)
394 cfa res_ty expected [] -- Args have run out; that's fine
395 = (Just (mkFunTys expected res_ty), errs)
397 cfa res_ty [] arg_tys -- Expected arg tys ran out first;
398 -- first see if res_ty is a tyvar template;
399 -- otherwise, maybe res_ty is a
400 -- dictionary type which is actually a function?
402 = (Just res_ty, errs)
404 = case splitFunTys res_ty of
405 ([], _) -> (Nothing, addErr errs msg loc) -- Too many args
406 (new_expected, new_res) -> cfa new_res new_expected arg_tys
408 cfa res_ty (expected_arg_ty:expected_arg_tys) (arg_ty:arg_tys)
409 = cfa res_ty expected_arg_tys arg_tys
413 checkInScope :: Id -> LintM ()
414 checkInScope id = LintM $ \loc scope errs
415 -> if isLocalId id && not (id `elemVarSet` scope) then
416 ((), addErr errs (hsep [ppr id, ptext SLIT("is out of scope")]) loc)
420 checkTys :: Type -> Type -> Message -> LintM ()
421 checkTys ty1 ty2 msg = LintM $ \loc scope errs
422 -> -- if (ty1 == ty2) then
424 -- else ((), addErr errs msg loc)
428 mkCaseAltMsg :: [StgAlt] -> Message
430 = ($$) (text "In some case alternatives, type of alternatives not all same:")
431 (empty) -- LATER: ppr alts
433 mkDefltMsg :: Id -> Message
435 = ($$) (ptext SLIT("Binder of a case expression doesn't match type of scrutinee:"))
438 mkFunAppMsg :: Type -> [Type] -> StgExpr -> Message
439 mkFunAppMsg fun_ty arg_tys expr
440 = vcat [text "In a function application, function type doesn't match arg types:",
441 hang (ptext SLIT("Function type:")) 4 (ppr fun_ty),
442 hang (ptext SLIT("Arg types:")) 4 (vcat (map (ppr) arg_tys)),
443 hang (ptext SLIT("Expression:")) 4 (ppr expr)]
445 mkRhsConMsg :: Type -> [Type] -> Message
446 mkRhsConMsg fun_ty arg_tys
447 = vcat [text "In a RHS constructor application, con type doesn't match arg types:",
448 hang (ptext SLIT("Constructor type:")) 4 (ppr fun_ty),
449 hang (ptext SLIT("Arg types:")) 4 (vcat (map (ppr) arg_tys))]
451 mkAltMsg1 :: Type -> Message
453 = ($$) (text "In a case expression, type of scrutinee does not match patterns")
456 mkAlgAltMsg2 :: Type -> DataCon -> Message
459 text "In some algebraic case alternative, constructor is not a constructor of scrutinee type:",
464 mkAlgAltMsg3 :: DataCon -> [Id] -> Message
465 mkAlgAltMsg3 con alts
467 text "In some algebraic case alternative, number of arguments doesn't match constructor:",
472 mkAlgAltMsg4 :: Type -> Id -> Message
475 text "In some algebraic case alternative, type of argument doesn't match data constructor:",
480 mkCaseOfCaseMsg :: StgExpr -> Message
482 = text "Case of non-tail-call:" $$ ppr e
484 mkRhsMsg :: Id -> Type -> Message
486 = vcat [hsep [ptext SLIT("The type of this binder doesn't match the type of its RHS:"),
488 hsep [ptext SLIT("Binder's type:"), ppr (idType binder)],
489 hsep [ptext SLIT("Rhs type:"), ppr ty]
492 mkUnLiftedTyMsg binder rhs
493 = (ptext SLIT("Let(rec) binder") <+> quotes (ppr binder) <+>
494 ptext SLIT("has unlifted type") <+> quotes (ppr (idType binder)))
496 (ptext SLIT("RHS:") <+> ppr rhs)