Skip to content

Commit

Permalink
Fixed #3977: Invalid memory access when using uninitialized Type
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Feb 8, 2017
1 parent 13d566b commit 1e2dc90
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
14 changes: 14 additions & 0 deletions spec/compiler/semantic/uninitialized_spec.cr
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/compiler/semantic/var_spec.cr
Expand Up @@ -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
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/crystal/syntax/ast.cr
Expand Up @@ -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
Expand Down

0 comments on commit 1e2dc90

Please sign in to comment.