[PATCH] PR libstdc++/71579 assert that type traits are not misused with an incomplete type

Antony Polukhin antoshkka@gmail.com
Mon May 6 11:20:00 GMT 2019


This patch adds static asserts for type traits misuse with incomplete
classes and unions. This gives a nice readable error message instead
of an UB and odr-violations.

Some features of the patch:
* each type trait has it's own static_assert inside. This gives better
diagnostics than the approach with putting the assert into a helper
structure and using it in each trait.
* the result of completeness check is not memorized by the compiler.
This gives no false positive after the first failed check.
* some of the compiler builtins already implement the check. But not
all of them! So the asserts are in all the type_traits that may
benefit from the check. This also makes the behavior of libstdc++ more
consistent across different (non GCC) compilers.
* std::is_base_of does not have the assert as it works well in many
cases with incomplete types

PR libstdc++/71579
    * include/std/type_traits: Add static_asserts to make sure that
    type traits are not misused with an incomplete type.
    * testsuite/20_util/__is_complete_or_unbounded/memoization.cc:
    New test.
    * testsuite/20_util/__is_complete_or_unbounded/memoization_neg.cc:
    Likewise.
    * testsuite/20_util/__is_complete_or_unbounded/value.cc: Likewise.
    * testsuite/20_util/is_abstract/incomplete_neg.cc: Likewise.
    * testsuite/20_util/is_aggregate/incomplete_neg.cc: Likewise.
    * testsuite/20_util/is_class/value.cc: Likewise.
    * testsuite/20_util/is_function/value.cc: Likewise.
    * testsuite/20_util/is_move_constructible/incomplete_neg.cc: Likewise.
    * testsuite/20_util/is_nothrow_move_assignable/incomplete_neg.cc:
    Likewise.
    * testsuite/20_util/is_polymorphic/incomplete_neg.cc: Likewise.
    * testsuite/20_util/is_reference/value.cc: Likewise.
    * testsuite/20_util/is_unbounded_array/value.cc: Likewise.
    * testsuite/20_util/is_union/value.cc: Likewise.
    * testsuite/20_util/is_void/value.cc: Likewise.
    * testsuite/util/testsuite_tr1.h: Add incomplete union type.

-- 
Best regards,
Antony Polukhin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: type_traits_hardening.patch
Type: text/x-patch
Size: 46983 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20190506/4afa6764/attachment.bin>


More information about the Gcc-patches mailing list