Skip to content

Commit

Permalink
Fix crystal tool unreachable & co visiting circular hierarchies (#1…
Browse files Browse the repository at this point in the history
…5065)

Avoid visiting circular hierarchies created by type aliases.
  • Loading branch information
straight-shoota authored Oct 7, 2024
1 parent a9b26ff commit 401eb47
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions spec/compiler/crystal/tools/unreachable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ describe "unreachable" do
CRYSTAL
end

it "handles circular hierarchy references (#14034)" do
assert_unreachable <<-CRYSTAL
class Foo
alias Bar = Foo
end
CRYSTAL
end

it "finds initializer" do
assert_unreachable <<-CRYSTAL
class Foo
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/crystal/tools/typed_def_processor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ module Crystal::TypedDefProcessor
end

private def process_type(type : Type) : Nil
# Avoid visiting circular hierarchies. There's no use in processing
# alias types anyway.
# For example:
#
# struct Foo
# alias Bar = Foo
# end
return if type.is_a?(AliasType) || type.is_a?(TypeDefType)

if type.is_a?(NamedType) || type.is_a?(Program) || type.is_a?(FileModule)
type.types?.try &.each_value do |inner_type|
process_type inner_type
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/crystal/tools/unreachable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ module Crystal
property excludes = [] of String

def process_type(type)
# Avoid visiting circular hierarchies. There's no use in processing
# alias types anyway.
# For example:
#
# struct Foo
# alias Bar = Foo
# end
return if type.is_a?(AliasType) || type.is_a?(TypeDefType)

if type.is_a?(ModuleType)
track_unused_defs type
end
Expand Down

0 comments on commit 401eb47

Please sign in to comment.