Skip to content

Commit

Permalink
Prevent None and All members in flags enums
Browse files Browse the repository at this point in the history
Fixes #1251
  • Loading branch information
RX14 committed May 10, 2017
1 parent 680187d commit b5b3292
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
24 changes: 0 additions & 24 deletions spec/compiler/codegen/enum_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -104,30 +104,6 @@ describe "Code gen: enum" do
)).to_i.should eq(1 + 2 + 4)
end

it "codegens enum None redefined" do
run(%(
@[Flags]
enum Foo
A
None = 10
end
Foo::None
)).to_i.should eq(10)
end

it "codegens enum All redefined" do
run(%(
@[Flags]
enum Foo
A
All = 10
end
Foo::All
)).to_i.should eq(10)
end

it "allows class vars in enum" do
run(%(
enum Foo
Expand Down
31 changes: 31 additions & 0 deletions spec/compiler/semantic/enum_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,37 @@ describe "Semantic: enum" do
)) { int32 }
end

it "disallows None value when defined with @[Flags]" do
assert_error %(
@[Flags]
enum Foo
None
end
),
"flags enum can't contain None or All members"
end

it "disallows All value when defined with @[Flags]" do
assert_error %(
@[Flags]
enum Foo
All = 50
end
),
"flags enum can't contain None or All members"
end

it "doesn't error when defining a non-flags enum with None or All" do
assert_type(%(
enum Foo
None
All = 50
end
Foo::None.value
)) { int32 }
end

it "doesn't error when defining a method for an enum with flags" do
assert_type(%(
@[Flags]
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/crystal/semantic/top_level_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,10 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor
node.raise "can't reopen enum and add more constants to it"
end

if is_flags && {"None", "All"}.includes?(member.name)
member.raise "flags enum can't contain None or All members"
end

if default_value = member.default_value
counter = interpret_enum_value(default_value, base_type)
end
Expand Down

0 comments on commit b5b3292

Please sign in to comment.