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 ( Bag, emptyBag, isEmptyBag, snocBag, foldBag )
16 import Id ( idType, isAlgCon, dataConArgTys,
17 emptyIdSet, isEmptyIdSet, elementOfIdSet,
18 mkIdSet, intersectIdSets,
19 unionIdSets, idSetToList, SYN_IE(IdSet),
20 GenId{-instanced NamedThing-}, SYN_IE(Id)
22 import Literal ( literalType, Literal{-instance Outputable-} )
23 import Maybes ( catMaybes )
24 import Name ( isLocallyDefined, getSrcLoc )
25 import Outputable ( PprStyle, 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, SYN_IE(Type)
33 import TyCon ( isDataTyCon )
34 import Util ( zipEqual, pprPanic, panic, panic# )
36 infixr 9 `thenL`, `thenL_`, `thenMaybeL`, `thenMaybeL_`
38 unDictifyTy = panic "StgLint.unDictifyTy (ToDo)"
42 (a) *some* type errors
43 (b) locally-defined variables used but not defined
45 %************************************************************************
47 \subsection{``lint'' for various constructs}
49 %************************************************************************
51 @lintStgBindings@ is the top-level interface function.
54 lintStgBindings :: PprStyle -> String -> [StgBinding] -> [StgBinding]
56 lintStgBindings sty whodunnit binds
58 case (initL (lint_binds binds)) of
60 Just msg -> pprPanic "" (vcat [
61 ptext SLIT("*** Stg Lint Errors: in "),text whodunnit, ptext SLIT(" ***"),
63 ptext SLIT("*** Offending Program ***"),
64 vcat (map (pprPlainStgBinding sty) binds),
65 ptext SLIT("*** End of Offense ***")])
67 lint_binds :: [StgBinding] -> LintM ()
69 lint_binds [] = returnL ()
70 lint_binds (bind:binds)
71 = lintStgBinds bind `thenL` \ binders ->
72 addInScopeVars binders (
79 lintStgArg :: StgArg -> LintM (Maybe Type)
81 lintStgArg (StgLitArg lit) = returnL (Just (literalType lit))
82 lintStgArg (StgConArg con) = returnL (Just (idType con))
83 lintStgArg a@(StgVarArg v)
84 = checkInScope v `thenL_`
85 returnL (Just (idType v))
89 lintStgBinds :: StgBinding -> LintM [Id] -- Returns the binders
90 lintStgBinds (StgNonRec binder rhs)
91 = lint_binds_help (binder,rhs) `thenL_`
94 lintStgBinds (StgRec pairs)
95 = addInScopeVars binders (
96 mapL lint_binds_help pairs `thenL_`
100 binders = [b | (b,_) <- pairs]
102 lint_binds_help (binder, rhs)
103 = addLoc (RhsOf binder) (
105 lintStgRhs rhs `thenL` \ maybe_rhs_ty ->
107 -- Check match to RHS type
108 (case maybe_rhs_ty of
109 Nothing -> returnL ()
110 Just rhs_ty -> checkTys (idType binder)
112 (mkRhsMsg binder rhs_ty)
120 lintStgRhs :: StgRhs -> LintM (Maybe Type)
122 lintStgRhs (StgRhsClosure _ _ _ _ binders expr)
123 = addLoc (LambdaBodyOf binders) (
124 addInScopeVars binders (
125 lintStgExpr expr `thenMaybeL` \ body_ty ->
126 returnL (Just (mkFunTys (map idType binders) body_ty))
129 lintStgRhs (StgRhsCon _ con args)
130 = mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
131 case maybe_arg_tys of
132 Nothing -> returnL Nothing
133 Just arg_tys -> checkFunApp con_ty arg_tys (mkRhsConMsg con_ty arg_tys)
139 lintStgExpr :: StgExpr -> LintM (Maybe Type) -- Nothing if error found
141 lintStgExpr e@(StgApp fun args _)
142 = lintStgArg fun `thenMaybeL` \ fun_ty ->
143 mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
144 case maybe_arg_tys of
145 Nothing -> returnL Nothing
146 Just arg_tys -> checkFunApp fun_ty arg_tys (mkFunAppMsg fun_ty arg_tys e)
148 lintStgExpr e@(StgCon con args _)
149 = mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
150 case maybe_arg_tys of
151 Nothing -> returnL Nothing
152 Just arg_tys -> checkFunApp con_ty arg_tys (mkFunAppMsg con_ty arg_tys e)
156 lintStgExpr e@(StgPrim op args _)
157 = mapMaybeL lintStgArg args `thenL` \ maybe_arg_tys ->
158 case maybe_arg_tys of
159 Nothing -> returnL Nothing
160 Just arg_tys -> checkFunApp op_ty arg_tys (mkFunAppMsg op_ty arg_tys e)
162 op_ty = primOpType op
164 lintStgExpr (StgLet binds body)
165 = lintStgBinds binds `thenL` \ binders ->
166 addLoc (BodyOfLetRec binders) (
167 addInScopeVars binders (
171 lintStgExpr (StgLetNoEscape _ _ binds body)
172 = lintStgBinds binds `thenL` \ binders ->
173 addLoc (BodyOfLetRec binders) (
174 addInScopeVars binders (
178 lintStgExpr (StgSCC _ _ expr) = lintStgExpr expr
180 lintStgExpr e@(StgCase scrut _ _ _ alts)
181 = lintStgExpr scrut `thenMaybeL` \ _ ->
183 -- Check that it is a data type
184 case (maybeAppDataTyConExpandingDicts scrut_ty) of
185 Just (tycon, _, _) | isDataTyCon tycon
186 -> lintStgAlts alts scrut_ty tycon
187 other -> addErrL (mkCaseDataConMsg e) `thenL_`
190 scrut_ty = get_ty alts
192 get_ty (StgAlgAlts ty _ _) = ty
193 get_ty (StgPrimAlts ty _ _) = ty
197 lintStgAlts :: StgCaseAlts
198 -> Type -- Type of scrutinee
199 -> TyCon -- TyCon pinned on the case
200 -> LintM (Maybe Type) -- Type of alternatives
202 lintStgAlts alts scrut_ty case_tycon
204 StgAlgAlts _ alg_alts deflt ->
205 mapL (lintAlgAlt scrut_ty) alg_alts `thenL` \ maybe_alt_tys ->
206 lintDeflt deflt scrut_ty `thenL` \ maybe_deflt_ty ->
207 returnL (maybe_deflt_ty : maybe_alt_tys)
209 StgPrimAlts _ prim_alts deflt ->
210 mapL (lintPrimAlt scrut_ty) prim_alts `thenL` \ maybe_alt_tys ->
211 lintDeflt deflt scrut_ty `thenL` \ maybe_deflt_ty ->
212 returnL (maybe_deflt_ty : maybe_alt_tys)
213 ) `thenL` \ maybe_result_tys ->
214 -- Check the result types
215 case catMaybes (maybe_result_tys) of
216 [] -> returnL Nothing
218 (first_ty:tys) -> mapL check tys `thenL_`
219 returnL (Just first_ty)
221 check ty = checkTys first_ty ty (mkCaseAltMsg alts)
223 lintAlgAlt scrut_ty (con, args, _, rhs)
224 = (case maybeAppDataTyConExpandingDicts scrut_ty of
226 addErrL (mkAlgAltMsg1 scrut_ty)
227 Just (tycon, tys_applied, cons) ->
229 arg_tys = dataConArgTys con tys_applied
231 checkL (con `elem` cons) (mkAlgAltMsg2 scrut_ty con) `thenL_`
232 checkL (length arg_tys == length args) (mkAlgAltMsg3 con args)
234 mapL check (zipEqual "lintAlgAlt:stg" arg_tys args) `thenL_`
237 addInScopeVars args (
241 check (ty, arg) = checkTys ty (idType arg) (mkAlgAltMsg4 ty arg)
243 -- elem: yes, the elem-list here can sometimes be long-ish,
244 -- but as it's use-once, probably not worth doing anything different
245 -- We give it its own copy, so it isn't overloaded.
247 elem x (y:ys) = x==y || elem x ys
249 lintPrimAlt scrut_ty alt@(lit,rhs)
250 = checkTys (literalType lit) scrut_ty (mkPrimAltMsg alt) `thenL_`
253 lintDeflt StgNoDefault scrut_ty = returnL Nothing
254 lintDeflt deflt@(StgBindDefault binder _ rhs) scrut_ty
255 = checkTys (idType binder) scrut_ty (mkDefltMsg deflt) `thenL_`
256 addInScopeVars [binder] (
262 %************************************************************************
264 \subsection[lint-monad]{The Lint monad}
266 %************************************************************************
269 type LintM a = [LintLocInfo] -- Locations
270 -> IdSet -- Local vars in scope
271 -> Bag ErrMsg -- Error messages so far
272 -> (a, Bag ErrMsg) -- Result and error messages (if any)
274 type ErrMsg = PprStyle -> Doc
277 = RhsOf Id -- The variable bound
278 | LambdaBodyOf [Id] -- The lambda-binder
279 | BodyOfLetRec [Id] -- One of the binders
281 instance Outputable LintLocInfo where
283 = hcat [ppr sty (getSrcLoc v), ptext SLIT(": [RHS of "), pp_binders sty [v], char ']']
285 ppr sty (LambdaBodyOf bs)
286 = hcat [ppr sty (getSrcLoc (head bs)),
287 ptext SLIT(": [in body of lambda with binders "), pp_binders sty bs, char ']']
289 ppr sty (BodyOfLetRec bs)
290 = hcat [ppr sty (getSrcLoc (head bs)),
291 ptext SLIT(": [in body of letrec with binders "), pp_binders sty bs, char ']']
293 pp_binders :: PprStyle -> [Id] -> Doc
295 = sep (punctuate comma (map pp_binder bs))
298 = hsep [ppr sty b, ptext SLIT("::"), ppr sty (idType b)]
302 initL :: LintM a -> Maybe ErrMsg
304 = case (m [] emptyIdSet emptyBag) of { (_, errs) ->
305 if isEmptyBag errs then
309 foldBag ($$) ( \ msg -> msg sty ) empty errs
313 returnL :: a -> LintM a
314 returnL r loc scope errs = (r, errs)
316 thenL :: LintM a -> (a -> LintM b) -> LintM b
317 thenL m k loc scope errs
318 = case m loc scope errs of
319 (r, errs') -> k r loc scope errs'
321 thenL_ :: LintM a -> LintM b -> LintM b
322 thenL_ m k loc scope errs
323 = case m loc scope errs of
324 (_, errs') -> k loc scope errs'
326 thenMaybeL :: LintM (Maybe a) -> (a -> LintM (Maybe b)) -> LintM (Maybe b)
327 thenMaybeL m k loc scope errs
328 = case m loc scope errs of
329 (Nothing, errs2) -> (Nothing, errs2)
330 (Just r, errs2) -> k r loc scope errs2
332 thenMaybeL_ :: LintM (Maybe a) -> LintM (Maybe b) -> LintM (Maybe b)
333 thenMaybeL_ m k loc scope errs
334 = case m loc scope errs of
335 (Nothing, errs2) -> (Nothing, errs2)
336 (Just _, errs2) -> k loc scope errs2
338 mapL :: (a -> LintM b) -> [a] -> LintM [b]
339 mapL f [] = returnL []
342 mapL f xs `thenL` \ rs ->
345 mapMaybeL :: (a -> LintM (Maybe b)) -> [a] -> LintM (Maybe [b])
346 -- Returns Nothing if anything fails
347 mapMaybeL f [] = returnL (Just [])
349 = f x `thenMaybeL` \ r ->
350 mapMaybeL f xs `thenMaybeL` \ rs ->
351 returnL (Just (r:rs))
355 checkL :: Bool -> ErrMsg -> LintM ()
356 checkL True msg loc scope errs = ((), errs)
357 checkL False msg loc scope errs = ((), addErr errs msg loc)
359 addErrL :: ErrMsg -> LintM ()
360 addErrL msg loc scope errs = ((), addErr errs msg loc)
362 addErr :: Bag ErrMsg -> ErrMsg -> [LintLocInfo] -> Bag ErrMsg
364 addErr errs_so_far msg locs
365 = errs_so_far `snocBag` ( \ sty ->
366 hang (ppr sty (head locs)) 4 (msg sty)
369 addLoc :: LintLocInfo -> LintM a -> LintM a
370 addLoc extra_loc m loc scope errs
371 = m (extra_loc:loc) scope errs
373 addInScopeVars :: [Id] -> LintM a -> LintM a
374 addInScopeVars ids m loc scope errs
375 = -- We check if these "new" ids are already
376 -- in scope, i.e., we have *shadowing* going on.
377 -- For now, it's just a "trace"; we may make
378 -- a real error out of it...
380 new_set = mkIdSet ids
382 shadowed = scope `intersectIdSets` new_set
384 -- After adding -fliberate-case, Simon decided he likes shadowed
385 -- names after all. WDP 94/07
386 -- (if isEmptyIdSet shadowed
388 -- else pprTrace "Shadowed vars:" (ppr PprDebug (idSetToList shadowed))) $
389 m loc (scope `unionIdSets` new_set) errs
393 checkFunApp :: Type -- The function type
394 -> [Type] -- The arg type(s)
395 -> ErrMsg -- Error messgae
396 -> LintM (Maybe Type) -- The result type
398 checkFunApp fun_ty arg_tys msg loc scope errs
399 = cfa res_ty expected_arg_tys arg_tys
401 (expected_arg_tys, res_ty) = splitFunTyExpandingDicts fun_ty
403 cfa res_ty expected [] -- Args have run out; that's fine
404 = (Just (mkFunTys expected res_ty), errs)
406 cfa res_ty [] arg_tys -- Expected arg tys ran out first;
407 -- first see if res_ty is a tyvar template;
408 -- otherwise, maybe res_ty is a
409 -- dictionary type which is actually a function?
411 = (Just res_ty, errs)
413 = case splitFunTy (unDictifyTy res_ty) of
414 ([], _) -> (Nothing, addErr errs msg loc) -- Too many args
415 (new_expected, new_res) -> cfa new_res new_expected arg_tys
417 cfa res_ty (expected_arg_ty:expected_arg_tys) (arg_ty:arg_tys)
418 = if (sleazy_eq_ty expected_arg_ty arg_ty)
419 then cfa res_ty expected_arg_tys arg_tys
420 else (Nothing, addErr errs msg loc) -- Arg mis-match
424 checkInScope :: Id -> LintM ()
425 checkInScope id loc scope errs
426 = if isLocallyDefined id && not (isAlgCon id) && not (id `elementOfIdSet` scope) then
427 ((), addErr errs (\ sty -> hsep [ppr sty id, ptext SLIT("is out of scope")]) loc)
431 checkTys :: Type -> Type -> ErrMsg -> LintM ()
432 checkTys ty1 ty2 msg loc scope errs
433 = if (sleazy_eq_ty ty1 ty2)
435 else ((), addErr errs msg loc)
439 mkCaseAltMsg :: StgCaseAlts -> ErrMsg
440 mkCaseAltMsg alts sty
441 = ($$) (text "In some case alternatives, type of alternatives not all same:")
442 -- LATER: (ppr sty alts)
443 (panic "mkCaseAltMsg")
445 mkCaseDataConMsg :: StgExpr -> ErrMsg
446 mkCaseDataConMsg expr sty
447 = ($$) (ptext SLIT("A case scrutinee not a type-constructor type:"))
450 mkCaseAbstractMsg :: TyCon -> ErrMsg
451 mkCaseAbstractMsg tycon sty
452 = ($$) (ptext SLIT("An algebraic case on an abstract type:"))
455 mkDefltMsg :: StgCaseDefault -> ErrMsg
457 = ($$) (ptext SLIT("Binder in default case of a case expression doesn't match type of scrutinee:"))
458 --LATER: (ppr sty deflt)
461 mkFunAppMsg :: Type -> [Type] -> StgExpr -> ErrMsg
462 mkFunAppMsg fun_ty arg_tys expr sty
463 = vcat [text "In a function application, function type doesn't match arg types:",
464 hang (ptext SLIT("Function type:")) 4 (ppr sty fun_ty),
465 hang (ptext SLIT("Arg types:")) 4 (vcat (map (ppr sty) arg_tys)),
466 hang (ptext SLIT("Expression:")) 4 (pp_expr sty expr)]
468 mkRhsConMsg :: Type -> [Type] -> ErrMsg
469 mkRhsConMsg fun_ty arg_tys sty
470 = vcat [text "In a RHS constructor application, con type doesn't match arg types:",
471 hang (ptext SLIT("Constructor type:")) 4 (ppr sty fun_ty),
472 hang (ptext SLIT("Arg types:")) 4 (vcat (map (ppr sty) arg_tys))]
474 mkUnappTyMsg :: Id -> Type -> ErrMsg
475 mkUnappTyMsg var ty sty
476 = vcat [text "Variable has a for-all type, but isn't applied to any types.",
477 (<>) (ptext SLIT("Var: ")) (ppr sty var),
478 (<>) (ptext SLIT("Its type: ")) (ppr sty ty)]
480 mkAlgAltMsg1 :: Type -> ErrMsg
482 = ($$) (text "In some case statement, type of scrutinee is not a data type:")
485 mkAlgAltMsg2 :: Type -> Id -> ErrMsg
486 mkAlgAltMsg2 ty con sty
488 text "In some algebraic case alternative, constructor is not a constructor of scrutinee type:",
493 mkAlgAltMsg3 :: Id -> [Id] -> ErrMsg
494 mkAlgAltMsg3 con alts sty
496 text "In some algebraic case alternative, number of arguments doesn't match constructor:",
501 mkAlgAltMsg4 :: Type -> Id -> ErrMsg
502 mkAlgAltMsg4 ty arg sty
504 text "In some algebraic case alternative, type of argument doesn't match data constructor:",
509 mkPrimAltMsg :: (Literal, StgExpr) -> ErrMsg
511 = ($$) (text "In a primitive case alternative, type of literal doesn't match type of scrutinee:")
514 mkRhsMsg :: Id -> Type -> ErrMsg
515 mkRhsMsg binder ty sty
516 = vcat [hsep [ptext SLIT("The type of this binder doesn't match the type of its RHS:"),
518 hsep [ptext SLIT("Binder's type:"), ppr sty (idType binder)],
519 hsep [ptext SLIT("Rhs type:"), ppr sty ty]
522 pp_expr :: PprStyle -> StgExpr -> Doc
523 pp_expr sty expr = ppr sty expr
526 -- NB: probably severe overkill (WDP 95/04)
527 = trace "StgLint.sleazy_eq_ty:use eqSimplTy?" $
528 case (splitFunTyExpandingDicts ty1) of { (tyargs1,tyres1) ->
529 case (splitFunTyExpandingDicts ty2) of { (tyargs2,tyres2) ->
531 ty11 = mkFunTys tyargs1 tyres1
532 ty22 = mkFunTys tyargs2 tyres2