Skip to content

Commit

Permalink
Prevent None and All members in flags enums (#4395)
Browse files Browse the repository at this point in the history
* Prevent None and All members in flags enums

Fixes #1251
  • Loading branch information
RX14 authored and Martin Verzilli committed May 14, 2017
1 parent 5455664 commit 0e78159
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
24 changes: 14 additions & 10 deletions spec/compiler/codegen/enum_spec.cr
Expand Up @@ -106,25 +106,29 @@ describe "Code gen: enum" do

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

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

Expand Down
45 changes: 45 additions & 0 deletions spec/compiler/semantic/enum_spec.cr
Expand Up @@ -183,6 +183,51 @@ 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 flags enum in a lib with None or All" do
assert_type(%(
lib Lib
@[Flags]
enum Foo
None
All = 50
end
end
Lib::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
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 && !@in_lib && {"None", "All"}.includes?(member.name)
member.raise "flags enum can't contain None or All members, they are autogenerated"
end

if default_value = member.default_value
counter = interpret_enum_value(default_value, base_type)
end
Expand Down
1 change: 0 additions & 1 deletion src/llvm/enums.cr
Expand Up @@ -2,7 +2,6 @@ module LLVM
{% if LibLLVM.has_constant?(:AttributeRef) %}
@[Flags]
enum Attribute : UInt64
None = 0
Alignment = 1 << 0
AllocSize = 1 << 1
AlwaysInline = 1 << 2
Expand Down

0 comments on commit 0e78159

Please sign in to comment.