GHC turns undefined into const undefined
Summary
GHC turns undefined
into const undefined
.
Steps to reproduce
Create the following two files:
-
M.hs
:module M where f :: Bool -> Bool -> Bool f True = undefined f False = const undefined
-
Main.hs
:import M main = putStrLn (f True `seq` "Bug!")
Compile and run the program:
$ ghc --make Main.hs -O -o bug -v0 && ./bug
Bug!
The output should not be Bug!
. If we look at the generated Core (-ddump-simpl -dsuppress-all
) we see that f
is compiled to a function with an outermost lambda that takes two arguments:
f = \ ds_dKD _ ->
case ds_dKD of {
False -> f2;
True -> case f1 of wild1_00 { }
}
We get the right output if optimisations are turned off:
$ ghc --make Main.hs -O0 -o bug -v0 && ./bug
bug: Prelude.undefined
CallStack (from HasCallStack):
undefined, called at ./M.hs:4:11 in main:M
Environment
- GHC version used: 9.4.1. The bug is present also in GHC 9.2.4, 9.2.3, 9.2.2 and 9.2.1, but not in GHC 9.0.2.
- Operating System: Linux.
- System Architecture: x86_64.