Use pp_item
[ghc.git] / compiler / rename / RnEnv.hs
1 {-
2 (c) The GRASP/AQUA Project, Glasgow University, 1992-2006
3
4 RnEnv contains functions which convert RdrNames into Names.
5
6 -}
7
8 {-# LANGUAGE CPP, MultiWayIf, NamedFieldPuns #-}
9
10 module RnEnv (
11 newTopSrcBinder,
12 lookupLocatedTopBndrRn, lookupTopBndrRn,
13 lookupLocatedOccRn, lookupOccRn, lookupOccRn_maybe,
14 lookupLocalOccRn_maybe, lookupInfoOccRn,
15 lookupLocalOccThLvl_maybe, lookupLocalOccRn,
16 lookupTypeOccRn,
17 lookupGlobalOccRn, lookupGlobalOccRn_maybe,
18 lookupOccRn_overloaded, lookupGlobalOccRn_overloaded, lookupExactOcc,
19
20 ChildLookupResult(..),
21 lookupSubBndrOcc_helper,
22 combineChildLookupResult, -- Called by lookupChildrenExport
23
24 HsSigCtxt(..), lookupLocalTcNames, lookupSigOccRn,
25 lookupSigCtxtOccRn,
26
27 lookupInstDeclBndr, lookupRecFieldOcc, lookupFamInstName,
28 lookupConstructorFields,
29
30 lookupGreAvailRn,
31
32 -- Rebindable Syntax
33 lookupSyntaxName, lookupSyntaxName', lookupSyntaxNames,
34 lookupIfThenElse,
35
36 -- Constructing usage information
37 addUsedGRE, addUsedGREs, addUsedDataCons,
38
39
40
41 dataTcOccs, --TODO: Move this somewhere, into utils?
42
43 ) where
44
45 #include "HsVersions.h"
46
47 import GhcPrelude
48
49 import LoadIface ( loadInterfaceForName, loadSrcInterface_maybe )
50 import IfaceEnv
51 import HsSyn
52 import RdrName
53 import HscTypes
54 import TcEnv
55 import TcRnMonad
56 import RdrHsSyn ( filterCTuple, setRdrNameSpace )
57 import TysWiredIn
58 import Name
59 import NameSet
60 import NameEnv
61 import Avail
62 import Module
63 import ConLike
64 import DataCon
65 import TyCon
66 import ErrUtils ( MsgDoc )
67 import PrelNames ( rOOT_MAIN )
68 import BasicTypes ( pprWarningTxtForMsg, TopLevelFlag(..))
69 import SrcLoc
70 import Outputable
71 import Util
72 import Maybes
73 import DynFlags
74 import FastString
75 import Control.Monad
76 import ListSetOps ( minusList )
77 import qualified GHC.LanguageExtensions as LangExt
78 import RnUnbound
79 import RnUtils
80 import qualified Data.Semigroup as Semi
81 import Data.Either ( partitionEithers )
82 import Data.List (find)
83
84 {-
85 *********************************************************
86 * *
87 Source-code binders
88 * *
89 *********************************************************
90
91 Note [Signature lazy interface loading]
92 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
93
94 GHC's lazy interface loading can be a bit confusing, so this Note is an
95 empirical description of what happens in one interesting case. When
96 compiling a signature module against an its implementation, we do NOT
97 load interface files associated with its names until after the type
98 checking phase. For example:
99
100 module ASig where
101 data T
102 f :: T -> T
103
104 Suppose we compile this with -sig-of "A is ASig":
105
106 module B where
107 data T = T
108 f T = T
109
110 module A(module B) where
111 import B
112
113 During type checking, we'll load A.hi because we need to know what the
114 RdrEnv for the module is, but we DO NOT load the interface for B.hi!
115 It's wholly unnecessary: our local definition 'data T' in ASig is all
116 the information we need to finish type checking. This is contrast to
117 type checking of ordinary Haskell files, in which we would not have the
118 local definition "data T" and would need to consult B.hi immediately.
119 (Also, this situation never occurs for hs-boot files, since you're not
120 allowed to reexport from another module.)
121
122 After type checking, we then check that the types we provided are
123 consistent with the backing implementation (in checkHiBootOrHsigIface).
124 At this point, B.hi is loaded, because we need something to compare
125 against.
126
127 I discovered this behavior when trying to figure out why type class
128 instances for Data.Map weren't in the EPS when I was type checking a
129 test very much like ASig (sigof02dm): the associated interface hadn't
130 been loaded yet! (The larger issue is a moot point, since an instance
131 declared in a signature can never be a duplicate.)
132
133 This behavior might change in the future. Consider this
134 alternate module B:
135
136 module B where
137 {-# DEPRECATED T, f "Don't use" #-}
138 data T = T
139 f T = T
140
141 One might conceivably want to report deprecation warnings when compiling
142 ASig with -sig-of B, in which case we need to look at B.hi to find the
143 deprecation warnings during renaming. At the moment, you don't get any
144 warning until you use the identifier further downstream. This would
145 require adjusting addUsedGRE so that during signature compilation,
146 we do not report deprecation warnings for LocalDef. See also
147 Note [Handling of deprecations]
148 -}
149
150 newTopSrcBinder :: Located RdrName -> RnM Name
151 newTopSrcBinder (L loc rdr_name)
152 | Just name <- isExact_maybe rdr_name
153 = -- This is here to catch
154 -- (a) Exact-name binders created by Template Haskell
155 -- (b) The PrelBase defn of (say) [] and similar, for which
156 -- the parser reads the special syntax and returns an Exact RdrName
157 -- We are at a binding site for the name, so check first that it
158 -- the current module is the correct one; otherwise GHC can get
159 -- very confused indeed. This test rejects code like
160 -- data T = (,) Int Int
161 -- unless we are in GHC.Tup
162 if isExternalName name then
163 do { this_mod <- getModule
164 ; unless (this_mod == nameModule name)
165 (addErrAt loc (badOrigBinding rdr_name))
166 ; return name }
167 else -- See Note [Binders in Template Haskell] in Convert.hs
168 do { this_mod <- getModule
169 ; externaliseName this_mod name }
170
171 | Just (rdr_mod, rdr_occ) <- isOrig_maybe rdr_name
172 = do { this_mod <- getModule
173 ; unless (rdr_mod == this_mod || rdr_mod == rOOT_MAIN)
174 (addErrAt loc (badOrigBinding rdr_name))
175 -- When reading External Core we get Orig names as binders,
176 -- but they should agree with the module gotten from the monad
177 --
178 -- We can get built-in syntax showing up here too, sadly. If you type
179 -- data T = (,,,)
180 -- the constructor is parsed as a type, and then RdrHsSyn.tyConToDataCon
181 -- uses setRdrNameSpace to make it into a data constructors. At that point
182 -- the nice Exact name for the TyCon gets swizzled to an Orig name.
183 -- Hence the badOrigBinding error message.
184 --
185 -- Except for the ":Main.main = ..." definition inserted into
186 -- the Main module; ugh!
187
188 -- Because of this latter case, we call newGlobalBinder with a module from
189 -- the RdrName, not from the environment. In principle, it'd be fine to
190 -- have an arbitrary mixture of external core definitions in a single module,
191 -- (apart from module-initialisation issues, perhaps).
192 ; newGlobalBinder rdr_mod rdr_occ loc }
193
194 | otherwise
195 = do { when (isQual rdr_name)
196 (addErrAt loc (badQualBndrErr rdr_name))
197 -- Binders should not be qualified; if they are, and with a different
198 -- module name, we get a confusing "M.T is not in scope" error later
199
200 ; stage <- getStage
201 ; if isBrackStage stage then
202 -- We are inside a TH bracket, so make an *Internal* name
203 -- See Note [Top-level Names in Template Haskell decl quotes] in RnNames
204 do { uniq <- newUnique
205 ; return (mkInternalName uniq (rdrNameOcc rdr_name) loc) }
206 else
207 do { this_mod <- getModule
208 ; traceRn "newTopSrcBinder" (ppr this_mod $$ ppr rdr_name $$ ppr loc)
209 ; newGlobalBinder this_mod (rdrNameOcc rdr_name) loc }
210 }
211
212 {-
213 *********************************************************
214 * *
215 Source code occurrences
216 * *
217 *********************************************************
218
219 Looking up a name in the RnEnv.
220
221 Note [Type and class operator definitions]
222 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
223 We want to reject all of these unless we have -XTypeOperators (#3265)
224 data a :*: b = ...
225 class a :*: b where ...
226 data (:*:) a b = ....
227 class (:*:) a b where ...
228 The latter two mean that we are not just looking for a
229 *syntactically-infix* declaration, but one that uses an operator
230 OccName. We use OccName.isSymOcc to detect that case, which isn't
231 terribly efficient, but there seems to be no better way.
232 -}
233
234 -- Can be made to not be exposed
235 -- Only used unwrapped in rnAnnProvenance
236 lookupTopBndrRn :: RdrName -> RnM Name
237 lookupTopBndrRn n = do nopt <- lookupTopBndrRn_maybe n
238 case nopt of
239 Just n' -> return n'
240 Nothing -> do traceRn "lookupTopBndrRn fail" (ppr n)
241 unboundName WL_LocalTop n
242
243 lookupLocatedTopBndrRn :: Located RdrName -> RnM (Located Name)
244 lookupLocatedTopBndrRn = wrapLocM lookupTopBndrRn
245
246 lookupTopBndrRn_maybe :: RdrName -> RnM (Maybe Name)
247 -- Look up a top-level source-code binder. We may be looking up an unqualified 'f',
248 -- and there may be several imported 'f's too, which must not confuse us.
249 -- For example, this is OK:
250 -- import Foo( f )
251 -- infix 9 f -- The 'f' here does not need to be qualified
252 -- f x = x -- Nor here, of course
253 -- So we have to filter out the non-local ones.
254 --
255 -- A separate function (importsFromLocalDecls) reports duplicate top level
256 -- decls, so here it's safe just to choose an arbitrary one.
257 --
258 -- There should never be a qualified name in a binding position in Haskell,
259 -- but there can be if we have read in an external-Core file.
260 -- The Haskell parser checks for the illegal qualified name in Haskell
261 -- source files, so we don't need to do so here.
262
263 lookupTopBndrRn_maybe rdr_name =
264 lookupExactOrOrig rdr_name Just $
265 do { -- Check for operators in type or class declarations
266 -- See Note [Type and class operator definitions]
267 let occ = rdrNameOcc rdr_name
268 ; when (isTcOcc occ && isSymOcc occ)
269 (do { op_ok <- xoptM LangExt.TypeOperators
270 ; unless op_ok (addErr (opDeclErr rdr_name)) })
271
272 ; env <- getGlobalRdrEnv
273 ; case filter isLocalGRE (lookupGRE_RdrName rdr_name env) of
274 [gre] -> return (Just (gre_name gre))
275 _ -> return Nothing -- Ambiguous (can't happen) or unbound
276 }
277
278 -----------------------------------------------
279 -- | Lookup an @Exact@ @RdrName@. See Note [Looking up Exact RdrNames].
280 -- This adds an error if the name cannot be found.
281 lookupExactOcc :: Name -> RnM Name
282 lookupExactOcc name
283 = do { result <- lookupExactOcc_either name
284 ; case result of
285 Left err -> do { addErr err
286 ; return name }
287 Right name' -> return name' }
288
289 -- | Lookup an @Exact@ @RdrName@. See Note [Looking up Exact RdrNames].
290 -- This never adds an error, but it may return one.
291 lookupExactOcc_either :: Name -> RnM (Either MsgDoc Name)
292 -- See Note [Looking up Exact RdrNames]
293 lookupExactOcc_either name
294 | Just thing <- wiredInNameTyThing_maybe name
295 , Just tycon <- case thing of
296 ATyCon tc -> Just tc
297 AConLike (RealDataCon dc) -> Just (dataConTyCon dc)
298 _ -> Nothing
299 , isTupleTyCon tycon
300 = do { checkTupSize (tyConArity tycon)
301 ; return (Right name) }
302
303 | isExternalName name
304 = return (Right name)
305
306 | otherwise
307 = do { env <- getGlobalRdrEnv
308 ; let -- See Note [Splicing Exact names]
309 main_occ = nameOccName name
310 demoted_occs = case demoteOccName main_occ of
311 Just occ -> [occ]
312 Nothing -> []
313 gres = [ gre | occ <- main_occ : demoted_occs
314 , gre <- lookupGlobalRdrEnv env occ
315 , gre_name gre == name ]
316 ; case gres of
317 [gre] -> return (Right (gre_name gre))
318
319 [] -> -- See Note [Splicing Exact names]
320 do { lcl_env <- getLocalRdrEnv
321 ; if name `inLocalRdrEnvScope` lcl_env
322 then return (Right name)
323 else
324 do { th_topnames_var <- fmap tcg_th_topnames getGblEnv
325 ; th_topnames <- readTcRef th_topnames_var
326 ; if name `elemNameSet` th_topnames
327 then return (Right name)
328 else return (Left exact_nm_err)
329 }
330 }
331 gres -> return (Left (sameNameErr gres)) -- Ugh! See Note [Template Haskell ambiguity]
332 }
333 where
334 exact_nm_err = hang (text "The exact Name" <+> quotes (ppr name) <+> ptext (sLit "is not in scope"))
335 2 (vcat [ text "Probable cause: you used a unique Template Haskell name (NameU), "
336 , text "perhaps via newName, but did not bind it"
337 , text "If that's it, then -ddump-splices might be useful" ])
338
339 sameNameErr :: [GlobalRdrElt] -> MsgDoc
340 sameNameErr [] = panic "addSameNameErr: empty list"
341 sameNameErr gres@(_ : _)
342 = hang (text "Same exact name in multiple name-spaces:")
343 2 (vcat (map pp_one sorted_names) $$ th_hint)
344 where
345 sorted_names = sortWith nameSrcLoc (map gre_name gres)
346 pp_one name
347 = hang (pprNameSpace (occNameSpace (getOccName name))
348 <+> quotes (ppr name) <> comma)
349 2 (text "declared at:" <+> ppr (nameSrcLoc name))
350
351 th_hint = vcat [ text "Probable cause: you bound a unique Template Haskell name (NameU),"
352 , text "perhaps via newName, in different name-spaces."
353 , text "If that's it, then -ddump-splices might be useful" ]
354
355
356 -----------------------------------------------
357 lookupInstDeclBndr :: Name -> SDoc -> RdrName -> RnM Name
358 -- This is called on the method name on the left-hand side of an
359 -- instance declaration binding. eg. instance Functor T where
360 -- fmap = ...
361 -- ^^^^ called on this
362 -- Regardless of how many unqualified fmaps are in scope, we want
363 -- the one that comes from the Functor class.
364 --
365 -- Furthermore, note that we take no account of whether the
366 -- name is only in scope qualified. I.e. even if method op is
367 -- in scope as M.op, we still allow plain 'op' on the LHS of
368 -- an instance decl
369 --
370 -- The "what" parameter says "method" or "associated type",
371 -- depending on what we are looking up
372 lookupInstDeclBndr cls what rdr
373 = do { when (isQual rdr)
374 (addErr (badQualBndrErr rdr))
375 -- In an instance decl you aren't allowed
376 -- to use a qualified name for the method
377 -- (Although it'd make perfect sense.)
378 ; mb_name <- lookupSubBndrOcc
379 False -- False => we don't give deprecated
380 -- warnings when a deprecated class
381 -- method is defined. We only warn
382 -- when it's used
383 cls doc rdr
384 ; case mb_name of
385 Left err -> do { addErr err; return (mkUnboundNameRdr rdr) }
386 Right nm -> return nm }
387 where
388 doc = what <+> text "of class" <+> quotes (ppr cls)
389
390 -----------------------------------------------
391 lookupFamInstName :: Maybe Name -> Located RdrName
392 -> RnM (Located Name)
393 -- Used for TyData and TySynonym family instances only,
394 -- See Note [Family instance binders]
395 lookupFamInstName (Just cls) tc_rdr -- Associated type; c.f RnBinds.rnMethodBind
396 = wrapLocM (lookupInstDeclBndr cls (text "associated type")) tc_rdr
397 lookupFamInstName Nothing tc_rdr -- Family instance; tc_rdr is an *occurrence*
398 = lookupLocatedOccRn tc_rdr
399
400 -----------------------------------------------
401 lookupConstructorFields :: Name -> RnM [FieldLabel]
402 -- Look up the fields of a given constructor
403 -- * For constructors from this module, use the record field env,
404 -- which is itself gathered from the (as yet un-typechecked)
405 -- data type decls
406 --
407 -- * For constructors from imported modules, use the *type* environment
408 -- since imported modles are already compiled, the info is conveniently
409 -- right there
410
411 lookupConstructorFields con_name
412 = do { this_mod <- getModule
413 ; if nameIsLocalOrFrom this_mod con_name then
414 do { field_env <- getRecFieldEnv
415 ; traceTc "lookupCF" (ppr con_name $$ ppr (lookupNameEnv field_env con_name) $$ ppr field_env)
416 ; return (lookupNameEnv field_env con_name `orElse` []) }
417 else
418 do { con <- tcLookupConLike con_name
419 ; traceTc "lookupCF 2" (ppr con)
420 ; return (conLikeFieldLabels con) } }
421
422
423 -- In CPS style as `RnM r` is monadic
424 lookupExactOrOrig :: RdrName -> (Name -> r) -> RnM r -> RnM r
425 lookupExactOrOrig rdr_name res k
426 | Just n <- isExact_maybe rdr_name -- This happens in derived code
427 = res <$> lookupExactOcc n
428 | Just (rdr_mod, rdr_occ) <- isOrig_maybe rdr_name
429 = res <$> lookupOrig rdr_mod rdr_occ
430 | otherwise = k
431
432
433
434 -----------------------------------------------
435 -- | Look up an occurrence of a field in record construction or pattern
436 -- matching (but not update). When the -XDisambiguateRecordFields
437 -- flag is on, take account of the data constructor name to
438 -- disambiguate which field to use.
439 --
440 -- See Note [DisambiguateRecordFields].
441 lookupRecFieldOcc :: Maybe Name -- Nothing => just look it up as usual
442 -- Just con => use data con to disambiguate
443 -> RdrName
444 -> RnM Name
445 lookupRecFieldOcc mb_con rdr_name
446 | Just con <- mb_con
447 , isUnboundName con -- Avoid error cascade
448 = return (mkUnboundNameRdr rdr_name)
449 | Just con <- mb_con
450 = do { flds <- lookupConstructorFields con
451 ; env <- getGlobalRdrEnv
452 ; let lbl = occNameFS (rdrNameOcc rdr_name)
453 mb_field = do fl <- find ((== lbl) . flLabel) flds
454 -- We have the label, now check it is in
455 -- scope (with the correct qualifier if
456 -- there is one, hence calling pickGREs).
457 gre <- lookupGRE_FieldLabel env fl
458 guard (not (isQual rdr_name
459 && null (pickGREs rdr_name [gre])))
460 return (fl, gre)
461 ; case mb_field of
462 Just (fl, gre) -> do { addUsedGRE True gre
463 ; return (flSelector fl) }
464 Nothing -> lookupGlobalOccRn rdr_name }
465 -- See Note [Fall back on lookupGlobalOccRn in lookupRecFieldOcc]
466 | otherwise
467 -- This use of Global is right as we are looking up a selector which
468 -- can only be defined at the top level.
469 = lookupGlobalOccRn rdr_name
470
471 {- Note [DisambiguateRecordFields]
472 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
473 When we are looking up record fields in record construction or pattern
474 matching, we can take advantage of the data constructor name to
475 resolve fields that would otherwise be ambiguous (provided the
476 -XDisambiguateRecordFields flag is on).
477
478 For example, consider:
479
480 data S = MkS { x :: Int }
481 data T = MkT { x :: Int }
482
483 e = MkS { x = 3 }
484
485 When we are renaming the occurrence of `x` in `e`, instead of looking
486 `x` up directly (and finding both fields), lookupRecFieldOcc will
487 search the fields of `MkS` to find the only possible `x` the user can
488 mean.
489
490 Of course, we still have to check the field is in scope, using
491 lookupGRE_FieldLabel. The handling of qualified imports is slightly
492 subtle: the occurrence may be unqualified even if the field is
493 imported only qualified (but if the occurrence is qualified, the
494 qualifier must be correct). For example:
495
496 module A where
497 data S = MkS { x :: Int }
498 data T = MkT { x :: Int }
499
500 module B where
501 import qualified A (S(..))
502 import A (T(MkT))
503
504 e1 = MkT { x = 3 } -- x not in scope, so fail
505 e2 = A.MkS { B.x = 3 } -- module qualifier is wrong, so fail
506 e3 = A.MkS { x = 3 } -- x in scope (lack of module qualifier permitted)
507
508 In case `e1`, lookupGRE_FieldLabel will return Nothing. In case `e2`,
509 lookupGRE_FieldLabel will return the GRE for `A.x`, but then the guard
510 will fail because the field RdrName `B.x` is qualified and pickGREs
511 rejects the GRE. In case `e3`, lookupGRE_FieldLabel will return the
512 GRE for `A.x` and the guard will succeed because the field RdrName `x`
513 is unqualified.
514
515
516 Note [Fall back on lookupGlobalOccRn in lookupRecFieldOcc]
517 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
518 Whenever we fail to find the field or it is not in scope, mb_field
519 will be False, and we fall back on looking it up normally using
520 lookupGlobalOccRn. We don't report an error immediately because the
521 actual problem might be located elsewhere. For example (#9975):
522
523 data Test = Test { x :: Int }
524 pattern Test wat = Test { x = wat }
525
526 Here there are multiple declarations of Test (as a data constructor
527 and as a pattern synonym), which will be reported as an error. We
528 shouldn't also report an error about the occurrence of `x` in the
529 pattern synonym RHS. However, if the pattern synonym gets added to
530 the environment first, we will try and fail to find `x` amongst the
531 (nonexistent) fields of the pattern synonym.
532
533 Alternatively, the scope check can fail due to Template Haskell.
534 Consider (#12130):
535
536 module Foo where
537 import M
538 b = $(funny)
539
540 module M(funny) where
541 data T = MkT { x :: Int }
542 funny :: Q Exp
543 funny = [| MkT { x = 3 } |]
544
545 When we splice, `MkT` is not lexically in scope, so
546 lookupGRE_FieldLabel will fail. But there is no need for
547 disambiguation anyway, because `x` is an original name, and
548 lookupGlobalOccRn will find it.
549 -}
550
551
552
553 -- | Used in export lists to lookup the children.
554 lookupSubBndrOcc_helper :: Bool -> Bool -> Name -> RdrName
555 -> RnM ChildLookupResult
556 lookupSubBndrOcc_helper must_have_parent warn_if_deprec parent rdr_name
557 | isUnboundName parent
558 -- Avoid an error cascade
559 = return (FoundName NoParent (mkUnboundNameRdr rdr_name))
560
561 | otherwise = do
562 gre_env <- getGlobalRdrEnv
563
564 let original_gres = lookupGlobalRdrEnv gre_env (rdrNameOcc rdr_name)
565 -- Disambiguate the lookup based on the parent information.
566 -- The remaining GREs are things that we *could* export here, note that
567 -- this includes things which have `NoParent`. Those are sorted in
568 -- `checkPatSynParent`.
569 traceRn "parent" (ppr parent)
570 traceRn "lookupExportChild original_gres:" (ppr original_gres)
571 traceRn "lookupExportChild picked_gres:" (ppr $ picked_gres original_gres)
572 case picked_gres original_gres of
573 NoOccurrence ->
574 noMatchingParentErr original_gres
575 UniqueOccurrence g ->
576 if must_have_parent then noMatchingParentErr original_gres
577 else checkFld g
578 DisambiguatedOccurrence g ->
579 checkFld g
580 AmbiguousOccurrence gres ->
581 mkNameClashErr gres
582 where
583 -- Convert into FieldLabel if necessary
584 checkFld :: GlobalRdrElt -> RnM ChildLookupResult
585 checkFld g@GRE{gre_name, gre_par} = do
586 addUsedGRE warn_if_deprec g
587 return $ case gre_par of
588 FldParent _ mfs ->
589 FoundFL (fldParentToFieldLabel gre_name mfs)
590 _ -> FoundName gre_par gre_name
591
592 fldParentToFieldLabel :: Name -> Maybe FastString -> FieldLabel
593 fldParentToFieldLabel name mfs =
594 case mfs of
595 Nothing ->
596 let fs = occNameFS (nameOccName name)
597 in FieldLabel fs False name
598 Just fs -> FieldLabel fs True name
599
600 -- Called when we find no matching GREs after disambiguation but
601 -- there are three situations where this happens.
602 -- 1. There were none to begin with.
603 -- 2. None of the matching ones were the parent but
604 -- a. They were from an overloaded record field so we can report
605 -- a better error
606 -- b. The original lookup was actually ambiguous.
607 -- For example, the case where overloading is off and two
608 -- record fields are in scope from different record
609 -- constructors, neither of which is the parent.
610 noMatchingParentErr :: [GlobalRdrElt] -> RnM ChildLookupResult
611 noMatchingParentErr original_gres = do
612 overload_ok <- xoptM LangExt.DuplicateRecordFields
613 case original_gres of
614 [] -> return NameNotFound
615 [g] -> return $ IncorrectParent parent
616 (gre_name g) (ppr $ gre_name g)
617 [p | Just p <- [getParent g]]
618 gss@(g:_:_) ->
619 if all isRecFldGRE gss && overload_ok
620 then return $
621 IncorrectParent parent
622 (gre_name g)
623 (ppr $ expectJust "noMatchingParentErr" (greLabel g))
624 [p | x <- gss, Just p <- [getParent x]]
625 else mkNameClashErr gss
626
627 mkNameClashErr :: [GlobalRdrElt] -> RnM ChildLookupResult
628 mkNameClashErr gres = do
629 addNameClashErrRn rdr_name gres
630 return (FoundName (gre_par (head gres)) (gre_name (head gres)))
631
632 getParent :: GlobalRdrElt -> Maybe Name
633 getParent (GRE { gre_par = p } ) =
634 case p of
635 ParentIs cur_parent -> Just cur_parent
636 FldParent { par_is = cur_parent } -> Just cur_parent
637 NoParent -> Nothing
638
639 picked_gres :: [GlobalRdrElt] -> DisambigInfo
640 -- For Unqual, find GREs that are in scope qualified or unqualified
641 -- For Qual, find GREs that are in scope with that qualification
642 picked_gres gres
643 | isUnqual rdr_name
644 = mconcat (map right_parent gres)
645 | otherwise
646 = mconcat (map right_parent (pickGREs rdr_name gres))
647
648 right_parent :: GlobalRdrElt -> DisambigInfo
649 right_parent p
650 = case getParent p of
651 Just cur_parent
652 | parent == cur_parent -> DisambiguatedOccurrence p
653 | otherwise -> NoOccurrence
654 Nothing -> UniqueOccurrence p
655
656
657 -- This domain specific datatype is used to record why we decided it was
658 -- possible that a GRE could be exported with a parent.
659 data DisambigInfo
660 = NoOccurrence
661 -- The GRE could never be exported. It has the wrong parent.
662 | UniqueOccurrence GlobalRdrElt
663 -- The GRE has no parent. It could be a pattern synonym.
664 | DisambiguatedOccurrence GlobalRdrElt
665 -- The parent of the GRE is the correct parent
666 | AmbiguousOccurrence [GlobalRdrElt]
667 -- For example, two normal identifiers with the same name are in
668 -- scope. They will both be resolved to "UniqueOccurrence" and the
669 -- monoid will combine them to this failing case.
670
671 instance Outputable DisambigInfo where
672 ppr NoOccurrence = text "NoOccurence"
673 ppr (UniqueOccurrence gre) = text "UniqueOccurrence:" <+> ppr gre
674 ppr (DisambiguatedOccurrence gre) = text "DiambiguatedOccurrence:" <+> ppr gre
675 ppr (AmbiguousOccurrence gres) = text "Ambiguous:" <+> ppr gres
676
677 instance Semi.Semigroup DisambigInfo where
678 -- This is the key line: We prefer disambiguated occurrences to other
679 -- names.
680 _ <> DisambiguatedOccurrence g' = DisambiguatedOccurrence g'
681 DisambiguatedOccurrence g' <> _ = DisambiguatedOccurrence g'
682
683 NoOccurrence <> m = m
684 m <> NoOccurrence = m
685 UniqueOccurrence g <> UniqueOccurrence g'
686 = AmbiguousOccurrence [g, g']
687 UniqueOccurrence g <> AmbiguousOccurrence gs
688 = AmbiguousOccurrence (g:gs)
689 AmbiguousOccurrence gs <> UniqueOccurrence g'
690 = AmbiguousOccurrence (g':gs)
691 AmbiguousOccurrence gs <> AmbiguousOccurrence gs'
692 = AmbiguousOccurrence (gs ++ gs')
693
694 instance Monoid DisambigInfo where
695 mempty = NoOccurrence
696 mappend = (Semi.<>)
697
698 -- Lookup SubBndrOcc can never be ambiguous
699 --
700 -- Records the result of looking up a child.
701 data ChildLookupResult
702 = NameNotFound -- We couldn't find a suitable name
703 | IncorrectParent Name -- Parent
704 Name -- Name of thing we were looking for
705 SDoc -- How to print the name
706 [Name] -- List of possible parents
707 | FoundName Parent Name -- We resolved to a normal name
708 | FoundFL FieldLabel -- We resolved to a FL
709
710 -- | Specialised version of msum for RnM ChildLookupResult
711 combineChildLookupResult :: [RnM ChildLookupResult] -> RnM ChildLookupResult
712 combineChildLookupResult [] = return NameNotFound
713 combineChildLookupResult (x:xs) = do
714 res <- x
715 case res of
716 NameNotFound -> combineChildLookupResult xs
717 _ -> return res
718
719 instance Outputable ChildLookupResult where
720 ppr NameNotFound = text "NameNotFound"
721 ppr (FoundName p n) = text "Found:" <+> ppr p <+> ppr n
722 ppr (FoundFL fls) = text "FoundFL:" <+> ppr fls
723 ppr (IncorrectParent p n td ns) = text "IncorrectParent"
724 <+> hsep [ppr p, ppr n, td, ppr ns]
725
726 lookupSubBndrOcc :: Bool
727 -> Name -- Parent
728 -> SDoc
729 -> RdrName
730 -> RnM (Either MsgDoc Name)
731 -- Find all the things the rdr-name maps to
732 -- and pick the one with the right parent namep
733 lookupSubBndrOcc warn_if_deprec the_parent doc rdr_name = do
734 res <-
735 lookupExactOrOrig rdr_name (FoundName NoParent) $
736 -- This happens for built-in classes, see mod052 for example
737 lookupSubBndrOcc_helper True warn_if_deprec the_parent rdr_name
738 case res of
739 NameNotFound -> return (Left (unknownSubordinateErr doc rdr_name))
740 FoundName _p n -> return (Right n)
741 FoundFL fl -> return (Right (flSelector fl))
742 IncorrectParent {}
743 -- See [Mismatched class methods and associated type families]
744 -- in TcInstDecls.
745 -> return $ Left (unknownSubordinateErr doc rdr_name)
746
747 {-
748 Note [Family instance binders]
749 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
750 Consider
751 data family F a
752 data instance F T = X1 | X2
753
754 The 'data instance' decl has an *occurrence* of F (and T), and *binds*
755 X1 and X2. (This is unlike a normal data type declaration which would
756 bind F too.) So we want an AvailTC F [X1,X2].
757
758 Now consider a similar pair:
759 class C a where
760 data G a
761 instance C S where
762 data G S = Y1 | Y2
763
764 The 'data G S' *binds* Y1 and Y2, and has an *occurrence* of G.
765
766 But there is a small complication: in an instance decl, we don't use
767 qualified names on the LHS; instead we use the class to disambiguate.
768 Thus:
769 module M where
770 import Blib( G )
771 class C a where
772 data G a
773 instance C S where
774 data G S = Y1 | Y2
775 Even though there are two G's in scope (M.G and Blib.G), the occurrence
776 of 'G' in the 'instance C S' decl is unambiguous, because C has only
777 one associated type called G. This is exactly what happens for methods,
778 and it is only consistent to do the same thing for types. That's the
779 role of the function lookupTcdName; the (Maybe Name) give the class of
780 the encloseing instance decl, if any.
781
782 Note [Looking up Exact RdrNames]
783 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
784 Exact RdrNames are generated by Template Haskell. See Note [Binders
785 in Template Haskell] in Convert.
786
787 For data types and classes have Exact system Names in the binding
788 positions for constructors, TyCons etc. For example
789 [d| data T = MkT Int |]
790 when we splice in and Convert to HsSyn RdrName, we'll get
791 data (Exact (system Name "T")) = (Exact (system Name "MkT")) ...
792 These System names are generated by Convert.thRdrName
793
794 But, constructors and the like need External Names, not System Names!
795 So we do the following
796
797 * In RnEnv.newTopSrcBinder we spot Exact RdrNames that wrap a
798 non-External Name, and make an External name for it. This is
799 the name that goes in the GlobalRdrEnv
800
801 * When looking up an occurrence of an Exact name, done in
802 RnEnv.lookupExactOcc, we find the Name with the right unique in the
803 GlobalRdrEnv, and use the one from the envt -- it will be an
804 External Name in the case of the data type/constructor above.
805
806 * Exact names are also use for purely local binders generated
807 by TH, such as \x_33. x_33
808 Both binder and occurrence are Exact RdrNames. The occurrence
809 gets looked up in the LocalRdrEnv by RnEnv.lookupOccRn, and
810 misses, because lookupLocalRdrEnv always returns Nothing for
811 an Exact Name. Now we fall through to lookupExactOcc, which
812 will find the Name is not in the GlobalRdrEnv, so we just use
813 the Exact supplied Name.
814
815 Note [Splicing Exact names]
816 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
817 Consider the splice $(do { x <- newName "x"; return (VarE x) })
818 This will generate a (HsExpr RdrName) term that mentions the
819 Exact RdrName "x_56" (or whatever), but does not bind it. So
820 when looking such Exact names we want to check that it's in scope,
821 otherwise the type checker will get confused. To do this we need to
822 keep track of all the Names in scope, and the LocalRdrEnv does just that;
823 we consult it with RdrName.inLocalRdrEnvScope.
824
825 There is another wrinkle. With TH and -XDataKinds, consider
826 $( [d| data Nat = Zero
827 data T = MkT (Proxy 'Zero) |] )
828 After splicing, but before renaming we get this:
829 data Nat_77{tc} = Zero_78{d}
830 data T_79{tc} = MkT_80{d} (Proxy 'Zero_78{tc}) |] )
831 The occurrence of 'Zero in the data type for T has the right unique,
832 but it has a TcClsName name-space in its OccName. (This is set by
833 the ctxt_ns argument of Convert.thRdrName.) When we check that is
834 in scope in the GlobalRdrEnv, we need to look up the DataName namespace
835 too. (An alternative would be to make the GlobalRdrEnv also have
836 a Name -> GRE mapping.)
837
838 Note [Template Haskell ambiguity]
839 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
840 The GlobalRdrEnv invariant says that if
841 occ -> [gre1, ..., gren]
842 then the gres have distinct Names (INVARIANT 1 of GlobalRdrEnv).
843 This is guaranteed by extendGlobalRdrEnvRn (the dups check in add_gre).
844
845 So how can we get multiple gres in lookupExactOcc_maybe? Because in
846 TH we might use the same TH NameU in two different name spaces.
847 eg (#7241):
848 $(newName "Foo" >>= \o -> return [DataD [] o [] [RecC o []] [''Show]])
849 Here we generate a type constructor and data constructor with the same
850 unique, but different name spaces.
851
852 It'd be nicer to rule this out in extendGlobalRdrEnvRn, but that would
853 mean looking up the OccName in every name-space, just in case, and that
854 seems a bit brutal. So it's just done here on lookup. But we might
855 need to revisit that choice.
856
857 Note [Usage for sub-bndrs]
858 ~~~~~~~~~~~~~~~~~~~~~~~~~~
859 If you have this
860 import qualified M( C( f ) )
861 instance M.C T where
862 f x = x
863 then is the qualified import M.f used? Obviously yes.
864 But the RdrName used in the instance decl is unqualified. In effect,
865 we fill in the qualification by looking for f's whose class is M.C
866 But when adding to the UsedRdrNames we must make that qualification
867 explicit (saying "used M.f"), otherwise we get "Redundant import of M.f".
868
869 So we make up a suitable (fake) RdrName. But be careful
870 import qualified M
871 import M( C(f) )
872 instance C T where
873 f x = x
874 Here we want to record a use of 'f', not of 'M.f', otherwise
875 we'll miss the fact that the qualified import is redundant.
876
877 --------------------------------------------------
878 -- Occurrences
879 --------------------------------------------------
880 -}
881
882
883 lookupLocatedOccRn :: Located RdrName -> RnM (Located Name)
884 lookupLocatedOccRn = wrapLocM lookupOccRn
885
886 lookupLocalOccRn_maybe :: RdrName -> RnM (Maybe Name)
887 -- Just look in the local environment
888 lookupLocalOccRn_maybe rdr_name
889 = do { local_env <- getLocalRdrEnv
890 ; return (lookupLocalRdrEnv local_env rdr_name) }
891
892 lookupLocalOccThLvl_maybe :: Name -> RnM (Maybe (TopLevelFlag, ThLevel))
893 -- Just look in the local environment
894 lookupLocalOccThLvl_maybe name
895 = do { lcl_env <- getLclEnv
896 ; return (lookupNameEnv (tcl_th_bndrs lcl_env) name) }
897
898 -- lookupOccRn looks up an occurrence of a RdrName
899 lookupOccRn :: RdrName -> RnM Name
900 lookupOccRn rdr_name
901 = do { mb_name <- lookupOccRn_maybe rdr_name
902 ; case mb_name of
903 Just name -> return name
904 Nothing -> reportUnboundName rdr_name }
905
906 -- Only used in one place, to rename pattern synonym binders.
907 -- See Note [Renaming pattern synonym variables] in RnBinds
908 lookupLocalOccRn :: RdrName -> RnM Name
909 lookupLocalOccRn rdr_name
910 = do { mb_name <- lookupLocalOccRn_maybe rdr_name
911 ; case mb_name of
912 Just name -> return name
913 Nothing -> unboundName WL_LocalOnly rdr_name }
914
915 -- lookupPromotedOccRn looks up an optionally promoted RdrName.
916 lookupTypeOccRn :: RdrName -> RnM Name
917 -- see Note [Demotion]
918 lookupTypeOccRn rdr_name
919 | isVarOcc (rdrNameOcc rdr_name) -- See Note [Promoted variables in types]
920 = badVarInType rdr_name
921 | otherwise
922 = do { mb_name <- lookupOccRn_maybe rdr_name
923 ; case mb_name of
924 Just name -> return name
925 Nothing -> lookup_demoted rdr_name }
926
927 lookup_demoted :: RdrName -> RnM Name
928 lookup_demoted rdr_name
929 | Just demoted_rdr <- demoteRdrName rdr_name
930 -- Maybe it's the name of a *data* constructor
931 = do { data_kinds <- xoptM LangExt.DataKinds
932 ; star_is_type <- xoptM LangExt.StarIsType
933 ; let star_info = starInfo star_is_type rdr_name
934 ; if data_kinds
935 then do { mb_demoted_name <- lookupOccRn_maybe demoted_rdr
936 ; case mb_demoted_name of
937 Nothing -> unboundNameX WL_Any rdr_name star_info
938 Just demoted_name ->
939 do { whenWOptM Opt_WarnUntickedPromotedConstructors $
940 addWarn
941 (Reason Opt_WarnUntickedPromotedConstructors)
942 (untickedPromConstrWarn demoted_name)
943 ; return demoted_name } }
944 else do { -- We need to check if a data constructor of this name is
945 -- in scope to give good error messages. However, we do
946 -- not want to give an additional error if the data
947 -- constructor happens to be out of scope! See #13947.
948 mb_demoted_name <- discardErrs $
949 lookupOccRn_maybe demoted_rdr
950 ; let suggestion | isJust mb_demoted_name = suggest_dk
951 | otherwise = star_info
952 ; unboundNameX WL_Any rdr_name suggestion } }
953
954 | otherwise
955 = reportUnboundName rdr_name
956
957 where
958 suggest_dk = text "A data constructor of that name is in scope; did you mean DataKinds?"
959 untickedPromConstrWarn name =
960 text "Unticked promoted constructor" <> colon <+> quotes (ppr name) <> dot
961 $$
962 hsep [ text "Use"
963 , quotes (char '\'' <> ppr name)
964 , text "instead of"
965 , quotes (ppr name) <> dot ]
966
967 badVarInType :: RdrName -> RnM Name
968 badVarInType rdr_name
969 = do { addErr (text "Illegal promoted term variable in a type:"
970 <+> ppr rdr_name)
971 ; return (mkUnboundNameRdr rdr_name) }
972
973 {- Note [Promoted variables in types]
974 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
975 Consider this (#12686):
976 x = True
977 data Bad = Bad 'x
978
979 The parser treats the quote in 'x as saying "use the term
980 namespace", so we'll get (Bad x{v}), with 'x' in the
981 VarName namespace. If we don't test for this, the renamer
982 will happily rename it to the x bound at top level, and then
983 the typecheck falls over because it doesn't have 'x' in scope
984 when kind-checking.
985
986 Note [Demotion]
987 ~~~~~~~~~~~~~~~
988 When the user writes:
989 data Nat = Zero | Succ Nat
990 foo :: f Zero -> Int
991
992 'Zero' in the type signature of 'foo' is parsed as:
993 HsTyVar ("Zero", TcClsName)
994
995 When the renamer hits this occurrence of 'Zero' it's going to realise
996 that it's not in scope. But because it is renaming a type, it knows
997 that 'Zero' might be a promoted data constructor, so it will demote
998 its namespace to DataName and do a second lookup.
999
1000 The final result (after the renamer) will be:
1001 HsTyVar ("Zero", DataName)
1002 -}
1003
1004 lookupOccRnX_maybe :: (RdrName -> RnM (Maybe r)) -> (Name -> r) -> RdrName
1005 -> RnM (Maybe r)
1006 lookupOccRnX_maybe globalLookup wrapper rdr_name
1007 = runMaybeT . msum . map MaybeT $
1008 [ fmap wrapper <$> lookupLocalOccRn_maybe rdr_name
1009 , globalLookup rdr_name ]
1010
1011 lookupOccRn_maybe :: RdrName -> RnM (Maybe Name)
1012 lookupOccRn_maybe = lookupOccRnX_maybe lookupGlobalOccRn_maybe id
1013
1014 lookupOccRn_overloaded :: Bool -> RdrName
1015 -> RnM (Maybe (Either Name [Name]))
1016 lookupOccRn_overloaded overload_ok
1017 = lookupOccRnX_maybe global_lookup Left
1018 where
1019 global_lookup :: RdrName -> RnM (Maybe (Either Name [Name]))
1020 global_lookup n =
1021 runMaybeT . msum . map MaybeT $
1022 [ lookupGlobalOccRn_overloaded overload_ok n
1023 , fmap Left . listToMaybe <$> lookupQualifiedNameGHCi n ]
1024
1025
1026
1027 lookupGlobalOccRn_maybe :: RdrName -> RnM (Maybe Name)
1028 -- Looks up a RdrName occurrence in the top-level
1029 -- environment, including using lookupQualifiedNameGHCi
1030 -- for the GHCi case
1031 -- No filter function; does not report an error on failure
1032 -- Uses addUsedRdrName to record use and deprecations
1033 lookupGlobalOccRn_maybe rdr_name =
1034 lookupExactOrOrig rdr_name Just $
1035 runMaybeT . msum . map MaybeT $
1036 [ fmap gre_name <$> lookupGreRn_maybe rdr_name
1037 , listToMaybe <$> lookupQualifiedNameGHCi rdr_name ]
1038 -- This test is not expensive,
1039 -- and only happens for failed lookups
1040
1041 lookupGlobalOccRn :: RdrName -> RnM Name
1042 -- lookupGlobalOccRn is like lookupOccRn, except that it looks in the global
1043 -- environment. Adds an error message if the RdrName is not in scope.
1044 -- You usually want to use "lookupOccRn" which also looks in the local
1045 -- environment.
1046 lookupGlobalOccRn rdr_name
1047 = do { mb_name <- lookupGlobalOccRn_maybe rdr_name
1048 ; case mb_name of
1049 Just n -> return n
1050 Nothing -> do { traceRn "lookupGlobalOccRn" (ppr rdr_name)
1051 ; unboundName WL_Global rdr_name } }
1052
1053 lookupInfoOccRn :: RdrName -> RnM [Name]
1054 -- lookupInfoOccRn is intended for use in GHCi's ":info" command
1055 -- It finds all the GREs that RdrName could mean, not complaining
1056 -- about ambiguity, but rather returning them all
1057 -- C.f. #9881
1058 lookupInfoOccRn rdr_name =
1059 lookupExactOrOrig rdr_name (:[]) $
1060 do { rdr_env <- getGlobalRdrEnv
1061 ; let ns = map gre_name (lookupGRE_RdrName rdr_name rdr_env)
1062 ; qual_ns <- lookupQualifiedNameGHCi rdr_name
1063 ; return (ns ++ (qual_ns `minusList` ns)) }
1064
1065 -- | Like 'lookupOccRn_maybe', but with a more informative result if
1066 -- the 'RdrName' happens to be a record selector:
1067 --
1068 -- * Nothing -> name not in scope (no error reported)
1069 -- * Just (Left x) -> name uniquely refers to x,
1070 -- or there is a name clash (reported)
1071 -- * Just (Right xs) -> name refers to one or more record selectors;
1072 -- if overload_ok was False, this list will be
1073 -- a singleton.
1074
1075 lookupGlobalOccRn_overloaded :: Bool -> RdrName
1076 -> RnM (Maybe (Either Name [Name]))
1077 lookupGlobalOccRn_overloaded overload_ok rdr_name =
1078 lookupExactOrOrig rdr_name (Just . Left) $
1079 do { res <- lookupGreRn_helper rdr_name
1080 ; case res of
1081 GreNotFound -> return Nothing
1082 OneNameMatch gre -> do
1083 let wrapper = if isRecFldGRE gre then Right . (:[]) else Left
1084 return $ Just (wrapper (gre_name gre))
1085 MultipleNames gres | all isRecFldGRE gres && overload_ok ->
1086 -- Don't record usage for ambiguous selectors
1087 -- until we know which is meant
1088 return $ Just (Right (map gre_name gres))
1089 MultipleNames gres -> do
1090 addNameClashErrRn rdr_name gres
1091 return (Just (Left (gre_name (head gres)))) }
1092
1093
1094 --------------------------------------------------
1095 -- Lookup in the Global RdrEnv of the module
1096 --------------------------------------------------
1097
1098 data GreLookupResult = GreNotFound
1099 | OneNameMatch GlobalRdrElt
1100 | MultipleNames [GlobalRdrElt]
1101
1102 lookupGreRn_maybe :: RdrName -> RnM (Maybe GlobalRdrElt)
1103 -- Look up the RdrName in the GlobalRdrEnv
1104 -- Exactly one binding: records it as "used", return (Just gre)
1105 -- No bindings: return Nothing
1106 -- Many bindings: report "ambiguous", return an arbitrary (Just gre)
1107 -- Uses addUsedRdrName to record use and deprecations
1108 lookupGreRn_maybe rdr_name
1109 = do
1110 res <- lookupGreRn_helper rdr_name
1111 case res of
1112 OneNameMatch gre -> return $ Just gre
1113 MultipleNames gres -> do
1114 traceRn "lookupGreRn_maybe:NameClash" (ppr gres)
1115 addNameClashErrRn rdr_name gres
1116 return $ Just (head gres)
1117 GreNotFound -> return Nothing
1118
1119 {-
1120
1121 Note [ Unbound vs Ambiguous Names ]
1122
1123 lookupGreRn_maybe deals with failures in two different ways. If a name
1124 is unbound then we return a `Nothing` but if the name is ambiguous
1125 then we raise an error and return a dummy name.
1126
1127 The reason for this is that when we call `lookupGreRn_maybe` we are
1128 speculatively looking for whatever we are looking up. If we don't find it,
1129 then we might have been looking for the wrong thing and can keep trying.
1130 On the other hand, if we find a clash then there is no way to recover as
1131 we found the thing we were looking for but can no longer resolve which
1132 the correct one is.
1133
1134 One example of this is in `lookupTypeOccRn` which first looks in the type
1135 constructor namespace before looking in the data constructor namespace to
1136 deal with `DataKinds`.
1137
1138 There is however, as always, one exception to this scheme. If we find
1139 an ambiguous occurence of a record selector and DuplicateRecordFields
1140 is enabled then we defer the selection until the typechecker.
1141
1142 -}
1143
1144
1145
1146
1147 -- Internal Function
1148 lookupGreRn_helper :: RdrName -> RnM GreLookupResult
1149 lookupGreRn_helper rdr_name
1150 = do { env <- getGlobalRdrEnv
1151 ; case lookupGRE_RdrName rdr_name env of
1152 [] -> return GreNotFound
1153 [gre] -> do { addUsedGRE True gre
1154 ; return (OneNameMatch gre) }
1155 gres -> return (MultipleNames gres) }
1156
1157 lookupGreAvailRn :: RdrName -> RnM (Name, AvailInfo)
1158 -- Used in export lists
1159 -- If not found or ambiguous, add error message, and fake with UnboundName
1160 -- Uses addUsedRdrName to record use and deprecations
1161 lookupGreAvailRn rdr_name
1162 = do
1163 mb_gre <- lookupGreRn_helper rdr_name
1164 case mb_gre of
1165 GreNotFound ->
1166 do
1167 traceRn "lookupGreAvailRn" (ppr rdr_name)
1168 name <- unboundName WL_Global rdr_name
1169 return (name, avail name)
1170 MultipleNames gres ->
1171 do
1172 addNameClashErrRn rdr_name gres
1173 let unbound_name = mkUnboundNameRdr rdr_name
1174 return (unbound_name, avail unbound_name)
1175 -- Returning an unbound name here prevents an error
1176 -- cascade
1177 OneNameMatch gre ->
1178 return (gre_name gre, availFromGRE gre)
1179
1180
1181 {-
1182 *********************************************************
1183 * *
1184 Deprecations
1185 * *
1186 *********************************************************
1187
1188 Note [Handling of deprecations]
1189 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1190 * We report deprecations at each *occurrence* of the deprecated thing
1191 (see #5867)
1192
1193 * We do not report deprecations for locally-defined names. For a
1194 start, we may be exporting a deprecated thing. Also we may use a
1195 deprecated thing in the defn of another deprecated things. We may
1196 even use a deprecated thing in the defn of a non-deprecated thing,
1197 when changing a module's interface.
1198
1199 * addUsedGREs: we do not report deprecations for sub-binders:
1200 - the ".." completion for records
1201 - the ".." in an export item 'T(..)'
1202 - the things exported by a module export 'module M'
1203 -}
1204
1205 addUsedDataCons :: GlobalRdrEnv -> TyCon -> RnM ()
1206 -- Remember use of in-scope data constructors (#7969)
1207 addUsedDataCons rdr_env tycon
1208 = addUsedGREs [ gre
1209 | dc <- tyConDataCons tycon
1210 , Just gre <- [lookupGRE_Name rdr_env (dataConName dc)] ]
1211
1212 addUsedGRE :: Bool -> GlobalRdrElt -> RnM ()
1213 -- Called for both local and imported things
1214 -- Add usage *and* warn if deprecated
1215 addUsedGRE warn_if_deprec gre
1216 = do { when warn_if_deprec (warnIfDeprecated gre)
1217 ; unless (isLocalGRE gre) $
1218 do { env <- getGblEnv
1219 ; traceRn "addUsedGRE" (ppr gre)
1220 ; updMutVar (tcg_used_gres env) (gre :) } }
1221
1222 addUsedGREs :: [GlobalRdrElt] -> RnM ()
1223 -- Record uses of any *imported* GREs
1224 -- Used for recording used sub-bndrs
1225 -- NB: no call to warnIfDeprecated; see Note [Handling of deprecations]
1226 addUsedGREs gres
1227 | null imp_gres = return ()
1228 | otherwise = do { env <- getGblEnv
1229 ; traceRn "addUsedGREs" (ppr imp_gres)
1230 ; updMutVar (tcg_used_gres env) (imp_gres ++) }
1231 where
1232 imp_gres = filterOut isLocalGRE gres
1233
1234 warnIfDeprecated :: GlobalRdrElt -> RnM ()
1235 warnIfDeprecated gre@(GRE { gre_name = name, gre_imp = iss })
1236 | (imp_spec : _) <- iss
1237 = do { dflags <- getDynFlags
1238 ; this_mod <- getModule
1239 ; when (wopt Opt_WarnWarningsDeprecations dflags &&
1240 not (nameIsLocalOrFrom this_mod name)) $
1241 -- See Note [Handling of deprecations]
1242 do { iface <- loadInterfaceForName doc name
1243 ; case lookupImpDeprec iface gre of
1244 Just txt -> addWarn (Reason Opt_WarnWarningsDeprecations)
1245 (mk_msg imp_spec txt)
1246 Nothing -> return () } }
1247 | otherwise
1248 = return ()
1249 where
1250 occ = greOccName gre
1251 name_mod = ASSERT2( isExternalName name, ppr name ) nameModule name
1252 doc = text "The name" <+> quotes (ppr occ) <+> ptext (sLit "is mentioned explicitly")
1253
1254 mk_msg imp_spec txt
1255 = sep [ sep [ text "In the use of"
1256 <+> pprNonVarNameSpace (occNameSpace occ)
1257 <+> quotes (ppr occ)
1258 , parens imp_msg <> colon ]
1259 , pprWarningTxtForMsg txt ]
1260 where
1261 imp_mod = importSpecModule imp_spec
1262 imp_msg = text "imported from" <+> ppr imp_mod <> extra
1263 extra | imp_mod == moduleName name_mod = Outputable.empty
1264 | otherwise = text ", but defined in" <+> ppr name_mod
1265
1266 lookupImpDeprec :: ModIface -> GlobalRdrElt -> Maybe WarningTxt
1267 lookupImpDeprec iface gre
1268 = mi_warn_fn iface (greOccName gre) `mplus` -- Bleat if the thing,
1269 case gre_par gre of -- or its parent, is warn'd
1270 ParentIs p -> mi_warn_fn iface (nameOccName p)
1271 FldParent { par_is = p } -> mi_warn_fn iface (nameOccName p)
1272 NoParent -> Nothing
1273
1274 {-
1275 Note [Used names with interface not loaded]
1276 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1277 It's (just) possible to find a used
1278 Name whose interface hasn't been loaded:
1279
1280 a) It might be a WiredInName; in that case we may not load
1281 its interface (although we could).
1282
1283 b) It might be GHC.Real.fromRational, or GHC.Num.fromInteger
1284 These are seen as "used" by the renamer (if -XRebindableSyntax)
1285 is on), but the typechecker may discard their uses
1286 if in fact the in-scope fromRational is GHC.Read.fromRational,
1287 (see tcPat.tcOverloadedLit), and the typechecker sees that the type
1288 is fixed, say, to GHC.Base.Float (see Inst.lookupSimpleInst).
1289 In that obscure case it won't force the interface in.
1290
1291 In both cases we simply don't permit deprecations;
1292 this is, after all, wired-in stuff.
1293
1294
1295 *********************************************************
1296 * *
1297 GHCi support
1298 * *
1299 *********************************************************
1300
1301 A qualified name on the command line can refer to any module at
1302 all: we try to load the interface if we don't already have it, just
1303 as if there was an "import qualified M" declaration for every
1304 module.
1305
1306 For example, writing `Data.List.sort` will load the interface file for
1307 `Data.List` as if the user had written `import qualified Data.List`.
1308
1309 If we fail we just return Nothing, rather than bleating
1310 about "attempting to use module ā€˜Dā€™ (./D.hs) which is not loaded"
1311 which is what loadSrcInterface does.
1312
1313 It is enabled by default and disabled by the flag
1314 `-fno-implicit-import-qualified`.
1315
1316 Note [Safe Haskell and GHCi]
1317 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1318 We DON'T do this Safe Haskell as we need to check imports. We can
1319 and should instead check the qualified import but at the moment
1320 this requires some refactoring so leave as a TODO
1321 -}
1322
1323
1324
1325 lookupQualifiedNameGHCi :: RdrName -> RnM [Name]
1326 lookupQualifiedNameGHCi rdr_name
1327 = -- We want to behave as we would for a source file import here,
1328 -- and respect hiddenness of modules/packages, hence loadSrcInterface.
1329 do { dflags <- getDynFlags
1330 ; is_ghci <- getIsGHCi
1331 ; go_for_it dflags is_ghci }
1332
1333 where
1334 go_for_it dflags is_ghci
1335 | Just (mod,occ) <- isQual_maybe rdr_name
1336 , is_ghci
1337 , gopt Opt_ImplicitImportQualified dflags -- Enables this GHCi behaviour
1338 , not (safeDirectImpsReq dflags) -- See Note [Safe Haskell and GHCi]
1339 = do { res <- loadSrcInterface_maybe doc mod False Nothing
1340 ; case res of
1341 Succeeded iface
1342 -> return [ name
1343 | avail <- mi_exports iface
1344 , name <- availNames avail
1345 , nameOccName name == occ ]
1346
1347 _ -> -- Either we couldn't load the interface, or
1348 -- we could but we didn't find the name in it
1349 do { traceRn "lookupQualifiedNameGHCi" (ppr rdr_name)
1350 ; return [] } }
1351
1352 | otherwise
1353 = do { traceRn "lookupQualifiedNameGHCi: off" (ppr rdr_name)
1354 ; return [] }
1355
1356 doc = text "Need to find" <+> ppr rdr_name
1357
1358 {-
1359 Note [Looking up signature names]
1360 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1361 lookupSigOccRn is used for type signatures and pragmas
1362 Is this valid?
1363 module A
1364 import M( f )
1365 f :: Int -> Int
1366 f x = x
1367 It's clear that the 'f' in the signature must refer to A.f
1368 The Haskell98 report does not stipulate this, but it will!
1369 So we must treat the 'f' in the signature in the same way
1370 as the binding occurrence of 'f', using lookupBndrRn
1371
1372 However, consider this case:
1373 import M( f )
1374 f :: Int -> Int
1375 g x = x
1376 We don't want to say 'f' is out of scope; instead, we want to
1377 return the imported 'f', so that later on the renamer will
1378 correctly report "misplaced type sig".
1379
1380 Note [Signatures for top level things]
1381 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1382 data HsSigCtxt = ... | TopSigCtxt NameSet | ....
1383
1384 * The NameSet says what is bound in this group of bindings.
1385 We can't use isLocalGRE from the GlobalRdrEnv, because of this:
1386 f x = x
1387 $( ...some TH splice... )
1388 f :: Int -> Int
1389 When we encounter the signature for 'f', the binding for 'f'
1390 will be in the GlobalRdrEnv, and will be a LocalDef. Yet the
1391 signature is mis-placed
1392
1393 * For type signatures the NameSet should be the names bound by the
1394 value bindings; for fixity declarations, the NameSet should also
1395 include class sigs and record selectors
1396
1397 infix 3 `f` -- Yes, ok
1398 f :: C a => a -> a -- No, not ok
1399 class C a where
1400 f :: a -> a
1401 -}
1402
1403 data HsSigCtxt
1404 = TopSigCtxt NameSet -- At top level, binding these names
1405 -- See Note [Signatures for top level things]
1406 | LocalBindCtxt NameSet -- In a local binding, binding these names
1407 | ClsDeclCtxt Name -- Class decl for this class
1408 | InstDeclCtxt NameSet -- Instance decl whose user-written method
1409 -- bindings are for these methods
1410 | HsBootCtxt NameSet -- Top level of a hs-boot file, binding these names
1411 | RoleAnnotCtxt NameSet -- A role annotation, with the names of all types
1412 -- in the group
1413
1414 instance Outputable HsSigCtxt where
1415 ppr (TopSigCtxt ns) = text "TopSigCtxt" <+> ppr ns
1416 ppr (LocalBindCtxt ns) = text "LocalBindCtxt" <+> ppr ns
1417 ppr (ClsDeclCtxt n) = text "ClsDeclCtxt" <+> ppr n
1418 ppr (InstDeclCtxt ns) = text "InstDeclCtxt" <+> ppr ns
1419 ppr (HsBootCtxt ns) = text "HsBootCtxt" <+> ppr ns
1420 ppr (RoleAnnotCtxt ns) = text "RoleAnnotCtxt" <+> ppr ns
1421
1422 lookupSigOccRn :: HsSigCtxt
1423 -> Sig GhcPs
1424 -> Located RdrName -> RnM (Located Name)
1425 lookupSigOccRn ctxt sig = lookupSigCtxtOccRn ctxt (hsSigDoc sig)
1426
1427 -- | Lookup a name in relation to the names in a 'HsSigCtxt'
1428 lookupSigCtxtOccRn :: HsSigCtxt
1429 -> SDoc -- ^ description of thing we're looking up,
1430 -- like "type family"
1431 -> Located RdrName -> RnM (Located Name)
1432 lookupSigCtxtOccRn ctxt what
1433 = wrapLocM $ \ rdr_name ->
1434 do { mb_name <- lookupBindGroupOcc ctxt what rdr_name
1435 ; case mb_name of
1436 Left err -> do { addErr err; return (mkUnboundNameRdr rdr_name) }
1437 Right name -> return name }
1438
1439 lookupBindGroupOcc :: HsSigCtxt
1440 -> SDoc
1441 -> RdrName -> RnM (Either MsgDoc Name)
1442 -- Looks up the RdrName, expecting it to resolve to one of the
1443 -- bound names passed in. If not, return an appropriate error message
1444 --
1445 -- See Note [Looking up signature names]
1446 lookupBindGroupOcc ctxt what rdr_name
1447 | Just n <- isExact_maybe rdr_name
1448 = lookupExactOcc_either n -- allow for the possibility of missing Exacts;
1449 -- see Note [dataTcOccs and Exact Names]
1450 -- Maybe we should check the side conditions
1451 -- but it's a pain, and Exact things only show
1452 -- up when you know what you are doing
1453
1454 | Just (rdr_mod, rdr_occ) <- isOrig_maybe rdr_name
1455 = do { n' <- lookupOrig rdr_mod rdr_occ
1456 ; return (Right n') }
1457
1458 | otherwise
1459 = case ctxt of
1460 HsBootCtxt ns -> lookup_top (`elemNameSet` ns)
1461 TopSigCtxt ns -> lookup_top (`elemNameSet` ns)
1462 RoleAnnotCtxt ns -> lookup_top (`elemNameSet` ns)
1463 LocalBindCtxt ns -> lookup_group ns
1464 ClsDeclCtxt cls -> lookup_cls_op cls
1465 InstDeclCtxt ns -> lookup_top (`elemNameSet` ns)
1466 where
1467 lookup_cls_op cls
1468 = lookupSubBndrOcc True cls doc rdr_name
1469 where
1470 doc = text "method of class" <+> quotes (ppr cls)
1471
1472 lookup_top keep_me
1473 = do { env <- getGlobalRdrEnv
1474 ; let all_gres = lookupGlobalRdrEnv env (rdrNameOcc rdr_name)
1475 ; let candidates_msg = candidates $ map gre_name
1476 $ filter isLocalGRE
1477 $ globalRdrEnvElts env
1478 ; case filter (keep_me . gre_name) all_gres of
1479 [] | null all_gres -> bale_out_with candidates_msg
1480 | otherwise -> bale_out_with local_msg
1481 (gre:_) -> return (Right (gre_name gre)) }
1482
1483 lookup_group bound_names -- Look in the local envt (not top level)
1484 = do { mname <- lookupLocalOccRn_maybe rdr_name
1485 ; env <- getLocalRdrEnv
1486 ; let candidates_msg = candidates $ localRdrEnvElts env
1487 ; case mname of
1488 Just n
1489 | n `elemNameSet` bound_names -> return (Right n)
1490 | otherwise -> bale_out_with local_msg
1491 Nothing -> bale_out_with candidates_msg }
1492
1493 bale_out_with msg
1494 = return (Left (sep [ text "The" <+> what
1495 <+> text "for" <+> quotes (ppr rdr_name)
1496 , nest 2 $ text "lacks an accompanying binding"]
1497 $$ nest 2 msg))
1498
1499 local_msg = parens $ text "The" <+> what <+> ptext (sLit "must be given where")
1500 <+> quotes (ppr rdr_name) <+> text "is declared"
1501
1502 -- Identify all similar names and produce a message listing them
1503 candidates :: [Name] -> MsgDoc
1504 candidates names_in_scope
1505 = case similar_names of
1506 [] -> Outputable.empty
1507 [n] -> text "Perhaps you meant" <+> pp_item n
1508 _ -> sep [ text "Perhaps you meant one of these:"
1509 , nest 2 (pprWithCommas pp_item similar_names) ]
1510 where
1511 similar_names
1512 = fuzzyLookup (unpackFS $ occNameFS $ rdrNameOcc rdr_name)
1513 $ map (\x -> ((unpackFS $ occNameFS $ nameOccName x), x))
1514 names_in_scope
1515
1516 pp_item x = quotes (ppr x) <+> parens (pprDefinedAt x)
1517
1518
1519 ---------------
1520 lookupLocalTcNames :: HsSigCtxt -> SDoc -> RdrName -> RnM [(RdrName, Name)]
1521 -- GHC extension: look up both the tycon and data con or variable.
1522 -- Used for top-level fixity signatures and deprecations.
1523 -- Complain if neither is in scope.
1524 -- See Note [Fixity signature lookup]
1525 lookupLocalTcNames ctxt what rdr_name
1526 = do { mb_gres <- mapM lookup (dataTcOccs rdr_name)
1527 ; let (errs, names) = partitionEithers mb_gres
1528 ; when (null names) $ addErr (head errs) -- Bleat about one only
1529 ; return names }
1530 where
1531 lookup rdr = do { this_mod <- getModule
1532 ; nameEither <- lookupBindGroupOcc ctxt what rdr
1533 ; return (guard_builtin_syntax this_mod rdr nameEither) }
1534
1535 -- Guard against the built-in syntax (ex: `infixl 6 :`), see #15233
1536 guard_builtin_syntax this_mod rdr (Right name)
1537 | Just _ <- isBuiltInOcc_maybe (occName rdr)
1538 , this_mod /= nameModule name
1539 = Left (hsep [text "Illegal", what, text "of built-in syntax:", ppr rdr])
1540 | otherwise
1541 = Right (rdr, name)
1542 guard_builtin_syntax _ _ (Left err) = Left err
1543
1544 dataTcOccs :: RdrName -> [RdrName]
1545 -- Return both the given name and the same name promoted to the TcClsName
1546 -- namespace. This is useful when we aren't sure which we are looking at.
1547 -- See also Note [dataTcOccs and Exact Names]
1548 dataTcOccs rdr_name
1549 | isDataOcc occ || isVarOcc occ
1550 = [rdr_name, rdr_name_tc]
1551 | otherwise
1552 = [rdr_name]
1553 where
1554 occ = rdrNameOcc rdr_name
1555 rdr_name_tc = setRdrNameSpace rdr_name tcName
1556
1557 {-
1558 Note [dataTcOccs and Exact Names]
1559 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1560 Exact RdrNames can occur in code generated by Template Haskell, and generally
1561 those references are, well, exact. However, the TH `Name` type isn't expressive
1562 enough to always track the correct namespace information, so we sometimes get
1563 the right Unique but wrong namespace. Thus, we still have to do the double-lookup
1564 for Exact RdrNames.
1565
1566 There is also an awkward situation for built-in syntax. Example in GHCi
1567 :info []
1568 This parses as the Exact RdrName for nilDataCon, but we also want
1569 the list type constructor.
1570
1571 Note that setRdrNameSpace on an Exact name requires the Name to be External,
1572 which it always is for built in syntax.
1573 -}
1574
1575
1576
1577 {-
1578 ************************************************************************
1579 * *
1580 Rebindable names
1581 Dealing with rebindable syntax is driven by the
1582 Opt_RebindableSyntax dynamic flag.
1583
1584 In "deriving" code we don't want to use rebindable syntax
1585 so we switch off the flag locally
1586
1587 * *
1588 ************************************************************************
1589
1590 Haskell 98 says that when you say "3" you get the "fromInteger" from the
1591 Standard Prelude, regardless of what is in scope. However, to experiment
1592 with having a language that is less coupled to the standard prelude, we're
1593 trying a non-standard extension that instead gives you whatever "Prelude.fromInteger"
1594 happens to be in scope. Then you can
1595 import Prelude ()
1596 import MyPrelude as Prelude
1597 to get the desired effect.
1598
1599 At the moment this just happens for
1600 * fromInteger, fromRational on literals (in expressions and patterns)
1601 * negate (in expressions)
1602 * minus (arising from n+k patterns)
1603 * "do" notation
1604
1605 We store the relevant Name in the HsSyn tree, in
1606 * HsIntegral/HsFractional/HsIsString
1607 * NegApp
1608 * NPlusKPat
1609 * HsDo
1610 respectively. Initially, we just store the "standard" name (PrelNames.fromIntegralName,
1611 fromRationalName etc), but the renamer changes this to the appropriate user
1612 name if Opt_NoImplicitPrelude is on. That is what lookupSyntaxName does.
1613
1614 We treat the original (standard) names as free-vars too, because the type checker
1615 checks the type of the user thing against the type of the standard thing.
1616 -}
1617
1618 lookupIfThenElse :: RnM (Maybe (SyntaxExpr GhcRn), FreeVars)
1619 -- Different to lookupSyntaxName because in the non-rebindable
1620 -- case we desugar directly rather than calling an existing function
1621 -- Hence the (Maybe (SyntaxExpr GhcRn)) return type
1622 lookupIfThenElse
1623 = do { rebindable_on <- xoptM LangExt.RebindableSyntax
1624 ; if not rebindable_on
1625 then return (Nothing, emptyFVs)
1626 else do { ite <- lookupOccRn (mkVarUnqual (fsLit "ifThenElse"))
1627 ; return ( Just (mkRnSyntaxExpr ite)
1628 , unitFV ite ) } }
1629
1630 lookupSyntaxName' :: Name -- ^ The standard name
1631 -> RnM Name -- ^ Possibly a non-standard name
1632 lookupSyntaxName' std_name
1633 = do { rebindable_on <- xoptM LangExt.RebindableSyntax
1634 ; if not rebindable_on then
1635 return std_name
1636 else
1637 -- Get the similarly named thing from the local environment
1638 lookupOccRn (mkRdrUnqual (nameOccName std_name)) }
1639
1640 lookupSyntaxName :: Name -- The standard name
1641 -> RnM (SyntaxExpr GhcRn, FreeVars) -- Possibly a non-standard
1642 -- name
1643 lookupSyntaxName std_name
1644 = do { rebindable_on <- xoptM LangExt.RebindableSyntax
1645 ; if not rebindable_on then
1646 return (mkRnSyntaxExpr std_name, emptyFVs)
1647 else
1648 -- Get the similarly named thing from the local environment
1649 do { usr_name <- lookupOccRn (mkRdrUnqual (nameOccName std_name))
1650 ; return (mkRnSyntaxExpr usr_name, unitFV usr_name) } }
1651
1652 lookupSyntaxNames :: [Name] -- Standard names
1653 -> RnM ([HsExpr GhcRn], FreeVars) -- See comments with HsExpr.ReboundNames
1654 -- this works with CmdTop, which wants HsExprs, not SyntaxExprs
1655 lookupSyntaxNames std_names
1656 = do { rebindable_on <- xoptM LangExt.RebindableSyntax
1657 ; if not rebindable_on then
1658 return (map (HsVar noExt . noLoc) std_names, emptyFVs)
1659 else
1660 do { usr_names <- mapM (lookupOccRn . mkRdrUnqual . nameOccName) std_names
1661 ; return (map (HsVar noExt . noLoc) usr_names, mkFVs usr_names) } }
1662
1663 -- Error messages
1664
1665
1666 opDeclErr :: RdrName -> SDoc
1667 opDeclErr n
1668 = hang (text "Illegal declaration of a type or class operator" <+> quotes (ppr n))
1669 2 (text "Use TypeOperators to declare operators in type and declarations")
1670
1671 badOrigBinding :: RdrName -> SDoc
1672 badOrigBinding name
1673 | Just _ <- isBuiltInOcc_maybe occ
1674 = text "Illegal binding of built-in syntax:" <+> ppr occ
1675 -- Use an OccName here because we don't want to print Prelude.(,)
1676 | otherwise
1677 = text "Cannot redefine a Name retrieved by a Template Haskell quote:"
1678 <+> ppr name
1679 -- This can happen when one tries to use a Template Haskell splice to
1680 -- define a top-level identifier with an already existing name, e.g.,
1681 --
1682 -- $(pure [ValD (VarP 'succ) (NormalB (ConE 'True)) []])
1683 --
1684 -- (See #13968.)
1685 where
1686 occ = rdrNameOcc $ filterCTuple name