Skip to content

Commit

Permalink
Fixed #225: Void is not an Object nor a Value, it doesn't have any me…
Browse files Browse the repository at this point in the history
…thods.
  • Loading branch information
Ary Borenszweig committed Oct 12, 2014
1 parent 39eea47 commit dab545f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 29 deletions.
10 changes: 10 additions & 0 deletions spec/compiler/type_inference/primitives_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,14 @@ describe "Type inference: primitives" do
it "types instance_sizeof" do
assert_type("instance_sizeof(Reference)") { int32 }
end

it "errors when comparing void (#225)" do
assert_error %(
lib Foo
fun foo
end
Foo.foo == 1
), "undefined method '==' for Void"
end
end
2 changes: 1 addition & 1 deletion src/compiler/crystal/program.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module Crystal
@number.abstract = true

@types["NoReturn"] = @no_return = NoReturnType.new self
@types["Void"] = @void = VoidType.new self, self, "Void", @value, 1
@types["Void"] = @void = VoidType.new self
@types["Nil"] = @nil = NilType.new self, self, "Nil", @value, 1
@types["Bool"] = @bool = BoolType.new self, self, "Bool", @value, 1
@types["Char"] = @char = CharType.new self, self, "Char", @value, 4
Expand Down
78 changes: 50 additions & 28 deletions src/compiler/crystal/types.cr
Original file line number Diff line number Diff line change
Expand Up @@ -395,33 +395,6 @@ module Crystal
end
end

class NoReturnType < Type
getter :program

def initialize(@program)
end

def lookup_matches(signature, owner = self, type_lookup = self, matches_array = nil)
Matches.new(nil, nil, self, false)
end

def no_return?
true
end

def primitive_like?
true
end

def parents
nil
end

def to_s(io)
io << "NoReturn"
end
end

abstract class ContainedType < Type
getter :program
getter :container
Expand Down Expand Up @@ -1459,10 +1432,59 @@ module Crystal
end
end

class VoidType < PrimitiveType
abstract class EmptyType < Type
getter :program

def initialize(@program)
end

def lookup_defs(name)
[] of Def
end

def lookup_matches(signature, owner = self, type_lookup = self, matches_array = nil)
Matches.new(nil, nil, self, false)
end

def parents
nil
end

def allocated
true
end

def abstract
false
end
end

class NoReturnType < EmptyType
def no_return?
true
end

def primitive_like?
true
end

def to_s(io)
io << "NoReturn"
end
end

class VoidType < EmptyType
def void?
true
end

def primitive_like?
true
end

def to_s(io)
io << "Void"
end
end

class ValueType < NonGenericClassType
Expand Down

0 comments on commit dab545f

Please sign in to comment.