Skip to content

Commit

Permalink
Fix ASTNode#raise macro method (crystal-lang#5670)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored and RX14 committed Feb 2, 2018
1 parent 995d3f9 commit aa6521f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
16 changes: 15 additions & 1 deletion spec/compiler/semantic/macro_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,27 @@ describe "Semantic: macro" do
end

it "executes raise inside macro" do
assert_error %(
ex = assert_error %(
macro foo
{{ raise "OH NO" }}
end
foo
), "OH NO"

ex.to_s.should_not contain("expanding macro")
end

it "executes raise inside macro, with node (#5669)" do
ex = assert_error %(
macro foo(x)
{{ x.raise "OH\nNO" }}
end
foo(1)
), "OH\nNO"

ex.to_s.should_not contain("expanding macro")
end

it "can specify tuple as return type" do
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/crystal/macros/interpreter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ module Crystal

begin
@last = receiver.interpret(node.name, args, node.block, self)
rescue ex : MacroRaiseException
raise ex
rescue ex : Crystal::Exception
node.raise ex.message, inner: ex
rescue ex
Expand Down
22 changes: 12 additions & 10 deletions src/compiler/crystal/macros/methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,7 @@ module Crystal
end

def interpret_raise(node)
msg = node.args.map do |arg|
arg.accept self
@last.to_macro_id
end
msg = msg.join " "

node.raise msg, exception_type: MacroRaiseException
macro_raise(node, node.args, self)
end

def interpret_run(node)
Expand Down Expand Up @@ -299,9 +293,7 @@ module Crystal
when "class_name"
interpret_argless_method("class_name", args) { class_name }
when "raise"
interpret_one_arg_method(method, args) do |arg|
raise arg.to_s
end
macro_raise self, args, interpreter
when "filename"
interpret_argless_method("filename", args) do
filename = location.try &.original_filename
Expand Down Expand Up @@ -2148,6 +2140,16 @@ private def visibility_to_symbol(visibility)
Crystal::SymbolLiteral.new(visibility_name)
end

private def macro_raise(node, args, interpreter)
msg = args.map do |arg|
arg.accept interpreter
interpreter.last.to_macro_id
end
msg = msg.join " "

node.raise msg, exception_type: Crystal::MacroRaiseException
end

def filter(object, klass, block, interpreter, keep = true)
block_arg = block.args.first?

Expand Down

0 comments on commit aa6521f

Please sign in to comment.