RnPat.lhs 21.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
%
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
%
\section[RnPat]{Renaming of patterns}

Basically dependency analysis.

Handles @Match@, @GRHSs@, @HsExpr@, and @Qualifier@ datatypes.  In
general, all of these functions return a renamed thing, and a set of
free variables.

\begin{code}
module RnPat (-- main entry points
14
              rnPat, rnPats, rnBindPat,
15 16 17 18 19

              NameMaker, applyNameMaker,     -- a utility for making names:
              localRecNameMaker, topRecNameMaker,  --   sometimes we want to make local names,
                                             --   sometimes we want to make top (qualified) names.

20
              rnHsRecFields1, HsRecFieldContext(..),
21 22 23 24 25 26 27 28 29 30

	      -- Literals
	      rnLit, rnOverLit,     

             -- Pattern Error messages that are also used elsewhere
             checkTupSize, patSigErr
             ) where

-- ENH: thin imports to only what is necessary for patterns

Ian Lynagh's avatar
Ian Lynagh committed
31
import {-# SOURCE #-} RnExpr ( rnLExpr )
32
#ifdef GHCI
Ian Lynagh's avatar
Ian Lynagh committed
33
import {-# SOURCE #-} TcSplice ( runQuasiQuotePat )
34
#endif 	/* GHCI */
35 36 37 38 39

#include "HsVersions.h"

import HsSyn            
import TcRnMonad
40
import TcHsSyn		( hsOverLitName )
41
import RnEnv
Ian Lynagh's avatar
Ian Lynagh committed
42
import RnTypes
43
import DynFlags
Ian Lynagh's avatar
Ian Lynagh committed
44
import PrelNames
45
import Constants	( mAX_TUPLE_SIZE )
Ian Lynagh's avatar
Ian Lynagh committed
46
import Name
47
import NameSet
Ian Lynagh's avatar
Ian Lynagh committed
48
import RdrName
49
import BasicTypes
50 51
import ListSetOps	( removeDups, minusList )
import Outputable
Ian Lynagh's avatar
Ian Lynagh committed
52
import SrcLoc
53
import FastString
54
import Literal		( inCharRange )
55
import Control.Monad	( when )
56 57 58
\end{code}


Thomas Schilling's avatar
Thomas Schilling committed
59 60
%*********************************************************
%*							*
61
	The CpsRn Monad
Thomas Schilling's avatar
Thomas Schilling committed
62 63
%*							*
%*********************************************************
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
Note [CpsRn monad]
~~~~~~~~~~~~~~~~~~
The CpsRn monad uses continuation-passing style to support this
style of programming:

	do { ...
           ; ns <- bindNames rs
           ; ...blah... }

   where rs::[RdrName], ns::[Name]

The idea is that '...blah...' 
  a) sees the bindings of ns
  b) returns the free variables it mentions
     so that bindNames can report unused ones

In particular, 
    mapM rnPatAndThen [p1, p2, p3]
has a *left-to-right* scoping: it makes the binders in 
p1 scope over p2,p3.

86
\begin{code}
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
newtype CpsRn b = CpsRn { unCpsRn :: forall r. (b -> RnM (r, FreeVars))
                                            -> RnM (r, FreeVars) }
	-- See Note [CpsRn monad]

instance Monad CpsRn where
  return x = CpsRn (\k -> k x)
  (CpsRn m) >>= mk = CpsRn (\k -> m (\v -> unCpsRn (mk v) k))

runCps :: CpsRn a -> RnM (a, FreeVars)
runCps (CpsRn m) = m (\r -> return (r, emptyFVs))

liftCps :: RnM a -> CpsRn a
liftCps rn_thing = CpsRn (\k -> rn_thing >>= k)

liftCpsFV :: RnM (a, FreeVars) -> CpsRn a
liftCpsFV rn_thing = CpsRn (\k -> do { (v,fvs1) <- rn_thing
                                     ; (r,fvs2) <- k v
                                     ; return (r, fvs1 `plusFV` fvs2) })

