Implement unboxed sum primitive type
[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 ( lintStgBindings ) 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 @lintStgBindings@ is the top-level interface function.
58 -}
59
60 lintStgBindings :: String -> [StgBinding] -> [StgBinding]
61
62 lintStgBindings 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 pprStgBindings binds,
72 text "*** End of Offense ***"])
73 where
74 lint_binds :: [StgBinding] -> LintM ()
75
76 lint_binds [] = return ()
77 lint_binds (bind:binds) = do
78 binders <- lintStgBinds bind
79 addInScopeVars binders $
80 lint_binds binds
81
82 lintStgArg :: StgArg -> LintM (Maybe Type)
83 lintStgArg (StgLitArg lit) = return (Just (literalType lit))
84 lintStgArg (StgVarArg v) = lintStgVar v
85 lintStgArg (StgRubbishArg ty) = return (Just ty)
86
87 lintStgVar :: Id -> LintM (Maybe Kind)
88 lintStgVar v = do checkInScope v
89 return (Just (idType v))
90
91 lintStgBinds :: StgBinding -> LintM [Id] -- Returns the binders
92 lintStgBinds (StgNonRec binder rhs) = do
93 lint_binds_help (binder,rhs)
94 return [binder]
95
96 lintStgBinds (StgRec pairs)
97 = addInScopeVars binders $ do
98 mapM_ lint_binds_help pairs
99 return binders
100 where
101 binders = [b | (b,_) <- pairs]
102
103 lint_binds_help :: (Id, StgRhs) -> LintM ()
104 lint_binds_help (binder, rhs)
105 = addLoc (RhsOf binder) $ do
106 -- Check the rhs
107 _maybe_rhs_ty <- lintStgRhs rhs
108
109 -- Check binder doesn't have unlifted type
110 checkL (not (isUnliftedType binder_ty))
111 (mkUnliftedTyMsg binder rhs)
112
113 -- Check match to RHS type
114 -- Actually we *can't* check the RHS type, because
115 -- unsafeCoerce means it really might not match at all
116 -- notably; eg x::Int = (error @Bool "urk") |> unsafeCoerce...
117 -- case maybe_rhs_ty of
118 -- Nothing -> return ()
119 -- Just rhs_ty -> checkTys binder_ty
120 -- rhs_ty
121 --- (mkRhsMsg binder rhs_ty)
122
123 return ()
124 where
125 binder_ty = idType binder
126
127 lintStgRhs :: StgRhs -> LintM (Maybe Type) -- Just ty => type is exact
128
129 lintStgRhs (StgRhsClosure _ _ _ _ [] expr)
130 = lintStgExpr expr
131
132 lintStgRhs (StgRhsClosure _ _ _ _ binders expr)
133 = addLoc (LambdaBodyOf binders) $
134 addInScopeVars binders $ runMaybeT $ do
135 body_ty <- MaybeT $ lintStgExpr expr
136 return (mkFunTys (map idType binders) body_ty)
137
138 lintStgRhs rhs@(StgRhsCon _ con args) = do
139 -- TODO: Check arg_tys
140 when (isUnboxedTupleCon con || isUnboxedSumCon con) $
141 addErrL (text "StgRhsCon is an unboxed tuple or sum application" $$
142 ppr rhs)
143 runMaybeT $ do
144 arg_tys <- mapM (MaybeT . lintStgArg) args
145 MaybeT $ checkFunApp con_ty arg_tys (mkRhsConMsg con_ty arg_tys)
146 where
147 con_ty = dataConRepType con
148
149 lintStgExpr :: StgExpr -> LintM (Maybe Type) -- Just ty => type is exact
150
151 lintStgExpr (StgLit l) = return (Just (literalType l))
152
153 lintStgExpr e@(StgApp fun args) = runMaybeT $ do
154 fun_ty <- MaybeT $ lintStgVar fun
155 arg_tys <- mapM (MaybeT . lintStgArg) args
156 MaybeT $ checkFunApp fun_ty arg_tys (mkFunAppMsg fun_ty arg_tys e)
157
158 lintStgExpr e@(StgConApp con args _arg_tys) = runMaybeT $ do
159 -- TODO: Check arg_tys
160 arg_tys <- mapM (MaybeT . lintStgArg) args
161 MaybeT $ checkFunApp con_ty arg_tys (mkFunAppMsg con_ty arg_tys e)
162 where
163 con_ty = dataConRepType con
164
165 lintStgExpr e@(StgOpApp (StgPrimOp op) args _) = runMaybeT $ do
166 arg_tys <- mapM (MaybeT . lintStgArg) args
167 MaybeT $ checkFunApp op_ty arg_tys (mkFunAppMsg op_ty arg_tys e)
168 where
169 op_ty = primOpType op
170
171 lintStgExpr (StgOpApp _ args res_ty) = runMaybeT $ do
172 -- We don't have enough type information to check
173 -- the application for StgFCallOp and StgPrimCallOp; ToDo
174 _maybe_arg_tys <- mapM (MaybeT . lintStgArg) args
175 return res_ty
176
177 lintStgExpr (StgLam bndrs _) = do
178 addErrL (text "Unexpected StgLam" <+> ppr bndrs)
179 return Nothing
180
181 lintStgExpr (StgLet binds body) = do
182 binders <- lintStgBinds binds
183 addLoc (BodyOfLetRec binders) $
184 addInScopeVars binders $
185 lintStgExpr body
186
187 lintStgExpr (StgLetNoEscape binds body) = do
188 binders <- lintStgBinds binds
189 addLoc (BodyOfLetRec binders) $
190 addInScopeVars binders $
191 lintStgExpr body
192
193 lintStgExpr (StgTick _ expr) = lintStgExpr expr
194
195 lintStgExpr (StgCase scrut bndr alts_type alts) = runMaybeT $ do
196 _ <- MaybeT $ lintStgExpr scrut
197
198 in_scope <- MaybeT $ liftM Just $
199 case alts_type of
200 AlgAlt tc -> check_bndr tc >> return True
201 PrimAlt tc -> check_bndr tc >> return True
202 MultiValAlt _ -> return False -- Binder is always dead in this case
203 PolyAlt -> return True
204
205 MaybeT $ addInScopeVars [bndr | in_scope] $
206 lintStgAlts alts scrut_ty
207 where
208 scrut_ty = idType bndr
209 UnaryRep scrut_rep = repType scrut_ty -- Not used if scrutinee is unboxed tuple or sum
210 check_bndr tc = case tyConAppTyCon_maybe scrut_rep of
211 Just bndr_tc -> checkL (tc == bndr_tc) bad_bndr
212 Nothing -> addErrL bad_bndr
213 where
214 bad_bndr = mkDefltMsg bndr tc
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 (repType orig_ty1) (repType orig_ty2)
423 where
424 gos :: RepType -> RepType -> Bool
425 gos (MultiRep slots1) (MultiRep slots2)
426 = slots1 == slots2
427 gos (UnaryRep ty1) (UnaryRep ty2) = go ty1 ty2
428 gos _ _ = False
429
430 go :: UnaryType -> UnaryType -> Bool
431 go ty1 ty2
432 | Just (tc1, tc_args1) <- splitTyConApp_maybe ty1
433 , Just (tc2, tc_args2) <- splitTyConApp_maybe ty2
434 , let res = if tc1 == tc2
435 then equalLength tc_args1 tc_args2 && and (zipWith (gos `on` repType) tc_args1 tc_args2)
436 else -- TyCons don't match; but don't bleat if either is a
437 -- family TyCon because a coercion might have made it
438 -- equal to something else
439 (isFamilyTyCon tc1 || isFamilyTyCon tc2)
440 = if res then True
441 else
442 pprTrace "stgEqType: unequal" (vcat [ppr ty1, ppr ty2])
443 False
444
445 | otherwise = True -- Conservatively say "fine".
446 -- Type variables in particular
447
448 checkInScope :: Id -> LintM ()
449 checkInScope id = LintM $ \loc scope errs
450 -> if isLocalId id && not (id `elemVarSet` scope) then
451 ((), addErr errs (hsep [ppr id, text "is out of scope"]) loc)
452 else
453 ((), errs)
454
455 checkTys :: Type -> Type -> MsgDoc -> LintM ()
456 checkTys ty1 ty2 msg = LintM $ \loc _scope errs
457 -> if (ty1 `stgEqType` ty2)
458 then ((), errs)
459 else ((), addErr errs msg loc)
460
461 _mkCaseAltMsg :: [StgAlt] -> MsgDoc
462 _mkCaseAltMsg _alts
463 = ($$) (text "In some case alternatives, type of alternatives not all same:")
464 (Outputable.empty) -- LATER: ppr alts
465
466 mkDefltMsg :: Id -> TyCon -> MsgDoc
467 mkDefltMsg bndr tc
468 = ($$) (text "Binder of a case expression doesn't match type of scrutinee:")
469 (ppr bndr $$ ppr (idType bndr) $$ ppr tc)
470
471 mkFunAppMsg :: Type -> [Type] -> StgExpr -> MsgDoc
472 mkFunAppMsg fun_ty arg_tys expr
473 = vcat [text "In a function application, function type doesn't match arg types:",
474 hang (text "Function type:") 4 (ppr fun_ty),
475 hang (text "Arg types:") 4 (vcat (map (ppr) arg_tys)),
476 hang (text "Expression:") 4 (ppr expr)]
477
478 mkRhsConMsg :: Type -> [Type] -> MsgDoc
479 mkRhsConMsg fun_ty arg_tys
480 = vcat [text "In a RHS constructor application, con type doesn't match arg types:",
481 hang (text "Constructor type:") 4 (ppr fun_ty),
482 hang (text "Arg types:") 4 (vcat (map (ppr) arg_tys))]
483
484 mkAltMsg1 :: Type -> MsgDoc
485 mkAltMsg1 ty
486 = ($$) (text "In a case expression, type of scrutinee does not match patterns")
487 (ppr ty)
488
489 mkAlgAltMsg2 :: Type -> DataCon -> MsgDoc
490 mkAlgAltMsg2 ty con
491 = vcat [
492 text "In some algebraic case alternative, constructor is not a constructor of scrutinee type:",
493 ppr ty,
494 ppr con
495 ]
496
497 mkAlgAltMsg3 :: DataCon -> [Id] -> MsgDoc
498 mkAlgAltMsg3 con alts
499 = vcat [
500 text "In some algebraic case alternative, number of arguments doesn't match constructor:",
501 ppr con,
502 ppr alts
503 ]
504
505 mkAlgAltMsg4 :: Type -> Id -> MsgDoc
506 mkAlgAltMsg4 ty arg
507 = vcat [
508 text "In some algebraic case alternative, type of argument doesn't match data constructor:",
509 ppr ty,
510 ppr arg
511 ]
512
513 _mkRhsMsg :: Id -> Type -> MsgDoc
514 _mkRhsMsg binder ty
515 = vcat [hsep [text "The type of this binder doesn't match the type of its RHS:",
516 ppr binder],
517 hsep [text "Binder's type:", ppr (idType binder)],
518 hsep [text "Rhs type:", ppr ty]
519 ]
520
521 mkUnliftedTyMsg :: Id -> StgRhs -> SDoc
522 mkUnliftedTyMsg binder rhs
523 = (text "Let(rec) binder" <+> quotes (ppr binder) <+>
524 text "has unlifted type" <+> quotes (ppr (idType binder)))
525 $$
526 (text "RHS:" <+> ppr rhs)