Skip to content

Commit

Permalink
Prevent invoking method_added macro hook recursively (crystal-lang#…
Browse files Browse the repository at this point in the history
  • Loading branch information
makenowjust authored and RX14 committed Apr 2, 2018
1 parent e17823f commit 4d2ad83
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
18 changes: 18 additions & 0 deletions spec/compiler/semantic/hooks_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ describe "Semantic: hooks" do
") { int32 }
end

it "does not invoke 'method_added' hook recusively" do
assert_type("
class Foo
macro method_added(d)
def {{d.name.id}}
1
end
end
def foo
nil
end
end
Foo.new.foo
") { int32 }
end

it "errors if wrong inherited args size" do
assert_error %(
class Foo
Expand Down
8 changes: 7 additions & 1 deletion src/compiler/crystal/semantic/top_level_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor
record FinishedHook, scope : ModuleType, macro : Macro
@finished_hooks = [] of FinishedHook

@method_added_running = false

@last_doc : String?

def visit(node : ClassDef)
Expand Down Expand Up @@ -343,7 +345,11 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor
new_expansions << {original: node, expanded: new_method}
end

run_hooks target_type.metaclass, target_type, :method_added, node, Call.new(nil, "method_added", [node] of ASTNode).at(node.location)
unless @method_added_running
@method_added_running = true
run_hooks target_type.metaclass, target_type, :method_added, node, Call.new(nil, "method_added", [node] of ASTNode).at(node.location)
@method_added_running = false
end
end

false
Expand Down

0 comments on commit 4d2ad83

Please sign in to comment.