wrapSrcSpanCps :: (a -> CpsRn b) -> Located a -> CpsRn (Located b)
-- Set the location, and also wrap it around the value returned
wrapSrcSpanCps fn (L loc a)
  = CpsRn (\k -> setSrcSpan loc $ 
                 unCpsRn (fn a) $ \v -> 
                 k (L loc v))

lookupConCps :: Located RdrName -> CpsRn (Located Name)
lookupConCps con_rdr 
  = CpsRn (\k -> do { con_name <- lookupLocatedOccRn con_rdr
                    ; (r, fvs) <- k con_name
                    ; return (r, fvs `plusFV` unitFV (unLoc con_name)) })
\end{code}
119

120 121 122 123 124
%*********************************************************
%*							*
	Name makers
%*							*
%*********************************************************
125

126 127 128 129 130 131 132
Externally abstract type of name makers,
which is how you go from a RdrName to a Name

\begin{code}
data NameMaker 
  = LamMk 	-- Lambdas 
      Bool	-- True <=> report unused bindings
133 134
      		--   (even if True, the warning only comes out 
		--    if -fwarn-unused-matches is on)
135 136

  | LetMk       -- Let bindings, incl top level
137
		-- Do *not* check for unused bindings
138
      TopLevelFlag
139 140
      MiniFixityEnv

141 142
topRecNameMaker :: MiniFixityEnv -> NameMaker
topRecNameMaker fix_env = LetMk TopLevel fix_env
143 144

localRecNameMaker :: MiniFixityEnv -> NameMaker
145
localRecNameMaker fix_env = LetMk NotTopLevel fix_env 
146

147 148 149 150 151 152 153 154
matchNameMaker :: HsMatchContext a -> NameMaker
matchNameMaker ctxt = LamMk report_unused
  where
    -- Do not report unused names in interactive contexts
    -- i.e. when you type 'x <- e' at the GHCi prompt
    report_unused = case ctxt of
                      StmtCtxt GhciStmt -> False
                      _                 -> True
155 156 157 158 159 160 161 162 163

newName :: NameMaker -> Located RdrName -> CpsRn Name
newName (LamMk report_unused) rdr_name
  = CpsRn (\ thing_inside -> 
	do { name <- newLocalBndrRn rdr_name
	   ; (res, fvs) <- bindLocalName name (thing_inside name)
	   ; when report_unused $ warnUnusedMatches [name] fvs
	   ; return (res, name `delFV` fvs) })

164
newName (LetMk is_top fix_env) rdr_name
165
  = CpsRn (\ thing_inside -> 
166 167 168
        do { name <- case is_top of
                       NotTopLevel -> newLocalBndrRn rdr_name
                       TopLevel    -> newTopSrcBinder rdr_name
169 170 171
	   ; bindLocalName name $	-- Do *not* use bindLocalNameFV here
					-- See Note [View pattern usage]
             addLocalFixities fix_env [name] $
172
	     thing_inside name })
173
			  
174
    -- Note: the bindLocalName is somewhat suspicious
175 176 177 178 179 180 181
    --       because it binds a top-level name as a local name.
    --       however, this binding seems to work, and it only exists for
    --       the duration of the patterns and the continuation;
    --       then the top-level name is added to the global env
    --       before going on to the RHSes (see RnSource.lhs).
\end{code}

182 183 184 185 186 187 188 189
Note [View pattern usage]
~~~~~~~~~~~~~~~~~~~~~~~~~
Consider
  let (r, (r -> x)) = x in ...
Here the pattern binds 'r', and then uses it *only* in the view pattern.
We want to "see" this use, and in let-bindings we collect all uses and
report unused variables at the binding level. So we must use bindLocalName
here, *not* bindLocalNameFV.  Trac #3943.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

%*********************************************************
%*							*
	External entry points
%*							*
%*********************************************************

