From ad22f5f4996f31433b5c75b518431e668ca9707a Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Mon, 27 May 2024 12:30:16 +0200 Subject: QtQml: Restore as-casting of incompatible type to value type This used to (mistakenly) return null, which is much better than constructing the value type. Amends commit 71e259837967f1eee50c057229094c2a971a1a61. Change-Id: I3da9d0a765d118dc8d85d7c5dde91936855bbf67 Reviewed-by: Fabian Kosmale --- src/qml/jsruntime/qv4runtime.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/qml/jsruntime/qv4runtime.cpp') diff --git a/src/qml/jsruntime/qv4runtime.cpp b/src/qml/jsruntime/qv4runtime.cpp index 184e7c7819..5977360080 100644 --- a/src/qml/jsruntime/qv4runtime.cpp +++ b/src/qml/jsruntime/qv4runtime.cpp @@ -390,11 +390,28 @@ QV4::ReturnedValue Runtime::As::call(ExecutionEngine *engine, const Value &lval, if (!typeWrapper) return Encode::undefined(); + const auto *stackFrame = engine->currentStackFrame; + if (lval.as()) { + qCWarning(lcCoercingTypeAssertion).nospace().noquote() + << stackFrame->source() << ':' << stackFrame->lineNumber() << ':' + << " Coercing between incompatible value types mistakenly yields null rather than" + << " undefined. Add 'pragma ValueTypeBehavior: Assertable' to prevent this."; + return Encode::null(); + } + + if (lval.as()) { + qCWarning(lcCoercingTypeAssertion).nospace().noquote() + << stackFrame->source() << ':' << stackFrame->lineNumber() << ':' + << " Coercing from instances of object types to value types mistakenly yields null" + << " rather than undefined. Add 'pragma ValueTypeBehavior: Assertable' to prevent" + << " this."; + return Encode::null(); + } + result = coerce(engine, lval, typeWrapper->d()->type(), false); if (result->isUndefined()) return Encode::undefined(); - const auto *stackFrame = engine->currentStackFrame; qCWarning(lcCoercingTypeAssertion).nospace().noquote() << stackFrame->source() << ':' << stackFrame->lineNumber() << ':' << " Coercing a value to " << typeWrapper->toQStringNoThrow() -- cgit v1.2.3