From e50b7c92998905efdd40314724480e3070bdbcb5 Mon Sep 17 00:00:00 2001 From: Roberto Raggi Date: Thu, 5 May 2011 17:19:01 +0200 Subject: [PATCH] Fix evaluation of boolean conditions Ensure that the operand of IR::OpIfTrue and IR::OpNot has boolean type. --- src/declarative/qml/v4/qdeclarativev4compiler.cpp | 4 ++-- .../qdeclarativev4/data/conditionalExpr.qml | 6 ++++++ .../qdeclarativev4/tst_qdeclarativev4.cpp | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 tests/auto/declarative/qdeclarativev4/data/conditionalExpr.qml diff --git a/src/declarative/qml/v4/qdeclarativev4compiler.cpp b/src/declarative/qml/v4/qdeclarativev4compiler.cpp index a7eecce..e67a382 100644 --- a/src/declarative/qml/v4/qdeclarativev4compiler.cpp +++ b/src/declarative/qml/v4/qdeclarativev4compiler.cpp @@ -404,15 +404,15 @@ void QDeclarativeV4CompilerPrivate::visitUnop(IR::Unop *e) break; case IR::OpIfTrue: + convertToBool(e->expr, src); if (src != currentReg) { i.move_reg_reg(currentReg, src); gen(i); - } else { - // nothing to do } break; case IR::OpNot: + convertToBool(e->expr, src); i.unary_not(currentReg, src); gen(i); break; diff --git a/tests/auto/declarative/qdeclarativev4/data/conditionalExpr.qml b/tests/auto/declarative/qdeclarativev4/data/conditionalExpr.qml new file mode 100644 index 0000000..b74a95a --- /dev/null +++ b/tests/auto/declarative/qdeclarativev4/data/conditionalExpr.qml @@ -0,0 +1,6 @@ +import Qt.v4 1.0 + +Result { + property int n: 2 + result: !n ? 100 : 0 +} diff --git a/tests/auto/declarative/qdeclarativev4/tst_qdeclarativev4.cpp b/tests/auto/declarative/qdeclarativev4/tst_qdeclarativev4.cpp index 0f8c5bc..fb34696 100644 --- a/tests/auto/declarative/qdeclarativev4/tst_qdeclarativev4.cpp +++ b/tests/auto/declarative/qdeclarativev4/tst_qdeclarativev4.cpp @@ -72,6 +72,7 @@ private slots: void unnecessaryReeval(); void logicalOr(); + void conditionalExpr(); void qtscript(); void qtscript_data(); void nestedObjectAccess(); @@ -121,6 +122,7 @@ void tst_qdeclarativev4::qtscript_data() QTest::newRow("qreal -> int rounding") << "qrealToIntRounding.qml"; QTest::newRow("exception on fetch") << "fetchException.qml"; QTest::newRow("logical or") << "logicalOr.qml"; + QTest::newRow("conditional expressions") << "conditionalExpr.qml"; QTest::newRow("double bool jump") << "doubleBoolJump.qml"; QTest::newRow("unary minus") << "unaryMinus.qml"; QTest::newRow("null qobject") << "nullQObject.qml"; @@ -188,6 +190,22 @@ void tst_qdeclarativev4::logicalOr() } } +void tst_qdeclarativev4::conditionalExpr() +{ + { + QDeclarativeComponent component(&engine, TEST_FILE("conditionalExpr.qml")); + + QObject *o = component.create(); + QVERIFY(o != 0); + + ResultObject *ro = qobject_cast(o); + QVERIFY(ro != 0); + + QCOMPARE(ro->result(), 0); + delete o; + } +} + // This would previously use the metaObject of the root element to result the nested access. // That is, the index for accessing "result" would have been RootObject::result, instead of // NestedObject::result. -- 1.7.2.5