diff --git a/ets2panda/checker/ETSAnalyzerHelpers.cpp b/ets2panda/checker/ETSAnalyzerHelpers.cpp index 2a5443ae232284fc548176857faf987d2efdb0ab..c1717b4ac437decabe0c5bc87be355597c3d42ea 100644 --- a/ets2panda/checker/ETSAnalyzerHelpers.cpp +++ b/ets2panda/checker/ETSAnalyzerHelpers.cpp @@ -174,7 +174,8 @@ void CheckExtensionMethod(checker::ETSChecker *checker, ir::ScriptFunction *exte static void CheckMethodBodyForNativeAbstractDeclare(ETSChecker *checker, ir::MethodDefinition *node, ir::ScriptFunction *scriptFunc) { - if ((node->IsNative() && !node->IsConstructor()) || node->IsAbstract() || node->IsDeclare()) { + if ((node->IsNative() && !node->IsConstructor()) || node->IsAbstract() || + (node->IsDeclare() && !node->Parent()->IsTSInterfaceBody())) { checker->LogError(diagnostic::UNEXPECTED_FUNC_BODY, {}, scriptFunc->Body()->Start()); } } diff --git a/ets2panda/test/ast/compiler/ets/ambient_method_body.ets b/ets2panda/test/ast/compiler/ets/ambient_method_body.ets new file mode 100644 index 0000000000000000000000000000000000000000..d7429358e03369baf59b57255b0a525edc438a44 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/ambient_method_body.ets @@ -0,0 +1,44 @@ +/* + * 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. + */ + +declare function foo(a: number): void {}; +declare function baz(a: number): void; + +declare interface XXX { + goo(): void + foo(): void { + let a = 1; + } +} + +class AAA { + native foo(): void + native baz(): void {} +} + +abstract class BBB { + abstract foo(): void + abstract baz(): void {} +} + +class CCC { + foo(): void {} + baz(): void +} + +/* @@? 16:39 Error TypeError: Native, Abstract and Declare methods cannot have body. */ +/* @@? 28:24 Error TypeError: Native, Abstract and Declare methods cannot have body. */ +/* @@? 33:26 Error TypeError: Native, Abstract and Declare methods cannot have body. */ +/* @@? 38:8 Error TypeError: Only abstract or native methods can't have body. */ diff --git a/ets2panda/test/ast/compiler/ets/ambient_namesapce04.ets b/ets2panda/test/ast/compiler/ets/ambient_namesapce04.ets index 2f94e6b211099caf8f854c0e11ad69626db12558..1efa1e69940715cb0c2d84f057f0785fa2900756 100644 --- a/ets2panda/test/ast/compiler/ets/ambient_namesapce04.ets +++ b/ets2panda/test/ast/compiler/ets/ambient_namesapce04.ets @@ -20,5 +20,4 @@ declare namespace MySpace{ } } -/* @@? 18:20 Error TypeError: Native, Abstract and Declare methods cannot have body. */ /* @@? 19:12 Error TypeError: Native and Declare methods should have explicit return type. */ \ No newline at end of file