diff --git a/src/compiler/crystal/tools/expand.cr b/src/compiler/crystal/tools/expand.cr index f9fa7c58f8a2..09bab30ad629 100644 --- a/src/compiler/crystal/tools/expand.cr +++ b/src/compiler/crystal/tools/expand.cr @@ -67,6 +67,7 @@ module Crystal class ExpandVisitor < Visitor def initialize(@target_location : Location) @nodes = [] of ASTNode + @in_def_instance = false @message = "no expansion found" end @@ -82,7 +83,9 @@ module Crystal if type.is_a?(DefInstanceContainer) type.def_instances.values.try do |typed_defs| typed_defs.each do |typed_def| + @in_def_instance = true typed_def.accept(self) + @in_def_instance = false end end end @@ -96,7 +99,9 @@ module Crystal def process(result : Compiler::Result) result.program.def_instances.each_value do |typed_def| + @in_def_instance = true typed_def.accept(self) + @in_def_instance = false end result.program.types?.try &.values.each do |type| @@ -114,8 +119,15 @@ module Crystal end end + def visit(node : Def | FunDef) + @in_def_instance && contains_target(node) + end + def visit(node : Call) if loc_start = node.location + # If node.obj (a.k.a. an receiver) is a Path, it may be macro call and node.obj has no expansion surely. + # Otherwise, we cannot decide node.obj has no expansion. + loc_start = node.name_location unless node.obj.is_a?(Path) loc_end = node.name_end_location if @target_location.between?(loc_start, loc_end) if node.expanded