2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1996
4 \section[StgLint]{A ``lint'' pass to check for Stg correctness}
7 #include "HsVersions.h"
9 module StgLint ( lintStgBindings ) where
15 import Bag ( emptyBag, isEmptyBag, snocBag, foldBag )
16 import Id ( idType, isDataCon, dataConArgTys,
17 emptyIdSet, isEmptyIdSet, elementOfIdSet,
18 mkIdSet, intersectIdSets,
19 unionIdSets, idSetToList, SYN_IE(IdSet),
20 GenId{-instanced NamedThing-}
22 import Literal ( literalType, Literal{-instance Outputable-} )
23 import Maybes ( catMaybes )
24 import Name ( isLocallyDefined, getSrcLoc )
25 import Outputable ( Outputable(..){-instance * []-} )
26 import PprType ( GenType{-instance Outputable-}, TyCon )
27 import Pretty -- quite a bit of it
28 import PrimOp ( primOpType )
29 import SrcLoc ( SrcLoc{-instance Outputable-} )
30 import Type ( mkFunTys, splitFunTy, maybeAppDataTyConExpandingDicts,
31 isTyVarTy, eqTy, splitFunTyExpandingDicts
33 import Util ( zipEqual, pprPanic, panic, panic# )
35 infixr 9 `thenL`, `thenL_`, `thenMaybeL`, `thenMaybeL_`
37 unDictifyTy = panic "StgLint.unDictifyTy (ToDo)"
41 (a) *some* type errors
42 (b) locally-defined variables used but not defined
44 %************************************************************************
46 \subsection{``lint'' for various constructs}
48 %************************************************************************
50 @lintStgBindings@ is the top-level interface function.
53 lintStgBindings :: PprStyle -> String -> [StgBinding] -> [StgBinding]
55 lintStgBindings sty whodunnit binds
57 case (initL (lint_binds binds)) of
59 Just msg -> pprPanic "" (ppAboves [
60 ppStr ("*** Stg Lint Errors: in "++whodunnit++" ***"),
62 ppStr "*** Offending Program ***",
63 ppAboves (map (pprPlainStgBinding sty) binds),
64 ppStr "*** End of Offense ***"])
66 lint_binds :: [StgBinding] -> LintM ()
68 lint_binds [] = returnL ()
69 lint_binds (bind:binds)
70 = lintStgBinds bind `thenL` \ binders ->
71 addInScopeVars binders (
78 lintStgArg :: StgArg -> LintM (Maybe Type)
80 lintStgArg (StgLitArg lit) = returnL (Just (literalType lit))
81 lintStgArg (StgConArg con) = returnL (Just (idType con))
82 lintStgArg a@(StgVarArg v)
83 = checkInScope v `thenL_`
84 returnL (Just (idType v))
88 lintStgBinds :: StgBinding -> LintM [Id] -- Returns the binders
89 lintStgBinds (StgNonRec binder rhs)
90 = lint_binds_help (binder,rhs) `thenL_`
93 lintStgBinds (StgRec pairs)
94 = addInScopeVars binders (
95 mapL lint_binds_help pairs `thenL_`
99 binders = [b | (b,_) <- pairs]
101 lint_binds_help (binder, rhs)
102 = addLoc (RhsOf binder) (
104 lintStgRhs rhs `thenL` \ maybe_rhs_ty ->
106 -- Check match to RHS type
107 (case maybe_rhs_ty of
108 Nothing -> returnL ()
109 Just rhs_ty -> checkTys (idType binder)
111 (mkRhsMsg binder rhs_ty)
119 lintStgRhs :: StgRhs -> LintM (Maybe Type)
121 lintStgRhs (StgRhsClosure _ _ _ _ binders expr)
122 = addLoc (LambdaBodyOf binders) (
123 addInScopeVars binders (
124 lintStgExpr expr `thenMaybeL` \ body_ty ->
125 returnL (Just (mkFunTys (map idType binders) body_ty))
128 lintStgRhs (StgRhsCon _ con args)
129 = mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
130 case maybe_arg_tys of
131 Nothing -> returnL Nothing
132 Just arg_tys -> checkFunApp con_ty arg_tys (mkRhsConMsg con_ty arg_tys)
138 lintStgExpr :: StgExpr -> LintM (Maybe Type) -- Nothing if error found
140 lintStgExpr e@(StgApp fun args _)
141 = lintStgArg fun `thenMaybeL` \ fun_ty ->
142 mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
143 case maybe_arg_tys of
144 Nothing -> returnL Nothing
145 Just arg_tys -> checkFunApp fun_ty arg_tys (mkFunAppMsg fun_ty arg_tys e)
147 lintStgExpr e@(StgCon con args _)
148 = mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
149 case maybe_arg_tys of
150 Nothing -> returnL Nothing
151 Just arg_tys -> checkFunApp con_ty arg_tys (mkFunAppMsg con_ty arg_tys e)
155 lintStgExpr e@(StgPrim op args _)
156 = mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
157 case maybe_arg_tys of
158 Nothing -> returnL Nothing
159 Just arg_tys -> checkFunApp op_ty arg_tys (mkFunAppMsg op_ty arg_tys e)
161 op_ty = primOpType op
163 lintStgExpr (StgLet binds body)
164 = lintStgBinds binds `thenL` \ binders ->
165 addLoc (BodyOfLetRec binders) (
166 addInScopeVars binders (
170 lintStgExpr (StgLetNoEscape _ _ binds body)
171 = lintStgBinds binds `thenL` \ binders ->
172 addLoc (BodyOfLetRec binders) (
173 addInScopeVars binders (
177 lintStgExpr (StgSCC _ _ expr) = lintStgExpr expr
179 lintStgExpr e@(StgCase scrut _ _ _ alts)
180 = lintStgExpr scrut `thenMaybeL` \ _ ->
182 -- Check that it is a data type
183 case (maybeAppDataTyConExpandingDicts scrut_ty) of
184 Nothing -> addErrL (mkCaseDataConMsg e) `thenL_`
187 -> lintStgAlts alts scrut_ty tycon
189 scrut_ty = get_ty alts
191 get_ty (StgAlgAlts ty _ _) = ty
192 get_ty (StgPrimAlts ty _ _) = ty
196 lintStgAlts :: StgCaseAlts
197 -> Type -- Type of scrutinee
198 -> TyCon -- TyCon pinned on the case
199 -> LintM (Maybe Type) -- Type of alternatives
201 lintStgAlts alts scrut_ty case_tycon
203 StgAlgAlts _ alg_alts deflt ->
204 mapL (lintAlgAlt scrut_ty) alg_alts `thenL` \ maybe_alt_tys ->
205 lintDeflt deflt scrut_ty `thenL` \ maybe_deflt_ty ->
206 returnL (maybe_deflt_ty : maybe_alt_tys)
208 StgPrimAlts _ prim_alts deflt ->
209 mapL (lintPrimAlt scrut_ty) prim_alts `thenL` \ maybe_alt_tys ->
210 lintDeflt deflt scrut_ty `thenL` \ maybe_deflt_ty ->
211 returnL (maybe_deflt_ty : maybe_alt_tys)
212 ) `thenL` \ maybe_result_tys ->
213 -- Check the result types
214 case catMaybes (maybe_result_tys) of
215 [] -> returnL Nothing
217 (first_ty:tys) -> mapL check tys `thenL_`
218 returnL (Just first_ty)
220 check ty = checkTys first_ty ty (mkCaseAltMsg alts)
222 lintAlgAlt scrut_ty (con, args, _, rhs)
223 = (case maybeAppDataTyConExpandingDicts scrut_ty of
225 addErrL (mkAlgAltMsg1 scrut_ty)
226 Just (tycon, tys_applied, cons) ->
228 arg_tys = dataConArgTys con tys_applied
230 checkL (con `elem` cons) (mkAlgAltMsg2 scrut_ty con) `thenL_`
231 checkL (length arg_tys == length args) (mkAlgAltMsg3 con args)
233 mapL check (zipEqual "lintAlgAlt:stg" arg_tys args) `thenL_`
236 addInScopeVars args (
240 check (ty, arg) = checkTys ty (idType arg) (mkAlgAltMsg4 ty arg)
242 -- elem: yes, the elem-list here can sometimes be long-ish,
243 -- but as it's use-once, probably not worth doing anything different
244 -- We give it its own copy, so it isn't overloaded.
246 elem x (y:ys) = x==y || elem x ys
248 lintPrimAlt scrut_ty alt@(lit,rhs)
249 = checkTys (literalType lit) scrut_ty (mkPrimAltMsg alt) `thenL_`
252 lintDeflt StgNoDefault scrut_ty = returnL Nothing
253 lintDeflt deflt@(StgBindDefault binder _ rhs) scrut_ty
254 = checkTys (idType binder) scrut_ty (mkDefltMsg deflt) `thenL_`
255 addInScopeVars [binder] (
261 %************************************************************************
263 \subsection[lint-monad]{The Lint monad}
265 %************************************************************************
268 type LintM a = [LintLocInfo] -- Locations
269 -> IdSet -- Local vars in scope
270 -> Bag ErrMsg -- Error messages so far
271 -> (a, Bag ErrMsg) -- Result and error messages (if any)
273 type ErrMsg = PprStyle -> Pretty
276 = RhsOf Id -- The variable bound
277 | LambdaBodyOf [Id] -- The lambda-binder
278 | BodyOfLetRec [Id] -- One of the binders
280 instance Outputable LintLocInfo where
282 = ppBesides [ppr sty (getSrcLoc v), ppStr ": [RHS of ", pp_binders sty [v], ppStr "]"]
284 ppr sty (LambdaBodyOf bs)
285 = ppBesides [ppr sty (getSrcLoc (head bs)),
286 ppStr ": [in body of lambda with binders ", pp_binders sty bs, ppStr "]"]
288 ppr sty (BodyOfLetRec bs)
289 = ppBesides [ppr sty (getSrcLoc (head bs)),
290 ppStr ": [in body of letrec with binders ", pp_binders sty bs, ppStr "]"]
292 pp_binders :: PprStyle -> [Id] -> Pretty
294 = ppInterleave ppComma (map pp_binder bs)
297 = ppCat [ppr sty b, ppStr "::", ppr sty (idType b)]
301 initL :: LintM a -> Maybe ErrMsg
303 = case (m [] emptyIdSet emptyBag) of { (_, errs) ->
304 if isEmptyBag errs then
308 foldBag ppAbove ( \ msg -> msg sty ) ppNil errs
312 returnL :: a -> LintM a
313 returnL r loc scope errs = (r, errs)
315 thenL :: LintM a -> (a -> LintM b) -> LintM b
316 thenL m k loc scope errs
317 = case m loc scope errs of
318 (r, errs') -> k r loc scope errs'
320 thenL_ :: LintM a -> LintM b -> LintM b
321 thenL_ m k loc scope errs
322 = case m loc scope errs of
323 (_, errs') -> k loc scope errs'
325 thenMaybeL :: LintM (Maybe a) -> (a -> LintM (Maybe b)) -> LintM (Maybe b)
326 thenMaybeL m k loc scope errs
327 = case m loc scope errs of
328 (Nothing, errs2) -> (Nothing, errs2)
329 (Just r, errs2) -> k r loc scope errs2
331 thenMaybeL_ :: LintM (Maybe a) -> LintM (Maybe b) -> LintM (Maybe b)
332 thenMaybeL_ m k loc scope errs
333 = case m loc scope errs of
334 (Nothing, errs2) -> (Nothing, errs2)
335 (Just _, errs2) -> k loc scope errs2
337 mapL :: (a -> LintM b) -> [a] -> LintM [b]
338 mapL f [] = returnL []
341 mapL f xs `thenL` \ rs ->
344 mapMaybeL :: (a -> LintM (Maybe b)) -> [a] -> LintM (Maybe [b])
345 -- Returns Nothing if anything fails
346 mapMaybeL f [] = returnL (Just [])
348 = f x `thenMaybeL` \ r ->
349 mapMaybeL f xs `thenMaybeL` \ rs ->
350 returnL (Just (r:rs))
354 checkL :: Bool -> ErrMsg -> LintM ()
355 checkL True msg loc scope errs = ((), errs)
356 checkL False msg loc scope errs = ((), addErr errs msg loc)
358 addErrL :: ErrMsg -> LintM ()
359 addErrL msg loc scope errs = ((), addErr errs msg loc)
361 addErr :: Bag ErrMsg -> ErrMsg -> [LintLocInfo] -> Bag ErrMsg
363 addErr errs_so_far msg locs
364 = errs_so_far `snocBag` ( \ sty ->
365 ppHang (ppr sty (head locs)) 4 (msg sty)
368 addLoc :: LintLocInfo -> LintM a -> LintM a
369 addLoc extra_loc m loc scope errs
370 = m (extra_loc:loc) scope errs
372 addInScopeVars :: [Id] -> LintM a -> LintM a
373 addInScopeVars ids m loc scope errs
374 = -- We check if these "new" ids are already
375 -- in scope, i.e., we have *shadowing* going on.
376 -- For now, it's just a "trace"; we may make
377 -- a real error out of it...
379 new_set = mkIdSet ids
381 shadowed = scope `intersectIdSets` new_set
383 -- After adding -fliberate-case, Simon decided he likes shadowed
384 -- names after all. WDP 94/07
385 -- (if isEmptyIdSet shadowed
387 -- else pprTrace "Shadowed vars:" (ppr PprDebug (idSetToList shadowed))) $
388 m loc (scope `unionIdSets` new_set) errs
392 checkFunApp :: Type -- The function type
393 -> [Type] -- The arg type(s)
394 -> ErrMsg -- Error messgae
395 -> LintM (Maybe Type) -- The result type
397 checkFunApp fun_ty arg_tys msg loc scope errs
398 = cfa res_ty expected_arg_tys arg_tys
400 (expected_arg_tys, res_ty) = splitFunTyExpandingDicts fun_ty
402 cfa res_ty expected [] -- Args have run out; that's fine
403 = (Just (mkFunTys expected res_ty), errs)
405 cfa res_ty [] arg_tys -- Expected arg tys ran out first;
406 -- first see if res_ty is a tyvar template;
407 -- otherwise, maybe res_ty is a
408 -- dictionary type which is actually a function?
410 = (Just res_ty, errs)
412 = case splitFunTy (unDictifyTy res_ty) of
413 ([], _) -> (Nothing, addErr errs msg loc) -- Too many args
414 (new_expected, new_res) -> cfa new_res new_expected arg_tys
416 cfa res_ty (expected_arg_ty:expected_arg_tys) (arg_ty:arg_tys)
417 = if (sleazy_eq_ty expected_arg_ty arg_ty)
418 then cfa res_ty expected_arg_tys arg_tys
419 else (Nothing, addErr errs msg loc) -- Arg mis-match
423 checkInScope :: Id -> LintM ()
424 checkInScope id loc scope errs
425 = if isLocallyDefined id && not (isDataCon id) && not (id `elementOfIdSet` scope) then
426 ((), addErr errs (\ sty -> ppCat [ppr sty id, ppStr "is out of scope"]) loc)
430 checkTys :: Type -> Type -> ErrMsg -> LintM ()
431 checkTys ty1 ty2 msg loc scope errs
432 = if (sleazy_eq_ty ty1 ty2)
434 else ((), addErr errs msg loc)
438 mkCaseAltMsg :: StgCaseAlts -> ErrMsg
439 mkCaseAltMsg alts sty
440 = ppAbove (ppStr "In some case alternatives, type of alternatives not all same:")
441 -- LATER: (ppr sty alts)
442 (panic "mkCaseAltMsg")
444 mkCaseDataConMsg :: StgExpr -> ErrMsg
445 mkCaseDataConMsg expr sty
446 = ppAbove (ppStr "A case scrutinee not a type-constructor type:")
449 mkCaseAbstractMsg :: TyCon -> ErrMsg
450 mkCaseAbstractMsg tycon sty
451 = ppAbove (ppStr "An algebraic case on an abstract type:")
454 mkDefltMsg :: StgCaseDefault -> ErrMsg
456 = ppAbove (ppStr "Binder in default case of a case expression doesn't match type of scrutinee:")
457 --LATER: (ppr sty deflt)
460 mkFunAppMsg :: Type -> [Type] -> StgExpr -> ErrMsg
461 mkFunAppMsg fun_ty arg_tys expr sty
462 = ppAboves [ppStr "In a function application, function type doesn't match arg types:",
463 ppHang (ppStr "Function type:") 4 (ppr sty fun_ty),
464 ppHang (ppStr "Arg types:") 4 (ppAboves (map (ppr sty) arg_tys)),
465 ppHang (ppStr "Expression:") 4 (pp_expr sty expr)]
467 mkRhsConMsg :: Type -> [Type] -> ErrMsg
468 mkRhsConMsg fun_ty arg_tys sty
469 = ppAboves [ppStr "In a RHS constructor application, con type doesn't match arg types:",
470 ppHang (ppStr "Constructor type:") 4 (ppr sty fun_ty),
471 ppHang (ppStr "Arg types:") 4 (ppAboves (map (ppr sty) arg_tys))]
473 mkUnappTyMsg :: Id -> Type -> ErrMsg
474 mkUnappTyMsg var ty sty
475 = ppAboves [ppStr "Variable has a for-all type, but isn't applied to any types.",
476 ppBeside (ppStr "Var: ") (ppr sty var),
477 ppBeside (ppStr "Its type: ") (ppr sty ty)]
479 mkAlgAltMsg1 :: Type -> ErrMsg
481 = ppAbove (ppStr "In some case statement, type of scrutinee is not a data type:")
484 mkAlgAltMsg2 :: Type -> Id -> ErrMsg
485 mkAlgAltMsg2 ty con sty
487 ppStr "In some algebraic case alternative, constructor is not a constructor of scrutinee type:",
492 mkAlgAltMsg3 :: Id -> [Id] -> ErrMsg
493 mkAlgAltMsg3 con alts sty
495 ppStr "In some algebraic case alternative, number of arguments doesn't match constructor:",
500 mkAlgAltMsg4 :: Type -> Id -> ErrMsg
501 mkAlgAltMsg4 ty arg sty
503 ppStr "In some algebraic case alternative, type of argument doesn't match data constructor:",
508 mkPrimAltMsg :: (Literal, StgExpr) -> ErrMsg
510 = ppAbove (ppStr "In a primitive case alternative, type of literal doesn't match type of scrutinee:")
513 mkRhsMsg :: Id -> Type -> ErrMsg
514 mkRhsMsg binder ty sty
515 = ppAboves [ppCat [ppStr "The type of this binder doesn't match the type of its RHS:",
517 ppCat [ppStr "Binder's type:", ppr sty (idType binder)],
518 ppCat [ppStr "Rhs type:", ppr sty ty]
521 pp_expr :: PprStyle -> StgExpr -> Pretty
522 pp_expr sty expr = ppr sty expr
525 -- NB: probably severe overkill (WDP 95/04)
526 = trace "StgLint.sleazy_eq_ty:use eqSimplTy?" $
527 case (splitFunTyExpandingDicts ty1) of { (tyargs1,tyres1) ->
528 case (splitFunTyExpandingDicts ty2) of { (tyargs2,tyres2) ->
530 ty11 = mkFunTys tyargs1 tyres1
531 ty22 = mkFunTys tyargs2 tyres2