diff --git a/ets2panda/checker/ets/object.cpp b/ets2panda/checker/ets/object.cpp index d371eac06cba28de4773f0140b9764788b582a68..388d4898c4abc7778d3d3f97245a425d25a9e52a 100644 --- a/ets2panda/checker/ets/object.cpp +++ b/ets2panda/checker/ets/object.cpp @@ -90,14 +90,9 @@ static bool CheckGetterSetterDecl(varbinder::LocalVariable const *child, varbind static bool CheckOverloadDecl(varbinder::LocalVariable *child, varbinder::LocalVariable *parent) { - if (!child->Declaration()->Node()->IsOverloadDeclaration() && - !parent->Declaration()->Node()->IsOverloadDeclaration()) { - return false; - } - if (!child->Declaration()->Node()->IsOverloadDeclaration() || !parent->Declaration()->Node()->IsOverloadDeclaration()) { - return true; + return false; } auto *childOverload = child->Declaration()->Node()->AsOverloadDeclaration(); diff --git a/ets2panda/checker/ets/typeCheckingHelpers.cpp b/ets2panda/checker/ets/typeCheckingHelpers.cpp index 6f0a218600371e76c86403bb4362ff029b81baca..367c45f104292dca5bc6cb6ede4359b3952372b7 100644 --- a/ets2panda/checker/ets/typeCheckingHelpers.cpp +++ b/ets2panda/checker/ets/typeCheckingHelpers.cpp @@ -1706,6 +1706,12 @@ static bool CheckAccessModifierForOverloadDeclaration(ETSChecker *const checker, return false; } + if ((overLoadAliasFlags & ir::ModifierFlags::EXPORT) != 0 && + (overloadedMethodFlags & ir::ModifierFlags::EXPORT) == 0) { + checker->LogError(diagnostic::OVERLOADED_NAME_MUST_ALSO_EXPORTED, {}, pos); + return false; + } + return true; } diff --git a/ets2panda/test/ast/compiler/ets/first_match/namespace.ets b/ets2panda/test/ast/compiler/ets/first_match/namespace.ets index 562555e43fd2f058a7686cb22849741a9e8b7c6c..38ba6b5ba032c5802aedb3a49563f7eae8680447 100644 --- a/ets2panda/test/ast/compiler/ets/first_match/namespace.ets +++ b/ets2panda/test/ast/compiler/ets/first_match/namespace.ets @@ -61,6 +61,9 @@ function main(){ NS.overloadfoo3("abc"); } +/* @@? 25:34 Error TypeError: Overload alias is exported, then overload functions must also be exported. */ +/* @@? 25:40 Error TypeError: Overload alias is exported, then overload functions must also be exported. */ +/* @@? 35:41 Error TypeError: Overload alias is exported, then overload functions must also be exported. */ /* @@? 49:8 Error TypeError: 'foo1' is not exported in 'NS' */ /* @@? 50:8 Error TypeError: 'foo2' is not exported in 'NS' */ /* @@? 52:8 Error TypeError: 'foo4' is not exported in 'NS' */ diff --git a/ets2panda/test/ast/compiler/ets/overload/export_function.ets b/ets2panda/test/ast/compiler/ets/overload/export_function.ets new file mode 100644 index 0000000000000000000000000000000000000000..e46cffd769552b10c5144b182db7b23d0bcc6386 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/overload/export_function.ets @@ -0,0 +1,21 @@ +/* + * 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. + */ + +export function foo1(p: string) {} +function foo2(p: number) {} +export overload foo { foo1, foo2 } +overload bar { foo1, foo2 } + +/* @@? 18:29 Error TypeError: Overload alias is exported, then overload functions must also be exported. */ \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/first_match/namespace5.ets b/ets2panda/test/ast/compiler/ets/overload/namespace5.ets similarity index 88% rename from ets2panda/test/runtime/ets/first_match/namespace5.ets rename to ets2panda/test/ast/compiler/ets/overload/namespace5.ets index b7d53c5871ec868758e9abe0f5aba3cafd3d88d3..78c0eea279ff368e38e3910556c0ee0e29e11bf7 100644 --- a/ets2panda/test/runtime/ets/first_match/namespace5.ets +++ b/ets2panda/test/ast/compiler/ets/overload/namespace5.ets @@ -25,6 +25,4 @@ namespace NS { export overload overloadfoo{ foo1, foo2 } } -function main() { - arktest.assertEQ(NS.overloadfoo(1), "invoke1"); -} +/* @@? 25:40 Error TypeError: Overload alias is exported, then overload functions must also be exported. */ \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/first_match/native_method.ets b/ets2panda/test/ast/compiler/ets/overload/native_method.ets similarity index 88% rename from ets2panda/test/runtime/ets/first_match/native_method.ets rename to ets2panda/test/ast/compiler/ets/overload/native_method.ets index 82c3e1e60f32a1606bf931cd3cb5bd9981d2803f..debc08266e83d3c87dcd82b3d46e813285ca4ca6 100644 --- a/ets2panda/test/runtime/ets/first_match/native_method.ets +++ b/ets2panda/test/ast/compiler/ets/overload/native_method.ets @@ -37,7 +37,7 @@ overload foo{ foo1, foo2 } function main() { let a = new A(); - arktest.assertEQ(a.foo("abc"), "invoke1"); - arktest.assertEQ(NS.foo("abc"), "invoke2"); - arktest.assertEQ(foo("abc"), "invoke3"); } + + +/* @@? 29:26 Error TypeError: Overload alias is exported, then overload functions must also be exported. */ \ No newline at end of file diff --git a/ets2panda/test/ast/compiler/ets/overload/overload_overloaded_method_exported.ets b/ets2panda/test/ast/compiler/ets/overload/overload_overloaded_method_exported.ets index 1510e3ff407d418b93f7ffe436fd07df66385cfd..5ae8220475a0ffc85e4696e35da09cc5e19c5150 100644 --- a/ets2panda/test/ast/compiler/ets/overload/overload_overloaded_method_exported.ets +++ b/ets2panda/test/ast/compiler/ets/overload/overload_overloaded_method_exported.ets @@ -16,3 +16,6 @@ export function foo1() {} function foo2() {} export overload foo {foo1, foo2} + + +/* @@? 18:28 Error TypeError: Overload alias is exported, then overload functions must also be exported. */ \ No newline at end of file diff --git a/ets2panda/util/diagnostic/semantic.yaml b/ets2panda/util/diagnostic/semantic.yaml index 53aa40aaeb92224825696c1f80667bfa47879426..44df4a81b83e7acedc0af9d1142f69fcfc0c87a3 100644 --- a/ets2panda/util/diagnostic/semantic.yaml +++ b/ets2panda/util/diagnostic/semantic.yaml @@ -1026,6 +1026,10 @@ semantic: id: 307 message: "Overloaded method is used as value" +- name: OVERLOADED_NAME_MUST_ALSO_EXPORTED + id: 148271 + message: "Overload alias is exported, then overload functions must also be exported." + - name: OVERLOADED_NAME_MUST_FUNCTION id: 384 message: "overloaded name must refer to an accessible method." @@ -1405,7 +1409,7 @@ semantic: - name: UNRESOLVED_REF id: 143 message: "Unresolved reference {}" - code_fix_ids: [FixSpelling,AddLocalVariable] + code_fix_ids: [FixSpelling, AddLocalVariable] - name: UNSUPPORTED_CLASS_LITERAL id: 20