There are various entry points to renaming patterns, depending on
 (1) whether the names created should be top-level names or local names
 (2) whether the scope of the names is entirely given in a continuation
     (e.g., in a case or lambda, but not in a let or at the top-level,
      because of the way mutually recursive bindings are handled)
 (3) whether the a type signature in the pattern can bind 
	lexically-scoped type variables (for unpacking existential 
	type vars in data constructors)
 (4) whether we do duplicate and unused variable checking
 (5) whether there are fixity declarations associated with the names
     bound by the patterns that need to be brought into scope with them.
     
 Rather than burdening the clients of this module with all of these choices,
 we export the three points in this design space that we actually need:

\begin{code}
-- ----------- Entry point 1: rnPats -------------------
-- Binds local names; the scope of the bindings is entirely in the thing_inside
--   * allows type sigs to bind type vars
--   * local namemaker
--   * unused and duplicate checking
--   * no fixities
rnPats :: HsMatchContext Name -- for error messages
       -> [LPat RdrName] 
       -> ([LPat Name] -> RnM (a, FreeVars))
       -> RnM (a, FreeVars)
rnPats ctxt pats thing_inside
224
  = do	{ envs_before <- getRdrEnvs
225 226 227 228

	  -- (0) bring into scope all of the type variables bound by the patterns
	  -- (1) rename the patterns, bringing into scope all of the term variables
	  -- (2) then do the thing inside.
229 230
	; bindPatSigTyVarsFV (collectSigTysFromPats pats)     $ 
	  unCpsRn (rnLPatsAndThen (matchNameMaker ctxt) pats) $ \ pats' -> do
231
        { -- Check for duplicated and shadowed names 
232 233 234 235
	         -- Because we don't bind the vars all at once, we can't
	         -- 	check incrementally for duplicates; 
	         -- Nor can we check incrementally for shadowing, else we'll
	         -- 	complain *twice* about duplicates e.g. f (x,x) = ...
236
        ; let names = collectPatsBinders pats'
237
        ; addErrCtxt doc_pat $ checkDupAndShadowedNames envs_before names
238
        ; thing_inside pats' } }
239
  where
Ian Lynagh's avatar
Ian Lynagh committed
240
    doc_pat = ptext (sLit "In") <+> pprMatchContext ctxt
241

242 243 244
rnPat :: HsMatchContext Name -- for error messages
      -> LPat RdrName 
      -> (LPat Name -> RnM (a, FreeVars))
245 246
      -> RnM (a, FreeVars)     -- Variables bound by pattern do not 
      	     	 	       -- appear in the result FreeVars 
247
rnPat ctxt pat thing_inside 
248
  = rnPats ctxt [pat] (\pats' -> let [pat'] = pats' in thing_inside pat')
249

250 251 252 253 254
applyNameMaker :: NameMaker -> Located RdrName -> RnM Name
applyNameMaker mk rdr = do { (n, _fvs) <- runCps (newName mk rdr); return n }

-- ----------- Entry point 2: rnBindPat -------------------
-- Binds local names; in a recursive scope that involves other bound vars
255
--	e.g let { (x, Just y) = e1; ... } in ...
256 257 258 259
--   * does NOT allows type sig to bind type vars
--   * local namemaker
--   * no unused and duplicate checking
--   * fixities might be coming in
260 261
rnBindPat :: NameMaker
          -> LPat RdrName
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
          -> RnM (LPat Name, FreeVars)
   -- Returned FreeVars are the free variables of the pattern,
   -- of course excluding variables bound by this pattern 

rnBindPat name_maker pat = runCps (rnLPatAndThen name_maker pat)
\end{code}


%*********************************************************
%*							*
	The main event
%*							*
%*********************************************************

\begin{code}
-- ----------- Entry point 3: rnLPatAndThen -------------------
-- General version: parametrized by how you make new names

rnLPatsAndThen :: NameMaker -> [LPat RdrName] -> CpsRn [LPat Name]
rnLPatsAndThen mk = mapM (rnLPatAndThen mk)
  -- Despite the map, the monad ensures that each pattern binds
  -- variables that may be mentioned in subsequent patterns in the list

--------------------
-- The workhorse
rnLPatAndThen :: NameMaker -> LPat RdrName -> CpsRn (LPat Name)
rnLPatAndThen nm lpat = wrapSrcSpanCps (rnPatAndThen nm) lpat

rnPatAndThen :: NameMaker -> Pat RdrName -> CpsRn (Pat Name)
rnPatAndThen _  (WildPat _)   = return (WildPat placeHolderType)
rnPatAndThen mk (ParPat pat)  = do { pat' <- rnLPatAndThen mk pat; return (ParPat pat') }
rnPatAndThen mk (LazyPat pat) = do { pat' <- rnLPatAndThen mk pat; return (LazyPat pat') }
rnPatAndThen mk (BangPat pat) = do { pat' <- rnLPatAndThen mk pat; return (BangPat pat') }
rnPatAndThen mk (VarPat rdr)  = do { loc <- liftCps getSrcSpanM
                                   ; name <- newName mk (L loc rdr)
                                   ; return (VarPat name) }
     -- we need to bind pattern variables for view pattern expressions
     -- (e.g. in the pattern (x, x -> y) x needs to be bound in the rhs of the tuple)
