[project @ 1996-06-30 15:56:44 by partain]
[ghc-hetmet.git] / ghc / compiler / rename / RnNames.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[RnNames]{Extracting imported and top-level names in scope}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module RnNames (
10         getGlobalNames,
11         SYN_IE(GlobalNameInfo)
12     ) where
13
14 import PreludeGlaST     ( SYN_IE(MutableVar) )
15
16 IMP_Ubiq()
17
18 import HsSyn
19 import RdrHsSyn
20 import RnHsSyn
21
22 import RnMonad
23 import RnIfaces         ( IfaceCache, cachedIface, cachedDecl, CachingResult(..) )
24 import RnUtils          ( SYN_IE(RnEnv), emptyRnEnv, extendGlobalRnEnv,
25                           lubExportFlag, qualNameErr, dupNamesErr
26                         )
27 import ParseUtils       ( ParsedIface(..), RdrIfaceDecl(..), ExportsMap(..), RdrIfaceInst )
28
29
30 import Bag              ( emptyBag, unitBag, consBag, snocBag, unionBags,
31                           unionManyBags, mapBag, filterBag, listToBag, bagToList )
32 import CmdLineOpts      ( opt_NoImplicitPrelude, opt_CompilingGhcInternals )
33 import ErrUtils         ( SYN_IE(Error), SYN_IE(Warning), addErrLoc, addShortErrLocLine, addShortWarnLocLine )
34 import FiniteMap        ( emptyFM, addToFM, addListToFM, lookupFM, fmToList, eltsFM, delListFromFM, keysFM{-ToDo:rm-}, FiniteMap )
35 import Id               ( GenId )
36 import Maybes           ( maybeToBool, catMaybes, MaybeErr(..) )
37 import Name             ( RdrName(..), Name, isQual, mkTopLevName, mkWiredInName, origName,
38                           nameOf, qualToOrigName, mkImportedName,
39                           nameExportFlag, nameImportFlag,
40                           getLocalName, getSrcLoc, getImpLocs,
41                           moduleNamePair, pprNonSym,
42                           isLexCon, ExportFlag(..), OrigName(..)
43                         )
44 import PrelInfo         ( SYN_IE(BuiltinNames), SYN_IE(BuiltinKeys) )
45 import PrelMods         ( pRELUDE, gHC_BUILTINS, modulesWithBuiltins )
46 import Pretty
47 import SrcLoc           ( SrcLoc, mkBuiltinSrcLoc )
48 import TyCon            ( tyConDataCons )
49 import UniqFM           ( emptyUFM, addListToUFM_C, lookupUFM )
50 import UniqSupply       ( splitUniqSupply )
51 import Util             ( isIn, assoc, cmpPString, sortLt, removeDups,
52                           equivClasses, panic, assertPanic, pprPanic{-ToDo:rm-}, pprTrace{-ToDo:rm-}
53                         )
54 import PprStyle --ToDo:rm 
55 \end{code}
56
57 \begin{code}
58 type GlobalNameInfo = (BuiltinNames,
59                        BuiltinKeys,
60                        Name -> ExportFlag,      -- export flag
61                        Name -> [RdrName])       -- occurrence names
62                        -- NB: both of the functions are in a *knot* and
63                        -- must be tugged on oh-so-gently...
64
65 type RnM_Info s r = RnMonad GlobalNameInfo s r
66
67 getGlobalNames ::
68            IfaceCache           
69         -> GlobalNameInfo       
70         -> UniqSupply
71         -> RdrNameHsModule
72         -> IO (RnEnv,
73                [Module],                -- directly imported modules
74                Bag (Module,RnName),     -- unqualified imports from module
75                Bag RenamedFixityDecl,   -- imported fixity decls
76                Bag Error,
77                Bag Warning)
78
79 getGlobalNames iface_cache info us
80                (HsModule mod _ _ imports _ ty_decls _ cls_decls _ _ _ binds _ _)
81   = let
82         (us1, us2) = splitUniqSupply us
83     in
84     case initRn True mod emptyRnEnv us1 
85                 (setExtraRn info $
86                  getSourceNames ty_decls cls_decls binds)
87     of { ((src_vals, src_tcs), src_errs, src_warns) ->
88
89     doImportDecls iface_cache info us2 imports  >>=
90         \ (imp_vals, imp_tcs, imp_mods, unqual_imps, imp_fixes, imp_errs, imp_warns) ->
91
92     let
93         unqual_vals = map (\rn -> (Unqual (getLocalName rn), rn)) (bagToList src_vals)
94         unqual_tcs  = map (\rn -> (Unqual (getLocalName rn), rn)) (bagToList src_tcs)
95
96         (src_env, src_dups) = extendGlobalRnEnv emptyRnEnv unqual_vals unqual_tcs
97         (all_env, imp_dups) = extendGlobalRnEnv src_env (bagToList imp_vals) (bagToList imp_tcs)
98
99         -- remove dups of the same imported thing
100         diff_imp_dups = filterBag diff_orig imp_dups
101         diff_orig (_,rn1,rn2) = origName "diff_orig" rn1 /= origName "diff_orig" rn2
102
103         all_dups = bagToList (src_dups `unionBags` diff_imp_dups)
104         dup_errs = map dup_err (equivClasses cmp_rdr all_dups)
105         cmp_rdr (rdr1,_,_) (rdr2,_,_) = cmp rdr1 rdr2
106         dup_err ((rdr,rn1,rn2):rest) = globalDupNamesErr rdr (rn1:rn2: [rn|(_,_,rn)<-rest])
107
108         all_errs  = src_errs  `unionBags` imp_errs `unionBags` listToBag dup_errs
109         all_warns = src_warns `unionBags` imp_warns
110     in
111     return (all_env, imp_mods, unqual_imps, imp_fixes, all_errs, all_warns) }
112 \end{code}
113
114 *********************************************************
115 *                                                       *
116 \subsection{Top-level source names}
117 *                                                       *
118 *********************************************************
119
120 \begin{code}
121 getSourceNames ::
122            [RdrNameTyDecl]
123         -> [RdrNameClassDecl]
124         -> RdrNameHsBinds
125         -> RnM_Info s (Bag RnName,      -- values
126                        Bag RnName)      -- tycons/classes
127
128 getSourceNames ty_decls cls_decls binds
129   = mapAndUnzip3Rn getTyDeclNames ty_decls      `thenRn` \ (tycon_s, constrs_s, fields_s) ->
130     mapAndUnzipRn  getClassNames cls_decls      `thenRn` \ (cls_s, cls_ops_s) ->
131     getTopBindsNames binds                      `thenRn` \ bind_names ->
132     returnRn (unionManyBags constrs_s `unionBags`
133               unionManyBags fields_s  `unionBags`
134               unionManyBags cls_ops_s `unionBags` bind_names,
135               listToBag tycon_s `unionBags` listToBag cls_s)
136
137 --------------
138 getTyDeclNames :: RdrNameTyDecl
139                -> RnM_Info s (RnName, Bag RnName, Bag RnName)   -- tycon, constrs and fields
140
141 getTyDeclNames (TyData _ tycon _ condecls _ _ src_loc)
142   = --getExtraRn                `thenRn` \ ((b_val_names,b_tc_names),b_keys,rec_exp_fn,rec_occ_fn) ->
143     --pprTrace "getTyDeclNames:" (ppr PprDebug tycon) $
144     --pprTrace "getTDN1:" (ppAboves [ ppCat [ppPStr m, ppPStr n] | ((OrigName m n), _) <- fmToList b_tc_names]) $
145
146     newGlobalName src_loc Nothing False{-not val-} tycon `thenRn` \ tycon_name ->
147     getConFieldNames (Just (nameExportFlag tycon_name)) emptyBag emptyBag emptyFM
148                      condecls           `thenRn` \ (con_names, field_names) ->
149     let
150         rn_tycon   = RnData tycon_name con_names field_names
151         rn_constrs = [ RnConstr name tycon_name | name <- con_names]
152         rn_fields  = [ RnField name tycon_name | name <- field_names]
153     in
154     returnRn (rn_tycon, listToBag rn_constrs, listToBag rn_fields)
155
156 getTyDeclNames (TyNew _ tycon _ [NewConDecl con _ con_loc] _ _ src_loc)
157   = newGlobalName src_loc Nothing False{-not val-} tycon        `thenRn` \ tycon_name ->
158     newGlobalName con_loc (Just (nameExportFlag tycon_name)) True{-val-} con
159                                         `thenRn` \ con_name ->
160     returnRn (RnData tycon_name [con_name] [],
161               unitBag (RnConstr con_name tycon_name),
162               emptyBag)
163
164 getTyDeclNames (TySynonym tycon _ _ src_loc)
165   = newGlobalName src_loc Nothing False{-not val-} tycon        `thenRn` \ tycon_name ->
166     returnRn (RnSyn tycon_name, emptyBag, emptyBag)
167
168 ----------------
169 getConFieldNames :: Maybe ExportFlag
170                  -> Bag Name -> Bag Name
171                  -> FiniteMap RdrName ()
172                  -> [RdrNameConDecl]
173                  -> RnM_Info s ([Name], [Name])
174
175 getConFieldNames exp constrs fields have []
176   = returnRn (bagToList constrs, bagToList fields)
177
178 getConFieldNames exp constrs fields have (ConDecl con _ src_loc : rest)
179   = newGlobalName src_loc exp True{-val-} con   `thenRn` \ con_name ->
180     getConFieldNames exp (constrs `snocBag` con_name) fields have rest
181
182 getConFieldNames exp constrs fields have (ConOpDecl _ con _ src_loc : rest)
183   = newGlobalName src_loc exp True{-val-} con   `thenRn` \ con_name ->
184     getConFieldNames exp (constrs `snocBag` con_name) fields have rest
185
186 getConFieldNames exp constrs fields have (RecConDecl con fielddecls src_loc : rest)
187   = mapRn (addErrRn . dupFieldErr con src_loc) dups     `thenRn_`
188     newGlobalName src_loc exp True{-val-} con           `thenRn` \ con_name ->
189     mapRn (newGlobalName src_loc exp True{-val-}) new_fields    `thenRn` \ field_names ->
190     let
191         all_constrs = constrs `snocBag` con_name
192         all_fields  = fields  `unionBags` listToBag field_names
193     in
194     getConFieldNames exp all_constrs all_fields new_have rest
195   where
196     (uniq_fields, dups) = removeDups cmp (concat (map fst fielddecls))
197     new_fields = filter (not . maybeToBool . lookupFM have) uniq_fields
198     new_have   = addListToFM have (zip new_fields (repeat ()))
199
200 -------------
201 getClassNames :: RdrNameClassDecl
202               -> RnM_Info s (RnName, Bag RnName)        -- class and class ops
203
204 getClassNames (ClassDecl _ cname _ sigs _ _ src_loc)
205   = newGlobalName src_loc Nothing False{-notval-} cname `thenRn` \ class_name ->
206     getClassOpNames (Just (nameExportFlag class_name))
207                                   sigs  `thenRn` \ op_names ->
208     returnRn (RnClass class_name op_names,
209               listToBag (map (\ n -> RnClassOp n class_name) op_names))
210
211 ---------------
212 getClassOpNames :: Maybe ExportFlag
213                 -> [RdrNameSig]
214                 -> RnM_Info s [Name]
215
216 getClassOpNames exp [] = returnRn []
217
218 getClassOpNames exp (ClassOpSig op _ _ src_loc : sigs)
219   = newGlobalName src_loc exp True{-val-} op `thenRn` \ op_name ->
220     getClassOpNames exp sigs     `thenRn` \ op_names ->
221     returnRn (op_name : op_names)
222 getClassOpNames exp (_ : sigs)
223   = getClassOpNames exp sigs
224 \end{code}
225
226 *********************************************************
227 *                                                       *
228 \subsection{Bindings}
229 *                                                       *
230 *********************************************************
231
232 \begin{code}
233 getTopBindsNames :: RdrNameHsBinds
234                  -> RnM_Info s (Bag RnName)
235
236 getTopBindsNames binds = doBinds binds
237
238 doBinds EmptyBinds           = returnRn emptyBag
239 doBinds (SingleBind bind)    = doBind bind
240 doBinds (BindWith bind sigs) = doBind bind
241 doBinds (ThenBinds binds1 binds2)
242   = andRn unionBags (doBinds binds1) (doBinds binds2)
243
244 doBind EmptyBind          = returnRn emptyBag
245 doBind (NonRecBind mbind) = doMBinds mbind
246 doBind (RecBind mbind)    = doMBinds mbind
247
248 doMBinds EmptyMonoBinds                         = returnRn emptyBag
249 doMBinds (PatMonoBind pat grhss_and_binds locn) = doPat locn pat
250 doMBinds (FunMonoBind p_name _ _ locn)          = doName locn p_name
251 doMBinds (AndMonoBinds mbinds1 mbinds2)
252   = andRn unionBags (doMBinds mbinds1) (doMBinds mbinds2)
253
254 doPats locn pats
255   = mapRn (doPat locn) pats     `thenRn` \ pats_s ->
256     returnRn (unionManyBags pats_s)
257
258 doPat locn WildPatIn             = returnRn emptyBag
259 doPat locn (LitPatIn _)          = returnRn emptyBag
260 doPat locn (LazyPatIn pat)       = doPat locn pat
261 doPat locn (VarPatIn var)        = doName locn var
262 doPat locn (NegPatIn pat)        = doPat locn pat
263 doPat locn (ParPatIn pat)        = doPat locn pat
264 doPat locn (ListPatIn pats)      = doPats locn pats
265 doPat locn (TuplePatIn pats)     = doPats locn pats
266 doPat locn (ConPatIn name pats)  = doPats locn pats
267 doPat locn (ConOpPatIn p1 op p2)
268   = andRn unionBags (doPat locn p1) (doPat locn p2)
269 doPat locn (AsPatIn as_name pat)
270   = andRn unionBags (doName locn as_name) (doPat locn pat)
271 doPat locn (RecPatIn name fields)
272   = mapRn (doField locn) fields `thenRn` \ fields_s ->
273     returnRn (unionManyBags fields_s)
274
275 doField locn (_, pat, _) = doPat locn pat
276
277 doName locn rdr
278   = newGlobalName locn Nothing True{-val-} rdr `thenRn` \ name ->
279     returnRn (unitBag (RnName name))
280 \end{code}
281
282 *********************************************************
283 *                                                       *
284 \subsection{Creating a new global name}
285 *                                                       *
286 *********************************************************
287
288 \begin{code}
289 newGlobalName :: SrcLoc
290               -> Maybe ExportFlag
291               -> Bool{-True<=>value name,False<=>tycon/class-}
292               -> RdrName
293               -> RnM_Info s Name
294
295 newGlobalName locn maybe_exp is_val_name (Unqual name)
296   = getExtraRn          `thenRn` \ ((b_val_names,b_tc_names),b_keys,rec_exp_fn,rec_occ_fn) ->
297     getModuleRn         `thenRn` \ mod ->
298     rnGetUnique         `thenRn` \ u ->
299     let
300         orig = OrigName mod name
301
302         (uniq, is_toplev)
303           = case (lookupFM b_keys orig) of
304               Just (key,_) -> (key, True)
305               Nothing      -> if not opt_CompilingGhcInternals then (u,True) else -- really here just to save gratuitous lookup
306                               case (lookupFM (if is_val_name then b_val_names else b_tc_names) orig) of
307                                 Nothing -> (u, True)
308                                 Just xx -> (uniqueOf xx, False{-builtin!-})
309
310         exp = case maybe_exp of
311                Just flag -> flag
312                Nothing   -> rec_exp_fn n
313
314         n = if is_toplev
315             then mkTopLevName  uniq orig locn exp (rec_occ_fn n) -- NB: two "n"s
316             else mkWiredInName uniq orig exp
317     in
318     returnRn n    
319
320 newGlobalName locn maybe_exp is_val_name rdr@(Qual mod name)
321   | opt_CompilingGhcInternals
322   -- we are actually defining something that compiler knows about (e.g., Bool)
323
324   = getExtraRn          `thenRn` \ ((b_val_names,b_tc_names),b_keys,rec_exp_fn,rec_occ_fn) ->
325     let
326         orig = OrigName mod name
327
328         (uniq, is_toplev)
329           = case (lookupFM b_keys orig) of
330               Just (key,_) -> (key, True)
331               Nothing      -> case (lookupFM (if is_val_name then b_val_names else b_tc_names) orig) of
332                                 Nothing -> (pprPanic "newGlobalName:Qual:uniq:" (ppr PprDebug rdr), True)
333                                 Just xx -> (uniqueOf xx, False{-builtin!-})
334
335         exp = case maybe_exp of
336                Just flag -> flag
337                Nothing   -> rec_exp_fn n
338
339         n = if is_toplev
340             then mkTopLevName  uniq orig locn exp (rec_occ_fn n) -- NB: two "n"s
341             else mkWiredInName uniq orig exp
342     in
343     returnRn n    
344
345   | otherwise
346   = addErrRn (qualNameErr "name in definition" (rdr, locn)) `thenRn_`
347     returnRn (pprPanic "newGlobalName:Qual:" (ppr PprDebug rdr))
348 \end{code}
349
350 *********************************************************
351 *                                                       *
352 \subsection{Imported names}
353 *                                                       *
354 *********************************************************
355
356 \begin{code}
357 type ImportNameInfo
358   = (GlobalNameInfo,
359      FiniteMap OrigName RnName,         -- values imported so far
360      FiniteMap OrigName RnName,         -- tycons/classes imported so far
361      Name -> (ExportFlag, [SrcLoc]))    -- import flag and src locns;
362                                         -- NB: this last field is in a knot
363                                         -- and mustn't be tugged on!
364
365 type RnM_IInfo s r = RnMonad ImportNameInfo s r
366
367 ------------------------------------------------------------------
368 doImportDecls ::
369            IfaceCache
370         -> GlobalNameInfo               -- builtin and knot name info
371         -> UniqSupply
372         -> [RdrNameImportDecl]          -- import declarations
373         -> IO (Bag (RdrName,RnName),    -- imported values in scope
374                Bag (RdrName,RnName),    -- imported tycons/classes in scope
375                [Module],                -- directly imported modules
376                Bag (Module,RnName),     -- unqualified import from module
377                Bag RenamedFixityDecl,   -- fixity info for imported names
378                Bag Error,
379                Bag Warning)
380
381 doImportDecls iface_cache g_info us src_imps
382   = fixIO ( \ ~(_, _, _, _, _, _, rec_imp_stuff) ->
383         let
384             rec_imp_fm = addListToUFM_C add_stuff emptyUFM (bagToList rec_imp_stuff)
385             add_stuff (imp1,locns1) (imp2,locns2) = (lubExportFlag imp1 imp2, locns1 `unionBags` locns2)
386
387             rec_imp_fn :: Name -> (ExportFlag, [SrcLoc])
388             rec_imp_fn n = case lookupUFM rec_imp_fm n of
389                              Nothing            -> panic "RnNames:rec_imp_fn"
390                              Just (flag, locns) -> (flag, bagToList locns)
391
392             i_info = (g_info, emptyFM, emptyFM, rec_imp_fn)
393         in
394         -- cache the imported modules
395         -- this ensures that all directly imported modules
396         -- will have their original name iface in scope
397         -- pprTrace "doImportDecls:" (ppCat (map ppPStr imp_mods)) $
398         accumulate (map (cachedIface iface_cache False SLIT("doImportDecls")) imp_mods) >>
399
400         -- process the imports
401         doImports iface_cache i_info us all_imps
402
403     ) >>= \ (vals, tcs, unquals, fixes, errs, warns, _) ->
404
405     return (vals, tcs, imp_mods, unquals, fixes,
406             imp_errs `unionBags` errs,
407             imp_warns `unionBags` warns)
408   where
409     the_imps = implicit_prel  ++ src_imps
410     all_imps = implicit_qprel ++ the_imps
411
412     implicit_qprel = ImportDecl gHC_BUILTINS True Nothing Nothing prel_loc
413                    : (if opt_NoImplicitPrelude
414                      then [{- no "import qualified Prelude" -}]
415                      else [ImportDecl pRELUDE True Nothing Nothing prel_loc])
416
417     explicit_prelude_imp = not (null [ () | (ImportDecl mod qual _ _ _) <- src_imps,
418                                             mod == pRELUDE ])
419
420     implicit_prel  = ImportDecl gHC_BUILTINS False Nothing Nothing prel_loc
421                    : (if explicit_prelude_imp || opt_NoImplicitPrelude
422                      then [{- no "import Prelude" -}]
423                      else [ImportDecl pRELUDE False Nothing Nothing prel_loc])
424
425     prel_loc = mkBuiltinSrcLoc
426
427     (uniq_imps, imp_dups) = removeDups cmp_mod the_imps
428     cmp_mod (ImportDecl m1 _ _ _ _) (ImportDecl m2 _ _ _ _) = cmpPString m1 m2
429
430     qprel_imps = [ imp | imp@(ImportDecl mod True Nothing _ _) <- src_imps,
431                          mod == pRELUDE ]
432
433     qual_mods = [ (qual_name mod as_mod, imp) | imp@(ImportDecl mod True as_mod _ _) <- src_imps ]
434     qual_name mod (Just as_mod) = as_mod
435     qual_name mod Nothing       = mod
436
437     (_, qual_dups) = removeDups cmp_qual qual_mods
438     bad_qual_dups = filter (not . all_same_mod) qual_dups
439
440     cmp_qual (q1,_) (q2,_) = cmpPString q1 q2
441     all_same_mod ((q,ImportDecl mod _ _ _ _):rest)
442       = all has_same_mod rest
443       where
444         has_same_mod (_,ImportDecl mod2 _ _ _ _) = mod == mod2
445
446     imp_mods  = [ mod | ImportDecl mod _ _ _ _ <- uniq_imps, mod /= gHC_BUILTINS ]
447
448     imp_warns = listToBag (map dupImportWarn imp_dups)
449                 `unionBags`
450                 listToBag (map qualPreludeImportWarn qprel_imps)
451
452     imp_errs  = listToBag (map dupQualImportErr bad_qual_dups)
453
454 -----------------------
455 doImports :: IfaceCache
456           -> ImportNameInfo
457           -> UniqSupply
458           -> [RdrNameImportDecl]        -- import declarations
459           -> IO (Bag (RdrName,RnName),  -- imported values in scope
460                  Bag (RdrName,RnName),  -- imported tycons/classes in scope
461                  Bag (Module, RnName),  -- unqualified import from module
462                  Bag RenamedFixityDecl, -- fixity info for imported names
463                  Bag Error,
464                  Bag Warning,
465                 Bag (RnName,(ExportFlag,Bag SrcLoc))) -- import flags and src locs
466
467 doImports iface_cache i_info us []
468   = return (emptyBag, emptyBag, emptyBag, emptyBag, emptyBag, emptyBag, emptyBag)
469
470 doImports iface_cache i_info@(g_info,done_vals,done_tcs,rec_imp_fn) us (imp:imps)
471   = let
472         (us1, us2) = splitUniqSupply us
473     in
474     doImport iface_cache i_info us1 imp
475         >>= \ (vals1, tcs1, unquals1, fixes1, errs1, warns1, imps1) ->
476     let
477         ext_vals = foldl add_new_one done_vals (bagToList vals1)
478         ext_tcs  = foldl add_new_one done_tcs  (bagToList tcs1) 
479     in
480     doImports iface_cache (g_info,ext_vals,ext_tcs,rec_imp_fn) us2 imps
481         >>= \ (vals2, tcs2, unquals2, fixes2, errs2, warns2, imps2) ->
482     return (vals1    `unionBags` vals2,
483             tcs1     `unionBags` tcs2,
484             unquals1 `unionBags` unquals2,
485             fixes1   `unionBags` fixes2,
486             errs1    `unionBags` errs2,
487             warns1   `unionBags` warns2,
488             imps1    `unionBags` imps2)
489   where
490     add_new_one :: FiniteMap OrigName RnName -- ones done so far
491                 -> (dont_care, RnName)
492                 -> FiniteMap OrigName RnName -- extended
493
494     add_new_one fm (_, rn)
495       = let
496             orig = origName "add_new_one" rn
497         in
498         case (lookupFM fm orig) of
499           Just  _ -> fm -- already there: no change
500           Nothing -> addToFM fm orig rn
501
502 ----------------------
503 doImport :: IfaceCache
504          -> ImportNameInfo
505          -> UniqSupply
506          -> RdrNameImportDecl
507          -> IO (Bag (RdrName,RnName),                   -- values
508                 Bag (RdrName,RnName),                   -- tycons/classes
509                 Bag (Module,RnName),                    -- unqual imports
510                 Bag RenamedFixityDecl,
511                 Bag Error,
512                 Bag Warning,
513                 Bag (RnName,(ExportFlag,Bag SrcLoc)))   -- import flags and src locs
514
515 doImport iface_cache info us (ImportDecl mod qual maybe_as maybe_spec src_loc)
516   = let
517         (b_vals, b_tcs, maybe_spec') = getBuiltins info mod maybe_spec -- NB: a no-op ToDo:rm
518     in
519     (if mod == gHC_BUILTINS then
520         return (Succeeded (panic "doImport:GHC fake import!"),
521                          \ iface -> ([], [], emptyBag))
522      else
523         --pprTrace "doImport:" (ppPStr mod) $
524         cachedIface iface_cache False SLIT("doImport") mod >>= \ maybe_iface ->
525         return (maybe_iface, \ iface -> getOrigIEs iface maybe_spec')
526     )   >>= \ (maybe_iface, do_ies) ->
527
528     case maybe_iface of
529       Failed err ->
530         return (emptyBag, emptyBag, emptyBag, emptyBag,
531                 unitBag err, emptyBag, emptyBag)
532       Succeeded iface -> 
533         let
534             (ies, chk_ies, get_errs) = do_ies iface
535         in
536         doOrigIEs iface_cache info mod src_loc us ies 
537                 >>= \ (ie_vals, ie_tcs, imp_flags, errs, warns) ->
538         accumulate (map (checkOrigIE iface_cache) chk_ies)
539                 >>= \ chk_errs_warns ->
540         let
541             final_vals = mapBag fst_occ b_vals `unionBags` mapBag pair_occ ie_vals
542             final_tcs  = mapBag fst_occ b_tcs  `unionBags` mapBag pair_occ ie_tcs
543             final_vals_list = bagToList final_vals
544         in
545         (if mod == gHC_BUILTINS then
546             return [ (Nothing, emptyBag) | _ <- final_vals_list ]
547          else
548             accumulate (map (getFixityDecl iface_cache . snd) final_vals_list)
549         )               >>= \ fix_maybes_errs ->
550         let
551             (chk_errs, chk_warns)  = unzip chk_errs_warns
552             (fix_maybes, fix_errs) = unzip fix_maybes_errs
553
554             unquals    = if qual{-ified import-}
555                          then emptyBag
556                          else mapBag pair_as (ie_vals `unionBags` ie_tcs)
557
558             final_fixes = listToBag (catMaybes fix_maybes)
559
560             final_errs  = mapBag (\ err -> err mod src_loc) (unionManyBags (get_errs:chk_errs))
561                           `unionBags` errs `unionBags` unionManyBags fix_errs
562             final_warns = mapBag (\ warn -> warn mod src_loc) (unionManyBags chk_warns)
563                           `unionBags` warns
564             imp_stuff   = mapBag (\ (n,imp) -> (n,(imp,unitBag src_loc))) imp_flags
565         in
566         return (final_vals, final_tcs, unquals, final_fixes,
567                 final_errs, final_warns, imp_stuff)
568   where
569     as_mod :: Module
570     as_mod = case maybe_as of {Nothing -> mod; Just as_this -> as_this}
571
572     mk_occ :: FAST_STRING -> RdrName
573     mk_occ str = if qual then Qual as_mod str else Unqual str
574
575     fst_occ :: (FAST_STRING, RnName) -> (RdrName, RnName)
576     fst_occ (str, rn) = (mk_occ str, rn)
577
578     pair_occ :: RnName -> (RdrName, RnName)
579     pair_occ rn = (mk_occ (getLocalName rn), rn)
580
581     pair_as :: RnName -> (Module, RnName)
582     pair_as  rn = (as_mod, rn)
583
584 -----------------------------
585 getBuiltins :: ImportNameInfo
586             -> Module
587             -> Maybe (Bool, [RdrNameIE])
588             -> (Bag (FAST_STRING, RnName),
589                 Bag (FAST_STRING, RnName),
590                 Maybe (Bool, [RdrNameIE])  -- return IEs that had no effect
591                )
592
593 getBuiltins _ modname maybe_spec
594 --OLD:  | modname `notElem` modulesWithBuiltins
595   = (emptyBag, emptyBag, maybe_spec)
596
597 {-
598 getBuiltins (((b_val_names,b_tc_names),_,_,_),_,_,_) modname maybe_spec
599   = case maybe_spec of 
600       Nothing           -> (all_vals, all_tcs, Nothing)
601
602       Just (True, ies)  -> -- hiding does not work for builtin names
603                            trace "NOTE: `import Prelude hiding ...' does not hide built-in names" $
604                            (all_vals, all_tcs, maybe_spec)
605
606       Just (False, ies) -> let 
607                               (vals,tcs,ies_left) = do_builtin ies
608                            in
609                            (vals, tcs, Just (False, ies_left))
610   where
611     all_vals = do_all_builtin (fmToList b_val_names)
612     all_tcs  = do_all_builtin (fmToList b_tc_names)
613
614     do_all_builtin [] = emptyBag
615     do_all_builtin (((OrigName mod str),rn):rest)
616       = --pprTrace "do_all_builtin:" (ppCat [ppPStr modname, ppPStr mod, ppPStr str]) $
617         (if mod == modname then consBag (str, rn) else id) (do_all_builtin rest)
618
619     do_builtin [] = (emptyBag,emptyBag,[]) 
620     do_builtin (ie:ies)
621       = let
622             (str, orig)
623               = case (ie_name ie) of
624                   Unqual s -> (s, OrigName modname s)
625                   Qual m s -> pprTrace "do_builtin:surprising qual!" (ppCat [ppPStr m, ppPStr s]) $
626                               (s, OrigName modname s)
627         in
628         case (lookupFM b_tc_names orig) of      -- NB: we favour the tycon/class FM...
629           Just rn -> case (ie,rn) of
630              (IEThingAbs _, WiredInTyCon tc)
631                 -> (vals, (str, rn) `consBag` tcs, ies_left)
632              (IEThingAll _, WiredInTyCon tc)
633                 -> (listToBag (map (\ id -> (getLocalName id, WiredInId id)) 
634                                    (tyConDataCons tc))
635                     `unionBags` vals,
636                     (str,rn) `consBag` tcs, ies_left)
637              (IEThingWith _ _, WiredInTyCon tc) -- No checking of With...
638                 -> (listToBag (map (\ id -> (nameOf (origName "IEThingWith" id), WiredInId id)) 
639                                    (tyConDataCons tc))
640                     `unionBags` vals,
641                     (str,rn) `consBag` tcs, ies_left)
642              _ -> panic "importing builtin names (1)"
643
644           Nothing ->
645             case (lookupFM b_val_names orig) of
646               Nothing -> (vals, tcs, ie:ies_left)
647               Just rn -> case (ie,rn) of
648                  (IEVar _, WiredInId _)        
649                     -> ((str, rn) `consBag` vals, tcs, ies_left)
650                  _ -> panic "importing builtin names (2)"
651       where
652         (vals, tcs, ies_left) = do_builtin ies
653 -}
654
655 -------------------------
656 getOrigIEs :: ParsedIface
657            -> Maybe (Bool, [RdrNameIE]) -- "hiding" or not, blah, blah, blah
658            -> ([IE OrigName],
659                [(IE OrigName, ExportFlag)],
660                Bag (Module -> SrcLoc -> Error))
661
662 getOrigIEs (ParsedIface _ _ _ _ _ _ exps _ _ _ _ _ _) Nothing                   -- import all
663   = (map mkAllIE (eltsFM exps), [], emptyBag)
664
665 getOrigIEs (ParsedIface _ _ _ _ _ _ exps _ _ _ _ _ _) (Just (True, ies))        -- import hiding
666   = (map mkAllIE (eltsFM exps_left), found_ies, errs)
667   where
668     (found_ies, errs) = lookupIEs exps ies
669     exps_left = delListFromFM exps (map (getLocalName.ie_name.fst) found_ies)
670
671 getOrigIEs (ParsedIface _ _ _ _ _ _ exps _ _ _ _ _ _) (Just (False, ies))       -- import these
672   = (map fst found_ies, found_ies, errs)
673   where
674     (found_ies, errs) = lookupIEs exps ies
675
676 ------------------------------------------------
677 mkAllIE :: (OrigName, ExportFlag) -> IE OrigName
678
679 mkAllIE (orig,ExportAbs)
680   = ASSERT(isLexCon (nameOf orig))
681     IEThingAbs orig
682 mkAllIE (orig, ExportAll)
683   | isLexCon (nameOf orig)
684   = IEThingAll orig
685   | otherwise
686   = IEVar orig
687
688 ------------
689 lookupIEs :: ExportsMap
690           -> [RdrNameIE]
691           -> ([(IE OrigName, ExportFlag)], -- IEs we found, orig-ified
692               Bag (Module -> SrcLoc -> Error))
693
694 lookupIEs exps ies
695   = foldr go ([], emptyBag) ies
696   where
697     go ie (already, errs)
698       = let
699             str = case (ie_name ie) of
700                     Unqual s -> s
701                     Qual m s -> s
702         in
703         case (lookupFM exps str) of
704           Nothing ->
705             (already, unknownImpSpecErr ie `consBag` errs)
706           Just (orig, flag) ->
707             ((orig_ie orig ie, flag) : already,
708              adderr_if (seen_ie orig already) (duplicateImpSpecErr ie) errs)
709
710     orig_ie orig (IEVar n)          = IEVar       orig
711     orig_ie orig (IEThingAbs n)     = IEThingAbs  orig
712     orig_ie orig (IEThingAll n)     = IEThingAll  orig
713     orig_ie orig (IEThingWith n ns) = IEThingWith orig (map re_orig ns)
714       where
715         (OrigName mod _) = orig
716         re_orig (Unqual s) = OrigName mod s
717
718     seen_ie orig seen_ies = any (\ (ie,_) -> orig == ie_name ie) seen_ies
719
720 --------------------------------------------
721 doOrigIEs iface_cache info mod src_loc us []
722   = return (emptyBag,emptyBag,emptyBag,emptyBag,emptyBag)
723
724 doOrigIEs iface_cache info mod src_loc us (ie:ies)
725   = let
726         (us1, us2) = splitUniqSupply us
727     in
728     doOrigIE iface_cache info mod src_loc us1 ie 
729         >>= \ (vals1, tcs1, imps1, errs1, warns1) ->
730     doOrigIEs iface_cache info mod src_loc us2 ies
731         >>= \ (vals2, tcs2, imps2, errs2, warns2) ->
732     return (vals1    `unionBags` vals2,
733             tcs1     `unionBags` tcs2,
734             imps1    `unionBags` imps2,
735             errs1    `unionBags` errs2,
736             warns1   `unionBags` warns2)
737
738 ----------------------
739 doOrigIE :: IfaceCache
740          -> ImportNameInfo
741          -> Module
742          -> SrcLoc
743          -> UniqSupply
744          -> IE OrigName
745          -> IO (Bag RnName,                     -- values
746                 Bag RnName,                     -- tycons/classes
747                 Bag (RnName,ExportFlag),        -- import flags
748                 Bag Error,
749                 Bag Warning)
750
751 doOrigIE iface_cache info mod src_loc us ie
752   = with_decl iface_cache (ie_name ie)
753         avoided_fn
754         (\ err  -> (emptyBag, emptyBag, emptyBag, unitBag err, emptyBag))
755         (\ decl -> case initRn True mod emptyRnEnv us
756                                (setExtraRn info $
757                                 pushSrcLocRn src_loc $
758                                 getIfaceDeclNames ie decl)
759                    of
760                    ((vals, tcs, imps), errs, warns) -> (vals, tcs, imps, errs, warns))
761   where
762     avoided_fn Nothing -- the thing should be in the source
763       = (emptyBag, emptyBag, emptyBag, emptyBag, emptyBag)
764     avoided_fn (Just (Left  rn)) -- a builtin value brought into scope
765       = (unitBag rn, emptyBag, emptyBag, emptyBag, emptyBag)
766     avoided_fn (Just (Right rn)) -- a builtin tc/class brought into scope
767       = --pprTrace "avoided:Right:" (ppr PprShowAll rn) $
768         (emptyBag, unitBag rn, emptyBag, emptyBag, emptyBag)
769
770 -------------------------
771 checkOrigIE :: IfaceCache
772             -> (IE OrigName, ExportFlag)
773             -> IO (Bag (Module -> SrcLoc -> Error), Bag (Module -> SrcLoc -> Warning))
774
775 checkOrigIE iface_cache (IEThingAll n, ExportAbs)
776   = with_decl iface_cache n
777         (\ _    -> (emptyBag, emptyBag))
778         (\ err  -> (unitBag (\ mod locn -> err), emptyBag))
779         (\ decl -> case decl of
780                 TypeSig _ _ _ -> (emptyBag, unitBag (allWhenSynImpSpecWarn n))
781                 other         -> (unitBag (allWhenAbsImpSpecErr n), emptyBag))
782
783 checkOrigIE iface_cache (IEThingWith n ns, ExportAbs)
784   = return (unitBag (withWhenAbsImpSpecErr n), emptyBag)
785
786 checkOrigIE iface_cache (IEThingWith n ns, ExportAll)
787   = with_decl iface_cache n
788         (\ _    -> (emptyBag, emptyBag))
789         (\ err  -> (unitBag (\ mod locn -> err), emptyBag))
790         (\ decl -> case decl of
791                 NewTypeSig _ con _ _         -> (check_with "constructors" [con] ns, emptyBag)
792                 DataSig    _ cons fields _ _ -> (check_with "constructors (and fields)" (cons++fields) ns, emptyBag)
793                 ClassSig   _ ops _ _         -> (check_with "class ops"   ops   ns, emptyBag))
794   where
795     check_with str has origs
796       | sortLt (<) (map getLocalName has) == sortLt (<) (map nameOf origs)
797       = emptyBag
798       | otherwise
799       = unitBag (withImpSpecErr str n has origs)
800
801 checkOrigIE iface_cache other
802   = return (emptyBag, emptyBag)
803
804 -----------------------
805 with_decl :: IfaceCache
806           -> OrigName
807           -> (Maybe (Either RnName RnName) -> something) -- if avoided..
808           -> (Error        -> something)                 -- if an error...
809           -> (RdrIfaceDecl -> something)                 -- if OK...
810           -> IO something
811
812 with_decl iface_cache n do_avoid do_err do_decl
813   = cachedDecl iface_cache (isLexCon (nameOf n)) n   >>= \ maybe_decl ->
814     case maybe_decl of
815       CachingAvoided info -> return (do_avoid info)
816       CachingFail    err  -> return (do_err   err)
817       CachingHit     decl -> return (do_decl  decl)
818
819 -------------
820 getFixityDecl :: IfaceCache
821               -> RnName
822               -> IO (Maybe RenamedFixityDecl, Bag Error)
823
824 getFixityDecl iface_cache rn
825   = let
826         (OrigName mod str) = origName "getFixityDecl" rn
827
828         succeeded infx i = return (Just (infx rn i), emptyBag)
829     in
830     cachedIface iface_cache True str mod >>= \ maybe_iface ->
831     case maybe_iface of
832       Failed err ->
833         return (Nothing, unitBag err)
834       Succeeded (ParsedIface _ _ _ _ _ _ _ _ fixes _ _ _ _) ->
835         case lookupFM fixes str of
836           Nothing           -> return (Nothing, emptyBag)
837           Just (InfixL _ i) -> succeeded InfixL i
838           Just (InfixR _ i) -> succeeded InfixR i
839           Just (InfixN _ i) -> succeeded InfixN i
840
841 ie_name (IEVar n)         = n
842 ie_name (IEThingAbs n)    = n
843 ie_name (IEThingAll n)    = n
844 ie_name (IEThingWith n _) = n
845
846 adderr_if True  err errs = err `consBag` errs
847 adderr_if False err errs = errs
848 \end{code}
849
850 *********************************************************
851 *                                                       *
852 \subsection{Actually creating the imported names}
853 *                                                       *
854 *********************************************************
855
856 \begin{code}
857 getIfaceDeclNames :: IE OrigName -> RdrIfaceDecl
858                   -> RnM_IInfo s (Bag RnName,                   -- values
859                                   Bag RnName,                   -- tycons/classes
860                                   Bag (RnName,ExportFlag))      -- import flags
861
862 getIfaceDeclNames ie (ValSig val src_loc _)
863   = newImportedName False src_loc Nothing Nothing val   `thenRn` \ val_name ->
864     returnRn (unitBag (RnName val_name),
865               emptyBag,
866               unitBag (RnName val_name, ExportAll))
867
868 getIfaceDeclNames ie (TypeSig tycon src_loc _)
869   = newImportedName True src_loc Nothing Nothing tycon  `thenRn` \ tycon_name ->
870     returnRn (emptyBag,
871               unitBag (RnSyn tycon_name),
872               unitBag (RnSyn tycon_name, ExportAll))
873
874 getIfaceDeclNames ie (NewTypeSig tycon con src_loc _)
875   = newImportedName True src_loc Nothing Nothing tycon  `thenRn` \ tycon_name ->
876     newImportedName False src_loc (Just (nameExportFlag tycon_name))
877                                   (Just (nameImportFlag tycon_name))
878                                   con                   `thenRn` \ con_name ->
879     returnRn (if imp_all (imp_flag ie) then
880                   unitBag (RnConstr con_name tycon_name)
881               else
882                   emptyBag,
883               unitBag (RnData tycon_name [con_name] []),
884               unitBag (RnData tycon_name [con_name] [], imp_flag ie))
885
886 getIfaceDeclNames ie (DataSig tycon cons fields src_loc _)
887   = newImportedName True src_loc Nothing Nothing tycon `thenRn` \ tycon_name ->
888     let
889         map_me = mapRn (newImportedName False src_loc
890                                 (Just (nameExportFlag tycon_name))
891                                 (Just (nameImportFlag tycon_name)))
892     in
893     map_me cons     `thenRn` \ con_names ->
894     map_me fields   `thenRn` \ field_names ->
895     let
896         rn_tycon   = RnData tycon_name con_names field_names
897         rn_constrs = [ RnConstr name tycon_name | name <- con_names ]
898         rn_fields  = [ RnField name tycon_name | name <- field_names ]
899     in
900     returnRn (if imp_all (imp_flag ie) then
901                   listToBag rn_constrs `unionBags` listToBag rn_fields
902               else
903                   emptyBag,
904               unitBag rn_tycon,
905               unitBag (rn_tycon, imp_flag ie))
906
907 getIfaceDeclNames ie (ClassSig cls ops src_loc _)
908   = newImportedName True src_loc Nothing Nothing cls `thenRn` \ cls_name ->
909     mapRn (newImportedName False src_loc (Just (nameExportFlag cls_name))
910                                          (Just (nameImportFlag cls_name)))
911                                             ops `thenRn` \ op_names ->
912     returnRn (if imp_all (imp_flag ie) then
913                   listToBag (map (\ n -> RnClassOp n cls_name) op_names)
914               else
915                   emptyBag,
916               unitBag (RnClass cls_name op_names),
917               unitBag (RnClass cls_name op_names, imp_flag ie))
918
919
920 imp_all ExportAll = True
921 imp_all _         = False
922
923 imp_flag (IEThingAbs _)    = ExportAbs
924 imp_flag (IEThingAll _)    = ExportAll
925 imp_flag (IEThingWith _ _) = ExportAll
926 \end{code}
927
928 *********************************************************
929 *                                                       *
930 \subsection{Creating a new imported name}
931 *                                                       *
932 *********************************************************
933
934 \begin{code}
935 newImportedName :: Bool                 -- True => tycon or class
936                 -> SrcLoc
937                 -> Maybe ExportFlag     -- maybe export flag
938                 -> Maybe ExportFlag     -- maybe import flag
939                 -> RdrName              -- orig name
940                 -> RnM_IInfo s Name
941
942 newImportedName tycon_or_class locn maybe_exp maybe_imp rdr
943   = let
944         orig = qualToOrigName rdr
945     in
946     getExtraRn `thenRn` \ ((_,b_keys,rec_exp_fn,rec_occ_fn),done_vals,done_tcs,rec_imp_fn) ->
947     case ((if tycon_or_class
948            then lookupFM done_tcs
949            else lookupFM done_vals) orig) of
950
951       Just rn -> returnRn (getName rn)
952       Nothing -> 
953         rnGetUnique     `thenRn` \ u ->
954         let 
955             uniq = case lookupFM b_keys orig of
956                      Nothing      -> u
957                      Just (key,_) -> key
958
959             exp  = case maybe_exp of
960                      Just xx -> xx
961                      Nothing -> rec_exp_fn n
962
963             imp  = case maybe_imp of
964                      Just xx -> xx
965                      Nothing -> imp_flag
966
967             (imp_flag, imp_locs) = rec_imp_fn n
968
969             n = mkImportedName uniq orig imp locn imp_locs exp (rec_occ_fn n) -- NB: two "n"s
970         in
971         returnRn n
972 \end{code}
973
974 \begin{code}
975 globalDupNamesErr rdr rns sty
976   = ppAboves (message : map pp_dup rns)
977   where
978     message   = ppBesides [ppStr "multiple declarations of `", pprNonSym sty rdr, ppStr "'"]
979
980     pp_dup rn = addShortErrLocLine (get_loc rn) (\ sty ->
981                 ppCat [pp_descrip rn, pprNonSym sty rn]) sty
982
983     get_loc rn = case getImpLocs rn of
984                      []   -> getSrcLoc rn
985                      locs -> head locs
986
987     pp_descrip (RnName _)      = ppStr "as a value:"
988     pp_descrip (RnSyn  _)      = ppStr "as a type synonym:"
989     pp_descrip (RnData _ _ _)  = ppStr "as a data type:"
990     pp_descrip (RnConstr _ _)  = ppStr "as a data constructor:"
991     pp_descrip (RnField _ _)   = ppStr "as a record field:"
992     pp_descrip (RnClass _ _)   = ppStr "as a class:"
993     pp_descrip (RnClassOp _ _) = ppStr "as a class method:"
994     pp_descrip _               = ppNil 
995
996 dupImportWarn (ImportDecl m1 _ _ _ locn1 : dup_imps) sty
997   = ppAboves (item1 : map dup_item dup_imps)
998   where
999     item1 = addShortWarnLocLine locn1 (\ sty ->
1000             ppCat [ppStr "multiple imports from module", ppPStr m1]) sty
1001
1002     dup_item (ImportDecl m _ _ _ locn)
1003           = addShortWarnLocLine locn (\ sty ->
1004             ppCat [ppStr "here was another import from module", ppPStr m]) sty
1005
1006 qualPreludeImportWarn (ImportDecl m _ _ _ locn)
1007   = addShortWarnLocLine locn (\ sty ->
1008     ppCat [ppStr "qualified import of prelude module", ppPStr m])
1009
1010 dupQualImportErr ((q1,ImportDecl _ _ _ _ locn1):dup_quals) sty
1011   = ppAboves (item1 : map dup_item dup_quals)
1012   where
1013     item1 = addShortErrLocLine locn1 (\ sty ->
1014             ppCat [ppStr "multiple imports (from different modules) with same qualified name", ppPStr q1]) sty
1015
1016     dup_item (q,ImportDecl _ _ _ _ locn)
1017           = addShortErrLocLine locn (\ sty ->
1018             ppCat [ppStr "here was another import with qualified name", ppPStr q]) sty
1019
1020 unknownImpSpecErr ie imp_mod locn
1021   = addShortErrLocLine locn (\ sty ->
1022     ppBesides [ppStr "module ", ppPStr imp_mod, ppStr " does not export `", ppr sty (ie_name ie), ppStr "'"])
1023
1024 duplicateImpSpecErr ie imp_mod locn
1025   = addShortErrLocLine locn (\ sty ->
1026     ppBesides [ppStr "`", ppr sty (ie_name ie), ppStr "' already seen in import list"])
1027
1028 allWhenSynImpSpecWarn n imp_mod locn
1029   = addShortWarnLocLine locn (\ sty ->
1030     ppBesides [ppStr "type synonym `", ppr sty n, ppStr "' should not be imported with (..)"])
1031
1032 allWhenAbsImpSpecErr n imp_mod locn
1033   = addShortErrLocLine locn (\ sty ->
1034     ppBesides [ppStr "module ", ppPStr imp_mod, ppStr " only exports `", ppr sty n, ppStr "' abstractly"])
1035
1036 withWhenAbsImpSpecErr n imp_mod locn
1037   = addShortErrLocLine locn (\ sty ->
1038     ppBesides [ppStr "module ", ppPStr imp_mod, ppStr " only exports `", ppr sty n, ppStr "' abstractly"])
1039
1040 withImpSpecErr str n has ns imp_mod locn
1041   = addErrLoc locn "" (\ sty ->
1042     ppAboves [ ppBesides [ppStr "inconsistent list of", ppStr str, ppStr "in import list for `", ppr sty n, ppStr "'"],
1043                ppCat [ppStr "    expected:", ppInterleave ppComma (map (ppr sty) has)],
1044                ppCat [ppStr "    found:   ", ppInterleave ppComma (map (ppr sty) ns)] ])
1045
1046 dupFieldErr con locn (dup:rest)
1047   = addShortErrLocLine locn (\ sty ->
1048     ppBesides [ppStr "record field `", ppr sty dup, ppStr "declared multiple times in `", ppr sty con, ppStr "'"])
1049 \end{code}