Skip to content

Commit

Permalink
Fix Enum.from_value? for flag enum with non Int32 base type (#10303)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored and bcardiff committed Jan 29, 2021
1 parent e1abc3f commit 1a65b22
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions spec/std/enum_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ enum SpecEnumFlags
Three
end

@[Flags]
enum SpecEnumFlags8 : Int8
One
Two
Three
end

@[Flags]
private enum PrivateFlagsEnum
FOO
Expand Down Expand Up @@ -167,6 +174,7 @@ describe Enum do
SpecEnumFlags.from_value?(2).should eq(SpecEnumFlags::Two)
SpecEnumFlags.from_value?(3).should eq(SpecEnumFlags::One | SpecEnumFlags::Two)
SpecEnumFlags.from_value?(8).should be_nil
SpecEnumFlags8.from_value?(1_i8).should eq(SpecEnumFlags8::One)
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/enum.cr
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ struct Enum
{% if @type.annotation(Flags) %}
all_mask = {{@type}}::All.value
return if all_mask & value != value
return new(value.to_i)
return new(all_mask.class.new(value))
{% else %}
{% for member in @type.constants %}
return new({{@type.constant(member)}}) if {{@type.constant(member)}} == value
Expand Down

0 comments on commit 1a65b22

Please sign in to comment.