2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[RnSource]{Main pass of renamer}
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
14 import DynFlags ( 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, geName, eqName,
29 negateName, minusName, lengthPName, indexPName,
30 plusIntegerName, fromIntegerName, timesIntegerName,
31 ratioDataConName, fromRationalName )
32 import TypeRep ( funTyCon )
33 import Constants ( mAX_TUPLE_SIZE )
35 import SrcLoc ( SrcSpan, Located(..), unLoc, noLoc )
38 import Literal ( inIntRange, inCharRange )
39 import BasicTypes ( compareFixity, Fixity(..), FixityDirection(..) )
40 import ListSetOps ( removeDups )
44 #include "HsVersions.h"
47 These type renamers are in a separate module, rather than in (say) RnSource,
48 to break several loop.
50 %*********************************************************
52 \subsection{Renaming types}
54 %*********************************************************
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')
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
69 rnHsType is here because we call it from loadInstDecl, and I didn't
70 want a gratuitous knot.
73 rnLHsType :: SDoc -> LHsType RdrName -> RnM (LHsType Name)
74 rnLHsType doc = wrapLocM (rnHsType doc)
76 rnHsType :: SDoc -> HsType RdrName -> RnM (HsType Name)
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 ->
84 mentioned = extractHsRhoRdrTyVars ctxt ty
86 -- Don't quantify over type variables that are in scope;
87 -- when GlasgowExts is off, there usually won't be any, except for
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 ]
93 rnForAll doc Implicit tyvar_bndrs ctxt ty
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
100 mentioned = map unLoc (extractHsRhoRdrTyVars ctxt tau)
101 forall_tyvar_names = hsLTyVarLocNames forall_tyvars
103 -- Explicitly quantified but not mentioned in ctxt or tau
104 warn_guys = filter ((`notElem` mentioned) . unLoc) forall_tyvar_names
106 mappM_ (forAllWarn doc tau) warn_guys `thenM_`
107 rnForAll doc Explicit forall_tyvars ctxt tau
109 rnHsType doc (HsTyVar tyvar)
110 = lookupOccRn tyvar `thenM` \ tyvar' ->
111 returnM (HsTyVar tyvar')
113 rnHsType doc (HsOpTy ty1 (L loc op) ty2)
115 lookupOccRn op `thenM` \ op' ->
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'
125 rnHsType doc (HsParTy ty)
126 = rnLHsType doc ty `thenM` \ ty' ->
127 returnM (HsParTy ty')
129 rnHsType doc (HsBangTy b ty)
130 = rnLHsType doc ty `thenM` \ ty' ->
131 returnM (HsBangTy b ty')
133 rnHsType doc (HsNumTy i)
134 | i == 1 = returnM (HsNumTy i)
135 | otherwise = addErr err_msg `thenM_` returnM (HsNumTy i)
137 err_msg = ptext SLIT("Only unit numeric type pattern is valid")
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
147 -- Check for fixity rearrangements
148 mkHsOpTyRn HsFunTy (ppr funTyCon) funTyFixity ty1' ty2'
150 rnHsType doc (HsListTy ty)
151 = rnLHsType doc ty `thenM` \ ty' ->
152 returnM (HsListTy ty')
154 rnHsType doc (HsKindSig ty k)
155 = rnLHsType doc ty `thenM` \ ty' ->
156 returnM (HsKindSig ty' k)
158 rnHsType doc (HsPArrTy ty)
159 = rnLHsType doc ty `thenM` \ ty' ->
160 returnM (HsPArrTy ty')
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')
168 rnHsType doc (HsAppTy ty1 ty2)
169 = rnLHsType doc ty1 `thenM` \ ty1' ->
170 rnLHsType doc ty2 `thenM` \ ty2' ->
171 returnM (HsAppTy ty1' ty2')
173 rnHsType doc (HsPredTy pred)
174 = rnPred doc pred `thenM` \ pred' ->
175 returnM (HsPredTy pred')
177 rnLHsTypes doc tys = mappM (rnLHsType doc) tys
182 rnForAll :: SDoc -> HsExplicitForAll -> [LHsTyVarBndr RdrName]
183 -> LHsContext RdrName -> LHsType RdrName -> RnM (HsType Name)
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
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
204 %*********************************************************
206 \subsection{Fixities}
208 %*********************************************************
210 Infix types are read in a *right-associative* way, so that
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.
221 -- Building (ty1 `op1` (ty21 `op2` ty22))
222 mkHsOpTyRn :: (LHsType Name -> LHsType Name -> HsType Name)
223 -> SDoc -> Fixity -> LHsType Name -> LHsType Name
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 }
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
236 mkHsOpTyRn mk1 pp_op1 fix1 ty1 ty2 -- Default case, no rearrangment
237 = return (mk1 ty1 ty2)
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
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) }
255 (nofix_error, associate_right) = compareFixity fix1 fix2
258 lookupTyFixityRn (L loc n)
259 = doptM Opt_GlasgowExts `thenM` \ glaExts ->
261 (setSrcSpan loc $ addWarn (infixTyConWarn n)) `thenM_`
265 funTyFixity = Fixity 0 InfixR -- Fixity of '->'
268 %*********************************************************
270 \subsection{Contexts and predicates}
272 %*********************************************************
275 rnContext :: SDoc -> LHsContext RdrName -> RnM (LHsContext Name)
276 rnContext doc = wrapLocM (rnContext' doc)
278 rnContext' :: SDoc -> HsContext RdrName -> RnM (HsContext Name)
279 rnContext' doc ctxt = mappM (rnLPred doc) ctxt
281 rnLPred :: SDoc -> LHsPred RdrName -> RnM (LHsPred Name)
282 rnLPred doc = wrapLocM (rnPred doc)
284 rnPred doc (HsClassP clas tys)
285 = lookupOccRn clas `thenM` \ clas_name ->
286 rnLHsTypes doc tys `thenM` \ tys' ->
287 returnM (HsClassP clas_name tys')
289 rnPred doc (HsIParam n ty)
290 = newIPNameRn n `thenM` \ name ->
291 rnLHsType doc ty `thenM` \ ty' ->
292 returnM (HsIParam name ty')
296 *********************************************************
298 \subsection{Patterns}
300 *********************************************************
303 rnPatsAndThen :: HsMatchContext Name
306 -> ([LPat Name] -> RnM (a, FreeVars))
308 -- Bring into scope all the binders and type variables
309 -- bound by the patterns; then rename the patterns; then
310 -- do the thing inside.
312 -- Note that we do a single bindLocalsRn for all the
313 -- matches together, so that we spot the repeated variable in
316 rnPatsAndThen ctxt repUnused pats thing_inside
317 = bindPatSigTyVarsFV pat_sig_tys $
318 bindLocatedLocalsFV doc_pat bndrs $ \ new_bndrs ->
319 rnLPats pats `thenM` \ (pats', pat_fvs) ->
320 thing_inside pats' `thenM` \ (res, res_fvs) ->
323 unused_binders = filter (not . (`elemNameSet` res_fvs)) new_bndrs
326 then warnUnusedMatches unused_binders
327 else returnM ()) `thenM_`
328 returnM (res, res_fvs `plusFV` pat_fvs)
330 pat_sig_tys = collectSigTysFromPats pats
331 bndrs = collectLocatedPatsBinders pats
332 doc_pat = ptext SLIT("In") <+> pprMatchContext ctxt
334 rnLPats :: [LPat RdrName] -> RnM ([LPat Name], FreeVars)
335 rnLPats ps = mapFvRn rnLPat ps
337 rnLPat :: LPat RdrName -> RnM (LPat Name, FreeVars)
338 rnLPat = wrapLocFstM rnPat
340 -- -----------------------------------------------------------------------------
343 rnPat :: Pat RdrName -> RnM (Pat Name, FreeVars)
345 rnPat (WildPat _) = returnM (WildPat placeHolderType, emptyFVs)
348 = lookupBndrRn name `thenM` \ vname ->
349 returnM (VarPat vname, emptyFVs)
351 rnPat (SigPatIn pat ty)
352 = doptM Opt_GlasgowExts `thenM` \ glaExts ->
355 then rnLPat pat `thenM` \ (pat', fvs1) ->
356 rnHsTypeFVs doc ty `thenM` \ (ty', fvs2) ->
357 returnM (SigPatIn pat' ty', fvs1 `plusFV` fvs2)
359 else addErr (patSigErr ty) `thenM_`
360 rnPat (unLoc pat) -- XXX shouldn't throw away the loc
362 doc = text "In a pattern type-signature"
366 returnM (LitPat lit, emptyFVs)
368 rnPat (NPat lit mb_neg eq _)
369 = rnOverLit lit `thenM` \ (lit', fvs1) ->
371 Nothing -> returnM (Nothing, emptyFVs)
372 Just _ -> lookupSyntaxName negateName `thenM` \ (neg, fvs) ->
373 returnM (Just neg, fvs)
374 ) `thenM` \ (mb_neg', fvs2) ->
375 lookupSyntaxName eqName `thenM` \ (eq', fvs3) ->
376 returnM (NPat lit' mb_neg' eq' placeHolderType,
377 fvs1 `plusFV` fvs2 `plusFV` fvs3 `addOneFV` eqClassName)
378 -- Needed to find equality on pattern
380 rnPat (NPlusKPat name lit _ _)
381 = rnOverLit lit `thenM` \ (lit', fvs1) ->
382 lookupLocatedBndrRn name `thenM` \ name' ->
383 lookupSyntaxName minusName `thenM` \ (minus, fvs2) ->
384 lookupSyntaxName geName `thenM` \ (ge, fvs3) ->
385 returnM (NPlusKPat name' lit' ge minus,
386 fvs1 `plusFV` fvs2 `plusFV` fvs3 `addOneFV` integralClassName)
387 -- The Report says that n+k patterns must be in Integral
390 = rnLPat pat `thenM` \ (pat', fvs) ->
391 returnM (LazyPat pat', fvs)
393 rnPat (AsPat name pat)
394 = rnLPat pat `thenM` \ (pat', fvs) ->
395 lookupLocatedBndrRn name `thenM` \ vname ->
396 returnM (AsPat vname pat', fvs)
398 rnPat (ConPatIn con stuff) = rnConPat con stuff
402 = rnLPat pat `thenM` \ (pat', fvs) ->
403 returnM (ParPat pat', fvs)
405 rnPat (ListPat pats _)
406 = rnLPats pats `thenM` \ (patslist, fvs) ->
407 returnM (ListPat patslist placeHolderType, fvs `addOneFV` listTyCon_name)
409 rnPat (PArrPat pats _)
410 = rnLPats pats `thenM` \ (patslist, fvs) ->
411 returnM (PArrPat patslist placeHolderType,
412 fvs `plusFV` implicit_fvs `addOneFV` parrTyCon_name)
414 implicit_fvs = mkFVs [lengthPName, indexPName]
416 rnPat (TuplePat pats boxed)
417 = checkTupSize tup_size `thenM_`
418 rnLPats pats `thenM` \ (patslist, fvs) ->
419 returnM (TuplePat patslist boxed, fvs `addOneFV` tycon_name)
421 tup_size = length pats
422 tycon_name = tupleTyCon_name boxed tup_size
424 rnPat (TypePat name) =
425 rnHsTypeFVs (text "In a type pattern") name `thenM` \ (name', fvs) ->
426 returnM (TypePat name', fvs)
428 -- -----------------------------------------------------------------------------
431 rnConPat con (PrefixCon pats)
432 = lookupLocatedOccRn con `thenM` \ con' ->
433 rnLPats pats `thenM` \ (pats', fvs) ->
434 returnM (ConPatIn con' (PrefixCon pats'), fvs `addOneFV` unLoc con')
436 rnConPat con (RecCon rpats)
437 = lookupLocatedOccRn con `thenM` \ con' ->
438 rnRpats rpats `thenM` \ (rpats', fvs) ->
439 returnM (ConPatIn con' (RecCon rpats'), fvs `addOneFV` unLoc con')
441 rnConPat con (InfixCon pat1 pat2)
442 = lookupLocatedOccRn con `thenM` \ con' ->
443 rnLPat pat1 `thenM` \ (pat1', fvs1) ->
444 rnLPat pat2 `thenM` \ (pat2', fvs2) ->
445 lookupFixityRn (unLoc con') `thenM` \ fixity ->
446 mkConOpPatRn con' fixity pat1' pat2' `thenM` \ pat' ->
447 returnM (pat', fvs1 `plusFV` fvs2 `addOneFV` unLoc con')
449 -- -----------------------------------------------------------------------------
452 rnRpats :: [(Located RdrName, LPat RdrName)]
453 -> RnM ([(Located Name, LPat Name)], FreeVars)
455 = mappM_ field_dup_err dup_fields `thenM_`
456 mapFvRn rn_rpat rpats `thenM` \ (rpats', fvs) ->
457 returnM (rpats', fvs)
459 (_, dup_fields) = removeDups compare [ unLoc f | (f,_) <- rpats ]
461 field_dup_err dups = addErr (dupFieldErr "pattern" dups)
464 = lookupLocatedGlobalOccRn field `thenM` \ fieldname ->
465 rnLPat pat `thenM` \ (pat', fvs) ->
466 returnM ((fieldname, pat'), fvs `addOneFV` unLoc fieldname)
468 -- -----------------------------------------------------------------------------
471 mkConOpPatRn :: Located Name -> Fixity -> LPat Name -> LPat Name
474 mkConOpPatRn op2 fix2 p1@(L loc (ConPatIn op1 (InfixCon p11 p12))) p2
475 = lookupFixityRn (unLoc op1) `thenM` \ fix1 ->
477 (nofix_error, associate_right) = compareFixity fix1 fix2
480 addErr (precParseErr (ppr_op op1,fix1) (ppr_op op2,fix2)) `thenM_`
481 returnM (ConPatIn op2 (InfixCon p1 p2))
483 if associate_right then
484 mkConOpPatRn op2 fix2 p12 p2 `thenM` \ new_p ->
485 returnM (ConPatIn op1 (InfixCon p11 (L loc new_p))) -- XXX loc right?
487 returnM (ConPatIn op2 (InfixCon p1 p2))
489 mkConOpPatRn op fix p1 p2 -- Default case, no rearrangment
490 = ASSERT( not_op_pat (unLoc p2) )
491 returnM (ConPatIn op (InfixCon p1 p2))
493 not_op_pat (ConPatIn _ (InfixCon _ _)) = False
494 not_op_pat other = True
498 %************************************************************************
500 \subsubsection{Literals}
502 %************************************************************************
504 When literals occur we have to make sure
505 that the types and classes they involve
509 rnLit :: HsLit -> RnM ()
510 rnLit (HsChar c) = checkErr (inCharRange c) (bogusCharError c)
511 rnLit other = returnM ()
513 rnOverLit (HsIntegral i _)
514 = lookupSyntaxName fromIntegerName `thenM` \ (from_integer_name, fvs) ->
516 returnM (HsIntegral i from_integer_name, fvs)
518 extra_fvs = mkFVs [plusIntegerName, timesIntegerName]
519 -- Big integer literals are built, using + and *,
520 -- out of small integers (DsUtils.mkIntegerLit)
521 -- [NB: plusInteger, timesInteger aren't rebindable...
522 -- they are used to construct the argument to fromInteger,
523 -- which is the rebindable one.]
525 returnM (HsIntegral i from_integer_name, fvs `plusFV` extra_fvs)
527 rnOverLit (HsFractional i _)
528 = lookupSyntaxName fromRationalName `thenM` \ (from_rat_name, fvs) ->
530 extra_fvs = mkFVs [ratioDataConName, plusIntegerName, timesIntegerName]
531 -- We have to make sure that the Ratio type is imported with
532 -- its constructor, because literals of type Ratio t are
533 -- built with that constructor.
534 -- The Rational type is needed too, but that will come in
535 -- as part of the type for fromRational.
536 -- The plus/times integer operations may be needed to construct the numerator
537 -- and denominator (see DsUtils.mkIntegerLit)
539 returnM (HsFractional i from_rat_name, fvs `plusFV` extra_fvs)
544 %*********************************************************
548 %*********************************************************
551 checkTupSize :: Int -> RnM ()
552 checkTupSize tup_size
553 | tup_size <= mAX_TUPLE_SIZE
556 = addErr (sep [ptext SLIT("A") <+> int tup_size <> ptext SLIT("-tuple is too large for GHC"),
557 nest 2 (parens (ptext SLIT("max size is") <+> int mAX_TUPLE_SIZE)),
558 nest 2 (ptext SLIT("Workaround: use nested tuples or define a data type"))])
560 forAllWarn doc ty (L loc tyvar)
561 = ifOptM Opt_WarnUnusedMatches $
563 addWarn (sep [ptext SLIT("The universally quantified type variable") <+> quotes (ppr tyvar),
564 nest 4 (ptext SLIT("does not appear in the type") <+> quotes (ppr ty))]
570 = ptext SLIT("character literal out of range: '\\") <> char c <> char '\''
573 = hang (ptext SLIT("precedence parsing error"))
574 4 (hsep [ptext SLIT("cannot mix"), ppr_opfix op1, ptext SLIT("and"),
576 ptext SLIT("in the same infix expression")])
578 sectionPrecErr op arg_op section
579 = vcat [ptext SLIT("The operator") <+> ppr_opfix op <+> ptext SLIT("of a section"),
580 nest 4 (ptext SLIT("must have lower precedence than the operand") <+> ppr_opfix arg_op),
581 nest 4 (ptext SLIT("in the section:") <+> quotes (ppr section))]
584 = vcat [ftext FSLIT("Accepting non-standard infix type constructor") <+> quotes (ppr op),
585 ftext FSLIT("Use -fglasgow-exts to avoid this warning")]
588 = (ptext SLIT("Illegal signature in pattern:") <+> ppr ty)
589 $$ nest 4 (ptext SLIT("Use -fglasgow-exts to permit it"))
592 = hsep [ptext SLIT("duplicate field name"),
594 ptext SLIT("in record"), text str]
596 ppr_op op = quotes (ppr op) -- Here, op can be a Name or a (Var n), where n is a Name
597 ppr_opfix (pp_op, fixity) = pp_op <+> brackets (ppr fixity)