File tree 4 files changed +63
-11
lines changed
compiler/crystal/semantic
4 files changed +63
-11
lines changed Original file line number Diff line number Diff line change @@ -106,25 +106,29 @@ describe "Code gen: enum" do
106
106
107
107
it " codegens enum None redefined" do
108
108
run(%(
109
- @[Flags]
110
- enum Foo
111
- A
112
- None = 10
109
+ lib Lib
110
+ @[Flags]
111
+ enum Foo
112
+ A
113
+ None = 10
114
+ end
113
115
end
114
116
115
- Foo::None
117
+ Lib:: Foo::None
116
118
) ).to_i.should eq(10 )
117
119
end
118
120
119
121
it " codegens enum All redefined" do
120
122
run(%(
121
- @[Flags]
122
- enum Foo
123
- A
124
- All = 10
123
+ lib Lib
124
+ @[Flags]
125
+ enum Foo
126
+ A
127
+ All = 10
128
+ end
125
129
end
126
130
127
- Foo::All
131
+ Lib:: Foo::All
128
132
) ).to_i.should eq(10 )
129
133
end
130
134
Original file line number Diff line number Diff line change @@ -183,6 +183,51 @@ describe "Semantic: enum" do
183
183
) ) { int32 }
184
184
end
185
185
186
+ it " disallows None value when defined with @[Flags]" do
187
+ assert_error %(
188
+ @[Flags]
189
+ enum Foo
190
+ None
191
+ end
192
+ ) ,
193
+ " flags enum can't contain None or All members"
194
+ end
195
+
196
+ it " disallows All value when defined with @[Flags]" do
197
+ assert_error %(
198
+ @[Flags]
199
+ enum Foo
200
+ All = 50
201
+ end
202
+ ) ,
203
+ " flags enum can't contain None or All members"
204
+ end
205
+
206
+ it " doesn't error when defining a non-flags enum with None or All" do
207
+ assert_type(%(
208
+ enum Foo
209
+ None
210
+ All = 50
211
+ end
212
+
213
+ Foo::None.value
214
+ ) ) { int32 }
215
+ end
216
+
217
+ it " doesn't error when defining a flags enum in a lib with None or All" do
218
+ assert_type(%(
219
+ lib Lib
220
+ @[Flags]
221
+ enum Foo
222
+ None
223
+ All = 50
224
+ end
225
+ end
226
+
227
+ Lib::Foo::None.value
228
+ ) ) { int32 }
229
+ end
230
+
186
231
it " doesn't error when defining a method for an enum with flags" do
187
232
assert_type(%(
188
233
@[Flags]
Original file line number Diff line number Diff line change @@ -550,6 +550,10 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor
550
550
node.raise " can't reopen enum and add more constants to it"
551
551
end
552
552
553
+ if is_flags && ! @in_lib && {" None" , " All" }.includes?(member.name)
554
+ member.raise " flags enum can't contain None or All members, they are autogenerated"
555
+ end
556
+
553
557
if default_value = member.default_value
554
558
counter = interpret_enum_value(default_value, base_type)
555
559
end
Original file line number Diff line number Diff line change @@ -2,7 +2,6 @@ module LLVM
2
2
{% if LibLLVM .has_constant?(:AttributeRef ) % }
3
3
@[Flags ]
4
4
enum Attribute : UInt64
5
- None = 0
6
5
Alignment = 1 << 0
7
6
AllocSize = 1 << 1
8
7
AlwaysInline = 1 << 2
You can’t perform that action at this time.
0 commit comments