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 7aaf518
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
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 7aaf518

Please sign in to comment.