2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[RnSource]{Main pass of renamer}
7 module RnTypes ( rnHsType, rnContext,
8 rnHsSigType, rnHsTypeFVs,
9 rnPat, rnPatsAndThen, -- Here because it's not part
10 rnOverLit, litFVs, -- of any mutual recursion
11 precParseErr, sectionPrecErr, dupFieldErr, patSigErr, checkTupSize
14 import CmdLineOpts ( DynFlag(Opt_WarnUnusedMatches, Opt_GlasgowExts) )
17 import RdrHsSyn ( RdrNameContext, RdrNameHsType, RdrNamePat,
18 extractHsRhoRdrTyVars )
19 import RnHsSyn ( RenamedContext, RenamedHsType, RenamedPat,
21 parrTyCon_name, tupleTyCon_name, listTyCon_name, charTyCon_name )
22 import RnEnv ( lookupOccRn, lookupBndrRn, lookupSyntaxName, lookupGlobalOccRn,
23 bindTyVarsRn, lookupFixityRn, mapFvRn, newIPNameRn,
24 bindPatSigTyVarsFV, bindLocalsFV, warnUnusedMatches )
26 import RdrName ( elemLocalRdrEnv )
27 import PrelNames( eqStringName, eqClassName, integralClassName,
28 negateName, minusName, lengthPName, indexPName, plusIntegerName, fromIntegerName,
29 timesIntegerName, ratioDataConName, fromRationalName )
30 import Constants ( mAX_TUPLE_SIZE )
31 import TysWiredIn ( intTyCon )
32 import TysPrim ( charPrimTyCon, addrPrimTyCon, intPrimTyCon,
33 floatPrimTyCon, doublePrimTyCon )
34 import Name ( Name, NamedThing(..) )
37 import Literal ( inIntRange, inCharRange )
38 import BasicTypes ( compareFixity )
39 import ListSetOps ( removeDups )
42 #include "HsVersions.h"
45 These type renamers are in a separate module, rather than in (say) RnSource,
46 to break several loop.
48 %*********************************************************
50 \subsection{Renaming types}
52 %*********************************************************
55 rnHsTypeFVs :: SDoc -> RdrNameHsType -> RnM (RenamedHsType, FreeVars)
56 rnHsTypeFVs doc_str ty
57 = rnHsType doc_str ty `thenM` \ ty' ->
58 returnM (ty', extractHsTyNames ty')
60 rnHsSigType :: SDoc -> RdrNameHsType -> RnM RenamedHsType
61 -- rnHsSigType is used for source-language type signatures,
62 -- which use *implicit* universal quantification.
63 rnHsSigType doc_str ty
64 = rnHsType (text "In the type signature for" <+> doc_str) ty
67 rnHsType is here because we call it from loadInstDecl, and I didn't
68 want a gratuitous knot.
71 rnHsType :: SDoc -> RdrNameHsType -> RnM RenamedHsType
73 rnHsType doc (HsForAllTy Implicit _ ctxt ty)
74 -- Implicit quantifiction in source code (no kinds on tyvars)
75 -- Given the signature C => T we universally quantify
76 -- over FV(T) \ {in-scope-tyvars}
77 = getLocalRdrEnv `thenM` \ name_env ->
79 mentioned = extractHsRhoRdrTyVars ctxt ty
81 -- Don't quantify over type variables that are in scope;
82 -- when GlasgowExts is off, there usually won't be any, except for
84 -- class C a where { op :: a -> a }
85 forall_tyvars = filter (not . (`elemLocalRdrEnv` name_env)) mentioned
87 rnForAll doc Implicit (map UserTyVar forall_tyvars) ctxt ty
89 rnHsType doc (HsForAllTy Explicit forall_tyvars ctxt tau)
90 -- Explicit quantification.
91 -- Check that the forall'd tyvars are actually
92 -- mentioned in the type, and produce a warning if not
94 mentioned = extractHsRhoRdrTyVars ctxt tau
95 forall_tyvar_names = hsTyVarNames forall_tyvars
97 -- Explicitly quantified but not mentioned in ctxt or tau
98 warn_guys = filter (`notElem` mentioned) forall_tyvar_names
100 mappM_ (forAllWarn doc tau) warn_guys `thenM_`
101 rnForAll doc Explicit forall_tyvars ctxt tau
103 rnHsType doc (HsTyVar tyvar)
104 = lookupOccRn tyvar `thenM` \ tyvar' ->
105 returnM (HsTyVar tyvar')
107 rnHsType doc (HsOpTy ty1 op ty2)
108 = lookupOccRn op `thenM` \ op' ->
109 rnHsType doc ty1 `thenM` \ ty1' ->
110 rnHsType doc ty2 `thenM` \ ty2' ->
111 lookupTyFixityRn op' `thenM` \ fix ->
112 mkHsOpTyRn op' fix ty1' ty2'
114 rnHsType doc (HsParTy ty)
115 = rnHsType doc ty `thenM` \ ty' ->
116 returnM (HsParTy ty')
118 rnHsType doc (HsNumTy i)
119 | i == 1 = returnM (HsNumTy i)
120 | otherwise = addErr err_msg `thenM_` returnM (HsNumTy i)
122 err_msg = ptext SLIT("Only unit numeric type pattern is valid")
125 rnHsType doc (HsFunTy ty1 ty2)
126 = rnHsType doc ty1 `thenM` \ ty1' ->
127 -- Might find a for-all as the arg of a function type
128 rnHsType doc ty2 `thenM` \ ty2' ->
129 -- Or as the result. This happens when reading Prelude.hi
130 -- when we find return :: forall m. Monad m -> forall a. a -> m a
131 returnM (HsFunTy ty1' ty2')
133 rnHsType doc (HsListTy ty)
134 = rnHsType doc ty `thenM` \ ty' ->
135 returnM (HsListTy ty')
137 rnHsType doc (HsKindSig ty k)
138 = rnHsType doc ty `thenM` \ ty' ->
139 returnM (HsKindSig ty' k)
141 rnHsType doc (HsPArrTy ty)
142 = rnHsType doc ty `thenM` \ ty' ->
143 returnM (HsPArrTy ty')
145 -- Unboxed tuples are allowed to have poly-typed arguments. These
146 -- sometimes crop up as a result of CPR worker-wrappering dictionaries.
147 rnHsType doc (HsTupleTy tup_con tys)
148 = mappM (rnHsType doc) tys `thenM` \ tys' ->
149 returnM (HsTupleTy tup_con tys')
151 rnHsType doc (HsAppTy ty1 ty2)
152 = rnHsType doc ty1 `thenM` \ ty1' ->
153 rnHsType doc ty2 `thenM` \ ty2' ->
154 returnM (HsAppTy ty1' ty2')
156 rnHsType doc (HsPredTy pred)
157 = rnPred doc pred `thenM` \ pred' ->
158 returnM (HsPredTy pred')
160 rnHsTypes doc tys = mappM (rnHsType doc) tys
165 rnForAll doc exp [] [] ty = rnHsType doc ty
166 -- One reason for this case is that a type like Int#
167 -- starts of as (HsForAllTy Nothing [] Int), in case
168 -- there is some quantification. Now that we have quantified
169 -- and discovered there are no type variables, it's nicer to turn
170 -- it into plain Int. If it were Int# instead of Int, we'd actually
171 -- get an error, because the body of a genuine for-all is
174 rnForAll doc exp forall_tyvars ctxt ty
175 = bindTyVarsRn doc forall_tyvars $ \ new_tyvars ->
176 rnContext doc ctxt `thenM` \ new_ctxt ->
177 rnHsType doc ty `thenM` \ new_ty ->
178 returnM (HsForAllTy exp new_tyvars new_ctxt new_ty)
179 -- Retain the same implicit/explicit flag as before
180 -- so that we can later print it correctly
184 %*********************************************************
186 \subsection{Fixities}
188 %*********************************************************
190 Infix types are read in a *right-associative* way, so that
195 mkHsOpTyRn rearranges where necessary. The two arguments
196 have already been renamed and rearranged. It's made rather tiresome
197 by the presence of ->
201 = doptM Opt_GlasgowExts `thenM` \ glaExts ->
202 warnIf (not glaExts) (infixTyConWarn n) `thenM_`
205 -- Building (ty1 `op1` (ty21 `op2` ty22))
206 mkHsOpTyRn :: Name -> Fixity
207 -> RenamedHsType -> RenamedHsType
210 mkHsOpTyRn op1 fix1 ty1 ty2@(HsOpTy ty21 op2 ty22)
211 = lookupTyFixityRn op2 `thenM` \ fix2 ->
213 (nofix_error, associate_right) = compareFixity fix1 fix2
216 addErr (precParseErr (quotes (ppr op1),fix1)
217 (quotes (ppr op2),fix2)) `thenM_`
218 returnM (HsOpTy ty1 op1 ty2)
220 if not associate_right then
221 -- Rearrange to ((ty1 `op1` ty21) `op2` ty22)
222 mkHsOpTyRn op1 fix1 ty1 ty21 `thenM` \ new_ty ->
223 returnM (HsOpTy new_ty op2 ty22)
225 returnM (HsOpTy ty1 op1 ty2)
227 mkHsOpTyRn op fix ty1 ty2 -- Default case, no rearrangment
228 = returnM (HsOpTy ty1 op ty2)
231 %*********************************************************
233 \subsection{Contexts and predicates}
235 %*********************************************************
238 rnContext :: SDoc -> RdrNameContext -> RnM RenamedContext
239 rnContext doc ctxt = mappM (rnPred doc) ctxt
241 rnPred doc (HsClassP clas tys)
242 = lookupOccRn clas `thenM` \ clas_name ->
243 rnHsTypes doc tys `thenM` \ tys' ->
244 returnM (HsClassP clas_name tys')
246 rnPred doc (HsIParam n ty)
247 = newIPNameRn n `thenM` \ name ->
248 rnHsType doc ty `thenM` \ ty' ->
249 returnM (HsIParam name ty')
253 *********************************************************
255 \subsection{Patterns}
257 *********************************************************
260 rnPatsAndThen :: HsMatchContext Name
263 -> ([RenamedPat] -> RnM (a, FreeVars))
265 -- Bring into scope all the binders and type variables
266 -- bound by the patterns; then rename the patterns; then
267 -- do the thing inside.
269 -- Note that we do a single bindLocalsRn for all the
270 -- matches together, so that we spot the repeated variable in
273 rnPatsAndThen ctxt repUnused pats thing_inside
274 = bindPatSigTyVarsFV pat_sig_tys $
275 bindLocalsFV doc_pat bndrs $ \ new_bndrs ->
276 rnPats pats `thenM` \ (pats', pat_fvs) ->
277 thing_inside pats' `thenM` \ (res, res_fvs) ->
280 unused_binders = filter (not . (`elemNameSet` res_fvs)) new_bndrs
283 then warnUnusedMatches unused_binders
284 else returnM ()) `thenM_`
285 returnM (res, res_fvs `plusFV` pat_fvs)
287 pat_sig_tys = collectSigTysFromPats pats
288 bndrs = collectPatsBinders pats
289 doc_pat = ptext SLIT("In") <+> pprMatchContext ctxt
291 rnPats :: [RdrNamePat] -> RnM ([RenamedPat], FreeVars)
292 rnPats ps = mapFvRn rnPat ps
294 rnPat :: RdrNamePat -> RnM (RenamedPat, FreeVars)
296 rnPat (WildPat _) = returnM (WildPat placeHolderType, emptyFVs)
299 = lookupBndrRn name `thenM` \ vname ->
300 returnM (VarPat vname, emptyFVs)
302 rnPat (SigPatIn pat ty)
303 = doptM Opt_GlasgowExts `thenM` \ glaExts ->
306 then rnPat pat `thenM` \ (pat', fvs1) ->
307 rnHsTypeFVs doc ty `thenM` \ (ty', fvs2) ->
308 returnM (SigPatIn pat' ty', fvs1 `plusFV` fvs2)
310 else addErr (patSigErr ty) `thenM_`
313 doc = text "In a pattern type-signature"
315 rnPat (LitPat s@(HsString _))
316 = returnM (LitPat s, unitFV eqStringName)
319 = litFVs lit `thenM` \ fvs ->
320 returnM (LitPat lit, fvs)
322 rnPat (NPatIn lit mb_neg)
323 = rnOverLit lit `thenM` \ (lit', fvs1) ->
325 Nothing -> returnM (Nothing, emptyFVs)
326 Just _ -> lookupSyntaxName negateName `thenM` \ (neg, fvs) ->
327 returnM (Just neg, fvs)
328 ) `thenM` \ (mb_neg', fvs2) ->
329 returnM (NPatIn lit' mb_neg',
330 fvs1 `plusFV` fvs2 `addOneFV` eqClassName)
331 -- Needed to find equality on pattern
333 rnPat (NPlusKPatIn name lit _)
334 = rnOverLit lit `thenM` \ (lit', fvs1) ->
335 lookupBndrRn name `thenM` \ name' ->
336 lookupSyntaxName minusName `thenM` \ (minus, fvs2) ->
337 returnM (NPlusKPatIn name' lit' minus,
338 fvs1 `plusFV` fvs2 `addOneFV` integralClassName)
339 -- The Report says that n+k patterns must be in Integral
342 = rnPat pat `thenM` \ (pat', fvs) ->
343 returnM (LazyPat pat', fvs)
345 rnPat (AsPat name pat)
346 = rnPat pat `thenM` \ (pat', fvs) ->
347 lookupBndrRn name `thenM` \ vname ->
348 returnM (AsPat vname pat', fvs)
350 rnPat (ConPatIn con stuff) = rnConPat con stuff
354 = rnPat pat `thenM` \ (pat', fvs) ->
355 returnM (ParPat pat', fvs)
357 rnPat (ListPat pats _)
358 = rnPats pats `thenM` \ (patslist, fvs) ->
359 returnM (ListPat patslist placeHolderType, fvs `addOneFV` listTyCon_name)
361 rnPat (PArrPat pats _)
362 = rnPats pats `thenM` \ (patslist, fvs) ->
363 returnM (PArrPat patslist placeHolderType,
364 fvs `plusFV` implicit_fvs `addOneFV` parrTyCon_name)
366 implicit_fvs = mkFVs [lengthPName, indexPName]
368 rnPat (TuplePat pats boxed)
369 = checkTupSize tup_size `thenM_`
370 rnPats pats `thenM` \ (patslist, fvs) ->
371 returnM (TuplePat patslist boxed, fvs `addOneFV` tycon_name)
373 tup_size = length pats
374 tycon_name = tupleTyCon_name boxed tup_size
376 rnPat (TypePat name) =
377 rnHsTypeFVs (text "In a type pattern") name `thenM` \ (name', fvs) ->
378 returnM (TypePat name', fvs)
380 ------------------------------
381 rnConPat con (PrefixCon pats)
382 = lookupOccRn con `thenM` \ con' ->
383 rnPats pats `thenM` \ (pats', fvs) ->
384 returnM (ConPatIn con' (PrefixCon pats'), fvs `addOneFV` con')
386 rnConPat con (RecCon rpats)
387 = lookupOccRn con `thenM` \ con' ->
388 rnRpats rpats `thenM` \ (rpats', fvs) ->
389 returnM (ConPatIn con' (RecCon rpats'), fvs `addOneFV` con')
391 rnConPat con (InfixCon pat1 pat2)
392 = lookupOccRn con `thenM` \ con' ->
393 rnPat pat1 `thenM` \ (pat1', fvs1) ->
394 rnPat pat2 `thenM` \ (pat2', fvs2) ->
395 lookupFixityRn con' `thenM` \ fixity ->
396 mkConOpPatRn con' fixity pat1' pat2' `thenM` \ pat' ->
397 returnM (pat', fvs1 `plusFV` fvs2 `addOneFV` con')
399 ------------------------
401 = mappM_ field_dup_err dup_fields `thenM_`
402 mapFvRn rn_rpat rpats `thenM` \ (rpats', fvs) ->
403 returnM (rpats', fvs)
405 (_, dup_fields) = removeDups compare [ f | (f,_) <- rpats ]
407 field_dup_err dups = addErr (dupFieldErr "pattern" dups)
410 = lookupGlobalOccRn field `thenM` \ fieldname ->
411 rnPat pat `thenM` \ (pat', fvs) ->
412 returnM ((fieldname, pat'), fvs `addOneFV` fieldname)
416 mkConOpPatRn :: Name -> Fixity -> RenamedPat -> RenamedPat
419 mkConOpPatRn op2 fix2 p1@(ConPatIn op1 (InfixCon p11 p12)) p2
420 = lookupFixityRn op1 `thenM` \ fix1 ->
422 (nofix_error, associate_right) = compareFixity fix1 fix2
425 addErr (precParseErr (ppr_op op1,fix1) (ppr_op op2,fix2)) `thenM_`
426 returnM (ConPatIn op2 (InfixCon p1 p2))
428 if associate_right then
429 mkConOpPatRn op2 fix2 p12 p2 `thenM` \ new_p ->
430 returnM (ConPatIn op1 (InfixCon p11 new_p))
432 returnM (ConPatIn op2 (InfixCon p1 p2))
434 mkConOpPatRn op fix p1 p2 -- Default case, no rearrangment
435 = ASSERT( not_op_pat p2 )
436 returnM (ConPatIn op (InfixCon p1 p2))
438 not_op_pat (ConPatIn _ (InfixCon _ _)) = False
439 not_op_pat other = True
443 %************************************************************************
445 \subsubsection{Literals}
447 %************************************************************************
449 When literals occur we have to make sure
450 that the types and classes they involve
455 = checkErr (inCharRange c) (bogusCharError c) `thenM_`
456 returnM (unitFV charTyCon_name)
458 litFVs (HsCharPrim c) = returnM (unitFV (getName charPrimTyCon))
459 litFVs (HsString s) = returnM (mkFVs [listTyCon_name, charTyCon_name])
460 litFVs (HsStringPrim s) = returnM (unitFV (getName addrPrimTyCon))
461 litFVs (HsInt i) = returnM (unitFV (getName intTyCon))
462 litFVs (HsIntPrim i) = returnM (unitFV (getName intPrimTyCon))
463 litFVs (HsFloatPrim f) = returnM (unitFV (getName floatPrimTyCon))
464 litFVs (HsDoublePrim d) = returnM (unitFV (getName doublePrimTyCon))
465 litFVs lit = pprPanic "RnExpr.litFVs" (ppr lit) -- HsInteger and HsRat only appear
466 -- in post-typechecker translations
468 = ptext SLIT("character literal out of range: '\\") <> int c <> char '\''
470 rnOverLit (HsIntegral i _)
471 = lookupSyntaxName fromIntegerName `thenM` \ (from_integer_name, fvs) ->
473 returnM (HsIntegral i from_integer_name, fvs)
475 extra_fvs = mkFVs [plusIntegerName, timesIntegerName]
476 -- Big integer literals are built, using + and *,
477 -- out of small integers (DsUtils.mkIntegerLit)
478 -- [NB: plusInteger, timesInteger aren't rebindable...
479 -- they are used to construct the argument to fromInteger,
480 -- which is the rebindable one.]
482 returnM (HsIntegral i from_integer_name, fvs `plusFV` extra_fvs)
484 rnOverLit (HsFractional i _)
485 = lookupSyntaxName fromRationalName `thenM` \ (from_rat_name, fvs) ->
487 extra_fvs = mkFVs [ratioDataConName, plusIntegerName, timesIntegerName]
488 -- We have to make sure that the Ratio type is imported with
489 -- its constructor, because literals of type Ratio t are
490 -- built with that constructor.
491 -- The Rational type is needed too, but that will come in
492 -- as part of the type for fromRational.
493 -- The plus/times integer operations may be needed to construct the numerator
494 -- and denominator (see DsUtils.mkIntegerLit)
496 returnM (HsFractional i from_rat_name, fvs `plusFV` extra_fvs)
501 %*********************************************************
505 %*********************************************************
508 checkTupSize :: Int -> RnM ()
509 checkTupSize tup_size
510 | tup_size <= mAX_TUPLE_SIZE
513 = addErr (sep [ptext SLIT("A") <+> int tup_size <> ptext SLIT("-tuple is too large for GHC"),
514 nest 2 (parens (ptext SLIT("max size is") <+> int mAX_TUPLE_SIZE)),
515 nest 2 (ptext SLIT("Workaround: use nested tuples or define a data type"))])
517 forAllWarn doc ty tyvar
518 = ifOptM Opt_WarnUnusedMatches $
519 addWarn (sep [ptext SLIT("The universally quantified type variable") <+> quotes (ppr tyvar),
520 nest 4 (ptext SLIT("does not appear in the type") <+> quotes (ppr ty))]
526 = hang (ptext SLIT("precedence parsing error"))
527 4 (hsep [ptext SLIT("cannot mix"), ppr_opfix op1, ptext SLIT("and"),
529 ptext SLIT("in the same infix expression")])
531 sectionPrecErr op arg_op section
532 = vcat [ptext SLIT("The operator") <+> ppr_opfix op <+> ptext SLIT("of a section"),
533 nest 4 (ptext SLIT("must have lower precedence than the operand") <+> ppr_opfix arg_op),
534 nest 4 (ptext SLIT("in the section:") <+> quotes (ppr section))]
537 = ftext FSLIT("Accepting non-standard infix type constructor") <+> quotes (ppr op)
540 = (ptext SLIT("Illegal signature in pattern:") <+> ppr ty)
541 $$ nest 4 (ptext SLIT("Use -fglasgow-exts to permit it"))
543 dupFieldErr str (dup:rest)
544 = hsep [ptext SLIT("duplicate field name"),
546 ptext SLIT("in record"), text str]
548 ppr_op op = quotes (ppr op) -- Here, op can be a Name or a (Var n), where n is a Name
549 ppr_opfix (pp_op, fixity) = pp_op <+> brackets (ppr fixity)