Skip to content

Commit

Permalink
Showing 4 changed files with 28 additions and 1 deletion.
14 changes: 14 additions & 0 deletions spec/compiler/semantic/uninitialized_spec.cr
Original file line number Diff line number Diff line change
@@ -41,6 +41,20 @@ describe "Semantic: uninitialized" do
"can't declare variable of generic non-instantiated type Foo"
end

it "errors if declaring generic type without type vars (with class var)" do
assert_error %(
class Foo(T)
end
class Bar
@@x = uninitialized Foo
end
Bar.new
),
"can't declare variable of generic non-instantiated type Foo"
end

it "errors if declares var and then assigns other type" do
assert_error %(
x = uninitialized Int32
10 changes: 10 additions & 0 deletions spec/compiler/semantic/var_spec.cr
Original file line number Diff line number Diff line change
@@ -128,4 +128,14 @@ describe "Semantic: var" do
),
"variable 'a' already declared"
end

it "errors if declaring generic type without type vars (with local var)" do
assert_error %(
class Foo(T)
end
x : Foo
),
"can't declare variable of generic non-instantiated type Foo"
end
end
3 changes: 2 additions & 1 deletion src/compiler/crystal/semantic/main_visitor.cr
Original file line number Diff line number Diff line change
@@ -363,7 +363,8 @@ module Crystal
check_not_a_constant(node.declared_type)

if declared_type = node.declared_type.type?
meta_var.freeze_type = declared_type
var_type = check_declare_var_type node, declared_type, "a variable"
meta_var.freeze_type = var_type
else
node.raise "can't infer type of type declaration"
end
2 changes: 2 additions & 0 deletions src/compiler/crystal/syntax/ast.cr
Original file line number Diff line number Diff line change
@@ -1413,6 +1413,8 @@ module Crystal
var.name.size
when InstanceVar
var.name.size
when ClassVar
var.name.size
else
raise "can't happen"
end

0 comments on commit 1e2dc90

Please sign in to comment.