diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 951f2217220594c4f069a014cf96b7d906716a2c..b6cfe53d81b90e4ad54e36b03fb3ce312cb66f72 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -3208,6 +3208,14 @@ static bool CheckIsValidReturnTypeAnnotation(ir::ReturnStatement *st, ir::Script // only extension function and class method could return `this`; bool inValidNormalFuncReturnThisType = st->Argument() == nullptr || !st->Argument()->IsThisExpression(); + + if (st->Argument() != nullptr && st->Argument()->IsCallExpression() && + st->Argument()->AsCallExpression()->Callee()->IsMemberExpression() && + st->Argument()->AsCallExpression()->Callee()->AsMemberExpression()->Object() != nullptr && + st->Argument()->AsCallExpression()->Callee()->AsMemberExpression()->Object()->IsThisExpression()) { + inValidNormalFuncReturnThisType = false; + } + bool inValidExtensionFuncReturnThisType = !containingFunc->HasReceiver() || (containingFunc->HasReceiver() && (st->Argument() == nullptr || !st->Argument()->IsIdentifier() || diff --git a/ets2panda/test/runtime/ets/method_that_returns_this.ets b/ets2panda/test/runtime/ets/method_that_returns_this.ets new file mode 100755 index 0000000000000000000000000000000000000000..cde6ff98fb183dc86f56b85f7562da238c5752ad --- /dev/null +++ b/ets2panda/test/runtime/ets/method_that_returns_this.ets @@ -0,0 +1,41 @@ +/* + * 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. + */ + +class C { + foo(): this { + return this + } + bar(): this { + return this.foo() + } + a: string = "classC"; +} + +class D extends C { + foo(): this { + return this + } + a: string = "classD"; +} + +function main() { + let c = new C() + arktest.assertEQ(c.foo().a, "classC") + arktest.assertEQ(c.bar().a, "classC") + let x = new C().foo() + let y = new D().foo() + arktest.assertEQ(x.a, "classC") + arktest.assertEQ(y.a, "classD") +}