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

Use C11/C++11 standard static assertion #12537

Merged
merged 2 commits into from
Oct 15, 2023
Merged

Conversation

MisterDA
Copy link
Contributor

@MisterDA MisterDA commented Sep 6, 2023

A new step in my quest of pushing usage of standard C/C++ functions in OCaml… let's use the standard static assert mechanism. "old" versions of C/C++ require a message, newer versions don't. Prefer using a keyword instead of a macro, that's one fewer level of indirection. The error message is as clear as it gets.

If we consider that all the compilers we support have full C11/C++11 support and are not buggy, we could remove the old code, or even use static_assert directly (as a macro for C11). The last commit does just that. If reviewers think it's too early to include it, I'll remove it from the PR. The macro CAML_STATIC_ASSERT isn't used in any of the opam packages.

@MisterDA MisterDA changed the title Use C/C++ standard static assert function, if available Use C11/C++11 standard static assertion Sep 8, 2023
@shindere
Copy link
Contributor

shindere commented Sep 11, 2023 via email

@MisterDA
Copy link
Contributor Author

Regarding the second commit, I have to say I don't really see the point because removing the macro makes it mandatory to specify an empty message at each call site so I am not convinced we are gaining anything here?

Note that the empty message is mandatory in C11/C++11 but optional in C23/C++17. Not using CAML_STATIC_ASSERT removes three levels of indirection of macro magic to print the line where the static assert failed.
We could also specify real explanation messages of what the assert is checking.

@shindere
Copy link
Contributor

shindere commented Sep 11, 2023 via email

runtime/caml/misc.h Outdated Show resolved Hide resolved
runtime/caml/domain_state.h Outdated Show resolved Hide resolved
Copy link
Contributor

@xavierleroy xavierleroy left a comment

Choose a reason for hiding this comment

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

Looks pretty good to me. I'd be happy to get rid of the horrible definition of CAML_STATIC_ASSERT that we currently use. A minor friction point is described below.

runtime/caml/misc.h Outdated Show resolved Hide resolved
Remove CAML_STATIC_ASSERT, use static_assert directly. The assertions
were moved to .c files as not to pollute the ocaml user-facing headers
with includes of assert.h.
@MisterDA
Copy link
Contributor Author

MisterDA commented Oct 4, 2023

I've squashed the commits.

Copy link
Contributor

@xavierleroy xavierleroy left a comment

Choose a reason for hiding this comment

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

Looks fine to me! Thanks.

@xavierleroy xavierleroy merged commit 3d4eb06 into ocaml:trunk Oct 15, 2023
3 of 6 checks passed
@MisterDA MisterDA deleted the static_assert branch October 15, 2023 15:59
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

6 participants