2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[RnSource]{Main pass of renamer}
7 module RnTypes ( rnHsType, rnHsSigType, rnHsTypeFVs, rnHsSigTypeFVs,
8 rnContext, precParseErr, sectionPrecErr ) where
10 import CmdLineOpts ( DynFlag(Opt_WarnMisc, Opt_WarnUnusedMatches, Opt_GlasgowExts) )
13 import RdrHsSyn ( RdrNameContext, RdrNameHsType, extractHsTyRdrTyVars, extractHsCtxtRdrTyVars )
14 import RnHsSyn ( RenamedContext, RenamedHsType, extractHsTyNames, tupleTyCon_name )
15 import RnEnv ( lookupOccRn, newIPName, bindTyVarsRn, lookupFixityRn )
18 import PrelInfo ( cCallishClassKeys )
19 import RdrName ( elemRdrEnv )
20 import NameSet ( FreeVars )
21 import Unique ( Uniquable(..) )
23 import BasicTypes ( compareFixity, arrowFixity )
25 import ListSetOps ( removeDupsEq )
28 #include "HsVersions.h"
31 These type renamers are in a separate module, rather than in (say) RnSource,
32 to break several loop.
34 %*********************************************************
36 \subsection{Renaming types}
38 %*********************************************************
41 rnHsTypeFVs :: SDoc -> RdrNameHsType -> RnMS (RenamedHsType, FreeVars)
42 rnHsTypeFVs doc_str ty
43 = rnHsType doc_str ty `thenRn` \ ty' ->
44 returnRn (ty', extractHsTyNames ty')
46 rnHsSigTypeFVs :: SDoc -> RdrNameHsType -> RnMS (RenamedHsType, FreeVars)
47 rnHsSigTypeFVs doc_str ty
48 = rnHsSigType doc_str ty `thenRn` \ ty' ->
49 returnRn (ty', extractHsTyNames ty')
51 rnHsSigType :: SDoc -> RdrNameHsType -> RnMS RenamedHsType
52 -- rnHsSigType is used for source-language type signatures,
53 -- which use *implicit* universal quantification.
54 rnHsSigType doc_str ty
55 = rnHsType (text "In the type signature for" <+> doc_str) ty
58 rnHsType is here because we call it from loadInstDecl, and I didn't
59 want a gratuitous knot.
62 rnHsType :: SDoc -> RdrNameHsType -> RnMS RenamedHsType
64 rnHsType doc (HsForAllTy Nothing ctxt ty)
65 -- Implicit quantifiction in source code (no kinds on tyvars)
66 -- Given the signature C => T we universally quantify
67 -- over FV(T) \ {in-scope-tyvars}
68 = getLocalNameEnv `thenRn` \ name_env ->
70 mentioned_in_tau = extractHsTyRdrTyVars ty
71 mentioned_in_ctxt = extractHsCtxtRdrTyVars ctxt
72 mentioned = nub (mentioned_in_tau ++ mentioned_in_ctxt)
74 -- Don't quantify over type variables that are in scope;
75 -- when GlasgowExts is off, there usually won't be any, except for
77 -- class C a where { op :: a -> a }
78 forall_tyvars = filter (not . (`elemRdrEnv` name_env)) mentioned
80 rnForAll doc (map UserTyVar forall_tyvars) ctxt ty
82 rnHsType doc (HsForAllTy (Just forall_tyvars) ctxt tau)
83 -- Explicit quantification.
84 -- Check that the forall'd tyvars are actually
85 -- mentioned in the type, and produce a warning if not
87 mentioned_in_tau = extractHsTyRdrTyVars tau
88 mentioned_in_ctxt = extractHsCtxtRdrTyVars ctxt
89 mentioned = nub (mentioned_in_tau ++ mentioned_in_ctxt)
90 forall_tyvar_names = hsTyVarNames forall_tyvars
92 -- Explicitly quantified but not mentioned in ctxt or tau
93 warn_guys = filter (`notElem` mentioned) forall_tyvar_names
95 mapRn_ (forAllWarn doc tau) warn_guys `thenRn_`
96 rnForAll doc forall_tyvars ctxt tau
98 rnHsType doc (HsTyVar tyvar)
99 = lookupOccRn tyvar `thenRn` \ tyvar' ->
100 returnRn (HsTyVar tyvar')
102 rnHsType doc (HsOpTy ty1 op ty2)
104 HsArrow -> returnRn HsArrow
105 HsTyOp n -> lookupOccRn n `thenRn` \ n' ->
108 rnHsType doc ty1 `thenRn` \ ty1' ->
109 rnHsType doc ty2 `thenRn` \ ty2' ->
110 lookupTyFixityRn op' `thenRn` \ fix ->
111 mkHsOpTyRn op' fix ty1' ty2'
114 rnHsType doc (HsNumTy i)
115 | i == 1 = returnRn (HsNumTy i)
116 | otherwise = failWithRn (HsNumTy i)
117 (ptext SLIT("Only unit numeric type pattern is valid"))
119 rnHsType doc (HsFunTy ty1 ty2)
120 = rnHsType doc ty1 `thenRn` \ ty1' ->
121 -- Might find a for-all as the arg of a function type
122 rnHsType doc ty2 `thenRn` \ ty2' ->
123 -- Or as the result. This happens when reading Prelude.hi
124 -- when we find return :: forall m. Monad m -> forall a. a -> m a
125 returnRn (HsFunTy ty1' ty2')
127 rnHsType doc (HsListTy ty)
128 = rnHsType doc ty `thenRn` \ ty' ->
129 returnRn (HsListTy ty')
131 rnHsType doc (HsKindSig ty k)
132 = rnHsType doc ty `thenRn` \ ty' ->
133 returnRn (HsKindSig ty' k)
135 rnHsType doc (HsPArrTy ty)
136 = rnHsType doc ty `thenRn` \ ty' ->
137 returnRn (HsPArrTy ty')
139 -- Unboxed tuples are allowed to have poly-typed arguments. These
140 -- sometimes crop up as a result of CPR worker-wrappering dictionaries.
141 rnHsType doc (HsTupleTy (HsTupCon _ boxity arity) tys)
142 -- Don't do lookupOccRn, because this is built-in syntax
143 -- so it doesn't need to be in scope
144 = mapRn (rnHsType doc) tys `thenRn` \ tys' ->
145 returnRn (HsTupleTy (HsTupCon tup_name boxity arity) tys')
147 tup_name = tupleTyCon_name boxity arity
150 rnHsType doc (HsAppTy ty1 ty2)
151 = rnHsType doc ty1 `thenRn` \ ty1' ->
152 rnHsType doc ty2 `thenRn` \ ty2' ->
153 returnRn (HsAppTy ty1' ty2')
155 rnHsType doc (HsPredTy pred)
156 = rnPred doc pred `thenRn` \ pred' ->
157 returnRn (HsPredTy pred')
159 rnHsTypes doc tys = mapRn (rnHsType doc) tys
164 rnForAll doc forall_tyvars ctxt ty
165 = bindTyVarsRn doc forall_tyvars $ \ new_tyvars ->
166 rnContext doc ctxt `thenRn` \ new_ctxt ->
167 rnHsType doc ty `thenRn` \ new_ty ->
168 returnRn (mkHsForAllTy (Just new_tyvars) new_ctxt new_ty)
172 %*********************************************************
174 \subsection{Fixities}
176 %*********************************************************
178 Infix types are read in a *right-associative* way, so that
183 mkHsOpTyRn rearranges where necessary. The two arguments
184 have already been renamed and rearranged. It's made rather tiresome
185 by the presence of ->
188 lookupTyFixityRn HsArrow = returnRn arrowFixity
189 lookupTyFixityRn (HsTyOp n)
190 = doptRn Opt_GlasgowExts `thenRn` \ glaExts ->
191 warnCheckRn glaExts (infixTyConWarn n) `thenRn_`
194 -- Building (ty1 `op1` (ty21 `op2` ty22))
195 mkHsOpTyRn :: HsTyOp Name -> Fixity
196 -> RenamedHsType -> RenamedHsType
197 -> RnMS RenamedHsType
199 mkHsOpTyRn op1 fix1 ty1 ty2@(HsOpTy ty21 op2 ty22)
200 = lookupTyFixityRn op2 `thenRn` \ fix2 ->
202 (nofix_error, associate_right) = compareFixity fix1 fix2
205 addErrRn (precParseErr (quotes (ppr op1),fix1)
206 (quotes (ppr op2),fix2)) `thenRn_`
207 returnRn (HsOpTy ty1 op1 ty2)
209 if not associate_right then
210 -- Rearrange to ((ty1 `op1` ty21) `op2` ty22)
211 mkHsOpTyRn op1 fix1 ty1 ty21 `thenRn` \ new_ty ->
212 returnRn (HsOpTy new_ty op2 ty22)
214 returnRn (HsOpTy ty1 op1 ty2)
216 mkHsOpTyRn op fix ty1 ty2 -- Default case, no rearrangment
217 = returnRn (HsOpTy ty1 op ty2)
219 mkHsFunTyRn ty1 ty2 -- Precedence of function arrow is 0
220 = returnRn (HsFunTy ty1 ty2) -- so no rearrangement reqd. Change
221 -- this if fixity of -> increases.
223 not_op_ty (HsOpTy _ _ _) = False
224 not_op_ty other = True
227 %*********************************************************
229 \subsection{Contexts and predicates}
231 %*********************************************************
234 rnContext :: SDoc -> RdrNameContext -> RnMS RenamedContext
236 = mapRn rn_pred ctxt `thenRn` \ theta ->
238 -- Check for duplicate assertions
239 -- If this isn't an error, then it ought to be:
240 ifOptRn Opt_WarnMisc (
242 (_, dups) = removeDupsEq theta
243 -- We only have equality, not ordering
245 mapRn (addWarnRn . dupClassAssertWarn theta) dups
250 --Someone discovered that @CCallable@ and @CReturnable@
251 -- could be used in contexts such as:
252 -- foo :: CCallable a => a -> PrimIO Int
253 -- Doing this utterly wrecks the whole point of introducing these
254 -- classes so we specifically check that this isn't being done.
255 rn_pred pred = rnPred doc pred `thenRn` \ pred'->
256 checkRn (not (bad_pred pred'))
257 (naughtyCCallContextErr pred') `thenRn_`
260 bad_pred (HsClassP clas _) = getUnique clas `elem` cCallishClassKeys
261 bad_pred other = False
264 rnPred doc (HsClassP clas tys)
265 = lookupOccRn clas `thenRn` \ clas_name ->
266 rnHsTypes doc tys `thenRn` \ tys' ->
267 returnRn (HsClassP clas_name tys')
269 rnPred doc (HsIParam n ty)
270 = newIPName n `thenRn` \ name ->
271 rnHsType doc ty `thenRn` \ ty' ->
272 returnRn (HsIParam name ty')
276 %*********************************************************
280 %*********************************************************
284 forAllWarn doc ty tyvar
285 = ifOptRn Opt_WarnUnusedMatches $
286 getModeRn `thenRn` \ mode ->
289 InterfaceMode -> returnRn () ; -- Don't warn of unused tyvars in interface files
290 -- unless DEBUG is on, in which case it is slightly
291 -- informative. They can arise from mkRhsTyLam,
292 #endif -- leading to (say) f :: forall a b. [b] -> [b]
295 sep [ptext SLIT("The universally quantified type variable") <+> quotes (ppr tyvar),
296 nest 4 (ptext SLIT("does not appear in the type") <+> quotes (ppr ty))]
302 dupClassAssertWarn ctxt (assertion : dups)
303 = sep [hsep [ptext SLIT("Duplicate class assertion"),
304 quotes (ppr assertion),
305 ptext SLIT("in the context:")],
306 nest 4 (pprHsContext ctxt <+> ptext SLIT("..."))]
308 naughtyCCallContextErr (HsClassP clas _)
309 = sep [ptext SLIT("Can't use class") <+> quotes (ppr clas),
310 ptext SLIT("in a context")]
313 = hang (ptext SLIT("precedence parsing error"))
314 4 (hsep [ptext SLIT("cannot mix"), ppr_opfix op1, ptext SLIT("and"),
316 ptext SLIT("in the same infix expression")])
318 sectionPrecErr op arg_op section
319 = vcat [ptext SLIT("The operator") <+> ppr_opfix op <+> ptext SLIT("of a section"),
320 nest 4 (ptext SLIT("must have lower precedence than the operand") <+> ppr_opfix arg_op),
321 nest 4 (ptext SLIT("in the section:") <+> quotes (ppr section))]
324 = ftext FSLIT("Accepting non-standard infix type constructor") <+> quotes (ppr op)
326 ppr_opfix (pp_op, fixity) = pp_op <+> brackets (ppr fixity)