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

Detect cyclic includes between generic modules #10529

Conversation

HertzDevil
Copy link
Contributor

The compiler reports an error when a module cyclically includes itself, but this doesn't work when any generic module is part of the cycle, so this breaks with a stack overflow:

module Foo(T)
  include self
end

This PR extends the checks to compare generic module instances by their underlying generic modules so that these cases are detected. The generic type arguments don't matter, as ultimately every instantiation of the same module will lead to a recursive include cycle:

module Foo(T)
  include Foo(Int32) # Foo(Int32) includes Foo(Int32)
end

Referencing self inside generic type arguments is still allowed, e.g. Comparable(self), because generic type arguments cannot be used as an included type:

module Comparable(T)
  include T # Error: T is not a module, it's a T
end

class String
  include Comparable(self) # okay
end

@straight-shoota straight-shoota added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:semantic labels Mar 22, 2021
Copy link
Member

@beta-ziliani beta-ziliani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@beta-ziliani beta-ziliani added this to the 1.2.0 milestone Aug 20, 2021
@straight-shoota straight-shoota merged commit 365463a into crystal-lang:master Aug 24, 2021
@HertzDevil HertzDevil deleted the bug/generic-module-cyclic-include branch August 25, 2021 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:semantic
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants