Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix #4639: Macro lookup from included module broken
  • Loading branch information
asterite authored and matiasgarciaisaia committed Aug 5, 2017
1 parent 0906a88 commit b5dd948
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
38 changes: 38 additions & 0 deletions spec/compiler/semantic/macro_spec.cr
Expand Up @@ -1084,4 +1084,42 @@ describe "Semantic: macro" do
Foo.new.main
)) { tuple_of [int32, char] }
end

it "finds macro in included module at class level (#4639)" do
assert_type(%(
module Moo
macro foo
def self.bar
2
end
end
end
class Foo
include Moo
foo
end
Foo.bar
), inject_primitives: false) { int32 }
end

it "finds macro in module in Object" do
assert_type(%(
class Object
macro foo
def self.bar
2
end
end
end
module Moo
foo
end
Moo.bar
), inject_primitives: false) { int32 }
end
end
5 changes: 4 additions & 1 deletion src/compiler/crystal/semantic/call.cr
Expand Up @@ -665,7 +665,10 @@ class Crystal::Call
node_scope = node_scope.base_type if node_scope.is_a?(VirtualType)

macros = yield node_scope
if !macros && node_scope.module?

# If the scope is a module (through its instance type), lookup in Object too
# (so macros like `property` and others, defined in Object, work at the module level)
if !macros && node_scope.instance_type.module?
macros = yield program.object
end

Expand Down
8 changes: 6 additions & 2 deletions src/compiler/crystal/types.cr
Expand Up @@ -407,7 +407,9 @@ module Crystal
return DefInMacroLookup.new
end

parents.try &.each do |parent|
# We need to go through the instance type because of module
# inclusion and inheritance.
instance_type.parents.try &.each do |parent|
parent_macro = parent.lookup_macro(name, args, named_args)
return parent_macro if parent_macro
end
Expand All @@ -431,7 +433,9 @@ module Crystal
return DefInMacroLookup.new
end

parents.try &.each do |parent|
# We need to go through the instance type because of module
# inclusion and inheritance.
instance_type.parents.try &.each do |parent|
parent_macros = parent.lookup_macros(name)
return parent_macros if parent_macros
end
Expand Down

0 comments on commit b5dd948

Please sign in to comment.