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