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

Support enums defined in C++ code. #187

Merged
merged 3 commits into from
May 5, 2020
Merged

Conversation

jgalenson
Copy link
Contributor

@jgalenson jgalenson commented May 4, 2020

This allows listing an enum in an extern "C" block as well as in the shared block. In this case, we do not emit the C++ declaration of the enum but instead emit static assertions that it has the values that we expect.

Do you know a good way to test the assertions? The ui tests don't seem to compile the generated C++ files, so I'm not sure how to do it.

I'm also considering refactoring the discriminant generation code somewhere now that it's mostly duplicated in three different places.

Closes #177.

This allows listing an enum in an extern "C" block as well as in the
shared block.  In this case, we do not emit the C++ declaration of the
enum but instead emit static assertions that it has the values that we
expect.
Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Thank you!

I filed #188 to follow up on a test harness that is able to cover static_asserts.

@jgalenson jgalenson requested a review from dtolnay May 5, 2020 03:08
@dtolnay
Copy link
Owner

dtolnay commented May 5, 2020

The reason I didn't compute the discriminant during parsing in my original patch was to keep it as close to the source as possible. If you want to change that, we can.

I think the goal of the representation under crate::syntax::Api would be to be useful to the later compiler stages (check.rs, expand.rs + write.rs), not necessarily to be close to the source. That means normalizing all insignificant differences. For example:

  • Whether the person typed fn f(&self) or fn f(self: &Ident) doesn't matter outside the parser, so those come out of the parser looking exactly the same.

  • Similarly, whether they typed extern "C" { fn f(); fn g(); } or extern "C" { fn f(); } extern "C" { fn g(); } isn't significant, so those come out of the parser the same.

  • I would want the same for enum E { A, B } vs enum E { A = 0, B } vs enum E { A, B = 1 } vs enum E { A = 0, B = 1 }. These are only different in ways that only matter to the parser.

@dtolnay dtolnay merged commit de471c9 into dtolnay:master May 5, 2020
@jgalenson jgalenson deleted the enums branch May 5, 2020 03:52
@jgalenson
Copy link
Contributor Author

That makes sense. I'll follow up with that tomorrow.

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.

Extern C-like enums
2 participants