Skip to content

Commit

Permalink
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions spec/compiler/semantic/instance_var_spec.cr
Original file line number Diff line number Diff line change
@@ -2851,7 +2851,7 @@ describe "Semantic: instance var" do
Foo.new.x
),
"Can't infer the type of instance variable '@x' of Foo"
"instance variable @x of Foo was inferred to be Nil, but Nil alone provides no information"
end

it "says can't infer type if only nil was assigned, in generic type" do
@@ -2868,7 +2868,7 @@ describe "Semantic: instance var" do
Foo(Int32).new.x
),
"Can't infer the type of instance variable '@x' of Foo"
"instance variable @x of Foo(T) was inferred to be Nil, but Nil alone provides no information"
end

it "allows nil instance var because it's a generic type" do
8 changes: 6 additions & 2 deletions src/compiler/crystal/semantic/type_declaration_processor.cr
Original file line number Diff line number Diff line change
@@ -323,7 +323,7 @@ struct Crystal::TypeDeclarationProcessor
# Nil is not useful and is the same as not having it at all, at
# least for non-generic types)
if type.nil_type?
return
raise_nil_instance_var owner, name, type_info.location
end

declare_meta_type_var(owner.instance_vars, owner, name, type, type_info.location, instance_var: true)
@@ -352,7 +352,7 @@ struct Crystal::TypeDeclarationProcessor

# Same as above, only Nil makes no sense
if type.nil_type?
return
raise_nil_instance_var owner, name, type_info.location
end

declare_meta_type_var(owner.instance_vars, owner, name, type, type_info.location, instance_var: true)
@@ -383,6 +383,10 @@ struct Crystal::TypeDeclarationProcessor
!non_nilable_vars || (non_nilable_vars && !non_nilable_vars.includes?(name))
end

private def raise_nil_instance_var(owner, name, location)
raise TypeException.new("instance variable #{name} of #{owner} was inferred to be Nil, but Nil alone provides no information", location)
end

private def compute_non_nilable_instance_vars
owners = sort_types_by_depth(@initialize_infos.keys).uniq!

0 comments on commit eceb2d8

Please sign in to comment.