[commit: base] master: Grotesque hack on Unsafe.Coerce (24c3a71)

Simon Peyton Jones simonpj at microsoft.com
Fri Jan 25 14:31:11 CET 2013


Repository : ssh://darcs.haskell.org//srv/darcs/packages/base

On branch  : master

http://hackage.haskell.org/trac/ghc/changeset/24c3a710abec5c9ade58cdc564d2939076bc5abb

>---------------------------------------------------------------

commit 24c3a710abec5c9ade58cdc564d2939076bc5abb
Author: Simon Peyton Jones <simonpj at microsoft.com>
Date:   Fri Jan 25 13:30:39 2013 +0000

    Grotesque hack on Unsafe.Coerce
    
    The hack is described in detail in Note [Meta-hack for coerce].
    It's a consequence of this, perfectly legitimate, commit:
    
      commit 7a1480c7c590d4d2fa7a105a4eebf299e921e056
      Author: Simon Peyton Jones <simonpj at microsoft.com>
      Date:   Tue Jan 22 22:43:22 2013 +0000
    
          Allow eta-reduction of eval'd functions if of arity 1
    
          See Note [Eta reduction of an eval'd function] in CoreUtils.
          This doesn't fix Trac #7542, but that was the ticket that
          pointed out this infelicity.
    
       compiler/coreSyn/CoreUtils.lhs |   24 ++++++++++++++++++++++--

>---------------------------------------------------------------

 Unsafe/Coerce.hs |   25 ++++++++++++++++++++++++-
 1 files changed, 24 insertions(+), 1 deletions(-)

diff --git a/Unsafe/Coerce.hs b/Unsafe/Coerce.hs
index 50f52ed..a802ba3 100644
--- a/Unsafe/Coerce.hs
+++ b/Unsafe/Coerce.hs
@@ -31,10 +31,33 @@
 
 module Unsafe.Coerce (unsafeCoerce) where
 
+
 #if defined(__GLASGOW_HASKELL__)
 import GHC.Prim (unsafeCoerce#)
+
+local_id :: a -> a
+local_id x = x   -- See Note [Mega-hack for coerce]
+
+{- Note [Meta-hack for coerce]
+
+If we just say
+  unsafeCoerce x = unsafeCoercs# x
+then the simple-optimiser that the desugarer runs will eta-reduce to
+  unsafeCoerce :: forall (a:*) (b:*). a -> b
+  unsafeCoercs = unsafeCoerce#
+And that, sadly, is ill-typed because unsafeCoercs# has OpenKind type variables
+And rightly so, becuase we shouldn't be calling unsafeCoerce# in a higher
+order way; it has a compulsory unfolding 
+   unsafeCoerce# a b x = x |> UnsafeCo a b
+and we really rely on it being inlined pronto. But the simple-optimiser doesn't.
+The identity function local_id delays the eta reduction just long enough
+for unsafeCoerce# to get inlined.
+
+Sigh. This is horrible, but then so is unsafeCoerce.
+-}
+
 unsafeCoerce :: a -> b
-unsafeCoerce x = unsafeCoerce# x
+unsafeCoerce x = local_id (unsafeCoerce# x)
   -- See Note [Unsafe coerce magic] in basicTypes/MkId
   -- NB: Do not eta-reduce this definition, else the type checker 
   -- give usafeCoerce the same (dangerous) type as unsafeCoerce#





More information about the ghc-commits mailing list