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

Disallow negative enum flag values #6159

Closed

Conversation

bew
Copy link
Contributor

@bew bew commented Jun 3, 2018

Closes #5955

@bew
Copy link
Contributor Author

bew commented Jun 4, 2018

The failing test is: (introduced in 0eb47f5)

  it "casts All value to base type" do
    run(%(
      @[Flags]
      enum Foo
        A = 1 << 30
        B = 1 << 31
      end

      Foo::All.value
      )).to_i.should eq(-1073741824)
  end

I'm not sure how to understand it and make the proper changes if needed.
@asterite any idea? Ty


It fails because 1 << 31 is -2147483648 as Int32 (which is the default enum base type). It is negative, so my PR make it raise.

I'm wondering now: should the default enum flag base type be UInt32 instead?

@mjago
Copy link
Contributor

mjago commented Jun 13, 2018

Unsigned I agree ~ for flags. Signed doesn't make sense 👍

@bew
Copy link
Contributor Author

bew commented Aug 14, 2018

Any opinions from core devs?

@asterite
Copy link
Member

C# allows negative enums just fine. Is there a bug related to this, or it's just something that we are considering we could change? Because if there's no bug, and everything is working fine, I'd rather not touch the language, specially if there's a new failing spec because of this (I understand it gives a negative value, but bitwise for flags it makes sense, even if it overflows).

@RX14
Copy link
Member

RX14 commented Aug 16, 2018

@asterite does C# have flags enums?

@asterite
Copy link
Member

@RX14 Yes. That's where we got the inspiration from ;-)

And flags enum just add +1 to enum values, just like non-flags enums. In C# you are supposed to explicitly list flags enum members values: 1, 2, 4, 8, etc. So Crystal is (maybe) a bit better. I don't know if I'd bother with doing something with negative values... just my personal opinion, though.

@bew
Copy link
Contributor Author

bew commented Aug 16, 2018

It turns out negative enum flags are quite buggy: https://play.crystal-lang.org/#/r/4rdj
I'm not even sure it should work if we want to make that work, as from my understanding negative enum flags makes no sense.

@bew
Copy link
Contributor Author

bew commented Dec 27, 2018

@asterite (and core team) as showed in previous comment, flags enums with negative values are quite buggy from user-point-of-view, I'd suggest we make enum flags always unsigned, wdt?

@bcardiff
Copy link
Member

@bew the sample you show as buggy is not because the enums can be negative but because you can define overlapping values.

It is not obvious what is the value assigned to the next member when overriding one value, that could be improved/reviewed maybe.

The bug I am aware of is that if the enum base is changed to a 64 bits integer then the internal counter is not using all the available range of values, hence 64 bits integer enum needs to be explicitly defined.

@bew
Copy link
Contributor Author

bew commented Dec 27, 2018

@bew the sample you show as buggy is not because the enums can be negative but because you can define overlapping values.

It is not obvious what is the value assigned to the next member when overriding one value, that could be improved/reviewed maybe.

Indeed that's better said.. This PR can be closed then I guess

The bug I am aware of is that if the enum base is changed to a 64 bits integer then the internal counter is not using all the available range of values, hence 64 bits integer enum needs to be explicitly defined.

Nice find, didn't find an issue about this yet, will you open one?

@straight-shoota
Copy link
Member

Seems this can be closed.

@bew bew deleted the disallow-negative-enum-flag-values branch November 27, 2019 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

What to do with negative enum flags values
6 participants