Go to file
Giuseppe D'Angelo c2d2757bcc QString/QByteArray: detach immediately in operator[]
Unlike any other implicitly shared container, QString/QByteArray
have a "lazy detach" mechanism: their operator[] returns a
special object; assignment into that object will actually
detach.

In other words:

  QString a("Hello");
  QCharRef c = a[0];  // does not detach
  c = 'J';            // detach happens here

This allows this behavior:

  QString a("Hello");
  QCharRef c = a[0];
  QString b = a;
  c = 'J';               // detach happens here
  assert(a == "Jello");
  assert(b == "Hello");

Note that this happens only with operator[] -- the mutating
iterator APIs instead detach immediately, making the above code
have visible side effects in b (at the end, b == "Jello").

The reasons for this special behavior seems to have been lost in
the dawn of time: this is something present all the way back
since Qt 2, maybe even Qt 1. Holding on to a "reference" while
taking copies of a container is documented [1] to be a bad idea,
so we shouldn't double check that the users don't do it.

This patch:

1) adds an immediate detach in operator[], just like all other
containers;

2) adds a warning in debug builds in case QByteRef/QCharRef is going
to cause a detach;

3) marks operator[] as [[nodiscard]] to warn users not using
Clazy about the (unintended) detach now happening in their code.

This paves the way for removal of QCharRef/QByteRef, likely in
Qt 7.

[1] https://doc.qt.io/qt-5/containers.html#implicit-sharing-iterator-problem

[ChangeLog][QtCore][QString] QString::operator[] detaches
immediately. Previously, the detach was delayed until a
modification was made to the string through the returned
QCharRef.

[ChangeLog][QtCore][QByteArray] QByteArray::operator[] detaches
immediately. Previously, the detach was delayed until a
modification was made to the byte array through the returned
QByteRef.

Change-Id: I9f77ae36759d80dc3202426a798f5b1e5fb2c2c5
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
2019-05-19 16:33:56 +02:00
bin syncqt: Fix resolution of injected headers for external modules 2019-04-23 09:09:33 +00:00
config.tests x86: Disable AVX support on 64-bit MinGW 2019-02-06 08:11:29 +00:00
dist Merge remote-tracking branch 'origin/5.12' into 5.13 2019-04-24 01:00:13 +02:00
doc Merge remote-tracking branch 'origin/5.12' into 5.13 2019-05-09 13:06:11 +00:00
examples Fix memory leak with arthur style 2019-05-23 15:57:51 +02:00
lib Purge all fonts 2015-08-18 19:59:14 +00:00
mkspecs Fix mingw pkgconfig file and dependency naming 2019-05-16 11:03:40 +00:00
qmake qmake: remove use of Java-style iterators 2019-05-21 08:58:35 +02:00
src QString/QByteArray: detach immediately in operator[] 2019-05-19 16:33:56 +02:00
tests Remove usages of Q_OS_WINCE 2019-05-23 13:51:05 +02:00
util Clean up some poorly-placed newlines in the TLD suffix data 2019-03-08 10:44:05 +01:00
.gitattributes Update the git-archive export options 2012-09-07 15:39:31 +02:00
.gitignore Remove support for qml1 plugins and modules 2018-11-15 19:52:13 +00:00
.qmake.conf Bump version 2019-03-22 05:35:40 +00:00
.tag Update the git-archive export options 2012-09-07 15:39:31 +02:00
config_help.txt Add -qtlibinfix-plugins for renaming Qt plugins according to QT_LIBINFIX 2019-05-16 06:22:25 +00:00
configure configure: Treat win32-clang-g++ the same as win32-g++ 2018-10-10 04:10:40 +00:00
configure.bat Configure: simplify logic 2019-03-14 12:13:58 +00:00
configure.json rtems: Disable features which RTEMS does not support 2019-05-14 13:53:35 +02:00
configure.pri Add qmake support for c++2a 2019-04-19 08:14:25 +00:00
header.BSD Use placeholder for year in header.* files 2018-04-16 11:02:22 +00:00
header.COMM Use placeholder for year in header.* files 2018-04-16 11:02:22 +00:00
header.FDL Use placeholder for year in header.* files 2018-04-16 11:02:22 +00:00
header.GPL Use placeholder for year in header.* files 2018-04-16 11:02:22 +00:00
header.GPL-EXCEPT Use placeholder for year in header.* files 2018-04-16 11:02:22 +00:00
header.LGPL Use placeholder for year in header.* files 2018-04-16 11:02:22 +00:00
header.LGPL3 Use placeholder for year in header.* files 2018-04-16 11:02:22 +00:00
header.LGPL3-COMM Use placeholder for year in header.* files 2018-04-16 11:02:22 +00:00
header.LGPL-NOGPL2 Use placeholder for year in header.* files 2018-04-16 11:02:22 +00:00
header.LGPL-ONLY Use placeholder for year in header.* files 2018-04-16 11:02:22 +00:00
header.MIT qsimd: add support for new x86 CPU features 2018-05-05 06:20:07 +00:00
INSTALL INSTALL: Remove outdated reference to Windows CE 2019-02-13 13:01:57 +00:00
LICENSE.FDL Initial import from the monolithic Qt. 2011-04-27 12:05:43 +02:00
LICENSE.GPL2 Add new license header templates and license files 2016-01-14 20:43:46 +00:00
LICENSE.GPL3 Add new license header templates and license files 2016-01-14 20:43:46 +00:00
LICENSE.GPL3-EXCEPT Add new license header templates and license files 2016-01-14 20:43:46 +00:00
LICENSE.LGPL3 Add new license header templates and license files 2016-01-14 20:43:46 +00:00
LICENSE.LGPLv3 Remove LICENSE.GPLv3, LICENSE.LGPLv21, LGPL_EXCEPTION.txt 2018-04-16 11:02:14 +00:00
LICENSE.QT-LICENSE-AGREEMENT-4.0 Replace commercial preview license with Qt License Agreement 4.0 2018-06-21 11:39:15 +00:00
qtbase.pro nuke configure -host-option 2016-12-13 18:55:59 +00:00
sync.profile platformsupport/input: add xkbcommon utilities lib 2019-03-01 14:31:28 +00:00