Commit 71221e24 authored by Simon Peyton Jones's avatar Simon Peyton Jones
Browse files

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 7a1480c7
  Author: Simon Peyton Jones <>
  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 ++++++++++++++++++++++--
parent 8564b7f3
......@@ -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#
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment