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

Fix logic for subclass restricted against uninstantiated nested generic superclass #10522

Conversation

HertzDevil
Copy link
Contributor

@HertzDevil HertzDevil commented Mar 19, 2021

Fixes #9660.

Also affects certain flow typing constructs:

class A(T); end
class B(T) < A(T); end
class C(T) < B(T); end
class C2 < B(Int32); end

# NoReturn before this PR
typeof((x = C(Int32).new).is_a?(A) ? x : raise "") # => C(Int32)

# NoReturn before this PR
typeof((y = C2.new).is_a?(A) ? y : raise "") # => C2

# [#<C(Int32):...>] before this PR
[C(Int32).new].reject(A) # => []

# [#<C2:...>] before this PR
[C2.new].reject(A) # => []

The second commit relates to virtual generic types and fixes the following:

# false even with the first commit alone
C(Int32).new.as(A(Int32)).is_a?(B) # => true
C2.new.as(A(Int32)).is_a?(B)       # => true

# B(Int32) (non-virtual) before this PR
typeof((z = C(Int32).new || A(Int32).new).is_a?(B) ? z : raise "") # => B(Int32)+

# [#<A(Int32):...>, #<C(Int32):...>, #<C2:...>] before this PR
[A(Int32).new, B(Int32).new, C(Int32).new, C2.new].reject(B) # => [#<A(Int32):...>]

The following cases, where a superclass is restricted against a subclass, are unaffected and left to a future PR: (this part is related to #10519)

# should be B(Int32)+
typeof((x = A(Int32).new).is_a?(B) ? x : raise "") # => NoReturn

# should be C2
typeof((x = A(Int32).new).is_a?(C2) ? x : raise "") # => NoReturn

Copy link
Member

@sdogruyol sdogruyol left a comment

Choose a reason for hiding this comment

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

Thank you @HertzDevil 🙏

Copy link
Member

@straight-shoota straight-shoota left a comment

Choose a reason for hiding this comment

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

For reference, the last commit is about #10519 (comment)

@straight-shoota straight-shoota added topic:compiler:generics kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:lang:type-system labels Mar 30, 2021
@straight-shoota straight-shoota merged commit e7b46c4 into crystal-lang:master Mar 30, 2021
@HertzDevil HertzDevil deleted the bug/nested-generic-inheritance-is-a branch March 30, 2021 09:56
HertzDevil added a commit to HertzDevil/crystal that referenced this pull request Mar 30, 2021
straight-shoota pushed a commit that referenced this pull request Apr 12, 2021
@bcardiff bcardiff added this to the 1.1.0 milestone Jun 21, 2021
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:generics topic:lang:type-system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

is_a? doesn't work with generic inheritance
4 participants