Skip to content

Commit

Permalink
Compiler: better type model for geenric types.
Browse files Browse the repository at this point in the history
There's still a lot to improve, but this is closer to how generics
need to be modelled to properly support inheritance.
  • Loading branch information
Ary Borenszweig committed Aug 18, 2016
1 parent 356cf4d commit 773eeca
Show file tree
Hide file tree
Showing 34 changed files with 1,359 additions and 1,228 deletions.
18 changes: 18 additions & 0 deletions spec/compiler/codegen/cast_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,22 @@ describe "Code gen: cast" do
B.new.as(Void*).as(A).hi
)).to_i.should eq(42)
end

it "upcasts from non-generic to generic" do
run(%(
class Foo(T)
def foo
1
end
end
class Bar < Foo(Int32)
def foo
2
end
end
Bar.new.as(Foo(Int32)).foo
)).to_i.should eq(2)
end
end
25 changes: 25 additions & 0 deletions spec/compiler/codegen/pointer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -457,4 +457,29 @@ describe "Code gen: pointer" do
foo.value
)).to_i.should eq(3)
end

it "compares pointers through typedef" do
run(%(
module Comparable(T)
def ==(other : T)
(self <=> other) == 0
end
end
struct Pointer(T)
include Comparable(Pointer)
def <=>(other : Pointer)
0
end
end
lib LibFoo
type Ptr = Void*
end
ptr = Pointer(Void).malloc(1_u64).as(LibFoo::Ptr)
ptr == ptr
)).to_b.should be_true
end
end
13 changes: 13 additions & 0 deletions spec/compiler/codegen/proc_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -714,4 +714,17 @@ describe "Code gen: proc" do
Foo.new.@f.call
)).to_i.should eq(42)
end

it "codegens proc of generic type" do
codegen(%(
class Gen(T)
end
class Foo < Gen(Int32)
end
f = ->(x : Gen(Int32)) {}
f.call(Foo.new)
))
end
end
26 changes: 1 addition & 25 deletions spec/compiler/semantic/block_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -826,30 +826,6 @@ describe "Block inference" do
)) { array_of int32 }
end

it "passed bug included generic module and typeof" do
assert_type(%(
module Moo(U)
def moo
U
end
end
class Foo(T)
include Moo(typeof(self.foo))
def initialize(@foo : T)
end
def foo
@foo
end
end
Foo.new(1).moo
Foo.new('a').moo
)) { char.metaclass }
end

it "errors if invoking new with block when no initialize is defined" do
assert_error %(
class Foo
Expand Down Expand Up @@ -1152,7 +1128,7 @@ describe "Block inference" do
)) { bool }
end

["Object", "Foo(Object)", "Bar | Object", "(Object ->)", "( -> Object)"].each do |string|
["Object", "Bar | Object", "(Object ->)", "( -> Object)"].each do |string|
it "errors if using #{string} as block return type (#2358)" do
assert_error %(
class Foo(T)
Expand Down
11 changes: 11 additions & 0 deletions spec/compiler/semantic/class_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1041,4 +1041,15 @@ describe "Semantic: class" do
instance_vars = result.program.types["Foo"].instance_vars.to_a.map(&.[0])
instance_vars.should eq(%w(@x @y))
end

it "errors if inherits from module" do
assert_error %(
module Moo
end
class Foo < Moo
end
),
"Moo is not a class, it's a module"
end
end
2 changes: 1 addition & 1 deletion spec/compiler/semantic/def_overload_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ describe "Semantic: def overload" do
end
class Foo
include Moo(T)
include Moo(Int32)
def foo(x)
1
Expand Down
6 changes: 3 additions & 3 deletions spec/compiler/semantic/generic_class_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe "Semantic: generic class" do
class Bar < Foo(T)
end
),
"Foo is not a generic class, it's a class"
"Foo is not a generic type, it's a class"
end

it "errors if inheriting from generic and incorrect number of type vars" do
Expand Down Expand Up @@ -50,7 +50,7 @@ describe "Semantic: generic class" do
end
Bar(Int32).new.t
)) { int32.metaclass }
), inject_primitives: false) { int32.metaclass }
end

it "inhertis from generic with forwarding (2)" do
Expand Down Expand Up @@ -86,7 +86,7 @@ describe "Semantic: generic class" do
)) { int32 }
end

pending "inherits twice" do
it "inherits twice" do
assert_type(%(
class Foo
def initialize
Expand Down
Loading

0 comments on commit 773eeca

Please sign in to comment.