02d989cec089f36751b5df61cb54f999c6a3d4d7
[ghc.git] / compiler / stgSyn / StgLint.hs
1 {-
2 (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
3
4 \section[StgLint]{A ``lint'' pass to check for Stg correctness}
5 -}
6
7 {-# LANGUAGE CPP #-}
8
9 module StgLint ( lintStgTopBindings ) where
10
11 import StgSyn
12
13 import Bag ( Bag, emptyBag, isEmptyBag, snocBag, bagToList )
14 import Id ( Id, idType, isLocalId )
15 import VarSet
16 import DataCon
17 import CoreSyn ( AltCon(..) )
18 import PrimOp ( primOpType )
19 import Literal ( literalType )
20 import Maybes
21 import Name ( getSrcLoc )
22 import ErrUtils ( MsgDoc, Severity(..), mkLocMessage )
23 import Type
24 import RepType
25 import TyCon
26 import Util
27 import SrcLoc
28 import Outputable
29 import Control.Monad
30 import Data.Function
31
32 #include "HsVersions.h"
33
34 {-
35 Checks for
36 (a) *some* type errors
37 (b) locally-defined variables used but not defined
38
39
40 Note: unless -dverbose-stg is on, display of lint errors will result
41 in "panic: bOGUS_LVs".
42
43 WARNING:
44 ~~~~~~~~
45
46 This module has suffered bit-rot; it is likely to yield lint errors
47 for Stg code that is currently perfectly acceptable for code
48 generation. Solution: don't use it! (KSW 2000-05).
49
50
51 ************************************************************************
52 * *
53 \subsection{``lint'' for various constructs}
54 * *
55 ************************************************************************
56
57 @lintStgTopBindings@ is the top-level interface function.
58 -}
59
60 lintStgTopBindings :: String -> [StgTopBinding] -> [StgTopBinding]
61
62 lintStgTopBindings whodunnit binds
63 = {-# SCC "StgLint" #-}
64 case (initL (lint_binds binds)) of
65 Nothing -> binds
66 Just msg -> pprPanic "" (vcat [
67 text "*** Stg Lint ErrMsgs: in" <+>
68 text whodunnit <+> text "***",
69 msg,
70 text "*** Offending Program ***",
71 pprStgTopBindings binds,
72 text "*** End of Offense ***"])
73 where
74 lint_binds :: [StgTopBinding] -> LintM ()
75
76 lint_binds [] = return ()
77 lint_binds (bind:binds) = do
78 binders <- lint_bind bind
79 addInScopeVars binders $
80 lint_binds binds
81
82 lint_bind (StgTopLifted bind) = lintStgBinds bind
83 lint_bind (StgTopStringLit v _) = return [v]
84
85 lintStgArg :: StgArg -> LintM (Maybe Type)
86 lintStgArg (StgLitArg lit) = return (Just (literalType lit))
87 lintStgArg (StgVarArg v) = lintStgVar v
88
89 lintStgVar :: Id -> LintM (Maybe Kind)
90 lintStgVar v = do checkInScope v
91 return (Just (idType v))
92
93 lintStgBinds :: StgBinding -> LintM [Id] -- Returns the binders
94 lintStgBinds (StgNonRec binder rhs) = do
95 lint_binds_help (binder,rhs)
96 return [binder]
97
98 lintStgBinds (StgRec pairs)
99 = addInScopeVars binders $ do
100 mapM_ lint_binds_help pairs
101 return binders
102 where
103 binders = [b | (b,_) <- pairs]
104
105 lint_binds_help :: (Id, StgRhs) -> LintM ()
106 lint_binds_help (binder, rhs)
107 = addLoc (RhsOf binder) $ do
108 -- Check the rhs
109 _maybe_rhs_ty <- lintStgRhs rhs
110
111 -- Check binder doesn't have unlifted type
112 checkL (not (isUnliftedType binder_ty))
113 (mkUnliftedTyMsg binder rhs)
114
115 -- Check match to RHS type
116 -- Actually we *can't* check the RHS type, because
117 -- unsafeCoerce means it really might not match at all
118 -- notably; eg x::Int = (error @Bool "urk") |> unsafeCoerce...
119 -- case maybe_rhs_ty of
120 -- Nothing -> return ()
121 -- Just rhs_ty -> checkTys binder_ty
122 -- rhs_ty
123 --- (mkRhsMsg binder rhs_ty)
124
125 return ()
126 where
127 binder_ty = idType binder
128
129 lintStgRhs :: StgRhs -> LintM (Maybe Type) -- Just ty => type is exact
130
131 lintStgRhs (StgRhsClosure _ _ _ _ [] expr)
132 = lintStgExpr expr
133
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)
139
140 lintStgRhs rhs@(StgRhsCon _ con args) = do
141 -- TODO: Check arg_tys
142 when (isUnboxedTupleCon con || isUnboxedSumCon con) $
143 addErrL (text "StgRhsCon is an unboxed tuple or sum application" $$
144 ppr rhs)
145 runMaybeT $ do
146 arg_tys <- mapM (MaybeT . lintStgArg) args
147 MaybeT $ checkFunApp con_ty arg_tys (mkRhsConMsg con_ty arg_tys)
148 where
149 con_ty = dataConRepType con
150
151 lintStgExpr :: StgExpr -> LintM (Maybe Type) -- Just ty => type is exact
152
153 lintStgExpr (StgLit l) = return (Just (literalType l))
154
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)
159
160 lintStgExpr e@(StgConApp con args _arg_tys) = runMaybeT $ do
161 -- TODO: Check arg_tys
162 arg_tys <- mapM (MaybeT . lintStgArg) args
163 MaybeT $ checkFunApp con_ty arg_tys (mkFunAppMsg con_ty arg_tys e)
164 where
165 con_ty = dataConRepType con
166
167 lintStgExpr e@(StgOpApp (StgPrimOp op) args _) = runMaybeT $ do
168 arg_tys <- mapM (MaybeT . lintStgArg) args
169 MaybeT $ checkFunApp op_ty arg_tys (mkFunAppMsg op_ty arg_tys e)
170 where
171 op_ty = primOpType op
172
173 lintStgExpr (StgOpApp _ args res_ty) = runMaybeT $ do
174 -- We don't have enough type information to check
175 -- the application for StgFCallOp and StgPrimCallOp; ToDo
176 _maybe_arg_tys <- mapM (MaybeT . lintStgArg) args
177 return res_ty
178
179 lintStgExpr (StgLam bndrs _) = do
180 addErrL (text "Unexpected StgLam" <+> ppr bndrs)
181 return Nothing
182
183 lintStgExpr (StgLet binds body) = do
184 binders <- lintStgBinds binds
185 addLoc (BodyOfLetRec binders) $
186 addInScopeVars binders $
187 lintStgExpr body
188
189 lintStgExpr (StgLetNoEscape binds body) = do
190 binders <- lintStgBinds binds
191 addLoc (BodyOfLetRec binders) $
192 addInScopeVars binders $
193 lintStgExpr body
194
195 lintStgExpr (StgTick _ expr) = lintStgExpr expr
196
197 lintStgExpr (StgCase scrut bndr alts_type alts) = runMaybeT $ do
198 _ <- MaybeT $ lintStgExpr scrut
199
200 in_scope <- MaybeT $ liftM Just $
201 case alts_type of
202 AlgAlt tc -> check_bndr (tyConPrimRep tc) >> return True
203 PrimAlt rep -> check_bndr [rep] >> return True
204 MultiValAlt _ -> return False -- Binder is always dead in this case
205 PolyAlt -> return True
206
207 MaybeT $ addInScopeVars [bndr | in_scope] $
208 lintStgAlts alts scrut_ty
209 where
210 scrut_ty = idType bndr
211 scrut_reps = typePrimRep scrut_ty
212 check_bndr reps = checkL (scrut_reps == reps) bad_bndr
213 where
214 bad_bndr = mkDefltMsg bndr reps
215
216 lintStgAlts :: [StgAlt]
217 -> Type -- Type of scrutinee
218 -> LintM (Maybe Type) -- Just ty => type is accurage
219
220 lintStgAlts alts scrut_ty = do
221 maybe_result_tys <- mapM (lintAlt scrut_ty) alts
222
223 -- Check the result types
224 case catMaybes (maybe_result_tys) of
225 [] -> return Nothing
226
227 (first_ty:_tys) -> do -- mapM_ check tys
228 return (Just first_ty)
229 where
230 -- check ty = checkTys first_ty ty (mkCaseAltMsg alts)
231 -- We can't check that the alternatives have the
232 -- same type, because they don't, with unsafeCoerce#
233
234 lintAlt :: Type -> (AltCon, [Id], StgExpr) -> LintM (Maybe Type)
235 lintAlt _ (DEFAULT, _, rhs)
236 = lintStgExpr rhs
237
238 lintAlt scrut_ty (LitAlt lit, _, rhs) = do
239 checkTys (literalType lit) scrut_ty (mkAltMsg1 scrut_ty)
240 lintStgExpr rhs
241
242 lintAlt scrut_ty (DataAlt con, args, rhs) = do
243 case splitTyConApp_maybe scrut_ty of
244 Just (tycon, tys_applied) | isAlgTyCon tycon &&
245 not (isNewTyCon tycon) -> do
246 let
247 cons = tyConDataCons tycon
248 arg_tys = dataConInstArgTys con tys_applied
249 -- This does not work for existential constructors
250
251 checkL (con `elem` cons) (mkAlgAltMsg2 scrut_ty con)
252 checkL (length args == dataConRepArity con) (mkAlgAltMsg3 con args)
253 when (isVanillaDataCon con) $
254 mapM_ check (zipEqual "lintAlgAlt:stg" arg_tys args)
255 return ()
256 _ ->
257 addErrL (mkAltMsg1 scrut_ty)
258
259 addInScopeVars args $
260 lintStgExpr rhs
261 where
262 check (ty, arg) = checkTys ty (idType arg) (mkAlgAltMsg4 ty arg)
263
264 -- elem: yes, the elem-list here can sometimes be long-ish,
265 -- but as it's use-once, probably not worth doing anything different
266 -- We give it its own copy, so it isn't overloaded.
267 elem _ [] = False
268 elem x (y:ys) = x==y || elem x ys
269
270 {-
271 ************************************************************************
272 * *
273 \subsection[lint-monad]{The Lint monad}
274 * *
275 ************************************************************************
276 -}
277
278 newtype LintM a = LintM
279 { unLintM :: [LintLocInfo] -- Locations
280 -> IdSet -- Local vars in scope
281 -> Bag MsgDoc -- Error messages so far
282 -> (a, Bag MsgDoc) -- Result and error messages (if any)
283 }
284
285 data LintLocInfo
286 = RhsOf Id -- The variable bound
287 | LambdaBodyOf [Id] -- The lambda-binder
288 | BodyOfLetRec [Id] -- One of the binders
289
290 dumpLoc :: LintLocInfo -> (SrcSpan, SDoc)
291 dumpLoc (RhsOf v) =
292 (srcLocSpan (getSrcLoc v), text " [RHS of " <> pp_binders [v] <> char ']' )
293 dumpLoc (LambdaBodyOf bs) =
294 (srcLocSpan (getSrcLoc (head bs)), text " [in body of lambda with binders " <> pp_binders bs <> char ']' )
295
296 dumpLoc (BodyOfLetRec bs) =
297 (srcLocSpan (getSrcLoc (head bs)), text " [in body of letrec with binders " <> pp_binders bs <> char ']' )
298
299
300 pp_binders :: [Id] -> SDoc
301 pp_binders bs
302 = sep (punctuate comma (map pp_binder bs))
303 where
304 pp_binder b
305 = hsep [ppr b, dcolon, ppr (idType b)]
306
307 initL :: LintM a -> Maybe MsgDoc
308 initL (LintM m)
309 = case (m [] emptyVarSet emptyBag) of { (_, errs) ->
310 if isEmptyBag errs then
311 Nothing
312 else
313 Just (vcat (punctuate blankLine (bagToList errs)))
314 }
315
316 instance Functor LintM where
317 fmap = liftM
318
319 instance Applicative LintM where
320 pure a = LintM $ \_loc _scope errs -> (a, errs)
321 (<*>) = ap
322 (*>) = thenL_
323
324 instance Monad LintM where
325 (>>=) = thenL
326 (>>) = (*>)
327
328 thenL :: LintM a -> (a -> LintM b) -> LintM b
329 thenL m k = LintM $ \loc scope errs
330 -> case unLintM m loc scope errs of
331 (r, errs') -> unLintM (k r) loc scope errs'
332
333 thenL_ :: LintM a -> LintM b -> LintM b
334 thenL_ m k = LintM $ \loc scope errs
335 -> case unLintM m loc scope errs of
336 (_, errs') -> unLintM k loc scope errs'
337
338 checkL :: Bool -> MsgDoc -> LintM ()
339 checkL True _ = return ()
340 checkL False msg = addErrL msg
341
342 addErrL :: MsgDoc -> LintM ()
343 addErrL msg = LintM $ \loc _scope errs -> ((), addErr errs msg loc)
344
345 addErr :: Bag MsgDoc -> MsgDoc -> [LintLocInfo] -> Bag MsgDoc
346 addErr errs_so_far msg locs
347 = errs_so_far `snocBag` mk_msg locs
348 where
349 mk_msg (loc:_) = let (l,hdr) = dumpLoc loc
350 in mkLocMessage SevWarning l (hdr $$ msg)
351 mk_msg [] = msg
352
353 addLoc :: LintLocInfo -> LintM a -> LintM a
354 addLoc extra_loc m = LintM $ \loc scope errs
355 -> unLintM m (extra_loc:loc) scope errs
356
357 addInScopeVars :: [Id] -> LintM a -> LintM a
358 addInScopeVars ids m = LintM $ \loc scope errs
359 -> let
360 new_set = mkVarSet ids
361 in unLintM m loc (scope `unionVarSet` new_set) errs
362
363 {-
364 Checking function applications: we only check that the type has the
365 right *number* of arrows, we don't actually compare the types. This
366 is because we can't expect the types to be equal - the type
367 applications and type lambdas that we use to calculate accurate types
368 have long since disappeared.
369 -}
370
371 checkFunApp :: Type -- The function type
372 -> [Type] -- The arg type(s)
373 -> MsgDoc -- Error message
374 -> LintM (Maybe Type) -- Just ty => result type is accurate
375
376 checkFunApp fun_ty arg_tys msg
377 = do { case mb_msg of
378 Just msg -> addErrL msg
379 Nothing -> return ()
380 ; return mb_ty }
381 where
382 (mb_ty, mb_msg) = cfa True fun_ty arg_tys
383
384 cfa :: Bool -> Type -> [Type] -> (Maybe Type -- Accurate result?
385 , Maybe MsgDoc) -- Errors?
386
387 cfa accurate fun_ty [] -- Args have run out; that's fine
388 = (if accurate then Just fun_ty else Nothing, Nothing)
389
390 cfa accurate fun_ty arg_tys@(arg_ty':arg_tys')
391 | Just (arg_ty, res_ty) <- splitFunTy_maybe fun_ty
392 = if accurate && not (arg_ty `stgEqType` arg_ty')
393 then (Nothing, Just msg) -- Arg type mismatch
394 else cfa accurate res_ty arg_tys'
395
396 | Just (_, fun_ty') <- splitForAllTy_maybe fun_ty
397 = cfa False fun_ty' arg_tys
398
399 | Just (tc,tc_args) <- splitTyConApp_maybe fun_ty
400 , isNewTyCon tc
401 = if length tc_args < tyConArity tc
402 then WARN( True, text "cfa: unsaturated newtype" <+> ppr fun_ty $$ msg )
403 (Nothing, Nothing) -- This is odd, but I've seen it
404 else cfa False (newTyConInstRhs tc tc_args) arg_tys
405
406 | Just tc <- tyConAppTyCon_maybe fun_ty
407 , not (isTypeFamilyTyCon tc) -- Definite error
408 = (Nothing, Just msg) -- Too many args
409
410 | otherwise
411 = (Nothing, Nothing)
412
413 stgEqType :: Type -> Type -> Bool
414 -- Compare types, but crudely because we have discarded
415 -- both casts and type applications, so types might look
416 -- different but be the same. So reply "True" if in doubt.
417 -- "False" means that the types are definitely different.
418 --
419 -- Fundamentally this is a losing battle because of unsafeCoerce
420
421 stgEqType orig_ty1 orig_ty2
422 = gos (typePrimRep orig_ty1) (typePrimRep orig_ty2)
423 where
424 gos :: [PrimRep] -> [PrimRep] -> Bool
425 gos [_] [_] = go orig_ty1 orig_ty2
426 gos reps1 reps2 = reps1 == reps2
427
428 go :: UnaryType -> UnaryType -> Bool
429 go ty1 ty2
430 | Just (tc1, tc_args1) <- splitTyConApp_maybe ty1
431 , Just (tc2, tc_args2) <- splitTyConApp_maybe ty2
432 , let res = if tc1 == tc2
433 then equalLength tc_args1 tc_args2 && and (zipWith (gos `on` typePrimRep) tc_args1 tc_args2)
434 else -- TyCons don't match; but don't bleat if either is a
435 -- family TyCon because a coercion might have made it
436 -- equal to something else
437 (isFamilyTyCon tc1 || isFamilyTyCon tc2)
438 = if res then True
439 else
440 pprTrace "stgEqType: unequal" (vcat [ppr ty1, ppr ty2])
441 False
442
443 | otherwise = True -- Conservatively say "fine".
444 -- Type variables in particular
445
446 checkInScope :: Id -> LintM ()
447 checkInScope id = LintM $ \loc scope errs
448 -> if isLocalId id && not (id `elemVarSet` scope) then
449 ((), addErr errs (hsep [ppr id, text "is out of scope"]) loc)
450 else
451 ((), errs)
452
453 checkTys :: Type -> Type -> MsgDoc -> LintM ()
454 checkTys ty1 ty2 msg = LintM $ \loc _scope errs
455 -> if (ty1 `stgEqType` ty2)
456 then ((), errs)
457 else ((), addErr errs msg loc)
458
459 _mkCaseAltMsg :: [StgAlt] -> MsgDoc
460 _mkCaseAltMsg _alts
461 = ($$) (text "In some case alternatives, type of alternatives not all same:")
462 (Outputable.empty) -- LATER: ppr alts
463
464 mkDefltMsg :: Id -> [PrimRep] -> MsgDoc
465 mkDefltMsg bndr reps
466 = ($$) (text "Binder of a case expression doesn't match representation of scrutinee:")
467 (ppr bndr $$ ppr (idType bndr) $$ ppr reps)
468
469 mkFunAppMsg :: Type -> [Type] -> StgExpr -> MsgDoc
470 mkFunAppMsg fun_ty arg_tys expr
471 = vcat [text "In a function application, function type doesn't match arg types:",
472 hang (text "Function type:") 4 (ppr fun_ty),
473 hang (text "Arg types:") 4 (vcat (map (ppr) arg_tys)),
474 hang (text "Expression:") 4 (ppr expr)]
475
476 mkRhsConMsg :: Type -> [Type] -> MsgDoc
477 mkRhsConMsg fun_ty arg_tys
478 = vcat [text "In a RHS constructor application, con type doesn't match arg types:",
479 hang (text "Constructor type:") 4 (ppr fun_ty),
480 hang (text "Arg types:") 4 (vcat (map (ppr) arg_tys))]
481
482 mkAltMsg1 :: Type -> MsgDoc
483 mkAltMsg1 ty
484 = ($$) (text "In a case expression, type of scrutinee does not match patterns")
485 (ppr ty)
486
487 mkAlgAltMsg2 :: Type -> DataCon -> MsgDoc
488 mkAlgAltMsg2 ty con
489 = vcat [
490 text "In some algebraic case alternative, constructor is not a constructor of scrutinee type:",
491 ppr ty,
492 ppr con
493 ]
494
495 mkAlgAltMsg3 :: DataCon -> [Id] -> MsgDoc
496 mkAlgAltMsg3 con alts
497 = vcat [
498 text "In some algebraic case alternative, number of arguments doesn't match constructor:",
499 ppr con,
500 ppr alts
501 ]
502
503 mkAlgAltMsg4 :: Type -> Id -> MsgDoc
504 mkAlgAltMsg4 ty arg
505 = vcat [
506 text "In some algebraic case alternative, type of argument doesn't match data constructor:",
507 ppr ty,
508 ppr arg
509 ]
510
511 _mkRhsMsg :: Id -> Type -> MsgDoc
512 _mkRhsMsg binder ty
513 = vcat [hsep [text "The type of this binder doesn't match the type of its RHS:",
514 ppr binder],
515 hsep [text "Binder's type:", ppr (idType binder)],
516 hsep [text "Rhs type:", ppr ty]
517 ]
518
519 mkUnliftedTyMsg :: Id -> StgRhs -> SDoc
520 mkUnliftedTyMsg binder rhs
521 = (text "Let(rec) binder" <+> quotes (ppr binder) <+>
522 text "has unlifted type" <+> quotes (ppr (idType binder)))
523 $$
524 (text "RHS:" <+> ppr rhs)