diff --git a/spec/compiler/codegen/const_spec.cr b/spec/compiler/codegen/const_spec.cr index c524f4039a0d6d905e3712230252575319863f2b..7e3d477e68e26800d2e4c4539265a3acc60ebed9 100644 --- a/spec/compiler/codegen/const_spec.cr +++ b/spec/compiler/codegen/const_spec.cr @@ -136,4 +136,18 @@ describe "Codegen: const" do Foo.new.compile ").to_i.should eq(1) end + + it "initialize const that might raise an exception" do + run(" + require \"prelude\" + CONST = (raise \"OH NO\" if 1 == 2) + + def doit + CONST + rescue + end + + doit.nil? + ").to_b.should be_true + end end diff --git a/src/compiler/crystal/codegen.cr b/src/compiler/crystal/codegen.cr index 9a0c569504aaa68a401cffb9cb6e5b38c71dacc5..2da119f122670ad1ecb174c94d657eb6ddec2037 100644 --- a/src/compiler/crystal/codegen.cr +++ b/src/compiler/crystal/codegen.cr @@ -2451,6 +2451,8 @@ module Crystal old_fun = @fun old_in_const_block = @in_const_block old_llvm_mod = @llvm_mod + old_exception_handlers = @exception_handlers + @exception_handlers = [] of Handler @in_const_block = true @llvm_mod = @main_mod @@ -2469,6 +2471,7 @@ module Crystal @fun = old_fun @llvm_mod = old_llvm_mod @in_const_block = old_in_const_block + @exception_handlers = old_exception_handlers end def printf(format, args = [] of LibLLVM::ValueRef)