From 490bca0c2d397f1be14fe4d0695ff4a5c7c18d5e Mon Sep 17 00:00:00 2001 From: zengzengran Date: Tue, 29 Jul 2025 17:10:55 +0800 Subject: [PATCH] Fix ambiguous ambient function call crash Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICPL0E Description: When an ambient and non-ambient function with the same name exists, report CTE and set TypeError. Tested-by: ninja tests (passed) ets_testrunner (passed) Signed-off-by: zengzengran # --- ets2panda/checker/ets/function.cpp | 6 +++- .../ast/compiler/ets/ambient_function.ets | 28 +++++++++++++++++++ .../overload_signature_neg_2.ets | 2 +- ets2panda/util/diagnostic/semantic.yaml | 4 +++ 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/ambient_function.ets diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index 317fc2afcb..98b2fd7e47 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -1435,7 +1435,11 @@ static bool CollectOverload(checker::ETSChecker *checker, ir::MethodDefinition * ArenaVector overloads(checker->ProgramAllocator()->Adapter()); for (ir::MethodDefinition *const currentFunc : method->Overloads()) { - ldInfo.isDeclare &= currentFunc->IsDeclare(); + if (currentFunc->IsDeclare() != ldInfo.isDeclare) { + checker->LogError(diagnostic::AMBIGUOUS_AMBIENT, {currentFunc->Id()->Name()}, currentFunc->Start()); + method->Id()->Variable()->SetTsType(checker->GlobalTypeError()); + return false; + } ES2PANDA_ASSERT(currentFunc->Function() != nullptr); ES2PANDA_ASSERT(currentFunc->Id() != nullptr); currentFunc->Function()->Id()->SetVariable(currentFunc->Id()->Variable()); diff --git a/ets2panda/test/ast/compiler/ets/ambient_function.ets b/ets2panda/test/ast/compiler/ets/ambient_function.ets new file mode 100644 index 0000000000..7f20bea7e3 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/ambient_function.ets @@ -0,0 +1,28 @@ +/* + * 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. + */ + +let a1: [number] | null = null; +let a2: [number] = [0x00000001]; + +declare function capset(...p: [number]): void; + +function capset(...p: [number]) {} + +function main() { + capset(...(a1 ?? a2)); +} + +/* @@? 21:1 Error TypeError: Method declaration `capset` must all ambient or non-ambient */ +/* @@? 24:3 Error TypeError: This expression is not callable. */ diff --git a/ets2panda/test/ast/compiler/ets/same_assembly_overload/overload_signature_neg_2.ets b/ets2panda/test/ast/compiler/ets/same_assembly_overload/overload_signature_neg_2.ets index 27671485ba..18bdf1ec9e 100644 --- a/ets2panda/test/ast/compiler/ets/same_assembly_overload/overload_signature_neg_2.ets +++ b/ets2panda/test/ast/compiler/ets/same_assembly_overload/overload_signature_neg_2.ets @@ -18,4 +18,4 @@ export class A {} export declare function foo(a:A):void export function foo(a:A):number {} -/* @@? 19:17 Error TypeError: Function with a non void return type must return a value. */ +/* @@? 19:8 Error TypeError: Method declaration `foo` must all ambient or non-ambient */ diff --git a/ets2panda/util/diagnostic/semantic.yaml b/ets2panda/util/diagnostic/semantic.yaml index 2db429ba58..986428b56f 100644 --- a/ets2panda/util/diagnostic/semantic.yaml +++ b/ets2panda/util/diagnostic/semantic.yaml @@ -69,6 +69,10 @@ semantic: id: 175 message: "A 'const' initializer in an ambient context must be a string or numeric literal: {}" +- name: AMBIGUOUS_AMBIENT + id: 399 + message: "Method declaration `{}` must all ambient or non-ambient" + - name: AMBIGUOUS_CALL id: 142 message: "Call to `{}` is ambiguous as `2` versions of `{}` are available: `{}{}` and `{}{}`" -- Gitee