Haskell 程序在混合编译代码和解释代码时挂起
Haskell program hangs when mixing compiled and interpreted code
考虑以下微不足道的模块:
module Fail1 where
identity x = x
module Main where
import Fail1
main = print (identity 7)
我将它们保存为 Fail1.hs
和 Fail2.hs
。如果我尝试 运行 这个程序,一切都很好:
> runhaskell Fail2
7
但是看看这个:
> ghc -O2 --make Fail1
[1 of 1] Compiling Fail1 ( Fail1.hs, Fail1.o )
> runhaskell Fail2
_
程序现在永远挂起,ghc.exe
消耗了一个 CPU 核心的 100%。什么鬼?
但情况会好转:
> ghc -O2 --make Fail2
[2 of 2] Compiling Main ( Fail2.hs, Fail2.o )
> runhaskell Fail2
Access violation in generated code when reading 0xffffffffffffffff
Attempting to reconstruct a stack trace...
Frame Code address
* 0x71fdd90 0x3d7ce28 C:\Program Files\Haskell Platform.6.3\bin\ghc.exe+0x397ce28
呃……什么?
具有讽刺意味的是,如果我只是 运行 Fail2.exe
本身,它就能完美地工作:
> Fail2
7
这到底是怎么回事?我要疯了吗?这真的非常像某种 GHC 错误。任何人都可以重现这个,还是只是我的系统?
> ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.6.3
(哇,好吧。这真的没有显示太多信息。我是 运行ning Windows 7 Pro 64 位,我 相信 我已经安装了 64 位版本的 GHC。)
这是 GHC 中的一个错误,已在 GHC 8.6.4 中修复。
我已经使用 GHC 8.6.3、GHC 8.6.4 和 GHC 8.6.5 对其进行了测试,后两个版本按预期工作。
我使用的是 x64 Windows haskell stack,我 运行 Windows 10 安装了所有更新(截至 2019-05-25。)
(对于所有 GHC 版本:无论我写 runhaskell Fail2
还是 runhaskell Fail2.hs
都没有任何区别。)
GHC 8.6.3
这个有效:
stack --resolver lts-13.11 runhaskell Fail2
然后我编译Fail1:
stack --resolver lts-13.11 ghc -- -O2 --make Fail1
然后当 运行 Fail2.
时,原始的 runhaskell 命令挂起
然后我编译Fail2:
stack --resolver lts-13.11 ghc -- -O2 --make Fail2
然后原来的 runhaskell 命令崩溃了,就像问题描述的那样。
GHC 8.6.4
使用 --resolver lts-13.19
(使用 GHC 8.6.4),所有命令都按预期工作。
GHC 8.6.5
--resolver lts-13.23
相同(使用 GHC 8.6.5)。
解决方案
只需更新到最新的 GHC 版本,或者至少更新到 ghc 8.6.4。
考虑以下微不足道的模块:
module Fail1 where
identity x = x
module Main where
import Fail1
main = print (identity 7)
我将它们保存为 Fail1.hs
和 Fail2.hs
。如果我尝试 运行 这个程序,一切都很好:
> runhaskell Fail2
7
但是看看这个:
> ghc -O2 --make Fail1
[1 of 1] Compiling Fail1 ( Fail1.hs, Fail1.o )
> runhaskell Fail2
_
程序现在永远挂起,ghc.exe
消耗了一个 CPU 核心的 100%。什么鬼?
但情况会好转:
> ghc -O2 --make Fail2
[2 of 2] Compiling Main ( Fail2.hs, Fail2.o )
> runhaskell Fail2
Access violation in generated code when reading 0xffffffffffffffff
Attempting to reconstruct a stack trace...
Frame Code address
* 0x71fdd90 0x3d7ce28 C:\Program Files\Haskell Platform.6.3\bin\ghc.exe+0x397ce28
呃……什么?
具有讽刺意味的是,如果我只是 运行 Fail2.exe
本身,它就能完美地工作:
> Fail2
7
这到底是怎么回事?我要疯了吗?这真的非常像某种 GHC 错误。任何人都可以重现这个,还是只是我的系统?
> ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.6.3
(哇,好吧。这真的没有显示太多信息。我是 运行ning Windows 7 Pro 64 位,我 相信 我已经安装了 64 位版本的 GHC。)
这是 GHC 中的一个错误,已在 GHC 8.6.4 中修复。
我已经使用 GHC 8.6.3、GHC 8.6.4 和 GHC 8.6.5 对其进行了测试,后两个版本按预期工作。
我使用的是 x64 Windows haskell stack,我 运行 Windows 10 安装了所有更新(截至 2019-05-25。)
(对于所有 GHC 版本:无论我写 runhaskell Fail2
还是 runhaskell Fail2.hs
都没有任何区别。)
GHC 8.6.3
这个有效:
stack --resolver lts-13.11 runhaskell Fail2
然后我编译Fail1:
stack --resolver lts-13.11 ghc -- -O2 --make Fail1
然后当 运行 Fail2.
时,原始的 runhaskell 命令挂起然后我编译Fail2:
stack --resolver lts-13.11 ghc -- -O2 --make Fail2
然后原来的 runhaskell 命令崩溃了,就像问题描述的那样。
GHC 8.6.4
使用 --resolver lts-13.19
(使用 GHC 8.6.4),所有命令都按预期工作。
GHC 8.6.5
--resolver lts-13.23
相同(使用 GHC 8.6.5)。
解决方案
只需更新到最新的 GHC 版本,或者至少更新到 ghc 8.6.4。