[project @ 2005-07-11 09:48:57 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / RnTypes.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[RnSource]{Main pass of renamer}
5
6 \begin{code}
7 module RnTypes ( rnHsType, rnLHsType, rnLHsTypes, rnContext,
8                  rnHsSigType, rnHsTypeFVs,
9                  rnLPat, rnPat, rnPatsAndThen,          -- Here because it's not part 
10                  rnLit, rnOverLit,                      -- of any mutual recursion      
11                  precParseErr, sectionPrecErr, dupFieldErr, patSigErr, checkTupSize
12   ) where
13
14 import DynFlags         ( DynFlag(Opt_WarnUnusedMatches, Opt_GlasgowExts) )
15
16 import HsSyn
17 import RdrHsSyn         ( extractHsRhoRdrTyVars )
18 import RnHsSyn          ( extractHsTyNames, parrTyCon_name, tupleTyCon_name, 
19                           listTyCon_name
20                         )
21 import RnEnv            ( lookupOccRn, lookupBndrRn, lookupSyntaxName,
22                           lookupLocatedOccRn, lookupLocatedBndrRn,
23                           lookupLocatedGlobalOccRn, bindTyVarsRn, lookupFixityRn,
24                           mapFvRn, warnUnusedMatches,
25                           newIPNameRn, bindPatSigTyVarsFV, bindLocatedLocalsFV )
26 import TcRnMonad
27 import RdrName          ( RdrName, elemLocalRdrEnv )
28 import PrelNames        ( eqClassName, integralClassName, geName, eqName,
29                           negateName, minusName, lengthPName, indexPName,
30                           plusIntegerName, fromIntegerName, timesIntegerName,
31                           ratioDataConName, fromRationalName )
32 import TypeRep          ( funTyCon )
33 import Constants        ( mAX_TUPLE_SIZE )
34 import Name             ( Name )
35 import SrcLoc           ( SrcSpan, Located(..), unLoc, noLoc )
36 import NameSet
37
38 import Literal          ( inIntRange, inCharRange )
39 import BasicTypes       ( compareFixity, Fixity(..), FixityDirection(..) )
40 import ListSetOps       ( removeDups )
41 import Outputable
42 import Monad            ( when )
43
44 #include "HsVersions.h"
45 \end{code}
46
47 These type renamers are in a separate module, rather than in (say) RnSource,
48 to break several loop.
49
50 %*********************************************************
51 %*                                                      *
52 \subsection{Renaming types}
53 %*                                                      *
54 %*********************************************************
55
56 \begin{code}
57 rnHsTypeFVs :: SDoc -> LHsType RdrName -> RnM (LHsType Name, FreeVars)
58 rnHsTypeFVs doc_str ty 
59   = rnLHsType doc_str ty        `thenM` \ ty' ->
60     returnM (ty', extractHsTyNames ty')
61
62 rnHsSigType :: SDoc -> LHsType RdrName -> RnM (LHsType Name)
63         -- rnHsSigType is used for source-language type signatures,
64         -- which use *implicit* universal quantification.
65 rnHsSigType doc_str ty
66   = rnLHsType (text "In the type signature for" <+> doc_str) ty
67 \end{code}
68
69 rnHsType is here because we call it from loadInstDecl, and I didn't
70 want a gratuitous knot.
71
72 \begin{code}
73 rnLHsType  :: SDoc -> LHsType RdrName -> RnM (LHsType Name)
74 rnLHsType doc = wrapLocM (rnHsType doc)
75
76 rnHsType :: SDoc -> HsType RdrName -> RnM (HsType Name)
77
78 rnHsType doc (HsForAllTy Implicit _ ctxt ty)
79         -- Implicit quantifiction in source code (no kinds on tyvars)
80         -- Given the signature  C => T  we universally quantify 
81         -- over FV(T) \ {in-scope-tyvars} 
82   = getLocalRdrEnv              `thenM` \ name_env ->
83     let
84         mentioned = extractHsRhoRdrTyVars ctxt ty
85
86         -- Don't quantify over type variables that are in scope;
87         -- when GlasgowExts is off, there usually won't be any, except for
88         -- class signatures:
89         --      class C a where { op :: a -> a }
90         forall_tyvars = filter (not . (`elemLocalRdrEnv` name_env) . unLoc) mentioned
91         tyvar_bndrs = [ L loc (UserTyVar v) | (L loc v) <- forall_tyvars ]
92     in
93     rnForAll doc Implicit tyvar_bndrs ctxt ty
94
95 rnHsType doc (HsForAllTy Explicit forall_tyvars ctxt tau)
96         -- Explicit quantification.
97         -- Check that the forall'd tyvars are actually 
98         -- mentioned in the type, and produce a warning if not
99   = let
100         mentioned          = map unLoc (extractHsRhoRdrTyVars ctxt tau)
101         forall_tyvar_names = hsLTyVarLocNames forall_tyvars
102
103         -- Explicitly quantified but not mentioned in ctxt or tau
104         warn_guys = filter ((`notElem` mentioned) . unLoc) forall_tyvar_names
105     in
106     mappM_ (forAllWarn doc tau) warn_guys       `thenM_`
107     rnForAll doc Explicit forall_tyvars ctxt tau
108
109 rnHsType doc (HsTyVar tyvar)
110   = lookupOccRn tyvar           `thenM` \ tyvar' ->
111     returnM (HsTyVar tyvar')
112
113 rnHsType doc (HsOpTy ty1 (L loc op) ty2)
114   = setSrcSpan loc (
115       lookupOccRn op                    `thenM` \ op' ->
116       let
117         l_op' = L loc op'
118       in
119       lookupTyFixityRn l_op'            `thenM` \ fix ->
120       rnLHsType doc ty1                 `thenM` \ ty1' ->
121       rnLHsType doc ty2                 `thenM` \ ty2' -> 
122       mkHsOpTyRn (\t1 t2 -> HsOpTy t1 l_op' t2) (ppr op') fix ty1' ty2'
123    )
124
125 rnHsType doc (HsParTy ty)
126   = rnLHsType doc ty            `thenM` \ ty' ->
127     returnM (HsParTy ty')
128
129 rnHsType doc (HsBangTy b ty)
130   = rnLHsType doc ty            `thenM` \ ty' ->
131     returnM (HsBangTy b ty')
132
133 rnHsType doc (HsNumTy i)
134   | i == 1    = returnM (HsNumTy i)
135   | otherwise = addErr err_msg  `thenM_`  returnM (HsNumTy i)
136   where
137     err_msg = ptext SLIT("Only unit numeric type pattern is valid")
138                            
139
140 rnHsType doc (HsFunTy ty1 ty2)
141   = rnLHsType doc ty1   `thenM` \ ty1' ->
142         -- Might find a for-all as the arg of a function type
143     rnLHsType doc ty2   `thenM` \ ty2' ->
144         -- Or as the result.  This happens when reading Prelude.hi
145         -- when we find return :: forall m. Monad m -> forall a. a -> m a
146
147         -- Check for fixity rearrangements
148     mkHsOpTyRn HsFunTy (ppr funTyCon) funTyFixity ty1' ty2'
149
150 rnHsType doc (HsListTy ty)
151   = rnLHsType doc ty                            `thenM` \ ty' ->
152     returnM (HsListTy ty')
153
154 rnHsType doc (HsKindSig ty k)
155   = rnLHsType doc ty                            `thenM` \ ty' ->
156     returnM (HsKindSig ty' k)
157
158 rnHsType doc (HsPArrTy ty)
159   = rnLHsType doc ty                            `thenM` \ ty' ->
160     returnM (HsPArrTy ty')
161
162 -- Unboxed tuples are allowed to have poly-typed arguments.  These
163 -- sometimes crop up as a result of CPR worker-wrappering dictionaries.
164 rnHsType doc (HsTupleTy tup_con tys)
165   = mappM (rnLHsType doc) tys           `thenM` \ tys' ->
166     returnM (HsTupleTy tup_con tys')
167
168 rnHsType doc (HsAppTy ty1 ty2)
169   = rnLHsType doc ty1           `thenM` \ ty1' ->
170     rnLHsType doc ty2           `thenM` \ ty2' ->
171     returnM (HsAppTy ty1' ty2')
172
173 rnHsType doc (HsPredTy pred)
174   = rnPred doc pred     `thenM` \ pred' ->
175     returnM (HsPredTy pred')
176
177 rnLHsTypes doc tys = mappM (rnLHsType doc) tys
178 \end{code}
179
180
181 \begin{code}
182 rnForAll :: SDoc -> HsExplicitForAll -> [LHsTyVarBndr RdrName]
183          -> LHsContext RdrName -> LHsType RdrName -> RnM (HsType Name)
184
185 rnForAll doc exp [] (L _ []) (L _ ty) = rnHsType doc ty
186         -- One reason for this case is that a type like Int#
187         -- starts off as (HsForAllTy Nothing [] Int), in case
188         -- there is some quantification.  Now that we have quantified
189         -- and discovered there are no type variables, it's nicer to turn
190         -- it into plain Int.  If it were Int# instead of Int, we'd actually
191         -- get an error, because the body of a genuine for-all is
192         -- of kind *.
193
194 rnForAll doc exp forall_tyvars ctxt ty
195   = bindTyVarsRn doc forall_tyvars      $ \ new_tyvars ->
196     rnContext doc ctxt                  `thenM` \ new_ctxt ->
197     rnLHsType doc ty                    `thenM` \ new_ty ->
198     returnM (HsForAllTy exp new_tyvars new_ctxt new_ty)
199         -- Retain the same implicit/explicit flag as before
200         -- so that we can later print it correctly
201 \end{code}
202
203
204 %*********************************************************
205 %*                                                      *
206 \subsection{Fixities}
207 %*                                                      *
208 %*********************************************************
209
210 Infix types are read in a *right-associative* way, so that
211         a `op` b `op` c
212 is always read in as
213         a `op` (b `op` c)
214
215 mkHsOpTyRn rearranges where necessary.  The two arguments
216 have already been renamed and rearranged.  It's made rather tiresome
217 by the presence of ->, which is a separate syntactic construct.
218
219 \begin{code}
220 ---------------
221 -- Building (ty1 `op1` (ty21 `op2` ty22))
222 mkHsOpTyRn :: (LHsType Name -> LHsType Name -> HsType Name)
223            -> SDoc -> Fixity -> LHsType Name -> LHsType Name 
224            -> RnM (HsType Name)
225
226 mkHsOpTyRn mk1 pp_op1 fix1 ty1 (L loc2 (HsOpTy ty21 op2 ty22))
227   = do  { fix2 <- lookupTyFixityRn op2
228         ; mk_hs_op_ty mk1 pp_op1 fix1 ty1 
229                       (\t1 t2 -> HsOpTy t1 op2 t2)
230                       (ppr op2) fix2 ty21 ty22 loc2 }
231
232 mkHsOpTyRn mk1 pp_op1 fix1 ty1 ty2@(L loc2 (HsFunTy ty21 ty22))
233   = mk_hs_op_ty mk1 pp_op1 fix1 ty1 
234                 HsFunTy (ppr funTyCon) funTyFixity ty21 ty22 loc2
235
236 mkHsOpTyRn mk1 pp_op1 fix1 ty1 ty2              -- Default case, no rearrangment
237   = return (mk1 ty1 ty2)
238
239 ---------------
240 mk_hs_op_ty :: (LHsType Name -> LHsType Name -> HsType Name)
241             -> SDoc -> Fixity -> LHsType Name
242             -> (LHsType Name -> LHsType Name -> HsType Name)
243             -> SDoc -> Fixity -> LHsType Name -> LHsType Name -> SrcSpan
244             -> RnM (HsType Name)
245 mk_hs_op_ty mk1 pp_op1 fix1 ty1 
246             mk2 pp_op2 fix2 ty21 ty22 loc2
247   | nofix_error     = do { addErr (precParseErr (quotes pp_op1,fix1) 
248                                                 (quotes pp_op2,fix2))
249                          ; return (mk1 ty1 (L loc2 (mk2 ty21 ty22))) }
250   | associate_right = return (mk1 ty1 (L loc2 (mk2 ty21 ty22)))
251   | otherwise       = do { -- Rearrange to ((ty1 `op1` ty21) `op2` ty22)
252                            new_ty <- mkHsOpTyRn mk1 pp_op1 fix1 ty1 ty21
253                          ; return (mk2 (noLoc new_ty) ty22) }
254   where
255     (nofix_error, associate_right) = compareFixity fix1 fix2
256
257 ---------------
258 lookupTyFixityRn (L loc n)
259   = doptM Opt_GlasgowExts                       `thenM` \ glaExts ->
260     when (not glaExts) 
261         (setSrcSpan loc $ addWarn (infixTyConWarn n))   `thenM_`
262     lookupFixityRn n
263
264 ---------------
265 funTyFixity = Fixity 0 InfixR   -- Fixity of '->'
266 \end{code}
267
268 %*********************************************************
269 %*                                                      *
270 \subsection{Contexts and predicates}
271 %*                                                      *
272 %*********************************************************
273
274 \begin{code}
275 rnContext :: SDoc -> LHsContext RdrName -> RnM (LHsContext Name)
276 rnContext doc = wrapLocM (rnContext' doc)
277
278 rnContext' :: SDoc -> HsContext RdrName -> RnM (HsContext Name)
279 rnContext' doc ctxt = mappM (rnLPred doc) ctxt
280
281 rnLPred :: SDoc -> LHsPred RdrName -> RnM (LHsPred Name)
282 rnLPred doc  = wrapLocM (rnPred doc)
283
284 rnPred doc (HsClassP clas tys)
285   = lookupOccRn clas            `thenM` \ clas_name ->
286     rnLHsTypes doc tys          `thenM` \ tys' ->
287     returnM (HsClassP clas_name tys')
288
289 rnPred doc (HsIParam n ty)
290   = newIPNameRn n               `thenM` \ name ->
291     rnLHsType doc ty            `thenM` \ ty' ->
292     returnM (HsIParam name ty')
293 \end{code}
294
295
296 *********************************************************
297 *                                                       *
298 \subsection{Patterns}
299 *                                                       *
300 *********************************************************
301
302 \begin{code}
303 rnPatsAndThen :: HsMatchContext Name
304               -> [LPat RdrName] 
305               -> ([LPat Name] -> RnM (a, FreeVars))
306               -> RnM (a, FreeVars)
307 -- Bring into scope all the binders and type variables
308 -- bound by the patterns; then rename the patterns; then
309 -- do the thing inside.
310 --
311 -- Note that we do a single bindLocalsRn for all the
312 -- matches together, so that we spot the repeated variable in
313 --      f x x = 1
314
315 rnPatsAndThen ctxt pats thing_inside
316   = bindPatSigTyVarsFV pat_sig_tys      $
317     bindLocatedLocalsFV doc_pat bndrs   $ \ new_bndrs ->
318     rnLPats pats                        `thenM` \ (pats', pat_fvs) ->
319     thing_inside pats'                  `thenM` \ (res, res_fvs) ->
320
321     let
322         unused_binders = filter (not . (`elemNameSet` res_fvs)) new_bndrs
323     in
324     warnUnusedMatches unused_binders   `thenM_`
325     returnM (res, res_fvs `plusFV` pat_fvs)
326   where
327     pat_sig_tys = collectSigTysFromPats pats
328     bndrs       = collectLocatedPatsBinders pats
329     doc_pat     = ptext SLIT("In") <+> pprMatchContext ctxt
330
331 rnLPats :: [LPat RdrName] -> RnM ([LPat Name], FreeVars)
332 rnLPats ps = mapFvRn rnLPat ps
333
334 rnLPat :: LPat RdrName -> RnM (LPat Name, FreeVars)
335 rnLPat = wrapLocFstM rnPat
336
337 -- -----------------------------------------------------------------------------
338 -- rnPat
339
340 rnPat :: Pat RdrName -> RnM (Pat Name, FreeVars)
341
342 rnPat (WildPat _) = returnM (WildPat placeHolderType, emptyFVs)
343
344 rnPat (VarPat name)
345   = lookupBndrRn  name                  `thenM` \ vname ->
346     returnM (VarPat vname, emptyFVs)
347
348 rnPat (SigPatIn pat ty)
349   = doptM Opt_GlasgowExts `thenM` \ glaExts ->
350     
351     if glaExts
352     then rnLPat pat             `thenM` \ (pat', fvs1) ->
353          rnHsTypeFVs doc ty     `thenM` \ (ty',  fvs2) ->
354          returnM (SigPatIn pat' ty', fvs1 `plusFV` fvs2)
355
356     else addErr (patSigErr ty)  `thenM_`
357          rnPat (unLoc pat) -- XXX shouldn't throw away the loc
358   where
359     doc = text "In a pattern type-signature"
360     
361 rnPat (LitPat lit) 
362   = rnLit lit   `thenM_` 
363     returnM (LitPat lit, emptyFVs) 
364
365 rnPat (NPat lit mb_neg eq _) 
366   = rnOverLit lit                       `thenM` \ (lit', fvs1) ->
367     (case mb_neg of
368         Nothing -> returnM (Nothing, emptyFVs)
369         Just _  -> lookupSyntaxName negateName  `thenM` \ (neg, fvs) ->
370                    returnM (Just neg, fvs)
371     )                                   `thenM` \ (mb_neg', fvs2) ->
372     lookupSyntaxName eqName             `thenM` \ (eq', fvs3) -> 
373     returnM (NPat lit' mb_neg' eq' placeHolderType, 
374               fvs1 `plusFV` fvs2 `plusFV` fvs3 `addOneFV` eqClassName)  
375         -- Needed to find equality on pattern
376
377 rnPat (NPlusKPat name lit _ _)
378   = rnOverLit lit                       `thenM` \ (lit', fvs1) ->
379     lookupLocatedBndrRn name            `thenM` \ name' ->
380     lookupSyntaxName minusName          `thenM` \ (minus, fvs2) ->
381     lookupSyntaxName geName             `thenM` \ (ge, fvs3) ->
382     returnM (NPlusKPat name' lit' ge minus,
383              fvs1 `plusFV` fvs2 `plusFV` fvs3 `addOneFV` integralClassName)
384         -- The Report says that n+k patterns must be in Integral
385
386 rnPat (LazyPat pat)
387   = rnLPat pat          `thenM` \ (pat', fvs) ->
388     returnM (LazyPat pat', fvs)
389
390 rnPat (AsPat name pat)
391   = rnLPat pat                  `thenM` \ (pat', fvs) ->
392     lookupLocatedBndrRn name    `thenM` \ vname ->
393     returnM (AsPat vname pat', fvs)
394
395 rnPat (ConPatIn con stuff) = rnConPat con stuff
396
397
398 rnPat (ParPat pat)
399   = rnLPat pat          `thenM` \ (pat', fvs) ->
400     returnM (ParPat pat', fvs)
401
402 rnPat (ListPat pats _)
403   = rnLPats pats                        `thenM` \ (patslist, fvs) ->
404     returnM (ListPat patslist placeHolderType, fvs `addOneFV` listTyCon_name)
405
406 rnPat (PArrPat pats _)
407   = rnLPats pats                        `thenM` \ (patslist, fvs) ->
408     returnM (PArrPat patslist placeHolderType, 
409               fvs `plusFV` implicit_fvs `addOneFV` parrTyCon_name)
410   where
411     implicit_fvs = mkFVs [lengthPName, indexPName]
412
413 rnPat (TuplePat pats boxed)
414   = checkTupSize tup_size       `thenM_`
415     rnLPats pats                        `thenM` \ (patslist, fvs) ->
416     returnM (TuplePat patslist boxed, fvs `addOneFV` tycon_name)
417   where
418     tup_size   = length pats
419     tycon_name = tupleTyCon_name boxed tup_size
420
421 rnPat (TypePat name) =
422     rnHsTypeFVs (text "In a type pattern") name `thenM` \ (name', fvs) ->
423     returnM (TypePat name', fvs)
424
425 -- -----------------------------------------------------------------------------
426 -- rnConPat
427
428 rnConPat con (PrefixCon pats)
429   = lookupLocatedOccRn con      `thenM` \ con' ->
430     rnLPats pats                `thenM` \ (pats', fvs) ->
431     returnM (ConPatIn con' (PrefixCon pats'), fvs `addOneFV` unLoc con')
432
433 rnConPat con (RecCon rpats)
434   = lookupLocatedOccRn con      `thenM` \ con' ->
435     rnRpats rpats               `thenM` \ (rpats', fvs) ->
436     returnM (ConPatIn con' (RecCon rpats'), fvs `addOneFV` unLoc con')
437
438 rnConPat con (InfixCon pat1 pat2)
439   = lookupLocatedOccRn con                      `thenM` \ con' ->
440     rnLPat pat1                                 `thenM` \ (pat1', fvs1) ->
441     rnLPat pat2                                 `thenM` \ (pat2', fvs2) ->
442     lookupFixityRn (unLoc con')                 `thenM` \ fixity ->
443     mkConOpPatRn con' fixity pat1' pat2'        `thenM` \ pat' ->
444     returnM (pat', fvs1 `plusFV` fvs2 `addOneFV` unLoc con')
445
446 -- -----------------------------------------------------------------------------
447 -- rnRpats
448
449 rnRpats :: [(Located RdrName, LPat RdrName)]
450         -> RnM ([(Located Name, LPat Name)], FreeVars)
451 rnRpats rpats
452   = mappM_ field_dup_err dup_fields     `thenM_`
453     mapFvRn rn_rpat rpats               `thenM` \ (rpats', fvs) ->
454     returnM (rpats', fvs)
455   where
456     (_, dup_fields) = removeDups compare [ unLoc f | (f,_) <- rpats ]
457
458     field_dup_err dups = addErr (dupFieldErr "pattern" dups)
459
460     rn_rpat (field, pat)
461       = lookupLocatedGlobalOccRn field  `thenM` \ fieldname ->
462         rnLPat pat                      `thenM` \ (pat', fvs) ->
463         returnM ((fieldname, pat'), fvs `addOneFV` unLoc fieldname)
464
465 -- -----------------------------------------------------------------------------
466 -- mkConOpPatRn
467
468 mkConOpPatRn :: Located Name -> Fixity -> LPat Name -> LPat Name
469              -> RnM (Pat Name)
470
471 mkConOpPatRn op2 fix2 p1@(L loc (ConPatIn op1 (InfixCon p11 p12))) p2
472   = lookupFixityRn (unLoc op1)  `thenM` \ fix1 ->
473     let
474         (nofix_error, associate_right) = compareFixity fix1 fix2
475     in
476     if nofix_error then
477         addErr (precParseErr (ppr_op op1,fix1) (ppr_op op2,fix2))       `thenM_`
478         returnM (ConPatIn op2 (InfixCon p1 p2))
479     else 
480     if associate_right then
481         mkConOpPatRn op2 fix2 p12 p2            `thenM` \ new_p ->
482         returnM (ConPatIn op1 (InfixCon p11 (L loc new_p)))  -- XXX loc right?
483     else
484     returnM (ConPatIn op2 (InfixCon p1 p2))
485
486 mkConOpPatRn op fix p1 p2                       -- Default case, no rearrangment
487   = ASSERT( not_op_pat (unLoc p2) )
488     returnM (ConPatIn op (InfixCon p1 p2))
489
490 not_op_pat (ConPatIn _ (InfixCon _ _)) = False
491 not_op_pat other                       = True
492 \end{code}
493
494
495 %************************************************************************
496 %*                                                                      *
497 \subsubsection{Literals}
498 %*                                                                      *
499 %************************************************************************
500
501 When literals occur we have to make sure
502 that the types and classes they involve
503 are made available.
504
505 \begin{code}
506 rnLit :: HsLit -> RnM ()
507 rnLit (HsChar c) = checkErr (inCharRange c) (bogusCharError c)
508 rnLit other      = returnM ()
509
510 rnOverLit (HsIntegral i _)
511   = lookupSyntaxName fromIntegerName    `thenM` \ (from_integer_name, fvs) ->
512     if inIntRange i then
513         returnM (HsIntegral i from_integer_name, fvs)
514     else let
515         extra_fvs = mkFVs [plusIntegerName, timesIntegerName]
516         -- Big integer literals are built, using + and *, 
517         -- out of small integers (DsUtils.mkIntegerLit)
518         -- [NB: plusInteger, timesInteger aren't rebindable... 
519         --      they are used to construct the argument to fromInteger, 
520         --      which is the rebindable one.]
521     in
522     returnM (HsIntegral i from_integer_name, fvs `plusFV` extra_fvs)
523
524 rnOverLit (HsFractional i _)
525   = lookupSyntaxName fromRationalName           `thenM` \ (from_rat_name, fvs) ->
526     let
527         extra_fvs = mkFVs [ratioDataConName, plusIntegerName, timesIntegerName]
528         -- We have to make sure that the Ratio type is imported with
529         -- its constructor, because literals of type Ratio t are
530         -- built with that constructor.
531         -- The Rational type is needed too, but that will come in
532         -- as part of the type for fromRational.
533         -- The plus/times integer operations may be needed to construct the numerator
534         -- and denominator (see DsUtils.mkIntegerLit)
535     in
536     returnM (HsFractional i from_rat_name, fvs `plusFV` extra_fvs)
537 \end{code}
538
539
540
541 %*********************************************************
542 %*                                                      *
543 \subsection{Errors}
544 %*                                                      *
545 %*********************************************************
546
547 \begin{code}
548 checkTupSize :: Int -> RnM ()
549 checkTupSize tup_size
550   | tup_size <= mAX_TUPLE_SIZE 
551   = returnM ()
552   | otherwise                  
553   = addErr (sep [ptext SLIT("A") <+> int tup_size <> ptext SLIT("-tuple is too large for GHC"),
554                  nest 2 (parens (ptext SLIT("max size is") <+> int mAX_TUPLE_SIZE)),
555                  nest 2 (ptext SLIT("Workaround: use nested tuples or define a data type"))])
556
557 forAllWarn doc ty (L loc tyvar)
558   = ifOptM Opt_WarnUnusedMatches        $
559     setSrcSpan loc $
560     addWarn (sep [ptext SLIT("The universally quantified type variable") <+> quotes (ppr tyvar),
561                    nest 4 (ptext SLIT("does not appear in the type") <+> quotes (ppr ty))]
562                    $$
563                    doc
564                 )
565
566 bogusCharError c
567   = ptext SLIT("character literal out of range: '\\") <> char c  <> char '\''
568
569 precParseErr op1 op2 
570   = hang (ptext SLIT("precedence parsing error"))
571       4 (hsep [ptext SLIT("cannot mix"), ppr_opfix op1, ptext SLIT("and"), 
572                ppr_opfix op2,
573                ptext SLIT("in the same infix expression")])
574
575 sectionPrecErr op arg_op section
576  = vcat [ptext SLIT("The operator") <+> ppr_opfix op <+> ptext SLIT("of a section"),
577          nest 4 (ptext SLIT("must have lower precedence than the operand") <+> ppr_opfix arg_op),
578          nest 4 (ptext SLIT("in the section:") <+> quotes (ppr section))]
579
580 infixTyConWarn op
581   = vcat [ftext FSLIT("Accepting non-standard infix type constructor") <+> quotes (ppr op),
582           ftext FSLIT("Use -fglasgow-exts to avoid this warning")]
583
584 patSigErr ty
585   =  (ptext SLIT("Illegal signature in pattern:") <+> ppr ty)
586         $$ nest 4 (ptext SLIT("Use -fglasgow-exts to permit it"))
587
588 dupFieldErr str dup
589   = hsep [ptext SLIT("duplicate field name"), 
590           quotes (ppr dup),
591           ptext SLIT("in record"), text str]
592
593 ppr_op op = quotes (ppr op)     -- Here, op can be a Name or a (Var n), where n is a Name
594 ppr_opfix (pp_op, fixity) = pp_op <+> brackets (ppr fixity)
595 \end{code}