Opened 11 years ago

Closed 11 years ago

Last modified 44 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: Difficulty:
Test Case: Blocked By:
Blocking: Related Tickets:

Description

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,
Remi


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 11 years ago by simonpj

  • Status changed from assigned to closed
Logged In: YES 
user_id=50165

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.