ghc -O2 and HUnit weirdness

Suppose I'm playing with HUnit for nearly the first time. I might write a test program like this, to see what a test failure looks like in practice: import Test.HUnit import Test.HUnit.Tools main = runVerboseTests $ TestList [ TestCase $ assertBool "should fail" False , TestCase $ assertEqual "should also fail" "foo" "bar"] # runghc test.hs Testing 0 ### Failure in: 0 should fail Testing 1 ### Failure in: 1 should also fail expected: "foo" but got: "bar" Cases: 2 Tried: 2 Errors: 0 Failures: 2 (Counts {cases = 2, tried = 2, errors = 0, failures = 2},0) Good, that works. Then I drop it into a Makefile that already knows how to build haskell programs: # make test ghc -odir build -hidir build -O2 --make test [1 of 1] Compiling Main ( test.hs, build/Main.o ) Linking test ... ./test Testing 0 Testing 1 ### Failure in: 1 should also fail expected: "foo" but got: "bar" Cases: 2 Tried: 2 Errors: 0 Failures: 1 Wait, what?! One of my test cases is impossibly, passing. So, the problem seems to be that ghc -O2 somehow optimises the static assertBool _ True away, in what seems to be a bad way. Remove the -O2 and the test fails as expected. Presumably, although I have not verified, less static boolean values would not trigger the optimisation. Is this a ghc or HUnit bug? (Versions: 6.12.1, 1.2.2.1) -- see shy jo

El jue, 06-01-2011 a las 16:41 -0400, Joey Hess escribió:
So, the problem seems to be that ghc -O2 somehow optimises the static assertBool _ True away, in what seems to be a bad way. Remove the -O2 and the test fails as expected. Presumably, although I have not verified, less static boolean values would not trigger the optimisation. Is this a ghc or HUnit bug?
(Versions: 6.12.1, 1.2.2.1)
Looks like a GHC bug. The following code mimics what HUnit does: -- import Control.Exception as E import Data.Typeable data Fail = Fail deriving (Show) instance Typeable Fail where typeOf _ = mkTyConApp (mkTyCon "Fail") [] instance Exception Fail t = (E.throw Fail >> return Nothing) `E.catch` (\Fail -> return $ Just Fail) main = do a <- t print a -- it runs fine with -O0, but produces an internal error when compiled with -O1 or -O2: $ ghc --make -O2 t.hs [1 of 1] Compiling Main ( t.hs, t.o ) Linking t ... $ ./t t: internal error: PAP object entered! (GHC version 6.12.1 for i386_unknown_linux) Please report this as a GHC bug: http://www.haskell.org/ghc/reportabug It looks like the following bug: http://hackage.haskell.org/trac/ghc/ticket/3959 I don't have a newer GHC version installed, though, so I can't test if it works in a newer GHC. Jürgen

* Jürgen Doser
El jue, 06-01-2011 a las 16:41 -0400, Joey Hess escribió:
So, the problem seems to be that ghc -O2 somehow optimises the static assertBool _ True away, in what seems to be a bad way. Remove the -O2 and the test fails as expected. Presumably, although I have not verified, less static boolean values would not trigger the optimisation. Is this a ghc or HUnit bug?
(Versions: 6.12.1, 1.2.2.1)
Looks like a GHC bug.
Works with -fno-state-hack. -- Roman I. Cheplyaka :: http://ro-che.info/ Don't worry what people think, they don't do it very often.

* Roman Cheplyaka
* Jürgen Doser
[2011-01-07 00:18:09+0100] El jue, 06-01-2011 a las 16:41 -0400, Joey Hess escribió:
So, the problem seems to be that ghc -O2 somehow optimises the static assertBool _ True away, in what seems to be a bad way. Remove the -O2 and the test fails as expected. Presumably, although I have not verified, less static boolean values would not trigger the optimisation. Is this a ghc or HUnit bug?
(Versions: 6.12.1, 1.2.2.1)
Looks like a GHC bug.
Works with -fno-state-hack.
Here's a particular quote from the GHC source [1] Technically, this isn't quite right, because (f True) `seq` 1 should diverge, but it'll converge if we eta-expand f. Nevertheless, we do so; it improves some programs significantly, and increasing convergence isn't a bad thing. As it turns out in the case of HUnit, it may be a bad thing. [1]: http://www.haskell.org/ghc/docs/6.12.3/html/libraries/ghc-6.12.3/src/CoreAri... -- Roman I. Cheplyaka :: http://ro-che.info/ Don't worry what people think, they don't do it very often.
participants (4)
-
Daniel Fischer
-
Joey Hess
-
Jürgen Doser
-
Roman Cheplyaka