Skip to content

Commit

Permalink
Showing 5 changed files with 34 additions and 9 deletions.
9 changes: 9 additions & 0 deletions spec/compiler/type_inference/class_var_spec.cr
Original file line number Diff line number Diff line change
@@ -333,4 +333,13 @@ describe "Type inference: class var" do
),
"can't use Class as the type of class variable @@class of Foo, use a more specific type"
end

it "gives correct error when trying to use Int as a class variable type" do
assert_error %(
class Foo
@@x : Int
end
),
"can't use Int as the type of a class variable yet, use a more specific type"
end
end
7 changes: 7 additions & 0 deletions spec/compiler/type_inference/global_spec.cr
Original file line number Diff line number Diff line change
@@ -687,4 +687,11 @@ describe "Global inference" do
),
"can't use Class as the type of global variable $class, use a more specific type"
end

it "gives correct error when trying to use Int as a global variable type" do
assert_error %(
$x : Int
),
"can't use Int as the type of a global variable yet, use a more specific type"
end
end
9 changes: 9 additions & 0 deletions spec/compiler/type_inference/instance_var_spec.cr
Original file line number Diff line number Diff line change
@@ -3081,6 +3081,15 @@ describe "Type inference: instance var" do
)) { fun_of(void) }
end

it "gives correct error when trying to use Int as an instance variable type" do
assert_error %(
class Foo
@x : Int
end
),
"can't use Int as the type of an instance variable yet, use a more specific type"
end

# -----------------
# ||| OLD SPECS |||
# vvv vvv
4 changes: 2 additions & 2 deletions src/compiler/crystal/semantic/base_type_visitor.cr
Original file line number Diff line number Diff line change
@@ -834,14 +834,14 @@ module Crystal
type
end

def check_declare_var_type(node, declared_type)
def check_declare_var_type(node, declared_type, variable_kind)
type = declared_type.instance_type

if type.is_a?(GenericClassType)
node.raise "can't declare variable of generic non-instantiated type #{type}"
end

Crystal.check_type_allowed_in_generics(node, type, "can't use #{type} as a Proc argument type")
Crystal.check_type_allowed_in_generics(node, type, "can't use #{type} as the type of #{variable_kind}")

declared_type
end
14 changes: 7 additions & 7 deletions src/compiler/crystal/semantic/type_declaration_visitor.cr
Original file line number Diff line number Diff line change
@@ -147,7 +147,7 @@ module Crystal
def declare_instance_var_on_non_generic(owner, node, var)
# For non-generic types we can solve the type now
var_type = lookup_type(node.declared_type)
var_type = check_declare_var_type(node, var_type)
var_type = check_declare_var_type(node, var_type, "an instance variable")
owner_vars = @instance_vars[owner] ||= {} of String => TypeDeclarationWithLocation
type_decl = TypeDeclarationWithLocation.new(var_type.virtual_type, node.location.not_nil!)
owner_vars[var.name] = type_decl
@@ -162,16 +162,16 @@ module Crystal

def declare_class_var(node, var)
owner = class_var_owner(node)
var_type = lookup_type(node.declared_type).virtual_type
var_type = check_declare_var_type(node, var_type)
var_type = lookup_type(node.declared_type)
var_type = check_declare_var_type(node, var_type, "a class variable")
owner_vars = @class_vars[owner] ||= {} of String => Type
owner_vars[var.name] = var_type
owner_vars[var.name] = var_type.virtual_type
end

def declare_global_var(node, var)
var_type = lookup_type(node.declared_type).virtual_type
var_type = check_declare_var_type(node, var_type)
@globals[var.name] = var_type
var_type = lookup_type(node.declared_type)
var_type = check_declare_var_type(node, var_type, "a global variable")
@globals[var.name] = var_type.virtual_type
end

def visit(node : Def)

0 comments on commit 4feb758

Please sign in to comment.