2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[RnSource]{Main pass of renamer}
7 module RnTypes ( rnHsType, rnLHsType, 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
14 import CmdLineOpts ( DynFlag(Opt_WarnUnusedMatches, Opt_GlasgowExts) )
17 import RdrHsSyn ( extractHsRhoRdrTyVars )
18 import RnHsSyn ( extractHsTyNames, parrTyCon_name, tupleTyCon_name,
21 import RnEnv ( lookupOccRn, lookupBndrRn, lookupSyntaxName,
22 lookupLocatedOccRn, lookupLocatedBndrRn,
23 lookupLocatedGlobalOccRn, bindTyVarsRn, lookupFixityRn,
24 mapFvRn, warnUnusedMatches,
25 newIPNameRn, bindPatSigTyVarsFV, bindLocatedLocalsFV )
27 import RdrName ( RdrName, elemLocalRdrEnv )
28 import PrelNames ( eqClassName, integralClassName,
29 negateName, minusName, lengthPName, indexPName,
30 plusIntegerName, fromIntegerName, timesIntegerName,
31 ratioDataConName, fromRationalName )
32 import Constants ( mAX_TUPLE_SIZE )
34 import SrcLoc ( Located(..), unLoc )
37 import Literal ( inIntRange, inCharRange )
38 import BasicTypes ( compareFixity )
39 import ListSetOps ( removeDups )
43 #include "HsVersions.h"
46 These type renamers are in a separate module, rather than in (say) RnSource,
47 to break several loop.
49 %*********************************************************
51 \subsection{Renaming types}
53 %*********************************************************
56 rnHsTypeFVs :: SDoc -> LHsType RdrName -> RnM (LHsType Name, FreeVars)
57 rnHsTypeFVs doc_str ty
58 = rnLHsType doc_str ty `thenM` \ ty' ->
59 returnM (ty', extractHsTyNames ty')
61 rnHsSigType :: SDoc -> LHsType RdrName -> RnM (LHsType Name)
62 -- rnHsSigType is used for source-language type signatures,
63 -- which use *implicit* universal quantification.
64 rnHsSigType doc_str ty
65 = rnLHsType (text "In the type signature for" <+> doc_str) ty
68 rnHsType is here because we call it from loadInstDecl, and I didn't
69 want a gratuitous knot.
72 rnLHsType :: SDoc -> LHsType RdrName -> RnM (LHsType Name)
73 rnLHsType doc = wrapLocM (rnHsType doc)
75 rnHsType :: SDoc -> HsType RdrName -> RnM (HsType Name)
77 rnHsType doc (HsForAllTy Implicit _ ctxt ty)
78 -- Implicit quantifiction in source code (no kinds on tyvars)
79 -- Given the signature C => T we universally quantify
80 -- over FV(T) \ {in-scope-tyvars}
81 = getLocalRdrEnv `thenM` \ name_env ->
83 mentioned = extractHsRhoRdrTyVars ctxt ty
85 -- Don't quantify over type variables that are in scope;
86 -- when GlasgowExts is off, there usually won't be any, except for
88 -- class C a where { op :: a -> a }
89 forall_tyvars = filter (not . (`elemLocalRdrEnv` name_env) . unLoc) mentioned
90 tyvar_bndrs = [ L loc (UserTyVar v) | (L loc v) <- forall_tyvars ]
92 rnForAll doc Implicit tyvar_bndrs ctxt ty
94 rnHsType doc (HsForAllTy Explicit forall_tyvars ctxt tau)
95 -- Explicit quantification.
96 -- Check that the forall'd tyvars are actually
97 -- mentioned in the type, and produce a warning if not
99 mentioned = map unLoc (extractHsRhoRdrTyVars ctxt tau)
100 forall_tyvar_names = hsLTyVarLocNames forall_tyvars
102 -- Explicitly quantified but not mentioned in ctxt or tau
103 warn_guys = filter ((`notElem` mentioned) . unLoc) forall_tyvar_names
105 mappM_ (forAllWarn doc tau) warn_guys `thenM_`
106 rnForAll doc Explicit forall_tyvars ctxt tau
108 rnHsType doc (HsTyVar tyvar)
109 = lookupOccRn tyvar `thenM` \ tyvar' ->
110 returnM (HsTyVar tyvar')
112 rnHsType doc (HsOpTy ty1 (L loc op) ty2)
114 lookupOccRn op `thenM` \ op' ->
115 lookupTyFixityRn (L loc op') `thenM` \ fix ->
116 rnLHsType doc ty1 `thenM` \ ty1' ->
117 rnLHsType doc ty2 `thenM` \ ty2' ->
118 mkHsOpTyRn (L loc op') fix ty1' ty2'
121 rnHsType doc (HsParTy ty)
122 = rnLHsType doc ty `thenM` \ ty' ->
123 returnM (HsParTy ty')
125 rnHsType doc (HsNumTy i)
126 | i == 1 = returnM (HsNumTy i)
127 | otherwise = addErr err_msg `thenM_` returnM (HsNumTy i)
129 err_msg = ptext SLIT("Only unit numeric type pattern is valid")
132 rnHsType doc (HsFunTy ty1 ty2)
133 = rnLHsType doc ty1 `thenM` \ ty1' ->
134 -- Might find a for-all as the arg of a function type
135 rnLHsType doc ty2 `thenM` \ ty2' ->
136 -- Or as the result. This happens when reading Prelude.hi
137 -- when we find return :: forall m. Monad m -> forall a. a -> m a
138 returnM (HsFunTy ty1' ty2')
140 rnHsType doc (HsListTy ty)
141 = rnLHsType doc ty `thenM` \ ty' ->
142 returnM (HsListTy ty')
144 rnHsType doc (HsKindSig ty k)
145 = rnLHsType doc ty `thenM` \ ty' ->
146 returnM (HsKindSig ty' k)
148 rnHsType doc (HsPArrTy ty)
149 = rnLHsType doc ty `thenM` \ ty' ->
150 returnM (HsPArrTy ty')
152 -- Unboxed tuples are allowed to have poly-typed arguments. These
153 -- sometimes crop up as a result of CPR worker-wrappering dictionaries.
154 rnHsType doc (HsTupleTy tup_con tys)
155 = mappM (rnLHsType doc) tys `thenM` \ tys' ->
156 returnM (HsTupleTy tup_con tys')
158 rnHsType doc (HsAppTy ty1 ty2)
159 = rnLHsType doc ty1 `thenM` \ ty1' ->
160 rnLHsType doc ty2 `thenM` \ ty2' ->
161 returnM (HsAppTy ty1' ty2')
163 rnHsType doc (HsPredTy pred)
164 = rnLPred doc pred `thenM` \ pred' ->
165 returnM (HsPredTy pred')
167 rnLHsTypes doc tys = mappM (rnLHsType doc) tys
172 rnForAll :: SDoc -> HsExplicitForAll -> [LHsTyVarBndr RdrName] -> LHsContext RdrName
173 -> LHsType RdrName -> RnM (HsType Name)
175 rnForAll doc exp [] (L _ []) (L _ ty) = rnHsType doc ty
176 -- One reason for this case is that a type like Int#
177 -- starts of as (HsForAllTy Nothing [] Int), in case
178 -- there is some quantification. Now that we have quantified
179 -- and discovered there are no type variables, it's nicer to turn
180 -- it into plain Int. If it were Int# instead of Int, we'd actually
181 -- get an error, because the body of a genuine for-all is
184 rnForAll doc exp forall_tyvars ctxt ty
185 = bindTyVarsRn doc forall_tyvars $ \ new_tyvars ->
186 rnContext doc ctxt `thenM` \ new_ctxt ->
187 rnLHsType doc ty `thenM` \ new_ty ->
188 returnM (HsForAllTy exp new_tyvars new_ctxt new_ty)
189 -- Retain the same implicit/explicit flag as before
190 -- so that we can later print it correctly
194 %*********************************************************
196 \subsection{Fixities}
198 %*********************************************************
200 Infix types are read in a *right-associative* way, so that
205 mkHsOpTyRn rearranges where necessary. The two arguments
206 have already been renamed and rearranged. It's made rather tiresome
207 by the presence of ->
210 lookupTyFixityRn (L loc n)
211 = doptM Opt_GlasgowExts `thenM` \ glaExts ->
213 (addSrcSpan loc $ addWarn (infixTyConWarn n)) `thenM_`
216 -- Building (ty1 `op1` (ty21 `op2` ty22))
217 mkHsOpTyRn :: Located Name -> Fixity
218 -> LHsType Name -> LHsType Name
221 mkHsOpTyRn op1 fix1 ty1 ty2@(L loc (HsOpTy ty21 op2 ty22))
222 = lookupTyFixityRn op2 `thenM` \ fix2 ->
224 (nofix_error, associate_right) = compareFixity fix1 fix2
227 addErr (precParseErr (quotes (ppr op1),fix1)
228 (quotes (ppr op2),fix2)) `thenM_`
229 returnM (HsOpTy ty1 op1 ty2)
231 if not associate_right then
232 -- Rearrange to ((ty1 `op1` ty21) `op2` ty22)
233 mkHsOpTyRn op1 fix1 ty1 ty21 `thenM` \ new_ty ->
234 returnM (HsOpTy (L loc new_ty) op2 ty22) -- XXX loc is wrong
236 returnM (HsOpTy ty1 op1 ty2)
238 mkHsOpTyRn op fix ty1 ty2 -- Default case, no rearrangment
239 = returnM (HsOpTy ty1 op ty2)
242 %*********************************************************
244 \subsection{Contexts and predicates}
246 %*********************************************************
249 rnContext :: SDoc -> LHsContext RdrName -> RnM (LHsContext Name)
250 rnContext doc = wrapLocM (rnContext' doc)
252 rnContext' :: SDoc -> HsContext RdrName -> RnM (HsContext Name)
253 rnContext' doc ctxt = mappM (rnLPred doc) ctxt
255 rnLPred :: SDoc -> LHsPred RdrName -> RnM (LHsPred Name)
256 rnLPred doc = wrapLocM (rnPred doc)
258 rnPred doc (HsClassP clas tys)
259 = lookupOccRn clas `thenM` \ clas_name ->
260 rnLHsTypes doc tys `thenM` \ tys' ->
261 returnM (HsClassP clas_name tys')
263 rnPred doc (HsIParam n ty)
264 = newIPNameRn n `thenM` \ name ->
265 rnLHsType doc ty `thenM` \ ty' ->
266 returnM (HsIParam name ty')
270 *********************************************************
272 \subsection{Patterns}
274 *********************************************************
277 rnPatsAndThen :: HsMatchContext Name
280 -> ([LPat Name] -> RnM (a, FreeVars))
282 -- Bring into scope all the binders and type variables
283 -- bound by the patterns; then rename the patterns; then
284 -- do the thing inside.
286 -- Note that we do a single bindLocalsRn for all the
287 -- matches together, so that we spot the repeated variable in
290 rnPatsAndThen ctxt repUnused pats thing_inside
291 = bindPatSigTyVarsFV pat_sig_tys $
292 bindLocatedLocalsFV doc_pat bndrs $ \ new_bndrs ->
293 rnLPats pats `thenM` \ (pats', pat_fvs) ->
294 thing_inside pats' `thenM` \ (res, res_fvs) ->
297 unused_binders = filter (not . (`elemNameSet` res_fvs)) new_bndrs
300 then warnUnusedMatches unused_binders
301 else returnM ()) `thenM_`
302 returnM (res, res_fvs `plusFV` pat_fvs)
304 pat_sig_tys = collectSigTysFromPats pats
305 bndrs = collectLocatedPatsBinders pats
306 doc_pat = ptext SLIT("In") <+> pprMatchContext ctxt
308 rnLPats :: [LPat RdrName] -> RnM ([LPat Name], FreeVars)
309 rnLPats ps = mapFvRn rnLPat ps
311 rnLPat :: LPat RdrName -> RnM (LPat Name, FreeVars)
312 rnLPat = wrapLocFstM rnPat
314 -- -----------------------------------------------------------------------------
317 rnPat :: Pat RdrName -> RnM (Pat Name, FreeVars)
319 rnPat (WildPat _) = returnM (WildPat placeHolderType, emptyFVs)
322 = lookupBndrRn name `thenM` \ vname ->
323 returnM (VarPat vname, emptyFVs)
325 rnPat (SigPatIn pat ty)
326 = doptM Opt_GlasgowExts `thenM` \ glaExts ->
329 then rnLPat pat `thenM` \ (pat', fvs1) ->
330 rnHsTypeFVs doc ty `thenM` \ (ty', fvs2) ->
331 returnM (SigPatIn pat' ty', fvs1 `plusFV` fvs2)
333 else addErr (patSigErr ty) `thenM_`
334 rnPat (unLoc pat) -- XXX shouldn't throw away the loc
336 doc = text "In a pattern type-signature"
340 returnM (LitPat lit, emptyFVs)
342 rnPat (NPatIn lit mb_neg)
343 = rnOverLit lit `thenM` \ (lit', fvs1) ->
345 Nothing -> returnM (Nothing, emptyFVs)
346 Just _ -> lookupSyntaxName negateName `thenM` \ (neg, fvs) ->
347 returnM (Just neg, fvs)
348 ) `thenM` \ (mb_neg', fvs2) ->
349 returnM (NPatIn lit' mb_neg',
350 fvs1 `plusFV` fvs2 `addOneFV` eqClassName)
351 -- Needed to find equality on pattern
353 rnPat (NPlusKPatIn name lit _)
354 = rnOverLit lit `thenM` \ (lit', fvs1) ->
355 lookupLocatedBndrRn name `thenM` \ name' ->
356 lookupSyntaxName minusName `thenM` \ (minus, fvs2) ->
357 returnM (NPlusKPatIn name' lit' minus,
358 fvs1 `plusFV` fvs2 `addOneFV` integralClassName)
359 -- The Report says that n+k patterns must be in Integral
362 = rnLPat pat `thenM` \ (pat', fvs) ->
363 returnM (LazyPat pat', fvs)
365 rnPat (AsPat name pat)
366 = rnLPat pat `thenM` \ (pat', fvs) ->
367 lookupLocatedBndrRn name `thenM` \ vname ->
368 returnM (AsPat vname pat', fvs)
370 rnPat (ConPatIn con stuff) = rnConPat con stuff
374 = rnLPat pat `thenM` \ (pat', fvs) ->
375 returnM (ParPat pat', fvs)
377 rnPat (ListPat pats _)
378 = rnLPats pats `thenM` \ (patslist, fvs) ->
379 returnM (ListPat patslist placeHolderType, fvs `addOneFV` listTyCon_name)
381 rnPat (PArrPat pats _)
382 = rnLPats pats `thenM` \ (patslist, fvs) ->
383 returnM (PArrPat patslist placeHolderType,
384 fvs `plusFV` implicit_fvs `addOneFV` parrTyCon_name)
386 implicit_fvs = mkFVs [lengthPName, indexPName]
388 rnPat (TuplePat pats boxed)
389 = checkTupSize tup_size `thenM_`
390 rnLPats pats `thenM` \ (patslist, fvs) ->
391 returnM (TuplePat patslist boxed, fvs `addOneFV` tycon_name)
393 tup_size = length pats
394 tycon_name = tupleTyCon_name boxed tup_size
396 rnPat (TypePat name) =
397 rnHsTypeFVs (text "In a type pattern") name `thenM` \ (name', fvs) ->
398 returnM (TypePat name', fvs)
400 -- -----------------------------------------------------------------------------
403 rnConPat con (PrefixCon pats)
404 = lookupLocatedOccRn con `thenM` \ con' ->
405 rnLPats pats `thenM` \ (pats', fvs) ->
406 returnM (ConPatIn con' (PrefixCon pats'), fvs `addOneFV` unLoc con')
408 rnConPat con (RecCon rpats)
409 = lookupLocatedOccRn con `thenM` \ con' ->
410 rnRpats rpats `thenM` \ (rpats', fvs) ->
411 returnM (ConPatIn con' (RecCon rpats'), fvs `addOneFV` unLoc con')
413 rnConPat con (InfixCon pat1 pat2)
414 = lookupLocatedOccRn con `thenM` \ con' ->
415 rnLPat pat1 `thenM` \ (pat1', fvs1) ->
416 rnLPat pat2 `thenM` \ (pat2', fvs2) ->
417 lookupFixityRn (unLoc con') `thenM` \ fixity ->
418 mkConOpPatRn con' fixity pat1' pat2' `thenM` \ pat' ->
419 returnM (pat', fvs1 `plusFV` fvs2 `addOneFV` unLoc con')
421 -- -----------------------------------------------------------------------------
424 rnRpats :: [(Located RdrName, LPat RdrName)]
425 -> RnM ([(Located Name, LPat Name)], FreeVars)
427 = mappM_ field_dup_err dup_fields `thenM_`
428 mapFvRn rn_rpat rpats `thenM` \ (rpats', fvs) ->
429 returnM (rpats', fvs)
431 (_, dup_fields) = removeDups compare [ unLoc f | (f,_) <- rpats ]
433 field_dup_err dups = addErr (dupFieldErr "pattern" dups)
436 = lookupLocatedGlobalOccRn field `thenM` \ fieldname ->
437 rnLPat pat `thenM` \ (pat', fvs) ->
438 returnM ((fieldname, pat'), fvs `addOneFV` unLoc fieldname)
440 -- -----------------------------------------------------------------------------
443 mkConOpPatRn :: Located Name -> Fixity -> LPat Name -> LPat Name
446 mkConOpPatRn op2 fix2 p1@(L loc (ConPatIn op1 (InfixCon p11 p12))) p2
447 = lookupFixityRn (unLoc op1) `thenM` \ fix1 ->
449 (nofix_error, associate_right) = compareFixity fix1 fix2
452 addErr (precParseErr (ppr_op op1,fix1) (ppr_op op2,fix2)) `thenM_`
453 returnM (ConPatIn op2 (InfixCon p1 p2))
455 if associate_right then
456 mkConOpPatRn op2 fix2 p12 p2 `thenM` \ new_p ->
457 returnM (ConPatIn op1 (InfixCon p11 (L loc new_p))) -- XXX loc right?
459 returnM (ConPatIn op2 (InfixCon p1 p2))
461 mkConOpPatRn op fix p1 p2 -- Default case, no rearrangment
462 = ASSERT( not_op_pat (unLoc p2) )
463 returnM (ConPatIn op (InfixCon p1 p2))
465 not_op_pat (ConPatIn _ (InfixCon _ _)) = False
466 not_op_pat other = True
470 %************************************************************************
472 \subsubsection{Literals}
474 %************************************************************************
476 When literals occur we have to make sure
477 that the types and classes they involve
481 rnLit :: HsLit -> RnM ()
482 rnLit (HsChar c) = checkErr (inCharRange c) (bogusCharError c)
483 rnLit other = returnM ()
485 rnOverLit (HsIntegral i _)
486 = lookupSyntaxName fromIntegerName `thenM` \ (from_integer_name, fvs) ->
488 returnM (HsIntegral i from_integer_name, fvs)
490 extra_fvs = mkFVs [plusIntegerName, timesIntegerName]
491 -- Big integer literals are built, using + and *,
492 -- out of small integers (DsUtils.mkIntegerLit)
493 -- [NB: plusInteger, timesInteger aren't rebindable...
494 -- they are used to construct the argument to fromInteger,
495 -- which is the rebindable one.]
497 returnM (HsIntegral i from_integer_name, fvs `plusFV` extra_fvs)
499 rnOverLit (HsFractional i _)
500 = lookupSyntaxName fromRationalName `thenM` \ (from_rat_name, fvs) ->
502 extra_fvs = mkFVs [ratioDataConName, plusIntegerName, timesIntegerName]
503 -- We have to make sure that the Ratio type is imported with
504 -- its constructor, because literals of type Ratio t are
505 -- built with that constructor.
506 -- The Rational type is needed too, but that will come in
507 -- as part of the type for fromRational.
508 -- The plus/times integer operations may be needed to construct the numerator
509 -- and denominator (see DsUtils.mkIntegerLit)
511 returnM (HsFractional i from_rat_name, fvs `plusFV` extra_fvs)
516 %*********************************************************
520 %*********************************************************
523 checkTupSize :: Int -> RnM ()
524 checkTupSize tup_size
525 | tup_size <= mAX_TUPLE_SIZE
528 = addErr (sep [ptext SLIT("A") <+> int tup_size <> ptext SLIT("-tuple is too large for GHC"),
529 nest 2 (parens (ptext SLIT("max size is") <+> int mAX_TUPLE_SIZE)),
530 nest 2 (ptext SLIT("Workaround: use nested tuples or define a data type"))])
532 forAllWarn doc ty (L loc tyvar)
533 = ifOptM Opt_WarnUnusedMatches $
535 addWarn (sep [ptext SLIT("The universally quantified type variable") <+> quotes (ppr tyvar),
536 nest 4 (ptext SLIT("does not appear in the type") <+> quotes (ppr ty))]
542 = ptext SLIT("character literal out of range: '\\") <> char c <> char '\''
545 = hang (ptext SLIT("precedence parsing error"))
546 4 (hsep [ptext SLIT("cannot mix"), ppr_opfix op1, ptext SLIT("and"),
548 ptext SLIT("in the same infix expression")])
550 sectionPrecErr op arg_op section
551 = vcat [ptext SLIT("The operator") <+> ppr_opfix op <+> ptext SLIT("of a section"),
552 nest 4 (ptext SLIT("must have lower precedence than the operand") <+> ppr_opfix arg_op),
553 nest 4 (ptext SLIT("in the section:") <+> quotes (ppr section))]
556 = ftext FSLIT("Accepting non-standard infix type constructor") <+> quotes (ppr op)
559 = (ptext SLIT("Illegal signature in pattern:") <+> ppr ty)
560 $$ nest 4 (ptext SLIT("Use -fglasgow-exts to permit it"))
563 = hsep [ptext SLIT("duplicate field name"),
565 ptext SLIT("in record"), text str]
567 ppr_op op = quotes (ppr op) -- Here, op can be a Name or a (Var n), where n is a Name
568 ppr_opfix (pp_op, fixity) = pp_op <+> brackets (ppr fixity)