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

Terminate in throw_exception when exceptions are disabled #106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mhx
Copy link

@mhx mhx commented Feb 21, 2022

My understanding of TL_EXPECTED_EXCEPTIONS_ENABLED is that if
exceptions are disabled, the code using expected must ensure
that no attempt is ever made to throw an exception.

I think the current implementation of throw_exception is
problematic because the branch in which exceptions are disabled
simply tells the compiler that this code is never reached. This
is most likely not true if throw_exception actually gets called.
Reaching e.g. a __builtin_unreachable is undefined behaviour.

I suggest that instead of using compiler-specific builtins, the
code simply calls std::terminate. This seems appropriate, as
we pretty much have a case very similar to an uncaught exception.

This also solves the problem that __builtin_unreachable isn't
implemented by a lot of embedded compilers (which is what made
me look into this code in the first place).

My understanding of `TL_EXPECTED_EXCEPTIONS_ENABLED` is that if
exceptions are *disabled*, the code using `expected` must ensure
that no attempt is ever made to throw an exception.

I think the current implementation of `throw_exception` is
problematic because the branch in which exceptions are disabled
simply tells the compiler that this code is never reached. This
is most likely not true if `throw_exception` actually gets called.
Reaching e.g. a `__builtin_unreachable` is undefined behaviour.

I suggest that instead of using compiler-specific builtins, the
code simply calls `std::terminate`. This seems appropriate, as
we pretty much have a case very similar to an uncaught exception.

This also solves the problem that `__builtin_unreachable` isn't
implemented by a lot of embedded compilers (which is what made
me look into this code in the first place).
@daira
Copy link
Contributor

daira commented Sep 5, 2022

There should probably be tests for this behaviour; currently tests/noexcept.cpp is a placeholder. One possible way of testing it is to add a hook for the termination behaviour (like I did with TL_ASSERT in #117) so that the test can check that it is called.

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.

2 participants