Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation: allow to lookup class and module implementations #7742

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions spec/compiler/crystal/tools/implementations_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,112 @@ describe "implementations" do
Bar.bar
)
end

it "find class implementation" do
assert_implementations %(
༓class Foo
end

F‸oo
straight-shoota marked this conversation as resolved.
Show resolved Hide resolved
)
end

it "find open class implementation" do
assert_implementations %(
༓class Foo
def foo
end
end

༓class Foo
def bar
end
end

F‸oo
)
end

it "find struct implementation" do
assert_implementations %(
༓struct Foo
end

F‸oo
)
end

it "find module implementation" do
assert_implementations %(
༓module Foo
end

F‸oo
)
end

it "find enum implementation" do
assert_implementations %(
༓enum Foo
Foo
end

F‸oo
)
end

it "find enum value implementation" do
assert_implementations %(
enum Foo
༓Foo
end

Foo::F‸oo
)
end

it "find alias implementation" do
assert_implementations %(
class Foo
end

༓alias Bar = Foo

B‸ar
)
end

it "find class defined by macro" do
assert_implementations %(
macro foo
class Foo
end
end

༓foo

F‸oo
)
end

it "find class inside method" do
assert_implementations %(
༓class Foo
end

def foo
F‸oo
end

foo
)
end

it "find const implementation" do
assert_implementations %(
༓Foo = 42

F‸oo
)
end
end
1 change: 1 addition & 0 deletions src/compiler/crystal/semantic/ast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ module Crystal

class Path
property target_const : Const?
property target_type : Type?
property syntax_replacement : ASTNode?
end

Expand Down
4 changes: 3 additions & 1 deletion src/compiler/crystal/semantic/main_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ module Crystal

type = lookup_scope.lookup_type_var(node,
free_vars: free_vars,
find_root_generic_type_parameters: find_root_generic_type_parameters)
find_root_generic_type_parameters: find_root_generic_type_parameters,
remove_alias: false)

case type
when Const
Expand Down Expand Up @@ -218,6 +219,7 @@ module Crystal
# It's different if from a virtual type we do `v.class.new`
# because the class could be any in the hierarchy.
node.type = check_type_in_type_args(type.remove_alias_if_simple).devirtualize
node.target_type = type
when ASTNode
type.accept self unless type.type?
node.syntax_replacement = type
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/crystal/semantic/type_lookup.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class Crystal::Type

# Similar to `lookup_type`, but the result might also be an ASTNode, for example when
# looking `N` relative to a StaticArray.
def lookup_type_var(node : Path, free_vars : Hash(String, TypeVar)? = nil, find_root_generic_type_parameters = true) : Type | ASTNode
TypeLookup.new(self, self.instance_type, true, false, free_vars, find_root_generic_type_parameters).lookup_type_var(node).not_nil!
def lookup_type_var(node : Path, free_vars : Hash(String, TypeVar)? = nil, find_root_generic_type_parameters = true, remove_alias = true) : Type | ASTNode
TypeLookup.new(self, self.instance_type, true, false, free_vars, find_root_generic_type_parameters, remove_alias).lookup_type_var(node).not_nil!
end

# Similar to `lookup_type_var`, but might return `nil`.
Expand All @@ -62,7 +62,7 @@ class Crystal::Type
end

private struct TypeLookup
def initialize(@root : Type, @self_type : Type, @raise : Bool, @allow_typeof : Bool, @free_vars : Hash(String, TypeVar)? = nil, @find_root_generic_type_parameters = true)
def initialize(@root : Type, @self_type : Type, @raise : Bool, @allow_typeof : Bool, @free_vars : Hash(String, TypeVar)? = nil, @find_root_generic_type_parameters = true, @remove_alias = true)
@in_generic_args = 0

# If we are looking types inside a non-instantiated generic type,
Expand Down Expand Up @@ -131,7 +131,7 @@ class Crystal::Type
type.process_value
end
end
type = type.remove_alias_if_simple
type = type.remove_alias_if_simple if @remove_alias
end

type
Expand Down
23 changes: 14 additions & 9 deletions src/compiler/crystal/tools/implementations.cr
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,24 @@ module Crystal
end

def visit(node : Call)
if node.location
if @target_location.between?(node.name_location, node.name_end_location)
if target_defs = node.target_defs
target_defs.each do |target_def|
@locations << target_def.location.not_nil!
end
end
else
contains_target(node)
return contains_target(node) unless node.location && @target_location.between?(node.name_location, node.name_end_location)

if target_defs = node.target_defs
target_defs.each do |target_def|
@locations << target_def.location.not_nil!
end
end
end

def visit(node : Path)
return contains_target(node) unless (loc = node.location) && (end_loc = node.end_location) && @target_location.between?(loc, end_loc)

target = node.target_const || node.target_type
target.try &.locations.try &.each do |loc|
@locations << loc
end
end

def visit(node)
contains_target(node)
end
Expand Down