Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Enum.from_value? for flag enum with non Int32 base type #10303

Merged
merged 1 commit into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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