From 83cc7ddea0e78c00c7a3cef180c9790236cfc5e3 Mon Sep 17 00:00:00 2001 From: zengzengran Date: Mon, 26 May 2025 18:56:48 +0800 Subject: [PATCH] Prohibit use StringLiteral as ObjectLiteralField Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICAN8Y Description: In the spec, only identifier is allowed as an ObjectLiteralField. Using String Literal should throw a CTE Tested-by: ninja tests (passed) ets_testrunner (passed) Signed-off-by: zengzengran # --- ets2panda/checker/ETSAnalyzer.cpp | 8 +--- .../ets/MultiPropertyWithSameName.ets | 8 ++-- .../ast/compiler/ets/objectLiteralBadKey.ets | 2 +- ...ectLiteral_stringliteral_as_identifier.ets | 43 +++++++++++++++++++ .../test/runtime/ets/objectLiteral-2.ets | 2 +- ets2panda/test/runtime/ets/objectLiteral.ets | 2 +- .../ets/objectLiteralInterfaceType.ets | 2 +- ets2panda/util/diagnostic/semantic.yaml | 2 +- 8 files changed, 53 insertions(+), 16 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/objectLiteral_stringliteral_as_identifier.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index e889e393d7..c8f4edd38e 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -1939,15 +1939,11 @@ void ETSAnalyzer::CheckObjectExprProps(const ir::ObjectExpression *expr, checker ir::Expression *key = propExpr->AsProperty()->Key(); ir::Expression *value = propExpr->AsProperty()->Value(); - util::StringView pname; - if (key->IsStringLiteral()) { - pname = key->AsStringLiteral()->Str(); - } else if (key->IsIdentifier()) { - pname = key->AsIdentifier()->Name(); - } else { + if (!key->IsIdentifier()) { checker->LogError(diagnostic::CLASS_COMPOSITE_INVALID_KEY, {}, expr->Start()); return; } + util::StringView pname = key->AsIdentifier()->Name(); varbinder::LocalVariable *lv = objType->GetProperty(pname, searchFlags); if (lv == nullptr) { checker->LogError(diagnostic::UNDEFINED_PROPERTY, {objType->Name(), pname}, propExpr->Start()); diff --git a/ets2panda/test/ast/compiler/ets/MultiPropertyWithSameName.ets b/ets2panda/test/ast/compiler/ets/MultiPropertyWithSameName.ets index 3c7d4737be..1b0436727a 100644 --- a/ets2panda/test/ast/compiler/ets/MultiPropertyWithSameName.ets +++ b/ets2panda/test/ast/compiler/ets/MultiPropertyWithSameName.ets @@ -33,8 +33,6 @@ let c: A = { "field": '3', } - -/* @@? 22:5 Error TypeError: An object literal cannot have multiple properties with the same name. */ -/* @@? 27:5 Error TypeError: An object literal cannot have multiple properties with the same name. */ -/* @@? 28:5 Error TypeError: An object literal cannot have multiple properties with the same name. */ -/* @@? 33:5 Error TypeError: An object literal cannot have multiple properties with the same name. */ +/* @@? 20:12 Error TypeError: key in class composite should be identifier. */ +/* @@? 25:12 Error TypeError: key in class composite should be identifier. */ +/* @@? 31:12 Error TypeError: key in class composite should be identifier. */ diff --git a/ets2panda/test/ast/compiler/ets/objectLiteralBadKey.ets b/ets2panda/test/ast/compiler/ets/objectLiteralBadKey.ets index d93a7dccdd..0a68e69ccd 100644 --- a/ets2panda/test/ast/compiler/ets/objectLiteralBadKey.ets +++ b/ets2panda/test/ast/compiler/ets/objectLiteralBadKey.ets @@ -20,4 +20,4 @@ let c: C = /* @@ label */{ 33: 44, }; -/* @@@ label Error TypeError: key in class composite should be either identifier or string literal */ +/* @@@ label Error TypeError: key in class composite should be identifier. */ diff --git a/ets2panda/test/ast/compiler/ets/objectLiteral_stringliteral_as_identifier.ets b/ets2panda/test/ast/compiler/ets/objectLiteral_stringliteral_as_identifier.ets new file mode 100644 index 0000000000..38e6b68064 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/objectLiteral_stringliteral_as_identifier.ets @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class C { + field: string = "abc"; +} + +interface I { + field: string; +} + +function main() { + let c1: C = { field: "pass" }; + let c2: C = /* @@ label1 */{ "field": "CTE" }; + + let i1: I = { field: "pass" }; + let i2: I = /* @@ label2 */{ "field": "CTE" }; + + let r1: Record = { "field": "pass" }; + + testClass(/* @@ label3 */{ "field": "CTE" }) + testInterface(/* @@ label4 */{ "field": "CTE" }) +} + +function testClass(c: C) { } +function testInterface(i: I) { } + +/* @@@ label1 Error TypeError: key in class composite should be identifier. */ +/* @@@ label2 Error TypeError: key in class composite should be identifier. */ +/* @@@ label3 Error TypeError: key in class composite should be identifier. */ +/* @@@ label4 Error TypeError: key in class composite should be identifier. */ diff --git a/ets2panda/test/runtime/ets/objectLiteral-2.ets b/ets2panda/test/runtime/ets/objectLiteral-2.ets index a537fb3627..7a48565205 100644 --- a/ets2panda/test/runtime/ets/objectLiteral-2.ets +++ b/ets2panda/test/runtime/ets/objectLiteral-2.ets @@ -39,7 +39,7 @@ function test(c: C = {}, x: int = 0, ivv: int = 0) { } function main(): int { - let c: C = {"x": 7, iv: {v: 8}}; // variable definition + let c: C = {x: 7, iv: {v: 8}}; // variable definition test(c, 7, 8) test() // optional parameter diff --git a/ets2panda/test/runtime/ets/objectLiteral.ets b/ets2panda/test/runtime/ets/objectLiteral.ets index 66c40e4e22..19154ee67b 100644 --- a/ets2panda/test/runtime/ets/objectLiteral.ets +++ b/ets2panda/test/runtime/ets/objectLiteral.ets @@ -41,7 +41,7 @@ function test(c: C, f: int, x: int, s: String, ivv: int) { function main(): int { let c: C = { // variable definition - "x": 7, + x: 7, s: "sss", }; test(c, 0, 7, "sss", 0) diff --git a/ets2panda/test/runtime/ets/objectLiteralInterfaceType.ets b/ets2panda/test/runtime/ets/objectLiteralInterfaceType.ets index 2bcc70dd0c..32437b9661 100644 --- a/ets2panda/test/runtime/ets/objectLiteralInterfaceType.ets +++ b/ets2panda/test/runtime/ets/objectLiteralInterfaceType.ets @@ -47,7 +47,7 @@ function test(i: I = {f: -1, x: -2, s: "default", iv: {v: -3}}, function main(): int { let i: I = { // variable definition f : 1, - "x": 2, + x: 2, s: "s1", iv: { v: 3} }; diff --git a/ets2panda/util/diagnostic/semantic.yaml b/ets2panda/util/diagnostic/semantic.yaml index 302489098a..3ea11f3781 100644 --- a/ets2panda/util/diagnostic/semantic.yaml +++ b/ets2panda/util/diagnostic/semantic.yaml @@ -302,7 +302,7 @@ semantic: - name: CLASS_COMPOSITE_INVALID_KEY id: 73 - message: "key in class composite should be either identifier or string literal" + message: "key in class composite should be identifier." - name: OBJECT_LITERAL_METHOD_KEY id: 74 -- Gitee