diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 2f97ff51179a905ed2f74b611bfefc82ce467d20..98fbac00ea7345884a3dd7965a31afe950c0972f 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -2501,6 +2501,10 @@ void ETSAnalyzer::CheckObjectExprProps(const ir::ObjectExpression *expr, ETSChecker *checker = GetETSChecker(); checker::ETSObjectType *objType = objectTypeForProperties; + if (objType->IsGlobalETSObjectType() && !expr->Properties().empty()) { + checker->LogError(diagnostic::ERROR_ARKTS_NO_UNTYPED_OBJ_LITERALS, expr->Start()); + } + for (ir::Expression *propExpr : expr->Properties()) { if (!propExpr->IsProperty()) { checker->LogError(diagnostic::OBJECT_LITERAL_NOT_KV, {}, expr->Start()); diff --git a/ets2panda/test/ast/compiler/ets/object_literal_initialization.ets b/ets2panda/test/ast/compiler/ets/object_literal_initialization.ets new file mode 100644 index 0000000000000000000000000000000000000000..de74d227c06eff032cf724aaf4bd50871a9bbe93 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/object_literal_initialization.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +// arkts-no-untyped-obj-literals +let o: Object = /* @@ label1 */{/* @@ label2 */n: 42, s: "foo"} + +/* @@@ label1 Error TypeError: 'Object', 'object', 'any' types cannot be initialized with object literals. */ +/* @@@ label2 Error TypeError: type Object has no property named n */ diff --git a/ets2panda/util/diagnostic/semantic.yaml b/ets2panda/util/diagnostic/semantic.yaml index 1df48358ebd838e6f18fc9f7af59cbda229a2834..0ea473212f62c7a967e72ec80ab33ed797b6a41e 100644 --- a/ets2panda/util/diagnostic/semantic.yaml +++ b/ets2panda/util/diagnostic/semantic.yaml @@ -463,6 +463,10 @@ semantic: id: 343 message: "Indexed signatures are not allowed. Use arrays instead!" +- name: ERROR_ARKTS_NO_UNTYPED_OBJ_LITERALS + id: 115343 + message: "'Object', 'object', 'any' types cannot be initialized with object literals." + - name: ERROR_ARKTS_SPREAD_ONLY_WITH_REST id: 44192 message: "The function or method being called needs a rest parameter to accept arguments passed via the spread operator." @@ -1605,7 +1609,6 @@ semantic: id: 347 message: "Wrong operand type for unary expression" - graveyard: - 8 - 29