From df6d4b9ae4f9b5e167aec723b70aa20a448c01d6 Mon Sep 17 00:00:00 2001 From: Simon Marlow Date: Tue, 28 Aug 2007 09:15:50 +0000 Subject: [PATCH] FIX #1533: foreign exporing the same identifier multiple times gave a link error We were generating a new top-level binding derived from the name of the existing top-level name, and making the name external. Multiple instances therefore clashed. The fix is to make each name unique, by appending an actual Unique to the derived name. --- compiler/typecheck/TcForeign.lhs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/compiler/typecheck/TcForeign.lhs b/compiler/typecheck/TcForeign.lhs index 49ecffc..3b6ecd8 100644 --- a/compiler/typecheck/TcForeign.lhs +++ b/compiler/typecheck/TcForeign.lhs @@ -35,11 +35,13 @@ import SMRep import MachOp #endif import Name +import OccName import TcType import DynFlags import Outputable import SrcLoc import Bag +import Unique \end{code} \begin{code} @@ -214,7 +216,17 @@ tcFExport fo@(ForeignExport (L loc nm) hs_ty spec) = newUnique `thenM` \ uniq -> getModule `thenM` \ mod -> let - gnm = mkExternalName uniq mod (mkForeignExportOcc (getOccName nm)) loc + -- We need to give a name to the new top-level binding that + -- is *stable* (i.e. the compiler won't change it later), + -- because this name will be referred to by the C code stub. + -- Furthermore, the name must be unique (see #1533). If the + -- same function is foreign-exported multiple times, the + -- top-level bindings generated must not have the same name. + -- Hence we create an External name (doesn't change), and we + -- append a Unique to the string right here. + uniq_str = showSDoc (pprUnique uniq) + occ = mkVarOcc (occNameString (getOccName nm) ++ '_' : uniq_str) + gnm = mkExternalName uniq mod (mkForeignExportOcc occ) loc id = mkExportedLocalId gnm sig_ty bind = L loc (VarBind id rhs) in -- 1.7.10.4