diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index 317fc2afcbd80ce89cacd11494ebb3b23deb6e3b..98b2fd7e4760d2fc8d3e080febb0450c395f35cf 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 0000000000000000000000000000000000000000..7f20bea7e3be6d1b0e1abfbbdf848db4ecef2343 --- /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 27671485ba3f5d7637c94c0fb5767e439e97f762..18bdf1ec9ef90f3ca5698970adb7c7e35c47de6d 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 2db429ba584df212f5b29839397c1a6fc7328b61..986428b56f954313c1789a1148a1e6d9307cbed2 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 `{}{}`"