Skip to content

readMutVar# is inlined/duplicated

The following code:

module Main where

import Control.Monad.ST.Lazy
import Data.STRef.Lazy
import Data.Array.ST
import Int
import Debug.Trace

data Refs s = Refs
    { memory    :: STArray s Int8 Int8
    , pc        :: STRef s Int8
    }

main :: IO ()
main = do
    print $ runST m
  where
    m = do
      m <- newArray_ (0,30)
      p <- newSTRef 0
      let r = Refs m p
      writeArray m 0 0x4
      v <- readSTRef p
      modifySTRef p (+1)
      trace ("v: " ++ show v) $ return ()
      op <- readArray m v
      case trace ("v: " ++ show v) $ op of
          0x4 -> modifySTRef p (+100) -- should run this
          n   -> error ("should never match this: " ++ show n)

generates this output:

> ./st                                              
v: 0
v: 1
st: MArray: undefined array element

That is, the two instances of trace (show v) are printing different results, for the same v!

Looking at the Core, the problem seems to be that a call to readMutVar# has been inlined and duplicated. The readMutVar# primop doesn't have the has_side_effects flag set, and setting it (in primops.txt.pp) fixes it, but I'm not completely sure if that's the right fix. If it is, then there are lots of other similar primops that also need that flag set.

Bug report originally from a haskell-cafe post: http://www.haskell.org/pipermail/haskell-cafe/2009-May/061010.html

Trac metadata
Trac field Value
Version 6.10.2
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information