Opened 14 years ago

Closed 14 years ago

Last modified 48 years ago

#148 closed bug (Fixed)

Premature Evalutation when using -O

Reported by: remit Owned by: nobody
Priority: normal Milestone:
Component: Compiler Version: 6.0
Keywords: Cc:
Operating System: Architecture:
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


Hi all,
the following program crashes (Prelude.head: empty
list) when compiled
with optimizations by both ghc-6.0 and ghc-5.04.3 .
When the out-commented line is activated it runs fine
with 5.04.3,
but still crashes when compiled by 6.0 .
Without -O both run perfectly fine.

Before each command the file is touched:

Without the commented-line:
    % ghc-5.04.3 test1.hs && ./a.out   
    % ghc-5.04.3 -O test1.hs && ./a.out

    Fail: Prelude.head: empty list
    % ghc-6.0 test1.hs && ./a.out 
    % ghc-6.0 -O test1.hs && ./a.out

    Fail: Prelude.head: empty list

With it:
    % ghc-5.04.3 test1.hs && ./a.out   
    % ghc-5.04.3 -O test1.hs && ./a.out
    % ghc-6.0 test1.hs && ./a.out 
    % ghc-6.0 -O test1.hs && ./a.out

    Fail: Prelude.head: empty list

Full test done on:
    % ghc --version
    The Glorious Glasgow Haskell Compilation System,
version 6.0
    % uname -a
    Linux localhost 2.4.20 #1 Sat Nov 30 14:46:26 CET
2002 i686 unknown

I also tested the prebuilt binaries for linux-x86 and
win98, but only
with ghc-5.04.3 -O and ghc-6.0 -O (same behaviour).

AFAICS this really seems to be a bug in GHC, as
    a) Laziness should prevent `len' to be evaluated
       until after program-exit ;o)
    b) I fail to see why this behaviour would change
       when upgrading my compiler
    c) AFAIK -O shouldn't change semantics
       (well, not in this way at least)
    d) I don't see why the "_ <- return ()"  would make any
       difference. (Except for messing up the optimizer ;))

If it isn't a bug, please enlighten me.

Happy hacking,

module Main where

import System
import Monad

main    = do
            args <- getArgs

            when (null args) $
                exitWith ExitSuccess

            -- _ <- return ()

            let len = read (head args) :: Int

            print (show len)
            print len

Change History (1)

comment:1 Changed 14 years ago by simonpj

Status: assignedclosed
Logged In: YES 

Good bug!  This showed up two separate problems with the 
way GHC treats exceptions in the IO monad.  Now fixed, 
thank you.
Note: See TracTickets for help on using tickets.