300
                                     
301
rnPatAndThen mk (SigPatIn pat ty)
302
  = do { patsigs <- liftCps (xoptM Opt_ScopedTypeVariables)
303 304 305 306 307 308 309 310
       ; if patsigs
         then do { pat' <- rnLPatAndThen mk pat
                 ; ty' <- liftCpsFV (rnHsTypeFVs tvdoc ty)
		 ; return (SigPatIn pat' ty') }
         else do { liftCps (addErr (patSigErr ty))
                 ; rnPatAndThen mk (unLoc pat) } }
  where
    tvdoc = text "In a pattern type-signature"
311
       
312 313
rnPatAndThen mk (LitPat lit)
  | HsString s <- lit
314
  = do { ovlStr <- liftCps (xoptM Opt_OverloadedStrings)
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
       ; if ovlStr 
         then rnPatAndThen mk (mkNPat (mkHsIsString s placeHolderType) Nothing)
         else normal_lit }
  | otherwise = normal_lit
  where
    normal_lit = do { liftCps (rnLit lit); return (LitPat lit) }

rnPatAndThen _ (NPat lit mb_neg _eq)
  = do { lit'    <- liftCpsFV $ rnOverLit lit
       ; mb_neg' <- liftCpsFV $ case mb_neg of
		      Nothing -> return (Nothing, emptyFVs)
		      Just _  -> do { (neg, fvs) <- lookupSyntaxName negateName
				    ; return (Just neg, fvs) }
       ; eq' <- liftCpsFV $ lookupSyntaxName eqName
       ; return (NPat lit' mb_neg' eq') }

rnPatAndThen mk (NPlusKPat rdr lit _ _)
  = do { new_name <- newName mk rdr
       ; lit'  <- liftCpsFV $ rnOverLit lit
       ; minus <- liftCpsFV $ lookupSyntaxName minusName
       ; ge    <- liftCpsFV $ lookupSyntaxName geName
       ; return (NPlusKPat (L (nameSrcSpan new_name) new_name) lit' ge minus) }
337 338
	   	-- The Report says that n+k patterns must be in Integral

339 340 341 342 343 344
rnPatAndThen mk (AsPat rdr pat)
  = do { new_name <- newName mk rdr
       ; pat' <- rnLPatAndThen mk pat
       ; return (AsPat (L (nameSrcSpan new_name) new_name) pat') }

rnPatAndThen mk p@(ViewPat expr pat ty)
345
  = do { liftCps $ do { vp_flag <- xoptM Opt_ViewPatterns
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
                      ; checkErr vp_flag (badViewPat p) }
         -- Because of the way we're arranging the recursive calls,
         -- this will be in the right context 
       ; expr' <- liftCpsFV $ rnLExpr expr 
       ; pat' <- rnLPatAndThen mk pat
       ; return (ViewPat expr' pat' ty) }

rnPatAndThen mk (ConPatIn con stuff)
   -- rnConPatAndThen takes care of reconstructing the pattern
  = rnConPatAndThen mk con stuff

rnPatAndThen mk (ListPat pats _)
  = do { pats' <- rnLPatsAndThen mk pats
       ; return (ListPat pats' placeHolderType) }

rnPatAndThen mk (PArrPat pats _)
  = do { pats' <- rnLPatsAndThen mk pats
       ; return (PArrPat pats' placeHolderType) }

rnPatAndThen mk (TuplePat pats boxed _)
  = do { liftCps $ checkTupSize (length pats)
       ; pats' <- rnLPatsAndThen mk pats
       ; return (TuplePat pats' boxed placeHolderType) }

370
#ifndef GHCI
371 372
rnPatAndThen _ p@(QuasiQuotePat {}) 
  = pprPanic "Can't do QuasiQuotePat without GHCi" (ppr p)
373
#else
374
rnPatAndThen mk (QuasiQuotePat qq)
375
  = do { pat <- liftCps $ runQuasiQuotePat qq
376 377
       ; L _ pat' <- rnLPatAndThen mk pat
       ; return pat' }
378 379
#endif 	/* GHCI */

380
rnPatAndThen _ pat = pprPanic "rnLPatAndThen" (ppr pat)
Ian Lynagh's avatar
Ian Lynagh committed
381

382

383
--------------------
384 385 386
rnConPatAndThen :: NameMaker
                -> Located RdrName          -- the constructor
                -> HsConPatDetails RdrName 
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
                -> CpsRn (Pat Name)

rnConPatAndThen mk con (PrefixCon pats)
  = do	{ con' <- lookupConCps con
	; pats' <- rnLPatsAndThen mk pats
	; return (ConPatIn con' (PrefixCon pats')) }

rnConPatAndThen mk con (InfixCon pat1 pat2)
  = do	{ con' <- lookupConCps con
   	; pat1' <- rnLPatAndThen mk pat1
	; pat2' <- rnLPatAndThen mk pat2
	; fixity <- liftCps $ lookupFixityRn (unLoc con')
	; liftCps $ mkConOpPatRn con' fixity pat1' pat2' }

rnConPatAndThen mk con (RecCon rpats)
  = do	{ con' <- lookupConCps con
  	; rpats' <- rnHsRecPatsAndThen mk con' rpats
	; return (ConPatIn con' (RecCon rpats')) }

--------------------
rnHsRecPatsAndThen :: NameMaker
                   -> Located Name	-- Constructor
		   -> HsRecFields RdrName (LPat RdrName)
		   -> CpsRn (HsRecFields Name (LPat Name))
rnHsRecPatsAndThen mk (L _ con) hs_rec_fields@(HsRecFields { rec_dotdot = dd })
  = do { flds <- liftCpsFV $ rnHsRecFields1 (HsRecFieldPat con) VarPat hs_rec_fields
       ; flds' <- mapM rn_field (flds `zip` [1..])
       ; return (HsRecFields { rec_flds = flds', rec_dotdot = dd }) }
  where 
    rn_field (fld, n') = do { arg' <- rnLPatAndThen (nested_mk dd mk n') 
                                                    (hsRecFieldArg fld)
                            ; return (fld { hsRecFieldArg = arg' }) }

	-- Suppress unused-match reporting for fields introduced by ".."
    nested_mk Nothing  mk                    _  = mk
    nested_mk (Just _) mk@(LetMk {})         _  = mk
    nested_mk (Just n) (LamMk report_unused) n' = LamMk (report_unused && (n' <= n))
\end{code}


%************************************************************************
%*									*
	Record fields
%*									*
%************************************************************************

\begin{code}
data HsRecFieldContext 
  = HsRecFieldCon Name
  | HsRecFieldPat Name
  | HsRecFieldUpd

rnHsRecFields1 
    :: HsRecFieldContext
    -> (RdrName -> arg) -- When punning, use this to build a new field
    -> HsRecFields RdrName (Located arg)
    -> RnM ([HsRecField Name (Located arg)], FreeVars)

-- This supprisingly complicated pass
--   a) looks up the field name (possibly using disambiguation)
--   b) fills in puns and dot-dot stuff
-- When we we've finished, we've renamed the LHS, but not the RHS,
-- of each x=e binding

rnHsRecFields1 ctxt mk_arg (HsRecFields { rec_flds = flds, rec_dotdot = dotdot })
452 453
  = do { pun_ok      <- xoptM Opt_RecordPuns
       ; disambig_ok <- xoptM Opt_DisambiguateRecordFields
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
       ; parent <- check_disambiguation disambig_ok mb_con
       ; flds1 <- mapM (rn_fld pun_ok parent) flds
       ; mapM_ (addErr . dupFieldErr ctxt) dup_flds
       ; flds2 <- rn_dotdot dotdot mb_con flds1
       ; return (flds2, mkFVs (getFieldIds flds2)) }
  where
    mb_con = case ctxt of
		HsRecFieldUpd     -> Nothing
		HsRecFieldCon con -> Just con
		HsRecFieldPat con -> Just con
    doc = case mb_con of
            Nothing  -> ptext (sLit "constructor field name")
            Just con -> ptext (sLit "field of constructor") <+> quotes (ppr con)

    name_to_arg (L loc n) = L loc (mk_arg (mkRdrUnqual (nameOccName n)))

    rn_fld pun_ok parent (HsRecField { hsRecFieldId = fld
                       	      	     , hsRecFieldArg = arg
                       	      	     , hsRecPun = pun })
473
      = do { fld' <- wrapLocM (lookupSubBndr parent doc) fld
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
           ; arg' <- if pun 
                     then do { checkErr pun_ok (badPun fld)
                             ; return (name_to_arg fld') }
                     else return arg
           ; return (HsRecField { hsRecFieldId = fld'
                                , hsRecFieldArg = arg'
                                , hsRecPun = pun }) }

    rn_dotdot Nothing _mb_con flds     -- No ".." at all
      = return flds
    rn_dotdot (Just {}) Nothing flds   -- ".." on record update
      = do { addErr (badDotDot ctxt); return flds }
    rn_dotdot (Just n) (Just con) flds -- ".." on record con/pat
      = ASSERT( n == length flds )
        do { loc <- getSrcSpanM	-- Rather approximate
489
           ; dd_flag <- xoptM Opt_RecordWildCards
490 491 492 493 494 495 496 497
           ; checkErr dd_flag (needFlagDotDot ctxt)

           ; con_fields <- lookupConstructorFields con
           ; let present_flds = getFieldIds flds
                 absent_flds  = con_fields `minusList` present_flds
                 extras = [ HsRecField
                              { hsRecFieldId = L loc f
                              , hsRecFieldArg = name_to_arg (L loc f)
498
                              , hsRecPun = False }
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
                          | f <- absent_flds ]

           ; return (flds ++ extras) }

    check_disambiguation :: Bool -> Maybe Name -> RnM Parent
    -- When disambiguation is on, return the parent *type constructor*
    -- That is, the parent of the data constructor.  That's the parent
    -- to use for looking up record fields.
    check_disambiguation disambig_ok mb_con
      | disambig_ok, Just con <- mb_con
      = do { env <- getGlobalRdrEnv
           ; return (case lookupGRE_Name env con of
	       	       [gre] -> gre_par gre
               	       gres  -> WARN( True, ppr con <+> ppr gres ) NoParent) }
      | otherwise = return NoParent
 
    dup_flds :: [[RdrName]]
        -- Each list represents a RdrName that occurred more than once
        -- (the list contains all occurrences)
        -- Each list in dup_fields is non-empty
    (_, dup_flds) = removeDups compare (getFieldIds flds)

getFieldIds :: [HsRecField id arg] -> [id]
getFieldIds flds = map (unLoc . hsRecFieldId) flds

needFlagDotDot :: HsRecFieldContext -> SDoc
needFlagDotDot ctxt = vcat [ptext (sLit "Illegal `..' in record") <+> pprRFC ctxt,
			    ptext (sLit "Use -XRecordWildCards to permit this")]

badDotDot :: HsRecFieldContext -> SDoc
badDotDot ctxt = ptext (sLit "You cannot use `..' in a record") <+> pprRFC ctxt
530

Ian Lynagh's avatar
Ian Lynagh committed
531
badPun :: Located RdrName -> SDoc
Ian Lynagh's avatar
Ian Lynagh committed
532
badPun fld = vcat [ptext (sLit "Illegal use of punning for field") <+> quotes (ppr fld),
533
		   ptext (sLit "Use -XNamedFieldPuns to permit this")]
534

535 536 537 538 539
dupFieldErr :: HsRecFieldContext -> [RdrName] -> SDoc
dupFieldErr ctxt dups
  = hsep [ptext (sLit "duplicate field name"), 
          quotes (ppr (head dups)),
	  ptext (sLit "in record"), pprRFC ctxt]
540

541 542 543 544
pprRFC :: HsRecFieldContext -> SDoc
pprRFC (HsRecFieldCon {}) = ptext (sLit "construction")
pprRFC (HsRecFieldPat {}) = ptext (sLit "pattern")
pprRFC (HsRecFieldUpd {}) = ptext (sLit "update")
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
\end{code}


%************************************************************************
%*									*
\subsubsection{Literals}
%*									*
%************************************************************************

When literals occur we have to make sure
that the types and classes they involve
are made available.

\begin{code}
rnLit :: HsLit -> RnM ()
rnLit (HsChar c) = checkErr (inCharRange c) (bogusCharError c)
Ian Lynagh's avatar
Ian Lynagh committed
561
rnLit _ = return ()
562

Ian Lynagh's avatar
Ian Lynagh committed
563
rnOverLit :: HsOverLit t -> RnM (HsOverLit Name, FreeVars)
564 565 566 567 568 569 570 571
rnOverLit lit@(OverLit {ol_val=val})
  = do	{ let std_name = hsOverLitName val
	; (from_thing_name, fvs) <- lookupSyntaxName std_name
	; let rebindable = case from_thing_name of
				HsVar v -> v /= std_name
				_	-> panic "rnOverLit"
	; return (lit { ol_witness = from_thing_name
		      , ol_rebindable = rebindable }, fvs) }
572 573
\end{code}

574
%************************************************************************
575 576 577 578 579 580 581 582 583
%*									*
\subsubsection{Errors}
%*									*
%************************************************************************

\begin{code}
checkTupSize :: Int -> RnM ()
checkTupSize tup_size
  | tup_size <= mAX_TUPLE_SIZE 
584
  = return ()
585
  | otherwise		       
Ian Lynagh's avatar
Ian Lynagh committed
586 587 588
  = addErr (sep [ptext (sLit "A") <+> int tup_size <> ptext (sLit "-tuple is too large for GHC"),
		 nest 2 (parens (ptext (sLit "max size is") <+> int mAX_TUPLE_SIZE)),
		 nest 2 (ptext (sLit "Workaround: use nested tuples or define a data type"))])
589

Ian Lynagh's avatar
Ian Lynagh committed
590
patSigErr :: Outputable a => a -> SDoc
591
patSigErr ty
Ian Lynagh's avatar
Ian Lynagh committed
592
  =  (ptext (sLit "Illegal signature in pattern:") <+> ppr ty)
593
	$$ nest 4 (ptext (sLit "Use -XScopedTypeVariables to permit it"))
594

Ian Lynagh's avatar
Ian Lynagh committed
595
bogusCharError :: Char -> SDoc
596
bogusCharError c
Ian Lynagh's avatar
Ian Lynagh committed
597
  = ptext (sLit "character literal out of range: '\\") <> char c  <> char '\''
598

Ian Lynagh's avatar
Ian Lynagh committed
599
badViewPat :: Pat RdrName -> SDoc
Ian Lynagh's avatar
Ian Lynagh committed
600 601
badViewPat pat = vcat [ptext (sLit "Illegal view pattern: ") <+> ppr pat,
                       ptext (sLit "Use -XViewPatterns to enable view patterns")]
602
\end{code}