Skip to content

Commit 1e2dc90

Browse files
author
Ary Borenszweig
committedFeb 8, 2017
Fixed #3977: Invalid memory access when using uninitialized Type
1 parent 13d566b commit 1e2dc90

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed
 

‎spec/compiler/semantic/uninitialized_spec.cr

+14
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ describe "Semantic: uninitialized" do
4141
"can't declare variable of generic non-instantiated type Foo"
4242
end
4343

44+
it "errors if declaring generic type without type vars (with class var)" do
45+
assert_error %(
46+
class Foo(T)
47+
end
48+
49+
class Bar
50+
@@x = uninitialized Foo
51+
end
52+
53+
Bar.new
54+
),
55+
"can't declare variable of generic non-instantiated type Foo"
56+
end
57+
4458
it "errors if declares var and then assigns other type" do
4559
assert_error %(
4660
x = uninitialized Int32

‎spec/compiler/semantic/var_spec.cr

+10
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,14 @@ describe "Semantic: var" do
128128
),
129129
"variable 'a' already declared"
130130
end
131+
132+
it "errors if declaring generic type without type vars (with local var)" do
133+
assert_error %(
134+
class Foo(T)
135+
end
136+
137+
x : Foo
138+
),
139+
"can't declare variable of generic non-instantiated type Foo"
140+
end
131141
end

‎src/compiler/crystal/semantic/main_visitor.cr

+2-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ module Crystal
363363
check_not_a_constant(node.declared_type)
364364

365365
if declared_type = node.declared_type.type?
366-
meta_var.freeze_type = declared_type
366+
var_type = check_declare_var_type node, declared_type, "a variable"
367+
meta_var.freeze_type = var_type
367368
else
368369
node.raise "can't infer type of type declaration"
369370
end

‎src/compiler/crystal/syntax/ast.cr

+2
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,8 @@ module Crystal
14131413
var.name.size
14141414
when InstanceVar
14151415
var.name.size
1416+
when ClassVar
1417+
var.name.size
14161418
else
14171419
raise "can't happen"
14181420
end

0 commit comments

Comments
 (0)
Please sign in to comment.