Skip to content

Commit

Permalink
Fix: disallow cast from module class to virtual metaclass (#6320)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored and sdogruyol committed Jul 3, 2018
1 parent 63536bd commit 48530e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
22 changes: 22 additions & 0 deletions spec/compiler/semantic/module_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1259,4 +1259,26 @@ describe "Semantic: module" do
),
"can't declare instance variables in Bar:Class"
end

it "can't pass module class to virtual metaclass (#6113)" do
assert_error %(
module Moo
end
class Foo
end
class Bar < Foo
include Moo
end
class Gen(T)
def self.foo(x : T)
end
end
Gen(Foo.class).foo(Moo)
),
"no overload matches"
end
end
3 changes: 3 additions & 0 deletions src/compiler/crystal/semantic/restrictions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,9 @@ module Crystal
end

def restrict(other : VirtualMetaclassType, context)
# A module class can't be restricted into a class
return nil if instance_type.module?

restricted = instance_type.restrict(other.instance_type.base_type, context)
restricted ? self : nil
end
Expand Down

0 comments on commit 48530e5

Please sign in to comment.