Skip to content

Commit 324d5ba

Browse files
author
Ary Borenszweig
committedDec 4, 2016
Compiler: fixed bug regarding generic type, module inclusion and instance vars. Fixes #3635
1 parent 4b7ef7d commit 324d5ba

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
 

‎spec/compiler/semantic/instance_var_spec.cr

+22
Original file line numberDiff line numberDiff line change
@@ -4538,4 +4538,26 @@ describe "Semantic: instance var" do
45384538
Foo.new
45394539
)) { types["Foo"] }
45404540
end
4541+
4542+
it "doesn't error if inheriting generic instance (#3635)" do
4543+
assert_type(%(
4544+
module Core(T)
4545+
@a : Bool
4546+
end
4547+
4548+
class Base(T)
4549+
include Core(Int32)
4550+
4551+
@a = true
4552+
end
4553+
4554+
class Foo < Base(String)
4555+
def a
4556+
@a
4557+
end
4558+
end
4559+
4560+
Foo.new.a
4561+
)) { bool }
4562+
end
45414563
end

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

+8
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ struct Crystal::TypeDeclarationProcessor
236236
end
237237

238238
private def process_owner_instance_var_declaration(owner, name, type_decl)
239+
# Generic instances already have their instance vars
240+
# set from uninstantiated generic types
241+
return if owner.is_a?(GenericInstanceType)
242+
239243
# Check if a superclass already defined this variable
240244
supervar = owner.lookup_instance_var?(name)
241245

@@ -313,6 +317,10 @@ struct Crystal::TypeDeclarationProcessor
313317
end
314318

315319
private def process_owner_guessed_instance_var_declaration(owner, name, type_info)
320+
# Generic instances already have their instance vars
321+
# set from uninstantiated generic types
322+
return if owner.is_a?(GenericInstanceType)
323+
316324
case owner
317325
when NonGenericClassType
318326
# If a superclass already defines this variable we ignore

0 commit comments

Comments
 (0)
Please sign in to comment.