diff --git a/spec/compiler/semantic/enum_spec.cr b/spec/compiler/semantic/enum_spec.cr index 0bf23ca28e3b..562e4022ff74 100644 --- a/spec/compiler/semantic/enum_spec.cr +++ b/spec/compiler/semantic/enum_spec.cr @@ -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] diff --git a/src/compiler/crystal/semantic/top_level_visitor.cr b/src/compiler/crystal/semantic/top_level_visitor.cr index 3b03f5bc351c..621203374804 100644 --- a/src/compiler/crystal/semantic/top_level_visitor.cr +++ b/src/compiler/crystal/semantic/top_level_visitor.cr @@ -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