Skip to content

Commit

Permalink
Fix bug related to types that have all their initialize methods as ma…
Browse files Browse the repository at this point in the history
…cro defs
  • Loading branch information
asterite authored and chris-huxtable committed Jun 6, 2018
1 parent 24d16be commit 9af4c21
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
29 changes: 29 additions & 0 deletions spec/compiler/semantic/instance_var_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4464,6 +4464,35 @@ describe "Semantic: instance var" do
), inject_primitives: false) { types["Foo"] }
end

it "is more permissive with macro def initialize, multiple" do
assert_type(%(
class Foo
@x : Int32
def initialize
{% begin %}
{% @type %}
@x = 1
{% end %}
end
def initialize(x)
{% begin %}
{% @type %}
@x = x
{% end %}
end
def x
@x
end
end
Foo.new
Foo.new(1).x
)) { int32 }
end

it "errors with macro def but another def doesn't initialize all" do
assert_error %(
class Foo
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/semantic/type_declaration_processor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ struct Crystal::TypeDeclarationProcessor
# removed if an explicit type is found (in remove_error).
@errors = {} of Type => Hash(String, Error)

# Types that have a single macro def initialize
# Types whose initialize methods are all macro defs
@has_macro_def = Set(Type).new

@type_decl_visitor = TypeDeclarationVisitor.new(@program, @explicit_instance_vars)
Expand Down Expand Up @@ -430,7 +430,7 @@ struct Crystal::TypeDeclarationProcessor
infos = find_initialize_infos(owner)

if infos
@has_macro_def << owner if infos.size == 1 && infos.first.def.macro_def?
@has_macro_def << owner if infos.all?(&.def.macro_def?)
non_nilable = compute_non_nilable_instance_vars_multi(owner, infos)
end

Expand Down

0 comments on commit 9af4c21

Please sign in to comment.