QObjectData: Return const QMetaObject* from dynamicMetaObject() already now

It's semi-private API (ie. undocumented) and for most platforms¹, the
change is a no-op, which is why the approach here is a bit different
than your usual REMOVED_SINCE:

Normally, when only the return value changes, we mark the new overload
as QT6_*_NEW_OVERLOAD to allow the two function to coexist and the old
function to call the new one.

But the extra argument backing QT6_*_NEW_OVERLOAD would change the
mangling of the function even on platforms that don't mangle the
return type (the majority), and we'd have to decorate all calls that
could possibly be seen by QtCore's removed_api.cpp TU with
QT6_CALL_NEW_OVERLOAD. The main user of the API is moc-generated code,
though, and so I didn't want to have to change moc's output, even
though, currently, nothing in removed_api.cpp includes moc-generated
code. But it may, at some point in time.

This means I needed to grasp the nettle and duplicate the (granted,
trivial) implementation. Even a private helper function would mean
(maintenance and runtime) overhead for "normal"¹ platforms, so I opted
not to go there, either.

¹ those that (rightfully) don't mangle the return type, ie. all except
  ... MSVC.

As a benefit, we catch the mistake of modifying the dynamic
QMetaObject under the object's radar already now.

[ChangeLog][Potentially Source-Incompatible Changes][QtCore][QObjectData] This
(undocumented) class' dynamicMetaObject() function now returns a const
QMetaObject* (was: non-const). The backwards-compatible fix is to
receive the result in a const QMetaObject* variable (or to use auto),
and applying a manual const_cast, if a non-const object pointer was
actually required. Modifying the meta object that was returned by this
function was never supported and may lead to problems elsewhere.

Amends 0b044e8b055f9c1d93b278ed69aba76f7c886cb1.

Change-Id: I4ebc43018a2a87433ab7a97554196842b97cf1ba
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 2c212e15f8b9dc2578d93ac69a0f5826ea9de18f)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2025-01-28 18:53:02 +01:00 committed by Qt Cherry-pick Bot
parent d2c4f6df21
commit 7dca077bb6
3 changed files with 17 additions and 5 deletions

View File

@ -1292,6 +1292,18 @@ void QBasicMutex::destroyInternal(QMutexPrivate *d)
}
#endif
#include "qobject.h"
#ifdef Q_COMPILER_MANGLES_RETURN_TYPE
QMetaObject *QObjectData::dynamicMetaObject() const
{
// ### keep in sync with the master version in qobject.cpp
return metaObject->toDynamicMetaObject(q_ptr);
}
#endif // Q_COMPILER_MANGLES_RETURN_TYPE
#include "qstring.h"
QString QString::arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const

View File

@ -152,9 +152,9 @@ void (*QAbstractDeclarativeData::setWidgetParent)(QObject *, QObject *) = nullpt
QObjectData::~QObjectData() {}
QT7_ONLY(const)
QMetaObject *QObjectData::dynamicMetaObject() const
const QMetaObject *QObjectData::dynamicMetaObject() const
{
// ### keep in sync with removed_api.cpp version
return metaObject->toDynamicMetaObject(q_ptr);
}

View File

@ -90,10 +90,10 @@ public:
QDynamicMetaObjectData *metaObject;
QBindingStorage bindingStorage;
#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0)
const QMetaObject *dynamicMetaObject() const;
#else
#if QT_CORE_REMOVED_SINCE(6, 9) && defined(Q_COMPILER_MANGLES_RETURN_TYPE)
QMetaObject *dynamicMetaObject() const;
#else
const QMetaObject *dynamicMetaObject() const;
#endif
#ifdef QT_DEBUG