Skip to content

Commit

Permalink
Compiler: fixed logic for macro vs. method resolution. Related to #236
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed May 21, 2017
1 parent 6141bcd commit c87fc6d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
24 changes: 24 additions & 0 deletions spec/compiler/semantic/macro_spec.cr
Expand Up @@ -1060,4 +1060,28 @@ describe "Semantic: macro" do
{global(1), global(1, 2)}
)) { tuple_of [int32, char] }
end

it "finds macro and method at the same scope inside included module" do
assert_type(%(
module Moo
macro global(x)
1
end
def global(x, y)
'a'
end
end
class Foo
include Moo
def main
{global(1), global(1, 2)}
end
end
Foo.new.main
)) { tuple_of [int32, char] }
end
end
10 changes: 7 additions & 3 deletions src/compiler/crystal/types.cr
Expand Up @@ -373,7 +373,11 @@ module Crystal
end

def has_def?(name)
defs.try(&.has_key?(name)) || parents.try(&.any?(&.has_def?(name)))
has_def_without_parents?(name) || parents.try(&.any?(&.has_def?(name)))
end

def has_def_without_parents?(name)
defs.try(&.has_key?(name))
end

record DefInMacroLookup
Expand All @@ -399,7 +403,7 @@ module Crystal
# First check if there are defs at this scope with that name.
# If so, make that a priority in the lookup and don't consider
# macro matches.
if has_def?(name)
if has_def_without_parents?(name)
return DefInMacroLookup.new
end

Expand All @@ -423,7 +427,7 @@ module Crystal
return macros
end

if has_def?(name)
if has_def_without_parents?(name)
return DefInMacroLookup.new
end

Expand Down

0 comments on commit c87fc6d

Please sign in to comment.