Skip to content

Commit

Permalink
Fixed #4254: Module validation failed with default debug
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Apr 7, 2017
1 parent 32e4dd5 commit 4ba262d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
26 changes: 26 additions & 0 deletions spec/compiler/codegen/debug_spec.cr
Expand Up @@ -78,4 +78,30 @@ describe "Code gen: debug" do
Foo.new
), debug: Crystal::Debug::All)
end

it "correctly restores debug location after fun change (#4254)" do
codegen(%(
require "prelude"
class Foo
def self.one
TWO.two { three }
self
end
def self.three
1 + 2
end
def two(&block)
block
end
end
ONE = Foo.one
TWO = Foo.new
ONE.three
), debug: Crystal::Debug::All)
end
end
16 changes: 15 additions & 1 deletion src/compiler/crystal/codegen/fun.cr
Expand Up @@ -59,6 +59,7 @@ class Crystal::CodeGenVisitor
old_llvm_typer = @llvm_typer
old_builder = self.builder
old_debug_location = @current_debug_location
old_fun = context.fun

old_needs_value = @needs_value

Expand Down Expand Up @@ -159,7 +160,20 @@ class Crystal::CodeGenVisitor
@alloca_block = old_alloca_block
@needs_value = old_needs_value

set_current_debug_location(old_debug_location)
if @debug.line_numbers?
# set_current_debug_location associates a scope from the current fun,
# and at this point the current one should be the old one before
# defining the fun. We do that. We also clear the debug location
# if there was none before.
if old_debug_location
new_fun = context.fun
context.fun = old_fun
set_current_debug_location(old_debug_location)
context.fun = new_fun
else
clear_current_debug_location
end
end

context.fun
end
Expand Down

0 comments on commit 4ba262d

Please sign in to comment.