From 1e2f60eb820db2845a2dd4aee4ccae103813178e Mon Sep 17 00:00:00 2001 From: gavin1012_hw Date: Tue, 1 Apr 2025 23:19:55 +0800 Subject: [PATCH] Fix objectLiteral crash Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/IBY3NC Description: a bug about object literal crash Reason: according to the spec 7.5, objectliteral cannot contain method. Signed-off-by: gavin1012_hw --- ets2panda/parser/ETSparserExpressions.cpp | 12 ++++-- .../ast/parser/ets/invalid_object_literal.ets | 41 +++++++++++++++++++ .../ast/parser/ets/unexpected_token_52.ets | 9 ++-- 3 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 ets2panda/test/ast/parser/ets/invalid_object_literal.ets diff --git a/ets2panda/parser/ETSparserExpressions.cpp b/ets2panda/parser/ETSparserExpressions.cpp index 726d66afd6..53117acfb4 100644 --- a/ets2panda/parser/ETSparserExpressions.cpp +++ b/ets2panda/parser/ETSparserExpressions.cpp @@ -207,9 +207,15 @@ ir::Expression *ETSParser::ParsePropertyDefinition(ExpressionParseFlags flags) ir::Expression *value = ParsePropertyValue(&propertyKind, &methodStatus, flags); lexer::SourcePosition end = value->End(); - auto *returnProperty = - AllocNode(propertyKind, key, value, methodStatus != ParserStatus::NO_OPTS, isComputed); - returnProperty->SetRange({start, end}); + ir::Expression *returnProperty = nullptr; + if (propertyKind == ir::PropertyKind::INIT) { + returnProperty = + AllocNode(propertyKind, key, value, methodStatus != ParserStatus::NO_OPTS, isComputed); + returnProperty->SetRange({start, end}); + } else { + returnProperty = AllocBrokenExpression(key->Start()); + LogError(diagnostic::OBJECT_PATTER_CONTAIN_METHODS, {}, returnProperty->Start()); + } return returnProperty; } diff --git a/ets2panda/test/ast/parser/ets/invalid_object_literal.ets b/ets2panda/test/ast/parser/ets/invalid_object_literal.ets new file mode 100644 index 0000000000..4929e16649 --- /dev/null +++ b/ets2panda/test/ast/parser/ets/invalid_object_literal.ets @@ -0,0 +1,41 @@ +/* + * 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 A { + public _num: number = 0; + get num(): number { + return this._num; + } + + set num(n: number) { + this._num = n; + } +} + +let a: A = /* @@ label1 */{ + _num:0, + + get /* @@ label2 */num(): number { + return this._num; + }, + + set /* @@ label3 */num(n: number) { + this._num = n; + } +} + +/* @@@ label1 Error TypeError: The object literal properties must be key-value pairs */ +/* @@@ label2 Error SyntaxError: Object pattern can't contain methods. */ +/* @@@ label3 Error SyntaxError: Object pattern can't contain methods. */ diff --git a/ets2panda/test/ast/parser/ets/unexpected_token_52.ets b/ets2panda/test/ast/parser/ets/unexpected_token_52.ets index 7dee5c70d5..b447ab92d5 100644 --- a/ets2panda/test/ast/parser/ets/unexpected_token_52.ets +++ b/ets2panda/test/ast/parser/ets/unexpected_token_52.ets @@ -13,10 +13,11 @@ * limitations under the License. */ -let a = { - get [b/* @@ label */)(){}, +let /* @@ label1 */a = { + get [/* @@ label2 */b/* @@ label3 */)(){}, ...a, } -/* @@@ label Error SyntaxError: Unexpected token, expected ']'. */ -/* @@? 16:5 Error TypeError: Cannot infer type for a because class composite needs an explicit target type */ +/* @@@ label1 Error TypeError: Cannot infer type for a because class composite needs an explicit target type */ +/* @@@ label2 Error SyntaxError: Object pattern can't contain methods. */ +/* @@@ label3 Error SyntaxError: Unexpected token, expected ']'. */ -- Gitee