Skip to content

Commit aa6521f

Browse files
asteriteRX14
authored andcommittedFeb 2, 2018
Fix ASTNode#raise macro method (#5670)
1 parent 995d3f9 commit aa6521f

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed
 

‎spec/compiler/semantic/macro_spec.cr

+15-1
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,27 @@ describe "Semantic: macro" do
275275
end
276276

277277
it "executes raise inside macro" do
278-
assert_error %(
278+
ex = assert_error %(
279279
macro foo
280280
{{ raise "OH NO" }}
281281
end
282282
283283
foo
284284
), "OH NO"
285+
286+
ex.to_s.should_not contain("expanding macro")
287+
end
288+
289+
it "executes raise inside macro, with node (#5669)" do
290+
ex = assert_error %(
291+
macro foo(x)
292+
{{ x.raise "OH\nNO" }}
293+
end
294+
295+
foo(1)
296+
), "OH\nNO"
297+
298+
ex.to_s.should_not contain("expanding macro")
285299
end
286300

287301
it "can specify tuple as return type" do

‎src/compiler/crystal/macros/interpreter.cr

+2
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ module Crystal
349349

350350
begin
351351
@last = receiver.interpret(node.name, args, node.block, self)
352+
rescue ex : MacroRaiseException
353+
raise ex
352354
rescue ex : Crystal::Exception
353355
node.raise ex.message, inner: ex
354356
rescue ex

‎src/compiler/crystal/macros/methods.cr

+12-10
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,7 @@ module Crystal
160160
end
161161

162162
def interpret_raise(node)
163-
msg = node.args.map do |arg|
164-
arg.accept self
165-
@last.to_macro_id
166-
end
167-
msg = msg.join " "
168-
169-
node.raise msg, exception_type: MacroRaiseException
163+
macro_raise(node, node.args, self)
170164
end
171165

172166
def interpret_run(node)
@@ -299,9 +293,7 @@ module Crystal
299293
when "class_name"
300294
interpret_argless_method("class_name", args) { class_name }
301295
when "raise"
302-
interpret_one_arg_method(method, args) do |arg|
303-
raise arg.to_s
304-
end
296+
macro_raise self, args, interpreter
305297
when "filename"
306298
interpret_argless_method("filename", args) do
307299
filename = location.try &.original_filename
@@ -2148,6 +2140,16 @@ private def visibility_to_symbol(visibility)
21482140
Crystal::SymbolLiteral.new(visibility_name)
21492141
end
21502142

2143+
private def macro_raise(node, args, interpreter)
2144+
msg = args.map do |arg|
2145+
arg.accept interpreter
2146+
interpreter.last.to_macro_id
2147+
end
2148+
msg = msg.join " "
2149+
2150+
node.raise msg, exception_type: Crystal::MacroRaiseException
2151+
end
2152+
21512153
def filter(object, klass, block, interpreter, keep = true)
21522154
block_arg = block.args.first?
21532155

0 commit comments

Comments
 (0)
Please sign in to comment.