[Haskell-cafe] ghc -O2 and HUnit weirdness

Joey Hess joey at kitenet.net
Thu Jan 6 21:41:20 CET 2011


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 828 bytes
Desc: Digital signature
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110106/97967e9a/attachment.pgp>


More information about the Haskell-Cafe mailing list