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

Struct #is_a? fails for generic module included in abstract superstruct #10302

Open
RespiteSage opened this issue Jan 26, 2021 · 4 comments · Fixed by #10304
Open

Struct #is_a? fails for generic module included in abstract superstruct #10302

RespiteSage opened this issue Jan 26, 2021 · 4 comments · Fixed by #10304
Labels
kind:bug kind:regression Something that used to correctly work but no longer works topic:compiler:generics topic:compiler:semantic

Comments

@RespiteSage
Copy link

In 0.36.0, if you have a struct Sub which extends an abstract struct Sup, where Sup includes a generic module Mod(T) as Mod(Sup), Sub#is_a? Mod(Sup) will evaluate to false, when it was formerly true on 0.35.1.

For example:

module Mod(T)
end

abstract struct Sup
  include Mod(Sup)
end

struct Sub < Sup
end

puts Sub.new.is_a? Mod(Sup) # => false

Notably, this only seems to occur when the abstract superstruct is a part of the generic type of the module when it's included. So,

module Mod
end

abstract struct Sup
  include Mod
end

struct Sub < Sup
end

puts Sub.new.is_a? Mod # => true

and

module Mod(T)
end

abstract struct Sup
  include Mod(Int32)
end

struct Sub < Sup
end

puts Sub.new.is_a? Mod(Int32) # => true

and

module Mod(T)
end

abstract struct Sup
  include Mod(Set(Int32))
end

struct Sub < Sup
end

puts Sub.new.is_a? Mod(Set(Int32)) # => true

but

module Mod(T)
end

abstract struct Sup
  include Mod(Set(Sup))
end

struct Sub < Sup
end

puts Sub.new.is_a? Mod(Set(Sup)) # => false
@straight-shoota
Copy link
Member

straight-shoota commented Jan 26, 2021

It seems #10214 broke this (and it stayed broken with #10224).

@straight-shoota
Copy link
Member

Possibly related to #9660, but this used to work.

@asterite asterite added the kind:regression Something that used to correctly work but no longer works label Jan 26, 2021
@HertzDevil
Copy link
Contributor

HertzDevil commented Jun 3, 2021

It is not enough that Sub.new.is_a?(Mod(Sup)). The following still fails after #10304:

module Mod(T)
end

abstract struct Sup
  include Mod(Sup)
end

struct Sub < Sup
end

Sup < Mod(Sup)       # => false
{% Sup < Mod(Sup) %} # => false
Sub < Mod(Sup)       # => false
{% Sub < Mod(Sup) %} # => false

This only happens with abstract structs.

@HertzDevil
Copy link
Contributor

Downcasts also fail in a manner unrelated to #10214 and not fixed by #10779:

module Mod(T)
end

abstract struct Sup
  include Mod(Sup)
end

struct Sub < Sup
end

{% p Mod(Sup).includers %} # => [] of ::NoReturn

Sub.new.as(Mod(Sup)).as(Sup)
# Error: can't cast Mod(Sup) to Sup
# 0.36.0 only: can't cast Sub to Mod(Sup)

It appears Sub is registered as an includer of Mod(Sup+) but not Mod(Sup). So maybe we should simply devirtualize the generic type arguments somewhere else...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug kind:regression Something that used to correctly work but no longer works topic:compiler:generics topic:compiler:semantic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants