typelead / eta

The Eta Programming Language, a dialect of Haskell on the JVM
https://eta-lang.org
BSD 3-Clause "New" or "Revised" License
2.61k stars 145 forks source link

Array code throws ClassCastException with -O0 #591

Open aiya000 opened 6 years ago

aiya000 commented 6 years ago

The below code is existed in this official example

{-# LANGUAGE ScopedTypeVariables #-}

import Java

main :: IO ()
main = java $ do
  (arr :: JLongArray) <- arrayFromList [1..10]
  elems <- withObject arr $ mapM aget [0..9]
  io $ print elems
  withObject arr $ mapM_ (\i -> aset i (fromIntegral (i * 2))) [0..9]
  arrList <- arr <.> arrayToList
  io $ print arrList

but it doesn't work in eta 0.0.9b3 šŸ˜ž


The below is my java :smile:

$ java -version
openjdk version "1.8.0_144"
OpenJDK Runtime Environment (build 1.8.0_144-b01)
OpenJDK 64-Bit Server VM (build 25.144-b01, mixed mode)

$ eta Main.hs && java -jar RunMain.jar
[1 of 1] Compiling Main             ( Main.hs, Main.jar )
Linking ./RunMain.jar ...
[1,2,3,4,5,6,7,8,9,10]
Exception in thread "main" java.lang.ClassCastException: [J cannot be cast to [Ljava.lang.Object;
        at base.java.Array$$LrK9I5a40.enter(Array.hs:236)
        at base.java.Array$arrayToList.enter(Array.hs)
        at eta.runtime.apply.PAP1_1.applyO(PAP1_1.java:51)
        at eta.runtime.thunk.Thunk.applyO(Thunk.java:150)
        at base.java.Core$$LrEHSBa2.enter(Core.hs:68)
        at base.java.Core$zlzizg.enter(Core.hs)
        at eta.runtime.apply.PAPSlow.apply(PAPSlow.java:101)
        at eta.runtime.apply.PAPSlow.applyO(PAPSlow.java:160)
        at eta.runtime.thunk.Thunk.applyO(Thunk.java:150)
        at base.ghc.Base$bindJava1.enter(Base.hs:1021)
        at eta.runtime.apply.PAPSlow.apply(PAPSlow.java:101)
        at eta.runtime.apply.PAPSlow.applyO(PAPSlow.java:160)
        at eta.runtime.thunk.Thunk.applyO(Thunk.java:150)
        at base.ghc.Base$$Lr9USa.enter(Base.hs:1094)
        at base.ghc.Base$$fMonad_Java_$czgzg.enter(Base.hs)
        at base.ghc.Base$sat_sAEA.enter(Base.hs)
        at eta.runtime.apply.PAPSlow.apply(PAPSlow.java:101)
        at eta.runtime.apply.PAPSlow.applyO(PAPSlow.java:160)
        at eta.runtime.thunk.Thunk.applyO(Thunk.java:150)
        at base.ghc.Base$$Lr9USa.enter(Base.hs:1094)
        at base.ghc.Base$$fMonad_Java_$czgzg.enter(Base.hs)
        at base.ghc.Base$sat_sAEA.enter(Base.hs)
        at eta.runtime.apply.PAPSlow.apply(PAPSlow.java:101)
        at eta.runtime.apply.PAPSlow.applyO(PAPSlow.java:160)
        at base.ghc.Base$bindJava1.enter(Base.hs:1103)
        at eta.runtime.apply.PAPSlow.apply(PAPSlow.java:101)
        at eta.runtime.apply.PAPSlow.applyO(PAPSlow.java:160)
        at base.ghc.Base$bindJava1.enter(Base.hs:1103)
        at eta.runtime.apply.PAPSlow.apply(PAPSlow.java:101)
        at eta.runtime.apply.PAPSlow.applyO(PAPSlow.java:160)
        at eta.runtime.thunk.Thunk.applyO(Thunk.java:150)
        at base.java.Core$$LrEHSDa4.apply1V(Core.hs:47)
        at base.java.Core$java.apply1V(Core.hs)
        at eta.runtime.apply.PAP1_1.applyV(PAP1_1.java:16)
        at eta.runtime.stg.Closures$EvalLazyIO.enter(Closures.java:104)
        at eta.runtime.stg.Capability.schedule(Capability.java:157)
        at eta.runtime.stg.Capability.scheduleClosure(Capability.java:102)
        at eta.runtime.Runtime.evalLazyIO(Runtime.java:189)
        at eta.runtime.Runtime.main(Runtime.java:182)
        at eta.main.main(Unknown Source)
rahulmutt commented 6 years ago

I am unable to reproduce this with 0.0.9b6, can you upgrade and try again?

aiya000 commented 6 years ago

I tried again, but it is shown...

(Test.hs is equivalent to Main.hs)

$ eta --version
Compiler for the Eta Programming Language, version 0.0.9b6

$ LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 eta Test.hs && java -jar RunTest.jar
[1 of 1] Compiling Main             ( Test.hs, Test.jar )
Linking ./RunTest.jar ...
[1,2,3,4,5,6,7,8,9,10]
Exception in thread "main" java.lang.ClassCastException: [J cannot be cast to [Ljava.lang.Object;
        at base.java.Array$$LrK9I5a40.enter(Array.hs:236)
        at base.java.Array$arrayToList.enter(Array.hs)
        at eta.runtime.apply.PAP1_1.applyO(PAP1_1.java:51)
        at eta.runtime.thunk.Thunk.applyO(Thunk.java:142)
        at base.java.Core$$LrEHSBa2.enter(Core.hs:68)
        at base.java.Core$zlzizg.enter(Core.hs)
        at eta.runtime.apply.PAPSlow.apply(PAPSlow.java:101)
        at eta.runtime.apply.PAPSlow.applyO(PAPSlow.java:160)
        at eta.runtime.thunk.Thunk.applyO(Thunk.java:142)
        at base.ghc.Base$bindJava1.enter(Base.hs:1021)
        at eta.runtime.apply.PAPSlow.apply(PAPSlow.java:101)
        at eta.runtime.apply.PAPSlow.applyO(PAPSlow.java:160)
        at eta.runtime.thunk.Thunk.applyO(Thunk.java:142)
        at base.ghc.Base$$Lr9USa.enter(Base.hs:1094)
        at base.ghc.Base$$fMonad_Java_$czgzg.enter(Base.hs)
        at base.ghc.Base$sat_sAEA.enter(Base.hs)
        at eta.runtime.apply.PAPSlow.apply(PAPSlow.java:101)
        at eta.runtime.apply.PAPSlow.applyO(PAPSlow.java:160)
        at eta.runtime.thunk.Thunk.applyO(Thunk.java:142)
        at base.ghc.Base$$Lr9USa.enter(Base.hs:1094)
        at base.ghc.Base$$fMonad_Java_$czgzg.enter(Base.hs)
        at base.ghc.Base$sat_sAEA.enter(Base.hs)
        at eta.runtime.apply.PAPSlow.apply(PAPSlow.java:101)
        at eta.runtime.apply.PAPSlow.applyO(PAPSlow.java:160)
        at base.ghc.Base$bindJava1.enter(Base.hs:1103)
        at eta.runtime.apply.PAPSlow.apply(PAPSlow.java:101)
        at eta.runtime.apply.PAPSlow.applyO(PAPSlow.java:160)
        at base.ghc.Base$bindJava1.enter(Base.hs:1103)
        at eta.runtime.apply.PAPSlow.apply(PAPSlow.java:101)
        at eta.runtime.apply.PAPSlow.applyO(PAPSlow.java:160)
        at eta.runtime.thunk.Thunk.applyO(Thunk.java:142)
        at base.java.Core$$LrEHSDa4.apply1V(Core.hs:47)
        at base.java.Core$java.apply1V(Core.hs)
        at eta.runtime.apply.PAP1_1.applyV(PAP1_1.java:16)
        at eta.runtime.stg.Closures$EvalLazyIO.enter(Closures.java:104)
        at eta.runtime.stg.Capability.schedule(Capability.java:157)
        at eta.runtime.stg.Capability.scheduleClosure(Capability.java:102)
        at eta.runtime.Runtime.evalLazyIO(Runtime.java:189)
        at eta.runtime.Runtime.main(Runtime.java:182)
        at eta.main.main(Unknown Source)
rahulmutt commented 6 years ago

@aiya000 Can you try doing a source installation of master or run the latest Docker image and let me know if it doesn't work even then?

rahulmutt commented 6 years ago

@aiya000 After setting -O0, I was able to reproduce your issue on master.

rahulmutt commented 6 years ago

You can work around this by setting an extra -O2 flag.

aiya000 commented 6 years ago

@rahulmutt Thanks šŸ‘
I'll check it !

aiya000 commented 6 years ago

I got the same bad result... :sob:

(I think my operation maybe wrong)

I wrote this diff in the master branch, and installed.

diff --git a/eta.cabal b/eta.cabal
index 14e0e43c..37bb168b 100644
--- a/eta.cabal
n+++ b/eta.cabal
@@ -24,7 +24,7 @@ source-repository head
 library
     c-sources:           compiler/cbits/genSym.c
                          compiler/cbits/cutils.c
-    ghc-options:         -Wall -fno-warn-name-shadowing -Werror
+    ghc-options:         -Wall -fno-warn-name-shadowing -Werror -O2
     default-language:    Haskell2010
     default-extensions:  RecordWildCards
                        , NamedFieldPuns
@@ -345,7 +345,7 @@ library
        ghc-options:      -DDEBUG

 executable eta
-    ghc-options:         -Wall -fno-warn-name-shadowing -threaded -rtsopts -Werror
+    ghc-options:         -Wall -fno-warn-name-shadowing -threaded -rtsopts -Werror -O2
     hs-source-dirs:      eta
     main-is:             Main.hs
     default-language:    Haskell2010
$ gco master
$ git pull
$ git submodule update --init --recursive
$ nvim eta.cabal # append above diff
$ LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 ./install.sh # This is looked like success
...
...
Build completed in 3:47m

very sorry, please help me :sob:

Is anything wrong ?

rahulmutt commented 6 years ago

@aiya000 Am I correct in understanding that it was working before you made the diff and it stopped working when you added -threaded -rtsopts? Why did you make the diff in the eta compiler options?

aiya000 commented 6 years ago

@rahulmutt Sorry I misunderstood you advice that means 'Add -O2 flag to eta.cabal before eta is made' :bow:

But I can run it now with below šŸ‘
Thanks !

$ eta -O2 Test.hs && java -jar RunTest.jar
[1 of 1] Compiling Main             ( Test.hs, Test.jar )
Linking ./RunTest.jar ...
[1,2,3,4,5,6,7,8,9,10]
[0,2,4,6,8,10,12,14,16,18]
rahulmutt commented 6 years ago

I investigated this for a bit and here's the problem:

The function alength in Java.Array is implemented using a single primop alength# which expects an array type. The problem is that Java has no common super type for all arrays, so the only way to make the bytecode verification work out is to define individual primops for each of the primitive array types as well as a single primop for object arrays.

As this problem can be avoided by using -O2 (which is the default for etlas), this fix has low priority as of now. If someone requires this for some reason, please let us know and we can add it to one of the milestones.