From 05b9a4b2deefd586356e1f36d84372b06e74cfe3 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 24 Dec 2024 13:39:25 +0100 Subject: [PATCH] tst_QSpan: check QList -> QSpan doesn't detach the former It does. While std::span, does, too, and so users should be using std::as_const(), it's quite simple to avoid for QSpan, so we'll fix it in QSpan. This patch adds the reproducer. Task-number: QTBUG-132133 Pick-to: 6.9 6.8 Change-Id: I2e416fb7344830cd5e0d945cce61491cd6f4a7a5 Reviewed-by: Giuseppe D'Angelo Reviewed-by: Thiago Macieira --- tests/auto/corelib/tools/qspan/tst_qspan.cpp | 28 ++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/auto/corelib/tools/qspan/tst_qspan.cpp b/tests/auto/corelib/tools/qspan/tst_qspan.cpp index 07dda7db85c..2d63b0a45a2 100644 --- a/tests/auto/corelib/tools/qspan/tst_qspan.cpp +++ b/tests/auto/corelib/tools/qspan/tst_qspan.cpp @@ -129,6 +129,8 @@ private Q_SLOTS: void fromQList() const; void fromInitList() const; + void constQSpansDontDetachQtContainers() const; + private: template void check_identical(QSpan lhs, QSpan rhs) const; @@ -468,6 +470,32 @@ void tst_QSpan::fromQList() const from_variable_size_container_impl(li); } +void tst_QSpan::constQSpansDontDetachQtContainers() const +{ + QList li = {42, 84, 168, 336}; + + { + [[maybe_unused]] const QList copy = li; + QVERIFY(!li.isDetached()); + [[maybe_unused]] QSpan cvspan = li; // should not detach (QTBUG-132133) + QEXPECT_FAIL("", "QTBUG-132133", Continue); + QVERIFY(!li.isDetached()); + [[maybe_unused]] QSpan mvspan = li; // this _has_ to detach, though + QVERIFY(li.isDetached()); + } + + // same check for fixed-size spans + { + [[maybe_unused]] const QList copy = li; + QVERIFY(!li.isDetached()); + [[maybe_unused]] QSpan cfspan = li; // should not detach (QTBUG-132133) + QEXPECT_FAIL("", "QTBUG-132133", Continue); + QVERIFY(!li.isDetached()); + [[maybe_unused]] QSpan mfspan = li; // this _has_ to detach, though + QVERIFY(li.isDetached()); + } +} + void tst_QSpan::fromInitList() const { from_variable_size_container_impl(std::initializer_list{42, 84, 168, 336});