-
Notifications
You must be signed in to change notification settings - Fork 2k
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
core/assert: check assert at compile time, if possible #17390
base: master
Are you sure you want to change the base?
Conversation
This seems extremely useful! Can we somehow condense assert, static_assert, magic_assert and magic_static_assert? |
I'm not so sure.
|
I like the idea, but I don't like the name - how about |
I chose Maybe about Btw: If we would add a special |
I'm not a fan of |
Reminder: Soft-freeze is soon! |
Murdock results❌ FAILED 68b233b treewide: use assert_unreachable() were needed
Build failures (1)
Artifacts |
This is fine with in-tree code, but what about external code and packages? |
4a7ee85
to
54cd48a
Compare
This allows header to not export RIOT specific headers to external code.
If value of the expression `expr` in `assert(expr)` is know at compile time, this abused the optimizer and linker to fail compilation if the expression is not true. It falls back to runtime checking if the value is not known at compile time. **Beware**: This will break the common pattern to add an `assert(0);` to label code that *should* be unreachable, as clearly the value of the literal `0` is known at compile time and never true. For this reason, `assert_unreachable()` is added that can be used instead. A semantic patch has been added, so that the CI will ask contributors to replace `assert(0)` with `assert_unreachable()`.
Replace all instances where an `assert(0)` / `assert(false)` / ... was used to check for code being unreachable with `assert_unreachable()`.
I changed the header so that when included from a package, it will only check at runtime. Hence, |
Contribution description
If value of the expression
expr
inassert(expr)
is know at compile time, this abused the optimizer and linker to fail compilation if the expression is not true. It falls back to runtime checking if the value is not known at compile time.For this reason,
assert_unreachable()
is added that can be used instead. A semantic patch has been added, so that the CI will ask contributors to replaceassert(0)
withassert_unreachable()
.Testing procedure
This should compile fine:
This should fail to compile:
Issues/PRs references