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
11 import Bag ( Bag, emptyBag, isEmptyBag, snocBag, bagToList )
12 import Id ( Id, idType, isLocalId )
14 import DataCon ( DataCon, dataConInstArgTys, dataConRepType )
15 import CoreSyn ( AltCon(..) )
16 import PrimOp ( primOpType )
17 import Literal ( literalType )
19 import Name ( getSrcLoc )
20 import ErrUtils ( Message, mkLocMessage )
22 import Type ( mkFunTys, splitFunTy_maybe, splitTyConApp_maybe,
23 isUnLiftedType, isTyVarTy, dropForAlls
25 import TyCon ( isAlgTyCon, isNewTyCon, tyConDataCons )
26 import Util ( zipEqual, equalLength )
34 (a) *some* type errors
35 (b) locally-defined variables used but not defined
38 Note: unless -dverbose-stg is on, display of lint errors will result
39 in "panic: bOGUS_LVs".
44 This module has suffered bit-rot; it is likely to yield lint errors
45 for Stg code that is currently perfectly acceptable for code
46 generation. Solution: don't use it! (KSW 2000-05).
49 %************************************************************************
51 \subsection{``lint'' for various constructs}
53 %************************************************************************
55 @lintStgBindings@ is the top-level interface function.
58 lintStgBindings :: String -> [StgBinding] -> [StgBinding]
60 lintStgBindings whodunnit binds
61 = {-# SCC "StgLint" #-}
62 case (initL (lint_binds binds)) of
64 Just msg -> pprPanic "" (vcat [
65 ptext (sLit "*** Stg Lint ErrMsgs: in") <+>
66 text whodunnit <+> ptext (sLit "***"),
68 ptext (sLit "*** Offending Program ***"),
70 ptext (sLit "*** End of Offense ***")])
72 lint_binds :: [StgBinding] -> LintM ()
74 lint_binds [] = return ()
75 lint_binds (bind:binds) = do
76 binders <- lintStgBinds bind
77 addInScopeVars binders $
83 lintStgArg :: StgArg -> LintM (Maybe Type)
84 lintStgArg (StgLitArg lit) = return (Just (literalType lit))
85 lintStgArg (StgVarArg v) = lintStgVar v
86 lintStgArg a = pprPanic "lintStgArg" (ppr a)
88 lintStgVar :: Id -> LintM (Maybe Kind)
89 lintStgVar v = do checkInScope v
90 return (Just (idType v))
94 lintStgBinds :: StgBinding -> LintM [Id] -- Returns the binders
95 lintStgBinds (StgNonRec binder rhs) = do
96 lint_binds_help (binder,rhs)
99 lintStgBinds (StgRec pairs)
100 = addInScopeVars binders $ do
101 mapM_ lint_binds_help pairs
104 binders = [b | (b,_) <- pairs]
106 lint_binds_help :: (Id, StgRhs) -> LintM ()
107 lint_binds_help (binder, rhs)
108 = addLoc (RhsOf binder) $ do
110 maybe_rhs_ty <- lintStgRhs rhs
112 -- Check binder doesn't have unlifted type
113 checkL (not (isUnLiftedType binder_ty))
114 (mkUnLiftedTyMsg binder rhs)
116 -- Check match to RHS type
119 Just rhs_ty -> checkTys binder_ty
121 (mkRhsMsg binder rhs_ty)
125 binder_ty = idType binder
129 lintStgRhs :: StgRhs -> LintM (Maybe Type)
131 lintStgRhs (StgRhsClosure _ _ _ _ _ [] expr)
134 lintStgRhs (StgRhsClosure _ _ _ _ _ binders expr)
135 = addLoc (LambdaBodyOf binders) $
136 addInScopeVars binders $ runMaybeT $ do
137 body_ty <- MaybeT $ lintStgExpr expr
138 return (mkFunTys (map idType binders) body_ty)
140 lintStgRhs (StgRhsCon _ con args) = runMaybeT $ do
141 arg_tys <- mapM (MaybeT . lintStgArg) args
142 MaybeT $ checkFunApp con_ty arg_tys (mkRhsConMsg con_ty arg_tys)
144 con_ty = dataConRepType con
148 lintStgExpr :: StgExpr -> LintM (Maybe Type) -- Nothing if error found
150 lintStgExpr (StgLit l) = return (Just (literalType l))
152 lintStgExpr e@(StgApp fun args) = runMaybeT $ do
153 fun_ty <- MaybeT $ lintStgVar fun
154 arg_tys <- mapM (MaybeT . lintStgArg) args
155 MaybeT $ checkFunApp fun_ty arg_tys (mkFunAppMsg fun_ty arg_tys e)
157 lintStgExpr e@(StgConApp con args) = runMaybeT $ do
158 arg_tys <- mapM (MaybeT . lintStgArg) args
159 MaybeT $ checkFunApp con_ty arg_tys (mkFunAppMsg con_ty arg_tys e)
161 con_ty = dataConRepType con
163 lintStgExpr (StgOpApp (StgFCallOp _ _) args res_ty) = runMaybeT $ do
164 -- We don't have enough type information to check
165 -- the application; ToDo
166 _maybe_arg_tys <- mapM (MaybeT . lintStgArg) args
169 lintStgExpr e@(StgOpApp (StgPrimOp op) args _) = runMaybeT $ do
170 arg_tys <- mapM (MaybeT . lintStgArg) args
171 MaybeT $ checkFunApp op_ty arg_tys (mkFunAppMsg op_ty arg_tys e)
173 op_ty = primOpType op
175 lintStgExpr (StgLam _ bndrs _) = do
176 addErrL (ptext (sLit "Unexpected StgLam") <+> ppr bndrs)
179 lintStgExpr (StgLet binds body) = do
180 binders <- lintStgBinds binds
181 addLoc (BodyOfLetRec binders) $
182 addInScopeVars binders $
185 lintStgExpr (StgLetNoEscape _ _ binds body) = do
186 binders <- lintStgBinds binds
187 addLoc (BodyOfLetRec binders) $
188 addInScopeVars binders $
191 lintStgExpr (StgSCC _ expr) = lintStgExpr expr
193 lintStgExpr e@(StgCase scrut _ _ bndr _ alts_type alts) = runMaybeT $ do
194 _ <- MaybeT $ lintStgExpr scrut
196 MaybeT $ liftM Just $
198 AlgAlt tc -> check_bndr tc
199 PrimAlt tc -> check_bndr tc
200 UbxTupAlt tc -> check_bndr tc
204 -- we only allow case of tail-call or primop.
206 StgApp _ _ -> return ()
207 StgConApp _ _ -> return ()
208 StgOpApp _ _ _ -> return ()
209 _ -> addErrL (mkCaseOfCaseMsg e)
211 addInScopeVars [bndr] $
212 lintStgAlts alts scrut_ty
214 scrut_ty = idType bndr
215 bad_bndr = mkDefltMsg bndr
216 check_bndr tc = case splitTyConApp_maybe scrut_ty of
217 Just (bndr_tc, _) -> checkL (tc == bndr_tc) bad_bndr
218 Nothing -> addErrL bad_bndr
220 lintStgExpr e = pprPanic "lintStgExpr" (ppr e)
222 lintStgAlts :: [StgAlt]
223 -> Type -- Type of scrutinee
224 -> LintM (Maybe Type) -- Type of alternatives
226 lintStgAlts alts scrut_ty = do
227 maybe_result_tys <- mapM (lintAlt scrut_ty) alts
229 -- Check the result types
230 case catMaybes (maybe_result_tys) of
233 (first_ty:tys) -> do mapM_ check tys
234 return (Just first_ty)
236 check ty = checkTys first_ty ty (mkCaseAltMsg alts)
238 lintAlt :: Type -> (AltCon, [Id], [Bool], StgExpr) -> LintM (Maybe Type)
239 lintAlt _ (DEFAULT, _, _, rhs)
242 lintAlt scrut_ty (LitAlt lit, _, _, rhs) = do
243 checkTys (literalType lit) scrut_ty (mkAltMsg1 scrut_ty)
246 lintAlt scrut_ty (DataAlt con, args, _, rhs) = do
247 case splitTyConApp_maybe scrut_ty of
248 Just (tycon, tys_applied) | isAlgTyCon tycon &&
249 not (isNewTyCon tycon) -> do
251 cons = tyConDataCons tycon
252 arg_tys = dataConInstArgTys con tys_applied
253 -- This almost certainly does not work for existential constructors
255 checkL (con `elem` cons) (mkAlgAltMsg2 scrut_ty con)
256 checkL (equalLength arg_tys args) (mkAlgAltMsg3 con args)
257 mapM_ check (zipEqual "lintAlgAlt:stg" arg_tys args)
260 addErrL (mkAltMsg1 scrut_ty)
262 addInScopeVars args $
265 check (ty, arg) = checkTys ty (idType arg) (mkAlgAltMsg4 ty arg)
267 -- elem: yes, the elem-list here can sometimes be long-ish,
268 -- but as it's use-once, probably not worth doing anything different
269 -- We give it its own copy, so it isn't overloaded.
271 elem x (y:ys) = x==y || elem x ys
275 %************************************************************************
277 \subsection[lint-monad]{The Lint monad}
279 %************************************************************************
282 newtype LintM a = LintM
283 { unLintM :: [LintLocInfo] -- Locations
284 -> IdSet -- Local vars in scope
285 -> Bag Message -- Error messages so far
286 -> (a, Bag Message) -- Result and error messages (if any)
290 = RhsOf Id -- The variable bound
291 | LambdaBodyOf [Id] -- The lambda-binder
292 | BodyOfLetRec [Id] -- One of the binders
294 dumpLoc :: LintLocInfo -> (SrcSpan, SDoc)
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 blankLine (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 _ = 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
392 cfa fun_ty [] -- Args have run out; that's fine
393 = (Just fun_ty, errs)
395 cfa fun_ty (_:arg_tys)
396 | Just (_arg_ty, res_ty) <- splitFunTy_maybe (dropForAlls fun_ty)
399 | isTyVarTy fun_ty -- Expected arg tys ran out first;
400 = (Just fun_ty, errs) -- first see if fun_ty is a tyvar template;
401 -- otherwise, maybe fun_ty is a
402 -- dictionary type which is actually a function?
404 = (Nothing, addErr errs msg loc) -- Too many args
408 checkInScope :: Id -> LintM ()
409 checkInScope id = LintM $ \loc scope errs
410 -> if isLocalId id && not (id `elemVarSet` scope) then
411 ((), addErr errs (hsep [ppr id, ptext (sLit "is out of scope")]) loc)
415 checkTys :: Type -> Type -> Message -> LintM ()
416 checkTys _ty1 _ty2 _msg = LintM $ \_loc _scope errs
417 -> -- if (ty1 == ty2) then
419 -- else ((), addErr errs msg loc)
423 mkCaseAltMsg :: [StgAlt] -> Message
425 = ($$) (text "In some case alternatives, type of alternatives not all same:")
426 (empty) -- LATER: ppr alts
428 mkDefltMsg :: Id -> Message
430 = ($$) (ptext (sLit "Binder of a case expression doesn't match type of scrutinee:"))
433 mkFunAppMsg :: Type -> [Type] -> StgExpr -> Message
434 mkFunAppMsg fun_ty arg_tys expr
435 = vcat [text "In a function application, function type doesn't match arg types:",
436 hang (ptext (sLit "Function type:")) 4 (ppr fun_ty),
437 hang (ptext (sLit "Arg types:")) 4 (vcat (map (ppr) arg_tys)),
438 hang (ptext (sLit "Expression:")) 4 (ppr expr)]
440 mkRhsConMsg :: Type -> [Type] -> Message
441 mkRhsConMsg fun_ty arg_tys
442 = vcat [text "In a RHS constructor application, con type doesn't match arg types:",
443 hang (ptext (sLit "Constructor type:")) 4 (ppr fun_ty),
444 hang (ptext (sLit "Arg types:")) 4 (vcat (map (ppr) arg_tys))]
446 mkAltMsg1 :: Type -> Message
448 = ($$) (text "In a case expression, type of scrutinee does not match patterns")
451 mkAlgAltMsg2 :: Type -> DataCon -> Message
454 text "In some algebraic case alternative, constructor is not a constructor of scrutinee type:",
459 mkAlgAltMsg3 :: DataCon -> [Id] -> Message
460 mkAlgAltMsg3 con alts
462 text "In some algebraic case alternative, number of arguments doesn't match constructor:",
467 mkAlgAltMsg4 :: Type -> Id -> Message
470 text "In some algebraic case alternative, type of argument doesn't match data constructor:",
475 mkCaseOfCaseMsg :: StgExpr -> Message
477 = text "Case of non-tail-call:" $$ ppr e
479 mkRhsMsg :: Id -> Type -> Message
481 = vcat [hsep [ptext (sLit "The type of this binder doesn't match the type of its RHS:"),
483 hsep [ptext (sLit "Binder's type:"), ppr (idType binder)],
484 hsep [ptext (sLit "Rhs type:"), ppr ty]
487 mkUnLiftedTyMsg :: Id -> StgRhs -> SDoc
488 mkUnLiftedTyMsg binder rhs
489 = (ptext (sLit "Let(rec) binder") <+> quotes (ppr binder) <+>
490 ptext (sLit "has unlifted type") <+> quotes (ppr (idType binder)))
492 (ptext (sLit "RHS:") <+> ppr rhs)