From 621c05e3b1869a524c1617fe9d1d178af3f7f227 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Mon, 9 Nov 2020 17:27:04 +0100 Subject: Don't allow storing types that throw in the destructor in our containers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Types that throw in their destructors are strongly discouraged in C++, and even the STL doesn't define what happens if such types are stored in their containers. Make this more explicit for Qt and disallow storing those types in our containers. This will hopefully preempty any potential future bug reports about us not handling such a case. It also helps simplify some code in QList and other cases and makes it possible to explicitly mark more methods as noexcept. Some care needs to be taken where to add the static asserts, so that we don't disallow forward declarations of types stored in containers. Place the static assert into the destructor of the container where possible or otherwise into the templated d-pointer. Change-Id: If3aa40888f668d0f1b6c6b3ad4862b169d31280e Reviewed-by: Thiago Macieira Reviewed-by: Andrei Golubev Reviewed-by: MÃ¥rten Nordheim --- tests/auto/corelib/tools/collections/tst_collections.cpp | 1 + tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp | 11 +---------- 2 files changed, 2 insertions(+), 10 deletions(-) (limited to 'tests/auto/corelib/tools') diff --git a/tests/auto/corelib/tools/collections/tst_collections.cpp b/tests/auto/corelib/tools/collections/tst_collections.cpp index 50f9f155d0..0d4623f7fd 100644 --- a/tests/auto/corelib/tools/collections/tst_collections.cpp +++ b/tests/auto/corelib/tools/collections/tst_collections.cpp @@ -2920,6 +2920,7 @@ void tst_Collections::forwardDeclared() TEST(QVector); TEST(QStack); TEST(QQueue); + TEST(QSet); #undef TEST #undef COMMA diff --git a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp index d82f4c4a22..9aa1b4e668 100644 --- a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp +++ b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp @@ -2297,7 +2297,6 @@ ThrowingTypeWatcher& throwingTypeWatcher() { static ThrowingTypeWatcher global; struct ThrowingType { static unsigned int throwOnce; - static unsigned int throwOnceInDtor; static constexpr char throwString[] = "Requested to throw"; static constexpr char throwStringDtor[] = "Requested to throw in dtor"; void checkThrow() { @@ -2326,22 +2325,14 @@ struct ThrowingType checkThrow(); return *this; } - ~ThrowingType() noexcept(false) + ~ThrowingType() { throwingTypeWatcher().destroyed(id); // notify global watcher id = -1; - // deferred throw - if (throwOnceInDtor > 0) { - --throwOnceInDtor; - if (throwOnceInDtor == 0) { - throw std::runtime_error(throwStringDtor); - } - } } }; unsigned int ThrowingType::throwOnce = 0; -unsigned int ThrowingType::throwOnceInDtor = 0; bool operator==(const ThrowingType &a, const ThrowingType &b) { return a.id == b.id; } -- cgit v1.2.3