Looking up a name in the RnEnv.
\begin{code}
-lookupRn :: RdrName
- -> Maybe Name -- Result of environment lookup
- -> RnMS s Name
-lookupRn rdr_name (Just name)
- = -- Found the name in the envt
- returnRn name -- In interface mode the only things in
- -- the environment are things in local (nested) scopes
-lookupRn rdr_name nm@Nothing
- = tryLookupRn rdr_name nm `thenRn` \ name_or_error ->
- case name_or_error of
- Left (nm,err) -> failWithRn nm err
- Right nm -> returnRn nm
-
-tryLookupRn :: RdrName
- -> Maybe Name -- Result of environment lookup
- -> RnMS s (Either (Name, ErrMsg) Name)
-tryLookupRn rdr_name (Just name)
- = -- Found the name in the envt
- returnRn (Right name) -- In interface mode the only things in
- -- the environment are things in local (nested) scopes
-
--- lookup in environment, but don't flag an error if
--- name is not found.
-tryLookupRn rdr_name Nothing
- = -- We didn't find the name in the environment
- getModeRn `thenRn` \ mode ->
- case mode of {
- SourceMode -> returnRn (Left ( mkUnboundName rdr_name
- , unknownNameErr rdr_name));
- -- Source mode; lookup failure is an error
-
- InterfaceMode _ _ ->
+checkUnboundRn :: RdrName -> Maybe Name -> RnMS s Name
+checkUnboundRn rdr_name (Just name)
+ = -- Found it!
+ returnRn name
+
+checkUnboundRn rdr_name Nothing
+ = -- Not found by lookup
+ getModeRn `thenRn` \ mode ->
+ case mode of
+ -- Not found when processing source code; so fail
+ SourceMode -> failWithRn (mkUnboundName rdr_name)
+ (unknownNameErr rdr_name)
+
+ -- Not found when processing an imported declaration,
+ -- so we create a new name for the purpose
+ InterfaceMode _ _ ->
+ case rdr_name of
+ Qual mod_name occ hif -> newImportedGlobalName mod_name occ hif
+
+ -- An Unqual is allowed; interface files contain
+ -- unqualified names for locally-defined things, such as
+ -- constructors of a data type.
+ Unqual occ -> getModuleRn `thenRn ` \ mod_name ->
+ newImportedGlobalName mod_name occ HiFile
- ----------------------------------------------------
- -- OK, so we're in interface mode
- -- An Unqual is allowed; interface files contain
- -- unqualified names for locally-defined things, such as
- -- constructors of a data type.
- -- So, qualify the unqualified name with the
- -- module of the interface file, and try again
- case rdr_name of
- Unqual occ ->
- getModuleRn `thenRn` \ mod ->
- newImportedGlobalName mod occ HiFile `thenRn` \ nm ->
- returnRn (Right nm)
- Qual mod occ hif ->
- newImportedGlobalName mod occ hif `thenRn` \ nm ->
- returnRn (Right nm)
-
- }
-
lookupBndrRn rdr_name
= lookupNameRn rdr_name `thenRn` \ maybe_name ->
- lookupRn rdr_name maybe_name `thenRn` \ name ->
+ checkUnboundRn rdr_name maybe_name `thenRn` \ name ->
if isLocalName name then
returnRn name
-- deciding which instance declarations to import.
lookupOccRn :: RdrName -> RnMS s Name
lookupOccRn rdr_name
- = tryLookupOccRn rdr_name `thenRn` \ name_or_error ->
- case name_or_error of
- Left (nm, err) -> failWithRn nm err
- Right nm -> returnRn nm
-
--- tryLookupOccRn is the fail-safe version of lookupOccRn, returning
--- back the error rather than immediately flagging it. It is only
--- directly used by RnExpr.rnExpr to catch and rewrite unbound
--- uses of `assert'.
-tryLookupOccRn :: RdrName -> RnMS s (Either (Name,ErrMsg) Name)
-tryLookupOccRn rdr_name
= lookupNameRn rdr_name `thenRn` \ maybe_name ->
- tryLookupRn rdr_name maybe_name `thenRn` \ name_or_error ->
- case name_or_error of
- Left _ -> returnRn name_or_error
- Right name ->
- let
+ checkUnboundRn rdr_name maybe_name `thenRn` \ name ->
+ let
name' = mungePrintUnqual rdr_name name
- in
- addOccurrenceName name' `thenRn_`
- returnRn name_or_error
-
+ in
+ addOccurrenceName name'
-- lookupGlobalOccRn is like lookupOccRn, except that it looks in the global
--- environment only. It's used for record field names only.
+-- environment. It's used only for
+-- record field names
+-- class op names in class and instance decls
lookupGlobalOccRn :: RdrName -> RnMS s Name
lookupGlobalOccRn rdr_name
= lookupGlobalNameRn rdr_name `thenRn` \ maybe_name ->
- lookupRn rdr_name maybe_name `thenRn` \ name ->
+ checkUnboundRn rdr_name maybe_name `thenRn` \ name ->
let
name' = mungePrintUnqual rdr_name name
in
addOccurrenceName name'
+
-- mungePrintUnqual is used to make *imported* *occurrences* print unqualified
-- if they were mentioned unqualified in the source code.
-- This improves error messages from the type checker.
filterAvail ie@(IEThingWith want wants) avail@(AvailTC n ns)
| sub_names_ok = AvailTC n (filter is_wanted ns)
- | otherwise = pprTrace "filterAvail" (hsep [ppr ie, pprAvail avail]) $
+ | otherwise =
+#ifdef DEBUG
+ pprTrace "filterAvail" (hsep [ppr ie, pprAvail avail]) $
+#endif
NotAvailable
where
is_wanted name = nameOccName name `elem` wanted_occs
ppr how_in_scope2])
shadowedNameWarn shadow
- = hcat [ptext SLIT("This binding for"),
+ = hsep [ptext SLIT("This binding for"),
quotes (ppr shadow),
ptext SLIT("shadows an existing binding")]