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

Prevent None and All members in flags enums #4395

Merged
merged 4 commits into from May 14, 2017

Conversation

RX14
Copy link
Contributor

@RX14 RX14 commented May 10, 2017

This is my first "real" compiler contribution, please tell me if there's anything I could improve.

See #1251.

@RX14
Copy link
Contributor Author

RX14 commented May 10, 2017

Just saw these specs. As per #1251 I don't see a usage for redefining those so i'll remove the specs.

@RX14 RX14 force-pushed the feature/flags-enum-error branch from 7aaf518 to b5b3292 Compare May 10, 2017 13:40
@bcardiff
Copy link
Member

Happy compiler hacking then!

I think we need to allow redefining All and None for enums inside libs. We can't assume the None/All semantic will hold outside crystal.

In the current state the user is able to override All and None members. It could lead to some inconsistencies where Member & All != Member or Member | None == Member, etc. So, we are missing that. But probably users will define none and all not knowing that they are autogenerated.

So.

  1. I would add in the compiler error a ", they are autogenerated."
  2. I would allow redefinition if the enum is inside a lib.

@RX14 RX14 force-pushed the feature/flags-enum-error branch from add508c to 96c0bc6 Compare May 10, 2017 14:11
@RX14
Copy link
Contributor Author

RX14 commented May 10, 2017

These two fixup commits should fix your feedback.

@@ -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 && !@in_lib && {"None", "All"}.includes?(member.name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't that be {"none", "all"}.includes?(member.name.downcase)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum Foo
  Foo
  FOo
  FOO
end

Foo.values # => [Foo, FOo, FOO]

https://carc.in/#/r/208b

@RX14
Copy link
Contributor Author

RX14 commented May 10, 2017

This PR seems to be fixed on macos now.

@mverzilli mverzilli added this to the Next milestone May 14, 2017
@mverzilli mverzilli merged commit 0e78159 into crystal-lang:master May 14, 2017
@mverzilli
Copy link

@RX14, I think I rushed a bit to merge this, and I should have requested you to review the related documentation to make sure it at least is consistent with these changes. Would mind doing that and sending another PR along?

@RX14
Copy link
Contributor Author

RX14 commented May 14, 2017

@mverzilli Not a problem, is it just the language reference that needs updating? I can't think of anywhere else where this would be documented.

@mverzilli
Copy link

Actually nevermind. It is a subtle detail and in general we try to keep the language reference simple and straight to the point. Thanks!

@RX14
Copy link
Contributor Author

RX14 commented May 14, 2017

@mverzilli I've looked through the language reference section and I agree with you, it's the wrong place for this detail.

@oprypin
Copy link
Member

oprypin commented May 14, 2017

Hi, I just noticed this and realized that my project has been "broken" by this. It's no big deal, I can just remove my definition of None, but I am losing a documented enum member with important information.
https://github.com/oprypin/crsfml/blob/v2.4.4/src/window/obj.cr#L1940
Talking in IRC, I think we agree that due to this usecase it should be possible to redefine None and All but only if their value matches what they would have been anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants