summaryrefslogtreecommitdiff
path: root/testsuite/tests/deriving/should_fail/T7148a.hs
blob: 6441058b242386e4ef97b8fb093a9bafc6b1d9df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{-# LANGUAGE TypeFamilies, ScopedTypeVariables,
             GeneralizedNewtypeDeriving #-}

module T7148a where

import Control.Monad.ST
data Proxy a = Proxy
type family Result a b
 
class Convert a where
  coerce :: Proxy b -> a -> Result a b
 
newtype SAFE a = SAFE a
type instance Result (SAFE a) b = a
 
instance Convert (SAFE a) where
  coerce _ (SAFE a) = a

newtype IS_NO_LONGER a = IS_NO_LONGER a deriving Convert
type instance Result (IS_NO_LONGER a) b = b

--infered type is 
unsafeCoerce :: forall a b. a -> b
unsafeCoerce = coerce (Proxy :: Proxy b) . IS_NO_LONGER . SAFE

--use it safely
id' :: a -> a
id' = unsafeCoerce
 
--segfault (with high probability)
crash :: segfault 
crash = unsafeCoerce . tail . tail . tail . unsafeCoerce $ True


--time for side effects
unsafePerformIO :: IO a -> a
unsafePerformIO x = runST $ unsafeCoerce x