2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-2006
4 \section[RnEnv]{Environment manipulation for the renamer monad}
8 newTopSrcBinder, lookupFamInstDeclBndr,
9 lookupLocatedBndrRn, lookupBndrRn,
10 lookupLocatedTopBndrRn, lookupTopBndrRn,
11 lookupLocatedOccRn, lookupOccRn,
12 lookupLocatedGlobalOccRn, lookupGlobalOccRn,
13 lookupLocalDataTcNames, lookupSrcOcc_maybe,
14 lookupFixityRn, lookupTyFixityRn, lookupLocatedSigOccRn,
15 lookupLocatedInstDeclBndr,
16 lookupSyntaxName, lookupSyntaxTable, lookupImportedName,
17 lookupGreRn, lookupGreRn_maybe,
19 newLocalsRn, newIPNameRn,
20 bindLocalNames, bindLocalNamesFV,
21 bindLocatedLocalsFV, bindLocatedLocalsRn,
22 bindSigTyVarsFV, bindPatSigTyVars, bindPatSigTyVarsFV,
23 bindTyVarsRn, extendTyVarEnvFVRn,
26 checkDupNames, mapFvRn,
27 warnUnusedMatches, warnUnusedModules, warnUnusedImports,
28 warnUnusedTopBinds, warnUnusedLocalBinds,
29 dataTcOccs, unknownNameErr,
32 #include "HsVersions.h"
34 import LoadIface ( loadInterfaceForName, loadSrcInterface )
35 import IfaceEnv ( lookupOrig, newGlobalBinder, newIPName )
36 import HsSyn ( FixitySig(..), HsExpr(..), SyntaxExpr, SyntaxTable,
37 LHsTyVarBndr, LHsType,
38 Fixity, hsLTyVarLocNames, replaceTyVarName )
39 import RdrHsSyn ( extractHsTyRdrTyVars )
40 import RdrName ( RdrName, isQual, isUnqual, isOrig_maybe,
42 mkRdrUnqual, setRdrNameSpace, rdrNameOcc,
43 pprGlobalRdrEnv, lookupGRE_RdrName,
44 isExact_maybe, isSrcRdrName,
46 GlobalRdrElt(..), GlobalRdrEnv, lookupGlobalRdrEnv,
47 isLocalGRE, extendLocalRdrEnv, elemLocalRdrEnv, lookupLocalRdrEnv,
48 Provenance(..), pprNameProvenance,
49 importSpecLoc, importSpecModule
51 import HscTypes ( availNames, ModIface(..), FixItem(..), lookupFixity )
53 import Name ( Name, nameIsLocalOrFrom, mkInternalName, isWiredInName,
54 nameSrcLoc, nameOccName, nameModule, isExternalName )
56 import OccName ( tcName, isDataOcc, pprNonVarNameSpace, occNameSpace,
58 import Module ( Module, ModuleName )
59 import PrelNames ( mkUnboundName, rOOT_MAIN, iNTERACTIVE, consDataConKey, hasKey )
61 import BasicTypes ( IPName, mapIPName )
62 import SrcLoc ( SrcSpan, srcSpanStart, Located(..), eqLocated, unLoc,
63 srcLocSpan, getLoc, combineSrcSpans, isOneLineSpan )
65 import Util ( sortLe )
66 import ListSetOps ( removeDups )
72 %*********************************************************
76 %*********************************************************
79 newTopSrcBinder :: Module -> Located RdrName -> RnM Name
80 newTopSrcBinder this_mod (L loc rdr_name)
81 | Just name <- isExact_maybe rdr_name
82 = -- This is here to catch
83 -- (a) Exact-name binders created by Template Haskell
84 -- (b) The PrelBase defn of (say) [] and similar, for which
85 -- the parser reads the special syntax and returns an Exact RdrName
86 -- We are at a binding site for the name, so check first that it
87 -- the current module is the correct one; otherwise GHC can get
88 -- very confused indeed. This test rejects code like
89 -- data T = (,) Int Int
90 -- unless we are in GHC.Tup
91 ASSERT2( isExternalName name, ppr name )
92 do { checkM (this_mod == nameModule name)
93 (addErrAt loc (badOrigBinding rdr_name))
97 | Just (rdr_mod, rdr_occ) <- isOrig_maybe rdr_name
98 = do { checkM (rdr_mod == this_mod || rdr_mod == rOOT_MAIN)
99 (addErrAt loc (badOrigBinding rdr_name))
100 -- When reading External Core we get Orig names as binders,
101 -- but they should agree with the module gotten from the monad
103 -- We can get built-in syntax showing up here too, sadly. If you type
105 -- the constructor is parsed as a type, and then RdrHsSyn.tyConToDataCon
106 -- uses setRdrNameSpace to make it into a data constructors. At that point
107 -- the nice Exact name for the TyCon gets swizzled to an Orig name.
108 -- Hence the badOrigBinding error message.
110 -- Except for the ":Main.main = ..." definition inserted into
111 -- the Main module; ugh!
113 -- Because of this latter case, we call newGlobalBinder with a module from
114 -- the RdrName, not from the environment. In principle, it'd be fine to
115 -- have an arbitrary mixture of external core definitions in a single module,
116 -- (apart from module-initialisation issues, perhaps).
117 ; newGlobalBinder rdr_mod rdr_occ (srcSpanStart loc) }
118 --TODO, should pass the whole span
121 = do { checkM (not (isQual rdr_name))
122 (addErrAt loc (badQualBndrErr rdr_name))
123 -- Binders should not be qualified; if they are, and with a different
124 -- module name, we we get a confusing "M.T is not in scope" error later
125 ; newGlobalBinder this_mod (rdrNameOcc rdr_name) (srcSpanStart loc) }
128 %*********************************************************
130 Source code occurrences
132 %*********************************************************
134 Looking up a name in the RnEnv.
137 lookupLocatedBndrRn :: Located RdrName -> RnM (Located Name)
138 lookupLocatedBndrRn = wrapLocM lookupBndrRn
140 lookupBndrRn :: RdrName -> RnM Name
141 -- NOTE: assumes that the SrcSpan of the binder has already been setSrcSpan'd
142 lookupBndrRn rdr_name
143 = getLocalRdrEnv `thenM` \ local_env ->
144 case lookupLocalRdrEnv local_env rdr_name of
145 Just name -> returnM name
146 Nothing -> lookupTopBndrRn rdr_name
148 lookupLocatedTopBndrRn :: Located RdrName -> RnM (Located Name)
149 lookupLocatedTopBndrRn = wrapLocM lookupTopBndrRn
151 lookupTopBndrRn :: RdrName -> RnM Name
152 -- Look up a top-level source-code binder. We may be looking up an unqualified 'f',
153 -- and there may be several imported 'f's too, which must not confuse us.
154 -- For example, this is OK:
156 -- infix 9 f -- The 'f' here does not need to be qualified
157 -- f x = x -- Nor here, of course
158 -- So we have to filter out the non-local ones.
160 -- A separate function (importsFromLocalDecls) reports duplicate top level
161 -- decls, so here it's safe just to choose an arbitrary one.
163 -- There should never be a qualified name in a binding position in Haskell,
164 -- but there can be if we have read in an external-Core file.
165 -- The Haskell parser checks for the illegal qualified name in Haskell
166 -- source files, so we don't need to do so here.
168 lookupTopBndrRn rdr_name
169 | Just name <- isExact_maybe rdr_name
172 | Just (rdr_mod, rdr_occ) <- isOrig_maybe rdr_name
173 -- This deals with the case of derived bindings, where
174 -- we don't bother to call newTopSrcBinder first
175 -- We assume there is no "parent" name
176 = do { loc <- getSrcSpanM
177 ; newGlobalBinder rdr_mod rdr_occ (srcSpanStart loc) }
180 = do { mb_gre <- lookupGreLocalRn rdr_name
183 traceRn $ text "lookupTopBndrRn"
185 Just gre -> returnM (gre_name gre) }
187 -- lookupLocatedSigOccRn is used for type signatures and pragmas
193 -- It's clear that the 'f' in the signature must refer to A.f
194 -- The Haskell98 report does not stipulate this, but it will!
195 -- So we must treat the 'f' in the signature in the same way
196 -- as the binding occurrence of 'f', using lookupBndrRn
197 lookupLocatedSigOccRn :: Located RdrName -> RnM (Located Name)
198 lookupLocatedSigOccRn = lookupLocatedBndrRn
200 -- lookupInstDeclBndr is used for the binders in an
201 -- instance declaration. Here we use the class name to
204 lookupLocatedInstDeclBndr :: Name -> Located RdrName -> RnM (Located Name)
205 lookupLocatedInstDeclBndr cls rdr = wrapLocM (lookupInstDeclBndr cls) rdr
207 lookupInstDeclBndr :: Name -> RdrName -> RnM Name
208 -- This is called on the method name on the left-hand side of an
209 -- instance declaration binding. eg. instance Functor T where
211 -- ^^^^ called on this
212 -- Regardless of how many unqualified fmaps are in scope, we want
213 -- the one that comes from the Functor class.
214 lookupInstDeclBndr cls_name rdr_name
215 | isUnqual rdr_name -- Find all the things the rdr-name maps to
216 = do { -- and pick the one with the right parent name
217 let { is_op gre@(GRE {gre_par = ParentIs n}) = cls_name == n
218 ; is_op other = False
219 ; occ = rdrNameOcc rdr_name
220 ; lookup_fn env = filter is_op (lookupGlobalRdrEnv env occ) }
221 ; mb_gre <- lookupGreRn_help rdr_name lookup_fn
223 Just gre -> return (gre_name gre)
224 Nothing -> do { addErr (unknownInstBndrErr cls_name rdr_name)
225 ; traceRn (text "lookupInstDeclBndr" <+> ppr rdr_name)
226 ; return (mkUnboundName rdr_name) } }
228 | otherwise -- Occurs in derived instances, where we just
229 -- refer directly to the right method
230 = ASSERT2( not (isQual rdr_name), ppr rdr_name )
231 -- NB: qualified names are rejected by the parser
232 lookupImportedName rdr_name
234 newIPNameRn :: IPName RdrName -> TcRnIf m n (IPName Name)
235 newIPNameRn ip_rdr = newIPName (mapIPName rdrNameOcc ip_rdr)
237 -- Looking up family names in type instances is a subtle affair. The family
238 -- may be imported, in which case we need to lookup the occurence of a global
239 -- name. Alternatively, the family may be in the same binding group (and in
240 -- fact in a declaration processed later), and we need to create a new top
243 -- So, also this is strictly speaking an occurence, we cannot raise an error
244 -- message yet for instances without a family declaration. This will happen
245 -- during renaming the type instance declaration in RnSource.rnTyClDecl.
247 lookupFamInstDeclBndr :: Module -> Located RdrName -> RnM Name
248 lookupFamInstDeclBndr mod lrdr_name@(L _ rdr_name)
249 = do { mb_gre <- lookupGreRn_maybe rdr_name
251 Just gre -> returnM (gre_name gre) ;
252 Nothing -> newTopSrcBinder mod lrdr_name }
254 --------------------------------------------------
256 --------------------------------------------------
258 lookupLocatedOccRn :: Located RdrName -> RnM (Located Name)
259 lookupLocatedOccRn = wrapLocM lookupOccRn
261 -- lookupOccRn looks up an occurrence of a RdrName
262 lookupOccRn :: RdrName -> RnM Name
264 = getLocalRdrEnv `thenM` \ local_env ->
265 case lookupLocalRdrEnv local_env rdr_name of
266 Just name -> returnM name
267 Nothing -> lookupGlobalOccRn rdr_name
269 lookupLocatedGlobalOccRn :: Located RdrName -> RnM (Located Name)
270 lookupLocatedGlobalOccRn = wrapLocM lookupGlobalOccRn
272 lookupGlobalOccRn :: RdrName -> RnM Name
273 -- lookupGlobalOccRn is like lookupOccRn, except that it looks in the global
274 -- environment. It's used only for
275 -- record field names
276 -- class op names in class and instance decls
278 lookupGlobalOccRn rdr_name
279 | not (isSrcRdrName rdr_name)
280 = lookupImportedName rdr_name
283 = -- First look up the name in the normal environment.
284 lookupGreRn_maybe rdr_name `thenM` \ mb_gre ->
286 Just gre -> returnM (gre_name gre) ;
289 -- We allow qualified names on the command line to refer to
290 -- *any* name exported by any module in scope, just as if
291 -- there was an "import qualified M" declaration for every
293 getModule `thenM` \ mod ->
294 if isQual rdr_name && mod == iNTERACTIVE then
295 -- This test is not expensive,
296 lookupQualifiedName rdr_name -- and only happens for failed lookups
298 traceRn $ text "lookupGlobalOccRn"
299 unboundName rdr_name }
301 lookupImportedName :: RdrName -> TcRnIf m n Name
302 -- Lookup the occurrence of an imported name
303 -- The RdrName is *always* qualified or Exact
304 -- Treat it as an original name, and conjure up the Name
305 -- Usually it's Exact or Orig, but it can be Qual if it
306 -- comes from an hi-boot file. (This minor infelicity is
307 -- just to reduce duplication in the parser.)
308 lookupImportedName rdr_name
309 | Just n <- isExact_maybe rdr_name
310 -- This happens in derived code
313 -- Always Orig, even when reading a .hi-boot file
314 | Just (rdr_mod, rdr_occ) <- isOrig_maybe rdr_name
315 = lookupOrig rdr_mod rdr_occ
318 = pprPanic "RnEnv.lookupImportedName" (ppr rdr_name)
320 unboundName :: RdrName -> RnM Name
322 = do { addErr (unknownNameErr rdr_name)
323 ; env <- getGlobalRdrEnv;
324 ; traceRn (vcat [unknownNameErr rdr_name,
325 ptext SLIT("Global envt is:"),
326 nest 3 (pprGlobalRdrEnv env)])
327 ; returnM (mkUnboundName rdr_name) }
329 --------------------------------------------------
330 -- Lookup in the Global RdrEnv of the module
331 --------------------------------------------------
333 lookupSrcOcc_maybe :: RdrName -> RnM (Maybe Name)
334 -- No filter function; does not report an error on failure
335 lookupSrcOcc_maybe rdr_name
336 = do { mb_gre <- lookupGreRn_maybe rdr_name
338 Nothing -> returnM Nothing
339 Just gre -> returnM (Just (gre_name gre)) }
341 -------------------------
342 lookupGreRn_maybe :: RdrName -> RnM (Maybe GlobalRdrElt)
343 -- Just look up the RdrName in the GlobalRdrEnv
344 lookupGreRn_maybe rdr_name
345 = lookupGreRn_help rdr_name (lookupGRE_RdrName rdr_name)
347 lookupGreRn :: RdrName -> RnM GlobalRdrElt
348 -- If not found, add error message, and return a fake GRE
350 = do { mb_gre <- lookupGreRn_maybe rdr_name
352 Just gre -> return gre ;
354 { traceRn $ text "lookupGreRn"
355 ; name <- unboundName rdr_name
356 ; return (GRE { gre_name = name, gre_par = NoParent,
357 gre_prov = LocalDef }) }}}
359 lookupGreLocalRn :: RdrName -> RnM (Maybe GlobalRdrElt)
360 -- Similar, but restricted to locally-defined things
361 lookupGreLocalRn rdr_name
362 = lookupGreRn_help rdr_name lookup_fn
364 lookup_fn env = filter isLocalGRE (lookupGRE_RdrName rdr_name env)
366 lookupGreRn_help :: RdrName -- Only used in error message
367 -> (GlobalRdrEnv -> [GlobalRdrElt]) -- Lookup function
368 -> RnM (Maybe GlobalRdrElt)
369 -- Checks for exactly one match; reports deprecations
370 -- Returns Nothing, without error, if too few
371 lookupGreRn_help rdr_name lookup
372 = do { env <- getGlobalRdrEnv
374 [] -> returnM Nothing
375 [gre] -> returnM (Just gre)
376 gres -> do { addNameClashErrRn rdr_name gres
377 ; returnM (Just (head gres)) } }
379 ------------------------------
381 ------------------------------
383 -- A qualified name on the command line can refer to any module at all: we
384 -- try to load the interface if we don't already have it.
385 lookupQualifiedName :: RdrName -> RnM Name
386 lookupQualifiedName rdr_name
387 | Just (mod,occ) <- isQual_maybe rdr_name
388 -- Note: we want to behave as we would for a source file import here,
389 -- and respect hiddenness of modules/packages, hence loadSrcInterface.
390 = loadSrcInterface doc mod False `thenM` \ iface ->
393 (mod,avails) <- mi_exports iface,
395 name <- availNames avail,
397 ((mod,occ):ns) -> ASSERT (null ns)
399 _ -> unboundName rdr_name
402 = pprPanic "RnEnv.lookupQualifiedName" (ppr rdr_name)
404 doc = ptext SLIT("Need to find") <+> ppr rdr_name
407 %*********************************************************
411 %*********************************************************
414 lookupLocalDataTcNames :: RdrName -> RnM [Name]
415 -- GHC extension: look up both the tycon and data con
416 -- for con-like things
417 -- Complain if neither is in scope
418 lookupLocalDataTcNames rdr_name
419 | Just n <- isExact_maybe rdr_name
420 -- Special case for (:), which doesn't get into the GlobalRdrEnv
421 = return [n] -- For this we don't need to try the tycon too
423 = do { mb_gres <- mapM lookupGreLocalRn (dataTcOccs rdr_name)
424 ; case [gre_name gre | Just gre <- mb_gres] of
425 [] -> do { addErr (unknownNameErr rdr_name)
427 names -> return names
430 --------------------------------
431 bindLocalFixities :: [FixitySig RdrName] -> RnM a -> RnM a
432 -- Used for nested fixity decls
433 -- No need to worry about type constructors here,
434 -- Should check for duplicates but we don't
435 bindLocalFixities fixes thing_inside
436 | null fixes = thing_inside
437 | otherwise = mappM rn_sig fixes `thenM` \ new_bit ->
438 extendFixityEnv new_bit thing_inside
440 rn_sig (FixitySig lv@(L loc v) fix)
441 = addLocM lookupBndrRn lv `thenM` \ new_v ->
442 returnM (new_v, (FixItem (rdrNameOcc v) fix loc))
445 --------------------------------
446 lookupFixity is a bit strange.
448 * Nested local fixity decls are put in the local fixity env, which we
449 find with getFixtyEnv
451 * Imported fixities are found in the HIT or PIT
453 * Top-level fixity decls in this module may be for Names that are
454 either Global (constructors, class operations)
455 or Local/Exported (everything else)
456 (See notes with RnNames.getLocalDeclBinders for why we have this split.)
457 We put them all in the local fixity environment
460 lookupFixityRn :: Name -> RnM Fixity
462 = getModule `thenM` \ this_mod ->
463 if nameIsLocalOrFrom this_mod name
464 then -- It's defined in this module
465 getFixityEnv `thenM` \ local_fix_env ->
466 traceRn (text "lookupFixityRn" <+> (ppr name $$ ppr local_fix_env)) `thenM_`
467 returnM (lookupFixity local_fix_env name)
469 else -- It's imported
470 -- For imported names, we have to get their fixities by doing a
471 -- loadInterfaceForName, and consulting the Ifaces that comes back
472 -- from that, because the interface file for the Name might not
473 -- have been loaded yet. Why not? Suppose you import module A,
474 -- which exports a function 'f', thus;
475 -- module CurrentModule where
477 -- module A( f ) where
479 -- Then B isn't loaded right away (after all, it's possible that
480 -- nothing from B will be used). When we come across a use of
481 -- 'f', we need to know its fixity, and it's then, and only
482 -- then, that we load B.hi. That is what's happening here.
484 -- loadInterfaceForName will find B.hi even if B is a hidden module,
485 -- and that's what we want.
486 loadInterfaceForName doc name `thenM` \ iface ->
487 returnM (mi_fix_fn iface (nameOccName name))
489 doc = ptext SLIT("Checking fixity for") <+> ppr name
492 lookupTyFixityRn :: Located Name -> RnM Fixity
493 lookupTyFixityRn (L loc n)
494 = do { glaExts <- doptM Opt_GlasgowExts
495 ; when (not glaExts) (addWarnAt loc (infixTyConWarn n))
499 dataTcOccs :: RdrName -> [RdrName]
500 -- If the input is a data constructor, return both it and a type
501 -- constructor. This is useful when we aren't sure which we are
504 | Just n <- isExact_maybe rdr_name -- Ghastly special case
505 , n `hasKey` consDataConKey = [rdr_name] -- see note below
506 | isDataOcc occ = [rdr_name_tc, rdr_name]
507 | otherwise = [rdr_name]
509 occ = rdrNameOcc rdr_name
510 rdr_name_tc = setRdrNameSpace rdr_name tcName
512 -- If the user typed "[]" or "(,,)", we'll generate an Exact RdrName,
513 -- and setRdrNameSpace generates an Orig, which is fine
514 -- But it's not fine for (:), because there *is* no corresponding type
515 -- constructor. If we generate an Orig tycon for GHC.Base.(:), it'll
516 -- appear to be in scope (because Orig's simply allocate a new name-cache
517 -- entry) and then we get an error when we use dataTcOccs in
518 -- TcRnDriver.tcRnGetInfo. Large sigh.
521 %************************************************************************
524 Dealing with rebindable syntax is driven by the
525 Opt_NoImplicitPrelude dynamic flag.
527 In "deriving" code we don't want to use rebindable syntax
528 so we switch off the flag locally
531 %************************************************************************
533 Haskell 98 says that when you say "3" you get the "fromInteger" from the
534 Standard Prelude, regardless of what is in scope. However, to experiment
535 with having a language that is less coupled to the standard prelude, we're
536 trying a non-standard extension that instead gives you whatever "Prelude.fromInteger"
537 happens to be in scope. Then you can
539 import MyPrelude as Prelude
540 to get the desired effect.
542 At the moment this just happens for
543 * fromInteger, fromRational on literals (in expressions and patterns)
544 * negate (in expressions)
545 * minus (arising from n+k patterns)
548 We store the relevant Name in the HsSyn tree, in
549 * HsIntegral/HsFractional
553 respectively. Initially, we just store the "standard" name (PrelNames.fromIntegralName,
554 fromRationalName etc), but the renamer changes this to the appropriate user
555 name if Opt_NoImplicitPrelude is on. That is what lookupSyntaxName does.
557 We treat the orignal (standard) names as free-vars too, because the type checker
558 checks the type of the user thing against the type of the standard thing.
561 lookupSyntaxName :: Name -- The standard name
562 -> RnM (SyntaxExpr Name, FreeVars) -- Possibly a non-standard name
563 lookupSyntaxName std_name
564 = doptM Opt_ImplicitPrelude `thenM` \ implicit_prelude ->
565 if implicit_prelude then normal_case
567 -- Get the similarly named thing from the local environment
568 lookupOccRn (mkRdrUnqual (nameOccName std_name)) `thenM` \ usr_name ->
569 returnM (HsVar usr_name, unitFV usr_name)
571 normal_case = returnM (HsVar std_name, emptyFVs)
573 lookupSyntaxTable :: [Name] -- Standard names
574 -> RnM (SyntaxTable Name, FreeVars) -- See comments with HsExpr.ReboundNames
575 lookupSyntaxTable std_names
576 = doptM Opt_ImplicitPrelude `thenM` \ implicit_prelude ->
577 if implicit_prelude then normal_case
579 -- Get the similarly named thing from the local environment
580 mappM (lookupOccRn . mkRdrUnqual . nameOccName) std_names `thenM` \ usr_names ->
582 returnM (std_names `zip` map HsVar usr_names, mkFVs usr_names)
584 normal_case = returnM (std_names `zip` map HsVar std_names, emptyFVs)
588 %*********************************************************
592 %*********************************************************
595 newLocalsRn :: [Located RdrName] -> RnM [Name]
596 newLocalsRn rdr_names_w_loc
597 = newUniqueSupply `thenM` \ us ->
598 returnM (zipWith mk rdr_names_w_loc (uniqsFromSupply us))
600 mk (L loc rdr_name) uniq
601 | Just name <- isExact_maybe rdr_name = name
602 -- This happens in code generated by Template Haskell
603 | otherwise = ASSERT2( isUnqual rdr_name, ppr rdr_name )
604 -- We only bind unqualified names here
605 -- lookupRdrEnv doesn't even attempt to look up a qualified RdrName
606 mkInternalName uniq (rdrNameOcc rdr_name) (srcSpanStart loc)
608 bindLocatedLocalsRn :: SDoc -- Documentation string for error message
612 bindLocatedLocalsRn doc_str rdr_names_w_loc enclosed_scope
613 = -- Check for duplicate names
614 checkDupNames doc_str rdr_names_w_loc `thenM_`
616 -- Warn about shadowing, but only in source modules
617 ifOptM Opt_WarnNameShadowing
618 (checkShadowing doc_str rdr_names_w_loc) `thenM_`
620 -- Make fresh Names and extend the environment
621 newLocalsRn rdr_names_w_loc `thenM` \ names ->
622 getLocalRdrEnv `thenM` \ local_env ->
623 setLocalRdrEnv (extendLocalRdrEnv local_env names)
624 (enclosed_scope names)
627 bindLocalNames :: [Name] -> RnM a -> RnM a
628 bindLocalNames names enclosed_scope
629 = getLocalRdrEnv `thenM` \ name_env ->
630 setLocalRdrEnv (extendLocalRdrEnv name_env names)
633 bindLocalNamesFV :: [Name] -> RnM (a, FreeVars) -> RnM (a, FreeVars)
634 bindLocalNamesFV names enclosed_scope
635 = do { (result, fvs) <- bindLocalNames names enclosed_scope
636 ; returnM (result, delListFromNameSet fvs names) }
639 -------------------------------------
640 -- binLocalsFVRn is the same as bindLocalsRn
641 -- except that it deals with free vars
642 bindLocatedLocalsFV :: SDoc -> [Located RdrName] -> ([Name] -> RnM (a,FreeVars))
644 bindLocatedLocalsFV doc rdr_names enclosed_scope
645 = bindLocatedLocalsRn doc rdr_names $ \ names ->
646 enclosed_scope names `thenM` \ (thing, fvs) ->
647 returnM (thing, delListFromNameSet fvs names)
649 -------------------------------------
650 bindTyVarsRn :: SDoc -> [LHsTyVarBndr RdrName]
651 -> ([LHsTyVarBndr Name] -> RnM a)
653 -- Haskell-98 binding of type variables; e.g. within a data type decl
654 bindTyVarsRn doc_str tyvar_names enclosed_scope
656 located_tyvars = hsLTyVarLocNames tyvar_names
658 bindLocatedLocalsRn doc_str located_tyvars $ \ names ->
659 enclosed_scope (zipWith replace tyvar_names names)
661 replace (L loc n1) n2 = L loc (replaceTyVarName n1 n2)
663 bindPatSigTyVars :: [LHsType RdrName] -> ([Name] -> RnM a) -> RnM a
664 -- Find the type variables in the pattern type
665 -- signatures that must be brought into scope
666 bindPatSigTyVars tys thing_inside
667 = do { scoped_tyvars <- doptM Opt_ScopedTypeVariables
668 ; if not scoped_tyvars then
671 do { name_env <- getLocalRdrEnv
672 ; let locd_tvs = [ tv | ty <- tys
673 , tv <- extractHsTyRdrTyVars ty
674 , not (unLoc tv `elemLocalRdrEnv` name_env) ]
675 nubbed_tvs = nubBy eqLocated locd_tvs
676 -- The 'nub' is important. For example:
677 -- f (x :: t) (y :: t) = ....
678 -- We don't want to complain about binding t twice!
680 ; bindLocatedLocalsRn doc_sig nubbed_tvs thing_inside }}
682 doc_sig = text "In a pattern type-signature"
684 bindPatSigTyVarsFV :: [LHsType RdrName]
687 bindPatSigTyVarsFV tys thing_inside
688 = bindPatSigTyVars tys $ \ tvs ->
689 thing_inside `thenM` \ (result,fvs) ->
690 returnM (result, fvs `delListFromNameSet` tvs)
692 bindSigTyVarsFV :: [Name]
695 bindSigTyVarsFV tvs thing_inside
696 = do { scoped_tyvars <- doptM Opt_ScopedTypeVariables
697 ; if not scoped_tyvars then
700 bindLocalNamesFV tvs thing_inside }
702 extendTyVarEnvFVRn :: [Name] -> RnM (a, FreeVars) -> RnM (a, FreeVars)
703 -- This function is used only in rnSourceDecl on InstDecl
704 extendTyVarEnvFVRn tyvars thing_inside = bindLocalNamesFV tyvars thing_inside
706 -------------------------------------
707 checkDupNames :: SDoc
710 checkDupNames doc_str rdr_names_w_loc
711 = -- Check for duplicated names in a binding group
712 mappM_ (dupNamesErr doc_str) dups
714 (_, dups) = removeDups (\n1 n2 -> unLoc n1 `compare` unLoc n2) rdr_names_w_loc
716 -------------------------------------
717 checkShadowing doc_str loc_rdr_names
718 = getLocalRdrEnv `thenM` \ local_env ->
719 getGlobalRdrEnv `thenM` \ global_env ->
721 check_shadow (L loc rdr_name)
722 | rdr_name `elemLocalRdrEnv` local_env
723 || not (null (lookupGRE_RdrName rdr_name global_env ))
724 = addWarnAt loc (shadowedNameWarn doc_str rdr_name)
725 | otherwise = returnM ()
727 mappM_ check_shadow loc_rdr_names
731 %************************************************************************
733 \subsection{Free variable manipulation}
735 %************************************************************************
739 mapFvRn f xs = mappM f xs `thenM` \ stuff ->
741 (ys, fvs_s) = unzip stuff
743 returnM (ys, plusFVs fvs_s)
747 %************************************************************************
749 \subsection{Envt utility functions}
751 %************************************************************************
754 warnUnusedModules :: [(ModuleName,SrcSpan)] -> RnM ()
755 warnUnusedModules mods
756 = ifOptM Opt_WarnUnusedImports (mappM_ bleat mods)
758 bleat (mod,loc) = addWarnAt loc (mk_warn mod)
759 mk_warn m = vcat [ptext SLIT("Module") <+> quotes (ppr m)
760 <+> text "is imported, but nothing from it is used,",
761 nest 2 (ptext SLIT("except perhaps instances visible in")
763 ptext SLIT("To suppress this warning, use:")
764 <+> ptext SLIT("import") <+> ppr m <> parens empty ]
767 warnUnusedImports, warnUnusedTopBinds :: [GlobalRdrElt] -> RnM ()
768 warnUnusedImports gres = ifOptM Opt_WarnUnusedImports (warnUnusedGREs gres)
769 warnUnusedTopBinds gres = ifOptM Opt_WarnUnusedBinds (warnUnusedGREs gres)
771 warnUnusedLocalBinds, warnUnusedMatches :: [Name] -> RnM ()
772 warnUnusedLocalBinds names = ifOptM Opt_WarnUnusedBinds (warnUnusedLocals names)
773 warnUnusedMatches names = ifOptM Opt_WarnUnusedMatches (warnUnusedLocals names)
775 -------------------------
778 = warnUnusedBinds [(n,p) | GRE {gre_name = n, gre_prov = p} <- gres]
780 warnUnusedLocals names
781 = warnUnusedBinds [(n,LocalDef) | n<-names]
783 warnUnusedBinds :: [(Name,Provenance)] -> RnM ()
784 warnUnusedBinds names = mappM_ warnUnusedName (filter reportable names)
785 where reportable (name,_)
786 | isWiredInName name = False -- Don't report unused wired-in names
787 -- Otherwise we get a zillion warnings
789 | otherwise = reportIfUnused (nameOccName name)
791 -------------------------
793 warnUnusedName :: (Name, Provenance) -> RnM ()
794 warnUnusedName (name, LocalDef)
795 = addUnusedWarning name (srcLocSpan (nameSrcLoc name))
796 (ptext SLIT("Defined but not used"))
798 warnUnusedName (name, Imported is)
801 warn spec = addUnusedWarning name span msg
803 span = importSpecLoc spec
804 pp_mod = quotes (ppr (importSpecModule spec))
805 msg = ptext SLIT("Imported from") <+> pp_mod <+> ptext SLIT("but not used")
807 addUnusedWarning name span msg
810 nest 2 $ pprNonVarNameSpace (occNameSpace (nameOccName name))
811 <+> quotes (ppr name)]
815 addNameClashErrRn rdr_name names
816 = addErr (vcat [ptext SLIT("Ambiguous occurrence") <+> quotes (ppr rdr_name),
817 ptext SLIT("It could refer to") <+> vcat (msg1 : msgs)])
820 msg1 = ptext SLIT("either") <+> mk_ref np1
821 msgs = [ptext SLIT(" or") <+> mk_ref np | np <- nps]
822 mk_ref gre = quotes (ppr (gre_name gre)) <> comma <+> pprNameProvenance gre
824 shadowedNameWarn doc shadow
825 = hsep [ptext SLIT("This binding for"),
827 ptext SLIT("shadows an existing binding")]
830 unknownNameErr rdr_name
831 = sep [ptext SLIT("Not in scope:"),
832 nest 2 $ pprNonVarNameSpace (occNameSpace (rdrNameOcc rdr_name))
833 <+> quotes (ppr rdr_name)]
835 unknownInstBndrErr cls op
836 = quotes (ppr op) <+> ptext SLIT("is not a (visible) method of class") <+> quotes (ppr cls)
839 = ptext SLIT("Illegal binding of built-in syntax:") <+> ppr (rdrNameOcc name)
840 -- The rdrNameOcc is because we don't want to print Prelude.(,)
842 dupNamesErr :: SDoc -> [Located RdrName] -> RnM ()
843 dupNamesErr descriptor located_names
845 vcat [ptext SLIT("Conflicting definitions for") <+> quotes (ppr name1),
846 locations, descriptor]
848 L _ name1 = head located_names
849 locs = map getLoc located_names
850 big_loc = foldr1 combineSrcSpans locs
851 one_line = isOneLineSpan big_loc
852 locations | one_line = empty
853 | otherwise = ptext SLIT("Bound at:") <+>
854 vcat (map ppr (sortLe (<=) locs))
856 badQualBndrErr rdr_name
857 = ptext SLIT("Qualified name in binding position:") <+> ppr rdr_name
860 = vcat [ftext FSLIT("Accepting non-standard infix type constructor") <+> quotes (ppr op),
861 ftext FSLIT("Use -fglasgow-exts to avoid this warning")]