Restore pre-Qt6 QList::fill() behavior
Somehow QList::fill(t, newSize) introduced a regression in Qt6: when newSize < QList::size() we should resize to the newSize. This is aligned with QVector::fill() in 5.15 and std::vector::assign() While 6.0 is already out, picking it to 6.0.x could save someone who haven't migrated yet as well as fix some accidental bugs in Qt's code [ChangeLog][QtCore][QList] Fixed QList::fill() regression introduced in 6.0: calling fill() with size < current list size wouldn't truncate the list Fixes: QTBUG-91042 Pick-to: 6.0 6.1 Change-Id: Ic166e2c5e42390b61df1030f7c705e344433f7f2 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
parent
3c412c93c2
commit
6512a7fc64
@ -834,8 +834,11 @@ inline QList<T> &QList<T>::fill(parameter_type t, qsizetype newSize)
|
|||||||
// we're detached
|
// we're detached
|
||||||
const T copy(t);
|
const T copy(t);
|
||||||
d->assign(d.begin(), d.begin() + qMin(size(), newSize), t);
|
d->assign(d.begin(), d.begin() + qMin(size(), newSize), t);
|
||||||
if (newSize > size())
|
if (newSize > size()) {
|
||||||
d->copyAppend(newSize - size(), copy);
|
d->copyAppend(newSize - size(), copy);
|
||||||
|
} else if (newSize < size()) {
|
||||||
|
d->truncate(newSize);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -256,7 +256,9 @@ private slots:
|
|||||||
void fillInt() const;
|
void fillInt() const;
|
||||||
void fillMovable() const;
|
void fillMovable() const;
|
||||||
void fillCustom() const;
|
void fillCustom() const;
|
||||||
void fillDetaches() const;
|
void fillDetachInt() const;
|
||||||
|
void fillDetachMovable() const;
|
||||||
|
void fillDetachCustom() const;
|
||||||
void first() const;
|
void first() const;
|
||||||
void fromListInt() const;
|
void fromListInt() const;
|
||||||
void fromListMovable() const;
|
void fromListMovable() const;
|
||||||
@ -363,6 +365,7 @@ private:
|
|||||||
template<typename T> void erase(bool shared) const;
|
template<typename T> void erase(bool shared) const;
|
||||||
template<typename T> void eraseReserved() const;
|
template<typename T> void eraseReserved() const;
|
||||||
template<typename T> void fill() const;
|
template<typename T> void fill() const;
|
||||||
|
template<typename T> void fillDetach() const;
|
||||||
template<typename T> void fromList() const;
|
template<typename T> void fromList() const;
|
||||||
template<typename T> void insert() const;
|
template<typename T> void insert() const;
|
||||||
template<typename T> void qhash() const;
|
template<typename T> void qhash() const;
|
||||||
@ -1443,6 +1446,10 @@ void tst_QList::fill() const
|
|||||||
<< SimpleValue<T>::at(2) << SimpleValue<T>::at(2)
|
<< SimpleValue<T>::at(2) << SimpleValue<T>::at(2)
|
||||||
<< SimpleValue<T>::at(2) << SimpleValue<T>::at(2)
|
<< SimpleValue<T>::at(2) << SimpleValue<T>::at(2)
|
||||||
<< SimpleValue<T>::at(2) << SimpleValue<T>::at(2));
|
<< SimpleValue<T>::at(2) << SimpleValue<T>::at(2));
|
||||||
|
|
||||||
|
// make sure it can resize to smaller size as well
|
||||||
|
myvec.fill(SimpleValue<T>::at(3), 2);
|
||||||
|
QCOMPARE(myvec, QList<T>() << SimpleValue<T>::at(3) << SimpleValue<T>::at(3));
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_QList::fillInt() const
|
void tst_QList::fillInt() const
|
||||||
@ -1464,14 +1471,63 @@ void tst_QList::fillCustom() const
|
|||||||
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
|
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_QList::fillDetaches() const
|
template<typename T>
|
||||||
|
void tst_QList::fillDetach() const
|
||||||
{
|
{
|
||||||
QList<int> test = { 1, 2, 3 };
|
// detaches to the same size
|
||||||
QList<int> copy = test;
|
{
|
||||||
copy.fill(42);
|
QList<T> original = { SimpleValue<T>::at(1), SimpleValue<T>::at(1), SimpleValue<T>::at(1) };
|
||||||
|
QList<T> copy = original;
|
||||||
|
copy.fill(SimpleValue<T>::at(2));
|
||||||
|
|
||||||
QCOMPARE(test, QList<int>({1, 2, 3}));
|
QCOMPARE(original,
|
||||||
QCOMPARE(copy, QList<int>({42, 42, 42}));
|
QList<T>({ SimpleValue<T>::at(1), SimpleValue<T>::at(1), SimpleValue<T>::at(1) }));
|
||||||
|
QCOMPARE(copy,
|
||||||
|
QList<T>({ SimpleValue<T>::at(2), SimpleValue<T>::at(2), SimpleValue<T>::at(2) }));
|
||||||
|
}
|
||||||
|
|
||||||
|
// detaches and grows in size
|
||||||
|
{
|
||||||
|
QList<T> original = { SimpleValue<T>::at(1), SimpleValue<T>::at(1), SimpleValue<T>::at(1) };
|
||||||
|
QList<T> copy = original;
|
||||||
|
copy.fill(SimpleValue<T>::at(2), 5);
|
||||||
|
|
||||||
|
QCOMPARE(original,
|
||||||
|
QList<T>({ SimpleValue<T>::at(1), SimpleValue<T>::at(1), SimpleValue<T>::at(1) }));
|
||||||
|
QCOMPARE(copy,
|
||||||
|
QList<T>({ SimpleValue<T>::at(2), SimpleValue<T>::at(2), SimpleValue<T>::at(2),
|
||||||
|
SimpleValue<T>::at(2), SimpleValue<T>::at(2) }));
|
||||||
|
}
|
||||||
|
|
||||||
|
// detaches and shrinks in size
|
||||||
|
{
|
||||||
|
QList<T> original = { SimpleValue<T>::at(1), SimpleValue<T>::at(1), SimpleValue<T>::at(1) };
|
||||||
|
QList<T> copy = original;
|
||||||
|
copy.fill(SimpleValue<T>::at(2), 1);
|
||||||
|
|
||||||
|
QCOMPARE(original,
|
||||||
|
QList<T>({ SimpleValue<T>::at(1), SimpleValue<T>::at(1), SimpleValue<T>::at(1) }));
|
||||||
|
QCOMPARE(copy, QList<T>({ SimpleValue<T>::at(2) }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void tst_QList::fillDetachInt() const
|
||||||
|
{
|
||||||
|
fillDetach<int>();
|
||||||
|
}
|
||||||
|
|
||||||
|
void tst_QList::fillDetachMovable() const
|
||||||
|
{
|
||||||
|
const int instancesCount = Movable::counter.loadAcquire();
|
||||||
|
fillDetach<Movable>();
|
||||||
|
QCOMPARE(instancesCount, Movable::counter.loadAcquire());
|
||||||
|
}
|
||||||
|
|
||||||
|
void tst_QList::fillDetachCustom() const
|
||||||
|
{
|
||||||
|
const int instancesCount = Custom::counter.loadAcquire();
|
||||||
|
fillDetach<Custom>();
|
||||||
|
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_QList::first() const
|
void tst_QList::first() const
|
||||||
|
Loading…
x
Reference in New Issue
Block a user