diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index e889e393d757b7c78bc43da78c0bbf9ff6d13fad..c8f4edd38ec8e48648fc0f493f3790ae6aac4045 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 3c7d4737be6c08d653a939fd752b78946e9ababd..1b0436727a2bdd1a57f7e7ceb04ce72a243211bc 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 d93a7dccdde10b944e31c4816265bfb36fe6dd55..0a68e69ccd1ab814781eaab11e5030e299070a4f 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 0000000000000000000000000000000000000000..38e6b680643f037bc6cd8f77f97e4d97aa8250c0 --- /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 a537fb3627e3c61d805da261579a5da6118c7737..7a48565205f197facaa473599008162cc109d694 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 66c40e4e22851a87c17a8a69a97e569d85cf31a8..19154ee67bef2d420347650d871e713657034316 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 2bcc70dd0c43053b73f2479c14c153fdfdff88d5..32437b9661275be6d15460e2463d1f6437ecd68d 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 302489098a9dc43b3dc23b6cded42765acf6cdd2..3ea11f3781954e5c34b3320656236bd6caf61146 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