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, bagToList )
14 import Id ( Id, idType, isLocalId )
16 import DataCon ( DataCon, dataConInstArgTys, dataConRepType )
17 import CoreSyn ( AltCon(..) )
18 import PrimOp ( primOpType )
19 import Literal ( literalType )
20 import Maybes ( catMaybes )
21 import Name ( getSrcLoc )
22 import ErrUtils ( Message, mkLocMessage )
23 import Type ( mkFunTys, splitFunTys, splitTyConApp_maybe,
24 isUnLiftedType, isTyVarTy, dropForAlls, Type
26 import TyCon ( isAlgTyCon, isNewTyCon, tyConDataCons )
27 import Util ( zipEqual, equalLength )
28 import SrcLoc ( srcLocSpan )
31 infixr 9 `thenL`, `thenL_`, `thenMaybeL`
35 (a) *some* type errors
36 (b) locally-defined variables used but not defined
39 Note: unless -dverbose-stg is on, display of lint errors will result
40 in "panic: bOGUS_LVs".
45 This module has suffered bit-rot; it is likely to yield lint errors
46 for Stg code that is currently perfectly acceptable for code
47 generation. Solution: don't use it! (KSW 2000-05).
50 %************************************************************************
52 \subsection{``lint'' for various constructs}
54 %************************************************************************
56 @lintStgBindings@ is the top-level interface function.
59 lintStgBindings :: String -> [StgBinding] -> [StgBinding]
61 lintStgBindings whodunnit binds
62 = {-# SCC "StgLint" #-}
63 case (initL (lint_binds binds)) of
65 Just msg -> pprPanic "" (vcat [
66 ptext SLIT("*** Stg Lint ErrMsgs: in") <+> text whodunnit <+> ptext SLIT("***"),
68 ptext SLIT("*** Offending Program ***"),
70 ptext SLIT("*** End of Offense ***")])
72 lint_binds :: [StgBinding] -> LintM ()
74 lint_binds [] = returnL ()
75 lint_binds (bind:binds)
76 = lintStgBinds bind `thenL` \ binders ->
77 addInScopeVars binders (
84 lintStgArg :: StgArg -> LintM (Maybe Type)
85 lintStgArg (StgLitArg lit) = returnL (Just (literalType lit))
86 lintStgArg (StgVarArg v) = lintStgVar v
88 lintStgVar v = checkInScope v `thenL_`
89 returnL (Just (idType v))
93 lintStgBinds :: StgBinding -> LintM [Id] -- Returns the binders
94 lintStgBinds (StgNonRec binder rhs)
95 = lint_binds_help (binder,rhs) `thenL_`
98 lintStgBinds (StgRec pairs)
99 = addInScopeVars binders (
100 mapL lint_binds_help pairs `thenL_`
104 binders = [b | (b,_) <- pairs]
106 lint_binds_help (binder, rhs)
107 = addLoc (RhsOf binder) (
109 lintStgRhs rhs `thenL` \ maybe_rhs_ty ->
111 -- Check binder doesn't have unlifted type
112 checkL (not (isUnLiftedType binder_ty))
113 (mkUnLiftedTyMsg binder rhs) `thenL_`
115 -- Check match to RHS type
116 (case maybe_rhs_ty of
117 Nothing -> returnL ()
118 Just rhs_ty -> checkTys binder_ty
120 (mkRhsMsg binder rhs_ty)
126 binder_ty = idType binder
130 lintStgRhs :: StgRhs -> LintM (Maybe Type)
132 lintStgRhs (StgRhsClosure _ _ _ _ _ [] expr)
135 lintStgRhs (StgRhsClosure _ _ _ _ _ binders expr)
136 = addLoc (LambdaBodyOf binders) (
137 addInScopeVars binders (
138 lintStgExpr expr `thenMaybeL` \ body_ty ->
139 returnL (Just (mkFunTys (map idType binders) body_ty))
142 lintStgRhs (StgRhsCon _ con args)
143 = mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
144 case maybe_arg_tys of
145 Nothing -> returnL Nothing
146 Just arg_tys -> 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) = returnL (Just (literalType l))
156 lintStgExpr e@(StgApp fun args)
157 = lintStgVar fun `thenMaybeL` \ fun_ty ->
158 mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
159 case maybe_arg_tys of
160 Nothing -> returnL Nothing
161 Just arg_tys -> checkFunApp fun_ty arg_tys (mkFunAppMsg fun_ty arg_tys e)
163 lintStgExpr e@(StgConApp con args)
164 = mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
165 case maybe_arg_tys of
166 Nothing -> returnL Nothing
167 Just arg_tys -> checkFunApp con_ty arg_tys (mkFunAppMsg con_ty arg_tys e)
169 con_ty = dataConRepType con
171 lintStgExpr e@(StgOpApp (StgFCallOp _ _) args res_ty)
172 = -- We don't have enough type information to check
173 -- the application; ToDo
174 mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
175 returnL (Just res_ty)
177 lintStgExpr e@(StgOpApp (StgPrimOp op) args _)
178 = mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
179 case maybe_arg_tys of
180 Nothing -> returnL Nothing
181 Just arg_tys -> checkFunApp op_ty arg_tys (mkFunAppMsg op_ty arg_tys e)
183 op_ty = primOpType op
185 lintStgExpr (StgLam _ bndrs _)
186 = addErrL (ptext SLIT("Unexpected StgLam") <+> ppr bndrs) `thenL_`
189 lintStgExpr (StgLet binds body)
190 = lintStgBinds binds `thenL` \ binders ->
191 addLoc (BodyOfLetRec binders) (
192 addInScopeVars binders (
196 lintStgExpr (StgLetNoEscape _ _ binds body)
197 = lintStgBinds binds `thenL` \ binders ->
198 addLoc (BodyOfLetRec binders) (
199 addInScopeVars binders (
203 lintStgExpr (StgSCC _ expr) = lintStgExpr expr
205 lintStgExpr e@(StgCase scrut _ _ bndr _ alts_type alts)
206 = lintStgExpr scrut `thenMaybeL` \ _ ->
209 AlgAlt tc -> check_bndr tc
210 PrimAlt tc -> check_bndr tc
211 UbxTupAlt tc -> check_bndr tc
212 PolyAlt -> returnL ()
215 (trace (showSDoc (ppr e)) $
216 -- we only allow case of tail-call or primop.
218 StgApp _ _ -> returnL ()
219 StgConApp _ _ -> returnL ()
220 StgOpApp _ _ _ -> returnL ()
221 other -> addErrL (mkCaseOfCaseMsg e)) `thenL_`
223 addInScopeVars [bndr] (lintStgAlts alts scrut_ty)
226 scrut_ty = idType bndr
227 bad_bndr = mkDefltMsg bndr
228 check_bndr tc = case splitTyConApp_maybe scrut_ty of
229 Just (bndr_tc, _) -> checkL (tc == bndr_tc) bad_bndr
230 Nothing -> addErrL bad_bndr
233 lintStgAlts :: [StgAlt]
234 -> Type -- Type of scrutinee
235 -> LintM (Maybe Type) -- Type of alternatives
237 lintStgAlts alts scrut_ty
238 = mapL (lintAlt scrut_ty) alts `thenL` \ maybe_result_tys ->
240 -- Check the result types
241 case catMaybes (maybe_result_tys) of
242 [] -> returnL Nothing
244 (first_ty:tys) -> mapL check tys `thenL_`
245 returnL (Just first_ty)
247 check ty = checkTys first_ty ty (mkCaseAltMsg alts)
249 lintAlt scrut_ty (DEFAULT, _, _, rhs)
252 lintAlt scrut_ty (LitAlt lit, _, _, rhs)
253 = checkTys (literalType lit) scrut_ty (mkAltMsg1 scrut_ty) `thenL_`
256 lintAlt scrut_ty (DataAlt con, args, _, rhs)
257 = (case splitTyConApp_maybe scrut_ty of
258 Just (tycon, tys_applied) | isAlgTyCon tycon &&
259 not (isNewTyCon tycon) ->
261 cons = tyConDataCons tycon
262 arg_tys = dataConInstArgTys con tys_applied
263 -- This almost certainly does not work for existential constructors
265 checkL (con `elem` cons) (mkAlgAltMsg2 scrut_ty con) `thenL_`
266 checkL (equalLength arg_tys args) (mkAlgAltMsg3 con args)
268 mapL check (zipEqual "lintAlgAlt:stg" arg_tys args) `thenL_`
271 addErrL (mkAltMsg1 scrut_ty)
273 addInScopeVars args (
277 check (ty, arg) = checkTys ty (idType arg) (mkAlgAltMsg4 ty arg)
279 -- elem: yes, the elem-list here can sometimes be long-ish,
280 -- but as it's use-once, probably not worth doing anything different
281 -- We give it its own copy, so it isn't overloaded.
283 elem x (y:ys) = x==y || elem x ys
287 %************************************************************************
289 \subsection[lint-monad]{The Lint monad}
291 %************************************************************************
294 type LintM a = [LintLocInfo] -- Locations
295 -> IdSet -- Local vars in scope
296 -> Bag Message -- Error messages so far
297 -> (a, Bag Message) -- Result and error messages (if any)
300 = RhsOf Id -- The variable bound
301 | LambdaBodyOf [Id] -- The lambda-binder
302 | BodyOfLetRec [Id] -- One of the binders
305 (srcLocSpan (getSrcLoc v), ptext SLIT(" [RHS of ") <> pp_binders [v] <> char ']' )
306 dumpLoc (LambdaBodyOf bs) =
307 (srcLocSpan (getSrcLoc (head bs)), ptext SLIT(" [in body of lambda with binders ") <> pp_binders bs <> char ']' )
309 dumpLoc (BodyOfLetRec bs) =
310 (srcLocSpan (getSrcLoc (head bs)), ptext SLIT(" [in body of letrec with binders ") <> pp_binders bs <> char ']' )
313 pp_binders :: [Id] -> SDoc
315 = sep (punctuate comma (map pp_binder bs))
318 = hsep [ppr b, dcolon, ppr (idType b)]
322 initL :: LintM a -> Maybe Message
324 = case (m [] emptyVarSet emptyBag) of { (_, errs) ->
325 if isEmptyBag errs then
328 Just (vcat (punctuate (text "") (bagToList errs)))
331 returnL :: a -> LintM a
332 returnL r loc scope errs = (r, errs)
334 thenL :: LintM a -> (a -> LintM b) -> LintM b
335 thenL m k loc scope errs
336 = case m loc scope errs of
337 (r, errs') -> k r loc scope errs'
339 thenL_ :: LintM a -> LintM b -> LintM b
340 thenL_ m k loc scope errs
341 = case m loc scope errs of
342 (_, errs') -> k loc scope errs'
344 thenMaybeL :: LintM (Maybe a) -> (a -> LintM (Maybe b)) -> LintM (Maybe b)
345 thenMaybeL m k loc scope errs
346 = case m loc scope errs of
347 (Nothing, errs2) -> (Nothing, errs2)
348 (Just r, errs2) -> k r loc scope errs2
350 mapL :: (a -> LintM b) -> [a] -> LintM [b]
351 mapL f [] = returnL []
354 mapL f xs `thenL` \ rs ->
357 mapMaybeL :: (a -> LintM (Maybe b)) -> [a] -> LintM (Maybe [b])
358 -- Returns Nothing if anything fails
359 mapMaybeL f [] = returnL (Just [])
361 = f x `thenMaybeL` \ r ->
362 mapMaybeL f xs `thenMaybeL` \ rs ->
363 returnL (Just (r:rs))
367 checkL :: Bool -> Message -> LintM ()
368 checkL True msg loc scope errs = ((), errs)
369 checkL False msg loc scope errs = ((), addErr errs msg loc)
371 addErrL :: Message -> LintM ()
372 addErrL msg loc scope errs = ((), addErr errs msg loc)
374 addErr :: Bag Message -> Message -> [LintLocInfo] -> Bag Message
376 addErr errs_so_far msg locs
377 = errs_so_far `snocBag` mk_msg locs
379 mk_msg (loc:_) = let (l,hdr) = dumpLoc loc
380 in mkLocMessage l (hdr $$ msg)
383 addLoc :: LintLocInfo -> LintM a -> LintM a
384 addLoc extra_loc m loc scope errs
385 = m (extra_loc:loc) scope errs
387 addInScopeVars :: [Id] -> LintM a -> LintM a
388 addInScopeVars ids m loc scope errs
389 = -- We check if these "new" ids are already
390 -- in scope, i.e., we have *shadowing* going on.
391 -- For now, it's just a "trace"; we may make
392 -- a real error out of it...
394 new_set = mkVarSet ids
396 -- After adding -fliberate-case, Simon decided he likes shadowed
397 -- names after all. WDP 94/07
398 -- (if isEmptyVarSet shadowed
400 -- else pprTrace "Shadowed vars:" (ppr (varSetElems shadowed))) $
401 m loc (scope `unionVarSet` new_set) errs
404 Checking function applications: we only check that the type has the
405 right *number* of arrows, we don't actually compare the types. This
406 is because we can't expect the types to be equal - the type
407 applications and type lambdas that we use to calculate accurate types
408 have long since disappeared.
411 checkFunApp :: Type -- The function type
412 -> [Type] -- The arg type(s)
413 -> Message -- Error messgae
414 -> LintM (Maybe Type) -- The result type
416 checkFunApp fun_ty arg_tys msg loc scope errs
417 = cfa res_ty expected_arg_tys arg_tys
419 (expected_arg_tys, res_ty) = splitFunTys (dropForAlls fun_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 isLocalId 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 :: [StgAlt] -> Message
457 = ($$) (text "In some case alternatives, type of alternatives not all same:")
458 (empty) -- LATER: ppr alts
460 mkDefltMsg :: Id -> Message
462 = ($$) (ptext SLIT("Binder of a case expression doesn't match type of scrutinee:"))
465 mkFunAppMsg :: Type -> [Type] -> StgExpr -> Message
466 mkFunAppMsg fun_ty arg_tys expr
467 = vcat [text "In a function application, function type doesn't match arg types:",
468 hang (ptext SLIT("Function type:")) 4 (ppr fun_ty),
469 hang (ptext SLIT("Arg types:")) 4 (vcat (map (ppr) arg_tys)),
470 hang (ptext SLIT("Expression:")) 4 (ppr expr)]
472 mkRhsConMsg :: Type -> [Type] -> Message
473 mkRhsConMsg fun_ty arg_tys
474 = vcat [text "In a RHS constructor application, con type doesn't match arg types:",
475 hang (ptext SLIT("Constructor type:")) 4 (ppr fun_ty),
476 hang (ptext SLIT("Arg types:")) 4 (vcat (map (ppr) arg_tys))]
478 mkAltMsg1 :: Type -> Message
480 = ($$) (text "In a case expression, type of scrutinee does not match patterns")
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 mkCaseOfCaseMsg :: StgExpr -> Message
509 = text "Case of non-tail-call:" $$ ppr e
511 mkRhsMsg :: Id -> Type -> Message
513 = vcat [hsep [ptext SLIT("The type of this binder doesn't match the type of its RHS:"),
515 hsep [ptext SLIT("Binder's type:"), ppr (idType binder)],
516 hsep [ptext SLIT("Rhs type:"), ppr ty]
519 mkUnLiftedTyMsg binder rhs
520 = (ptext SLIT("Let(rec) binder") <+> quotes (ppr binder) <+>
521 ptext SLIT("has unlifted type") <+> quotes (ppr (idType binder)))
523 (ptext SLIT("RHS:") <+> ppr rhs)