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, rnHsSigTypeFVs,
9 rnPat, rnPats, rnPatsAndThen, -- Here because it's not part
10 rnOverLit, litFVs, -- of any mutual recursion
11 precParseErr, sectionPrecErr, dupFieldErr, patSigErr
14 import CmdLineOpts ( DynFlag(Opt_WarnMisc, Opt_WarnUnusedMatches, Opt_GlasgowExts) )
17 import RdrHsSyn ( RdrNameContext, RdrNameHsType, RdrNamePat,
18 extractHsTyRdrTyVars, extractHsCtxtRdrTyVars )
19 import RnHsSyn ( RenamedContext, RenamedHsType, RenamedPat,
21 parrTyCon_name, tupleTyCon_name, listTyCon_name, charTyCon_name )
22 import RnEnv ( lookupOccRn, lookupBndrRn, lookupSyntaxName, lookupGlobalOccRn,
23 newIPName, bindTyVarsRn, lookupFixityRn, mapFvRn,
24 bindPatSigTyVars, bindLocalsFVRn, warnUnusedMatches )
27 import PrelNames( cCallishClassKeys, eqStringName, eqClassName, ordClassName,
28 negateName, minusName, lengthPName, indexPName, plusIntegerName, fromIntegerName,
29 timesIntegerName, ratioDataConName, fromRationalName, cCallableClassName )
30 import TysWiredIn ( intTyCon )
31 import TysPrim ( charPrimTyCon, addrPrimTyCon, intPrimTyCon,
32 floatPrimTyCon, doublePrimTyCon )
33 import RdrName ( elemRdrEnv )
34 import Name ( Name, NamedThing(..) )
36 import Unique ( Uniquable(..) )
38 import Literal ( inIntRange, inCharRange )
39 import BasicTypes ( compareFixity, arrowFixity )
41 import ListSetOps ( removeDupsEq, 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 -> RdrNameHsType -> RnM (RenamedHsType, FreeVars)
58 rnHsTypeFVs doc_str ty
59 = rnHsType doc_str ty `thenM` \ ty' ->
60 returnM (ty', extractHsTyNames ty')
62 rnHsSigTypeFVs :: SDoc -> RdrNameHsType -> RnM (RenamedHsType, FreeVars)
63 rnHsSigTypeFVs doc_str ty
64 = rnHsSigType doc_str ty `thenM` \ ty' ->
65 returnM (ty', extractHsTyNames ty')
67 rnHsSigType :: SDoc -> RdrNameHsType -> RnM RenamedHsType
68 -- rnHsSigType is used for source-language type signatures,
69 -- which use *implicit* universal quantification.
70 rnHsSigType doc_str ty
71 = rnHsType (text "In the type signature for" <+> doc_str) ty
74 rnHsType is here because we call it from loadInstDecl, and I didn't
75 want a gratuitous knot.
78 rnHsType :: SDoc -> RdrNameHsType -> RnM RenamedHsType
80 rnHsType doc (HsForAllTy Nothing ctxt ty)
81 -- Implicit quantifiction in source code (no kinds on tyvars)
82 -- Given the signature C => T we universally quantify
83 -- over FV(T) \ {in-scope-tyvars}
84 = getLocalRdrEnv `thenM` \ name_env ->
86 mentioned_in_tau = extractHsTyRdrTyVars ty
87 mentioned_in_ctxt = extractHsCtxtRdrTyVars ctxt
88 mentioned = nub (mentioned_in_tau ++ mentioned_in_ctxt)
90 -- Don't quantify over type variables that are in scope;
91 -- when GlasgowExts is off, there usually won't be any, except for
93 -- class C a where { op :: a -> a }
94 forall_tyvars = filter (not . (`elemRdrEnv` name_env)) mentioned
96 rnForAll doc (map UserTyVar forall_tyvars) ctxt ty
98 rnHsType doc (HsForAllTy (Just forall_tyvars) ctxt tau)
99 -- Explicit quantification.
100 -- Check that the forall'd tyvars are actually
101 -- mentioned in the type, and produce a warning if not
103 mentioned_in_tau = extractHsTyRdrTyVars tau
104 mentioned_in_ctxt = extractHsCtxtRdrTyVars ctxt
105 mentioned = nub (mentioned_in_tau ++ mentioned_in_ctxt)
106 forall_tyvar_names = hsTyVarNames forall_tyvars
108 -- Explicitly quantified but not mentioned in ctxt or tau
109 warn_guys = filter (`notElem` mentioned) forall_tyvar_names
111 mappM_ (forAllWarn doc tau) warn_guys `thenM_`
112 rnForAll doc forall_tyvars ctxt tau
114 rnHsType doc (HsTyVar tyvar)
115 = lookupOccRn tyvar `thenM` \ tyvar' ->
116 returnM (HsTyVar tyvar')
118 rnHsType doc (HsOpTy ty1 op ty2)
120 HsArrow -> returnM HsArrow
121 HsTyOp n -> lookupOccRn n `thenM` \ n' ->
124 rnHsType doc ty1 `thenM` \ ty1' ->
125 rnHsType doc ty2 `thenM` \ ty2' ->
126 lookupTyFixityRn op' `thenM` \ fix ->
127 mkHsOpTyRn op' fix ty1' ty2'
129 rnHsType doc (HsParTy ty)
130 = rnHsType doc ty `thenM` \ ty' ->
131 returnM (HsParTy 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 = rnHsType doc ty1 `thenM` \ ty1' ->
142 -- Might find a for-all as the arg of a function type
143 rnHsType 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 returnM (HsFunTy ty1' ty2')
148 rnHsType doc (HsListTy ty)
149 = rnHsType doc ty `thenM` \ ty' ->
150 returnM (HsListTy ty')
152 rnHsType doc (HsKindSig ty k)
153 = rnHsType doc ty `thenM` \ ty' ->
154 returnM (HsKindSig ty' k)
156 rnHsType doc (HsPArrTy ty)
157 = rnHsType doc ty `thenM` \ ty' ->
158 returnM (HsPArrTy ty')
160 -- Unboxed tuples are allowed to have poly-typed arguments. These
161 -- sometimes crop up as a result of CPR worker-wrappering dictionaries.
162 rnHsType doc (HsTupleTy tup_con tys)
163 = mappM (rnHsType doc) tys `thenM` \ tys' ->
164 returnM (HsTupleTy tup_con tys')
166 rnHsType doc (HsAppTy ty1 ty2)
167 = rnHsType doc ty1 `thenM` \ ty1' ->
168 rnHsType doc ty2 `thenM` \ ty2' ->
169 returnM (HsAppTy ty1' ty2')
171 rnHsType doc (HsPredTy pred)
172 = rnPred doc pred `thenM` \ pred' ->
173 returnM (HsPredTy pred')
175 rnHsTypes doc tys = mappM (rnHsType doc) tys
180 rnForAll doc forall_tyvars ctxt ty
181 = bindTyVarsRn doc forall_tyvars $ \ new_tyvars ->
182 rnContext doc ctxt `thenM` \ new_ctxt ->
183 rnHsType doc ty `thenM` \ new_ty ->
184 returnM (mkHsForAllTy (Just new_tyvars) new_ctxt new_ty)
188 %*********************************************************
190 \subsection{Fixities}
192 %*********************************************************
194 Infix types are read in a *right-associative* way, so that
199 mkHsOpTyRn rearranges where necessary. The two arguments
200 have already been renamed and rearranged. It's made rather tiresome
201 by the presence of ->
204 lookupTyFixityRn HsArrow = returnM arrowFixity
205 lookupTyFixityRn (HsTyOp n)
206 = doptM Opt_GlasgowExts `thenM` \ glaExts ->
207 warnIf (not glaExts) (infixTyConWarn n) `thenM_`
210 -- Building (ty1 `op1` (ty21 `op2` ty22))
211 mkHsOpTyRn :: HsTyOp Name -> Fixity
212 -> RenamedHsType -> RenamedHsType
215 mkHsOpTyRn op1 fix1 ty1 ty2@(HsOpTy ty21 op2 ty22)
216 = lookupTyFixityRn op2 `thenM` \ fix2 ->
218 (nofix_error, associate_right) = compareFixity fix1 fix2
221 addErr (precParseErr (quotes (ppr op1),fix1)
222 (quotes (ppr op2),fix2)) `thenM_`
223 returnM (HsOpTy ty1 op1 ty2)
225 if not associate_right then
226 -- Rearrange to ((ty1 `op1` ty21) `op2` ty22)
227 mkHsOpTyRn op1 fix1 ty1 ty21 `thenM` \ new_ty ->
228 returnM (HsOpTy new_ty op2 ty22)
230 returnM (HsOpTy ty1 op1 ty2)
232 mkHsOpTyRn op fix ty1 ty2 -- Default case, no rearrangment
233 = returnM (HsOpTy ty1 op ty2)
235 mkHsFunTyRn ty1 ty2 -- Precedence of function arrow is 0
236 = returnM (HsFunTy ty1 ty2) -- so no rearrangement reqd. Change
237 -- this if fixity of -> increases.
239 not_op_ty (HsOpTy _ _ _) = False
240 not_op_ty other = True
243 %*********************************************************
245 \subsection{Contexts and predicates}
247 %*********************************************************
250 rnContext :: SDoc -> RdrNameContext -> RnM RenamedContext
252 = mappM rn_pred ctxt `thenM` \ theta ->
254 -- Check for duplicate assertions
255 -- If this isn't an error, then it ought to be:
256 ifOptM Opt_WarnMisc (
258 (_, dups) = removeDupsEq theta
259 -- We only have equality, not ordering
261 mappM_ (addWarn . dupClassAssertWarn theta) dups
266 --Someone discovered that @CCallable@ and @CReturnable@
267 -- could be used in contexts such as:
268 -- foo :: CCallable a => a -> PrimIO Int
269 -- Doing this utterly wrecks the whole point of introducing these
270 -- classes so we specifically check that this isn't being done.
271 rn_pred pred = rnPred doc pred `thenM` \ pred'->
272 checkErr (not (bad_pred pred'))
273 (naughtyCCallContextErr pred') `thenM_`
276 bad_pred (HsClassP clas _) = getUnique clas `elem` cCallishClassKeys
277 bad_pred other = False
280 rnPred doc (HsClassP clas tys)
281 = lookupOccRn clas `thenM` \ clas_name ->
282 rnHsTypes doc tys `thenM` \ tys' ->
283 returnM (HsClassP clas_name tys')
285 rnPred doc (HsIParam n ty)
286 = newIPName n `thenM` \ name ->
287 rnHsType doc ty `thenM` \ ty' ->
288 returnM (HsIParam name ty')
292 *********************************************************
294 \subsection{Patterns}
296 *********************************************************
299 rnPatsAndThen :: HsMatchContext Name
301 -> ([RenamedPat] -> RnM (a, FreeVars))
303 -- Bring into scope all the binders and type variables
304 -- bound by the patterns; then rename the patterns; then
305 -- do the thing inside.
307 -- Note that we do a single bindLocalsRn for all the
308 -- matches together, so that we spot the repeated variable in
311 rnPatsAndThen ctxt pats thing_inside
312 = bindPatSigTyVars pat_sig_tys $
313 bindLocalsFVRn doc_pat bndrs $ \ new_bndrs ->
314 rnPats pats `thenM` \ (pats', pat_fvs) ->
315 thing_inside pats' `thenM` \ (res, res_fvs) ->
318 unused_binders = filter (not . (`elemNameSet` res_fvs)) new_bndrs
320 warnUnusedMatches unused_binders `thenM_`
322 returnM (res, res_fvs `plusFV` pat_fvs)
324 pat_sig_tys = collectSigTysFromPats pats
325 bndrs = collectPatsBinders pats
326 doc_pat = ptext SLIT("In") <+> pprMatchContext ctxt
328 rnPats :: [RdrNamePat] -> RnM ([RenamedPat], FreeVars)
329 rnPats ps = mapFvRn rnPat ps
331 rnPat :: RdrNamePat -> RnM (RenamedPat, FreeVars)
333 rnPat (WildPat _) = returnM (WildPat placeHolderType, emptyFVs)
336 = lookupBndrRn name `thenM` \ vname ->
337 returnM (VarPat vname, emptyFVs)
339 rnPat (SigPatIn pat ty)
340 = doptM Opt_GlasgowExts `thenM` \ glaExts ->
343 then rnPat pat `thenM` \ (pat', fvs1) ->
344 rnHsTypeFVs doc ty `thenM` \ (ty', fvs2) ->
345 returnM (SigPatIn pat' ty', fvs1 `plusFV` fvs2)
347 else addErr (patSigErr ty) `thenM_`
350 doc = text "In a pattern type-signature"
352 rnPat (LitPat s@(HsString _))
353 = returnM (LitPat s, unitFV eqStringName)
356 = litFVs lit `thenM` \ fvs ->
357 returnM (LitPat lit, fvs)
359 rnPat (NPatIn lit mb_neg)
360 = rnOverLit lit `thenM` \ (lit', fvs1) ->
362 Nothing -> returnM (Nothing, emptyFVs)
363 Just _ -> lookupSyntaxName negateName `thenM` \ (neg, fvs) ->
364 returnM (Just neg, fvs)
365 ) `thenM` \ (mb_neg', fvs2) ->
366 returnM (NPatIn lit' mb_neg',
367 fvs1 `plusFV` fvs2 `addOneFV` eqClassName)
368 -- Needed to find equality on pattern
370 rnPat (NPlusKPatIn name lit _)
371 = rnOverLit lit `thenM` \ (lit', fvs1) ->
372 lookupBndrRn name `thenM` \ name' ->
373 lookupSyntaxName minusName `thenM` \ (minus, fvs2) ->
374 returnM (NPlusKPatIn name' lit' minus,
375 fvs1 `plusFV` fvs2 `addOneFV` ordClassName)
378 = rnPat pat `thenM` \ (pat', fvs) ->
379 returnM (LazyPat pat', fvs)
381 rnPat (AsPat name pat)
382 = rnPat pat `thenM` \ (pat', fvs) ->
383 lookupBndrRn name `thenM` \ vname ->
384 returnM (AsPat vname pat', fvs)
386 rnPat (ConPatIn con stuff) = rnConPat con stuff
390 = rnPat pat `thenM` \ (pat', fvs) ->
391 returnM (ParPat pat', fvs)
393 rnPat (ListPat pats _)
394 = rnPats pats `thenM` \ (patslist, fvs) ->
395 returnM (ListPat patslist placeHolderType, fvs `addOneFV` listTyCon_name)
397 rnPat (PArrPat pats _)
398 = rnPats pats `thenM` \ (patslist, fvs) ->
399 returnM (PArrPat patslist placeHolderType,
400 fvs `plusFV` implicit_fvs `addOneFV` parrTyCon_name)
402 implicit_fvs = mkFVs [lengthPName, indexPName]
404 rnPat (TuplePat pats boxed)
405 = rnPats pats `thenM` \ (patslist, fvs) ->
406 returnM (TuplePat patslist boxed, fvs `addOneFV` tycon_name)
408 tycon_name = tupleTyCon_name boxed (length pats)
410 rnPat (TypePat name) =
411 rnHsTypeFVs (text "In a type pattern") name `thenM` \ (name', fvs) ->
412 returnM (TypePat name', fvs)
414 ------------------------------
415 rnConPat con (PrefixCon pats)
416 = lookupOccRn con `thenM` \ con' ->
417 rnPats pats `thenM` \ (pats', fvs) ->
418 returnM (ConPatIn con' (PrefixCon pats'), fvs `addOneFV` con')
420 rnConPat con (RecCon rpats)
421 = lookupOccRn con `thenM` \ con' ->
422 rnRpats rpats `thenM` \ (rpats', fvs) ->
423 returnM (ConPatIn con' (RecCon rpats'), fvs `addOneFV` con')
425 rnConPat con (InfixCon pat1 pat2)
426 = lookupOccRn con `thenM` \ con' ->
427 rnPat pat1 `thenM` \ (pat1', fvs1) ->
428 rnPat pat2 `thenM` \ (pat2', fvs2) ->
430 getModeRn `thenM` \ mode ->
431 -- See comments with rnExpr (OpApp ...)
432 (if isInterfaceMode mode
433 then returnM (ConPatIn con' (InfixCon pat1' pat2'))
434 else lookupFixityRn con' `thenM` \ fixity ->
435 mkConOpPatRn con' fixity pat1' pat2'
437 returnM (pat', fvs1 `plusFV` fvs2 `addOneFV` con')
439 ------------------------
441 = mappM_ field_dup_err dup_fields `thenM_`
442 mapFvRn rn_rpat rpats `thenM` \ (rpats', fvs) ->
443 returnM (rpats', fvs)
445 (_, dup_fields) = removeDups compare [ f | (f,_) <- rpats ]
447 field_dup_err dups = addErr (dupFieldErr "pattern" dups)
450 = lookupGlobalOccRn field `thenM` \ fieldname ->
451 rnPat pat `thenM` \ (pat', fvs) ->
452 returnM ((fieldname, pat'), fvs `addOneFV` fieldname)
456 mkConOpPatRn :: Name -> Fixity -> RenamedPat -> RenamedPat
459 mkConOpPatRn op2 fix2 p1@(ConPatIn op1 (InfixCon p11 p12)) p2
460 = lookupFixityRn op1 `thenM` \ fix1 ->
462 (nofix_error, associate_right) = compareFixity fix1 fix2
465 addErr (precParseErr (ppr_op op1,fix1) (ppr_op op2,fix2)) `thenM_`
466 returnM (ConPatIn op2 (InfixCon p1 p2))
468 if associate_right then
469 mkConOpPatRn op2 fix2 p12 p2 `thenM` \ new_p ->
470 returnM (ConPatIn op1 (InfixCon p11 new_p))
472 returnM (ConPatIn op2 (InfixCon p1 p2))
474 mkConOpPatRn op fix p1 p2 -- Default case, no rearrangment
475 = ASSERT( not_op_pat p2 )
476 returnM (ConPatIn op (InfixCon p1 p2))
478 not_op_pat (ConPatIn _ (InfixCon _ _)) = False
479 not_op_pat other = True
483 %************************************************************************
485 \subsubsection{Literals}
487 %************************************************************************
489 When literals occur we have to make sure
490 that the types and classes they involve
495 = checkErr (inCharRange c) (bogusCharError c) `thenM_`
496 returnM (unitFV charTyCon_name)
498 litFVs (HsCharPrim c) = returnM (unitFV (getName charPrimTyCon))
499 litFVs (HsString s) = returnM (mkFVs [listTyCon_name, charTyCon_name])
500 litFVs (HsStringPrim s) = returnM (unitFV (getName addrPrimTyCon))
501 litFVs (HsInt i) = returnM (unitFV (getName intTyCon))
502 litFVs (HsIntPrim i) = returnM (unitFV (getName intPrimTyCon))
503 litFVs (HsFloatPrim f) = returnM (unitFV (getName floatPrimTyCon))
504 litFVs (HsDoublePrim d) = returnM (unitFV (getName doublePrimTyCon))
505 litFVs (HsLitLit l bogus_ty) = returnM (unitFV cCallableClassName)
506 litFVs lit = pprPanic "RnExpr.litFVs" (ppr lit) -- HsInteger and HsRat only appear
507 -- in post-typechecker translations
509 = ptext SLIT("character literal out of range: '\\") <> int c <> char '\''
511 rnOverLit (HsIntegral i _)
512 = lookupSyntaxName fromIntegerName `thenM` \ (from_integer_name, fvs) ->
514 returnM (HsIntegral i from_integer_name, fvs)
516 extra_fvs = mkFVs [plusIntegerName, timesIntegerName]
517 -- Big integer literals are built, using + and *,
518 -- out of small integers (DsUtils.mkIntegerLit)
519 -- [NB: plusInteger, timesInteger aren't rebindable...
520 -- they are used to construct the argument to fromInteger,
521 -- which is the rebindable one.]
523 returnM (HsIntegral i from_integer_name, fvs `plusFV` extra_fvs)
525 rnOverLit (HsFractional i _)
526 = lookupSyntaxName fromRationalName `thenM` \ (from_rat_name, fvs) ->
528 extra_fvs = mkFVs [ratioDataConName, plusIntegerName, timesIntegerName]
529 -- We have to make sure that the Ratio type is imported with
530 -- its constructor, because literals of type Ratio t are
531 -- built with that constructor.
532 -- The Rational type is needed too, but that will come in
533 -- as part of the type for fromRational.
534 -- The plus/times integer operations may be needed to construct the numerator
535 -- and denominator (see DsUtils.mkIntegerLit)
537 returnM (HsFractional i from_rat_name, fvs `plusFV` extra_fvs)
542 %*********************************************************
546 %*********************************************************
550 forAllWarn doc ty tyvar
551 = ifOptM Opt_WarnUnusedMatches $
552 getModeRn `thenM` \ mode ->
555 InterfaceMode _ -> returnM () ; -- Don't warn of unused tyvars in interface files
556 -- unless DEBUG is on, in which case it is slightly
557 -- informative. They can arise from mkRhsTyLam,
558 #endif -- leading to (say) f :: forall a b. [b] -> [b]
561 sep [ptext SLIT("The universally quantified type variable") <+> quotes (ppr tyvar),
562 nest 4 (ptext SLIT("does not appear in the type") <+> quotes (ppr ty))]
568 dupClassAssertWarn ctxt (assertion : dups)
569 = sep [hsep [ptext SLIT("Duplicate class assertion"),
570 quotes (ppr assertion),
571 ptext SLIT("in the context:")],
572 nest 4 (pprHsContext ctxt <+> ptext SLIT("..."))]
574 naughtyCCallContextErr (HsClassP clas _)
575 = sep [ptext SLIT("Can't use class") <+> quotes (ppr clas),
576 ptext SLIT("in a context")]
579 = hang (ptext SLIT("precedence parsing error"))
580 4 (hsep [ptext SLIT("cannot mix"), ppr_opfix op1, ptext SLIT("and"),
582 ptext SLIT("in the same infix expression")])
584 sectionPrecErr op arg_op section
585 = vcat [ptext SLIT("The operator") <+> ppr_opfix op <+> ptext SLIT("of a section"),
586 nest 4 (ptext SLIT("must have lower precedence than the operand") <+> ppr_opfix arg_op),
587 nest 4 (ptext SLIT("in the section:") <+> quotes (ppr section))]
590 = ftext FSLIT("Accepting non-standard infix type constructor") <+> quotes (ppr op)
593 = (ptext SLIT("Illegal signature in pattern:") <+> ppr ty)
594 $$ nest 4 (ptext SLIT("Use -fglasgow-exts to permit it"))
596 dupFieldErr str (dup:rest)
597 = hsep [ptext SLIT("duplicate field name"),
599 ptext SLIT("in record"), text str]
601 ppr_op op = quotes (ppr op) -- Here, op can be a Name or a (Var n), where n is a Name
602 ppr_opfix (pp_op, fixity) = pp_op <+> brackets (ppr fixity)