From 5f807a62761571ea6ec0fa646e6754b65d0f6d3d Mon Sep 17 00:00:00 2001 From: Robin Burchell Date: Sat, 28 Jan 2017 14:48:23 +0100 Subject: Codegen: Disallow duplicate declarations of const properties Spec 13.3.1.1 (Static Semantics: Early Errors) says: It is a Syntax Error if the BoundNames of BindingList contains any duplicate entries. Only let/const are supposed to be treated in this way, so we ensure that one of them has been marked read-only (since we don't support "let" yet). There's still no runtime check on assigning to a constant-declared variable. [ChangeLog][QtQml] "const" variable declarations now throw a SyntaxError if multiple attempts to declare the same variable name are found. Note that "const" is still not fully spec-compliant (i.e. reassignment at runtime is not disallowed). Task-number: QTBUG-58493 Change-Id: I31fd5f2bf3e79d48734e8ecb714c4e7f47e31d2a Reviewed-by: Simon Hausmann --- .../auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'tests/auto/qml/qqmlecmascript') diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index 1f0248c258..53d0912d1c 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -8208,6 +8208,12 @@ void tst_qqmlecmascript::constkw_data() "v + i\n" << false << QVariant(25); + QTest::newRow("const-multiple-scopes-same-var") + << "const v = 3\n" + "function f() { const v = 1; return v; }\n" + "v + f()\n" + << false + << QVariant(4); // error cases QTest::newRow("const-no-initializer") @@ -8218,6 +8224,25 @@ void tst_qqmlecmascript::constkw_data() << "const v = 1, i\n" << true << QVariant("SyntaxError: Missing initializer in const declaration"); + QTest::newRow("const-no-duplicate") + << "const v = 1, v = 2\n" + << true + << QVariant("SyntaxError: Identifier v has already been declared"); + QTest::newRow("const-no-duplicate-2") + << "const v = 1\n" + "const v = 2\n" + << true + << QVariant("SyntaxError: Identifier v has already been declared"); + QTest::newRow("const-no-duplicate-var") + << "const v = 1\n" + "var v = 1\n" + << true + << QVariant("SyntaxError: Identifier v has already been declared"); + QTest::newRow("var-no-duplicate-const") + << "var v = 1\n" + "const v = 1\n" + << true + << QVariant("SyntaxError: Identifier v has already been declared"); } void tst_qqmlecmascript::constkw() -- cgit v1.2.3