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 VirtualMetaclassType#implements? to ignore base_type #12632

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
12 changes: 12 additions & 0 deletions spec/compiler/codegen/cast_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,16 @@ describe "Code gen: cast" do
A.as(A.class)
))
end

it "cast virtual metaclass type to nilable virtual instance type (#12628)" do
run(<<-CR).to_b.should be_true
abstract struct Base
end

struct Impl < Base
end

Base.as(Base | Base.class).as?(Base | Impl).nil?
CR
end
end
12 changes: 12 additions & 0 deletions spec/compiler/codegen/is_a_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -922,4 +922,16 @@ describe "Codegen: is_a?" do
b.is_a?(B(Int32))
)).to_b.should be_true
end

it "virtual metaclass type is not virtual instance type (#12628)" do
run(<<-CR).to_b.should be_false
abstract struct Base
end

struct Impl < Base
end

Base.as(Base | Base.class).is_a?(Base | Impl)
CR
end
end
12 changes: 12 additions & 0 deletions spec/compiler/semantic/metaclass_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ describe "Semantic: metaclass" do
bar_class.should be_a_proper_subtype_of(foo_class)
end

it "virtual metaclass type with virtual type (#12628)" do
mod = semantic(%(
class Base; end
class Impl < Base; end
)).program

base = mod.types["Base"]
base.virtual_type!.metaclass.implements?(base).should be_false
base.virtual_type!.metaclass.implements?(base.metaclass).should be_true
base.virtual_type!.metaclass.implements?(base.metaclass.virtual_type!).should be_true
end

it "generic classes (1)" do
mod = semantic(%(
class Foo(T); end
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/types.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3513,7 +3513,7 @@ module Crystal
end

def implements?(other_type)
super || base_type.implements?(other_type)
base_type.metaclass.implements?(other_type)
end

def to_s_with_options(io : IO, skip_union_parens : Bool = false, generic_args : Bool = true, codegen : Bool = false) : Nil
Expand Down