diff --git a/ets2panda/compiler/lowering/ets/objectLiteralLowering.cpp b/ets2panda/compiler/lowering/ets/objectLiteralLowering.cpp index 0a17664868cd5e997a8ef6118da133af8e2287ca..b75ea43406c8a929fefd553e606b019be303d4cc 100644 --- a/ets2panda/compiler/lowering/ets/objectLiteralLowering.cpp +++ b/ets2panda/compiler/lowering/ets/objectLiteralLowering.cpp @@ -126,11 +126,21 @@ static void PopulateCtorArgumentsFromMap(public_lib::Context *ctx, ir::ObjectExp for (auto param : classType->ConstructSignatures().front()->Params()) { auto ctorArgument = ctorArgumentsMap[param->Declaration()->Name()]; - if (ctorArgument == nullptr && objExpr->PreferredType()->AsETSObjectType()->IsPartial()) { - ctorArguments.push_back(allocator->New()); - continue; - } - if (ctorArgument == nullptr && param->TsType()->PossiblyETSUndefined()) { + if (ctorArgument == nullptr) { + auto *preferredType = objExpr->PreferredType(); + if (preferredType != nullptr && preferredType->IsETSObjectType() && + preferredType->AsETSObjectType()->IsPartial()) { + ctorArguments.push_back(allocator->New()); + continue; + } + + if (param->TsType()->PossiblyETSUndefined()) { + ctorArguments.push_back(allocator->New()); + continue; + } + ctx->diagnosticEngine->LogSemanticError("Missing required property '" + + param->Declaration()->Name().Mutf8() + "' in object literal.", + objExpr->Start()); ctorArguments.push_back(allocator->New()); continue; } diff --git a/ets2panda/test/ast/compiler/ets/interface_inference_invalid_return.ets b/ets2panda/test/ast/compiler/ets/interface_inference_invalid_return.ets new file mode 100644 index 0000000000000000000000000000000000000000..e2f313cb37cd1df84cbf16b0c25dbaf23c12950b --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/interface_inference_invalid_return.ets @@ -0,0 +1,30 @@ +/* + * 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. + */ + +/*--- +desc: Test interface inference when a function returns an object that does not match the interface. +name: interface_inference_invalid_return +---*/ + +interface A { + readonly name: string; +} + +// This should fail compilation due to missing 'name' property +const a: A = {}; + +/* @@? 1:1 Error TypeError: Type 'undefined' is not compatible with type 'String' at index 1 */ +/* @@? 1:3 Error TypeError: No matching construct signature for interface_inference_invalid_return.interface_inference_invalid_return$A$ObjectLiteral(undefined) */ +/* @@? 26:14 Error TypeError: Missing required property 'name' in object literal. */