From 21c32090285524aff88563edfd6ffb92b27191b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Gel=C3=A1nyi?= Date: Thu, 28 Sep 2023 17:31:33 +0200 Subject: [PATCH] Fix getter and setter inheritance Set the object type to the supertype where we found the prop name. Use this type's instance and static methods for searching for the prop. Related to #13330 internal issue Signed-off-by: Andras Gelanyi --- ir/expressions/memberExpression.cpp | 31 ++- ...rExpressionFromStaticContext8-expected.txt | 176 +++++++++--------- ...alidMemberExpressionFromStaticContext8.ets | 15 ++ 3 files changed, 129 insertions(+), 93 deletions(-) diff --git a/ir/expressions/memberExpression.cpp b/ir/expressions/memberExpression.cpp index 909371b71..de8e772c9 100644 --- a/ir/expressions/memberExpression.cpp +++ b/ir/expressions/memberExpression.cpp @@ -191,7 +191,13 @@ void MemberExpression::Compile(compiler::ETSGen *etsg) const if (object_->TsType()->IsETSObjectType() && HasMemberKind(MemberExpressionKind::GETTER | MemberExpressionKind::SETTER)) { - const auto &get_set = object_->TsType()->AsETSObjectType()->InstanceMethods(); + ASSERT(obj_type_->IsETSObjectType()); + auto *object_type = object_->TsType()->AsETSObjectType(); + if (!object_->TsType()->AsETSObjectType()->Name().Is(obj_type_->AsETSObjectType()->Name().Mutf8())) { + object_type = obj_type_->AsETSObjectType(); + } + + const auto &get_set = object_type->InstanceMethods(); const auto res = get_set.find(prop_name); if (res != get_set.end()) { auto *decl = res->second->Declaration(); @@ -210,7 +216,7 @@ void MemberExpression::Compile(compiler::ETSGen *etsg) const return; } } else { - const auto &static_get_set = object_->TsType()->AsETSObjectType()->StaticMethods(); + const auto &static_get_set = object_type->StaticMethods(); const auto static_res = static_get_set.find(prop_name); if (static_res != static_get_set.end()) { auto *decl = static_res->second->Declaration(); @@ -390,9 +396,24 @@ checker::Type *MemberExpression::Check(checker::ETSChecker *checker) obj_type_ = base_type->AsETSObjectType(); if (Property()->IsIdentifier()) { - const auto *prop = ObjType()->GetProperty(Property()->AsIdentifier()->Name(), - checker::PropertySearchFlags::SEARCH_INSTANCE_METHOD | - checker::PropertySearchFlags::SEARCH_STATIC_METHOD); + auto *prop = ObjType()->GetProperty(Property()->AsIdentifier()->Name(), + checker::PropertySearchFlags::SEARCH_INSTANCE_METHOD | + checker::PropertySearchFlags::SEARCH_STATIC_METHOD); + + auto *current_obj_type = ObjType(); + if (prop == nullptr) { + while (current_obj_type->SuperType() != nullptr) { + current_obj_type = current_obj_type->SuperType(); + prop = current_obj_type->GetProperty(Property()->AsIdentifier()->Name(), + checker::PropertySearchFlags::SEARCH_INSTANCE_METHOD | + checker::PropertySearchFlags::SEARCH_STATIC_METHOD); + + if (prop != nullptr) { + SetObjectType(current_obj_type); + break; + } + } + } if (prop != nullptr && prop->TsType() != nullptr && prop->TsType()->IsETSFunctionType()) { const auto &func_type = prop->TsType()->AsETSFunctionType(); diff --git a/test/compiler/ets/invalidMemberExpressionFromStaticContext8-expected.txt b/test/compiler/ets/invalidMemberExpressionFromStaticContext8-expected.txt index 041a44eb1..6057f5a72 100644 --- a/test/compiler/ets/invalidMemberExpressionFromStaticContext8-expected.txt +++ b/test/compiler/ets/invalidMemberExpressionFromStaticContext8-expected.txt @@ -10,11 +10,11 @@ "decorators": [], "loc": { "start": { - "line": 1, + "line": 16, "column": 7 }, "end": { - "line": 1, + "line": 16, "column": 8 } } @@ -30,11 +30,11 @@ "decorators": [], "loc": { "start": { - "line": 2, + "line": 17, "column": 11 }, "end": { - "line": 2, + "line": 17, "column": 19 } } @@ -54,11 +54,11 @@ "decorators": [], "loc": { "start": { - "line": 2, + "line": 17, "column": 11 }, "end": { - "line": 2, + "line": 17, "column": 19 } } @@ -77,33 +77,33 @@ "decorators": [], "loc": { "start": { - "line": 2, + "line": 17, "column": 24 }, "end": { - "line": 2, + "line": 17, "column": 28 } } }, "loc": { "start": { - "line": 2, + "line": 17, "column": 24 }, "end": { - "line": 2, + "line": 17, "column": 30 } } }, "loc": { "start": { - "line": 2, + "line": 17, "column": 24 }, "end": { - "line": 2, + "line": 17, "column": 30 } } @@ -113,33 +113,33 @@ "statements": [], "loc": { "start": { - "line": 2, + "line": 17, "column": 29 }, "end": { - "line": 2, + "line": 17, "column": 31 } } }, "loc": { "start": { - "line": 2, + "line": 17, "column": 19 }, "end": { - "line": 2, + "line": 17, "column": 31 } } }, "loc": { "start": { - "line": 2, + "line": 17, "column": 19 }, "end": { - "line": 2, + "line": 17, "column": 31 } } @@ -148,11 +148,11 @@ "decorators": [], "loc": { "start": { - "line": 2, + "line": 17, "column": 5 }, "end": { - "line": 2, + "line": 17, "column": 31 } } @@ -165,11 +165,11 @@ "decorators": [], "loc": { "start": { - "line": 3, + "line": 18, "column": 12 }, "end": { - "line": 3, + "line": 18, "column": 27 } } @@ -189,11 +189,11 @@ "decorators": [], "loc": { "start": { - "line": 3, + "line": 18, "column": 12 }, "end": { - "line": 3, + "line": 18, "column": 27 } } @@ -212,33 +212,33 @@ "decorators": [], "loc": { "start": { - "line": 3, + "line": 18, "column": 32 }, "end": { - "line": 3, + "line": 18, "column": 36 } } }, "loc": { "start": { - "line": 3, + "line": 18, "column": 32 }, "end": { - "line": 3, + "line": 18, "column": 38 } } }, "loc": { "start": { - "line": 3, + "line": 18, "column": 32 }, "end": { - "line": 3, + "line": 18, "column": 38 } } @@ -248,33 +248,33 @@ "statements": [], "loc": { "start": { - "line": 3, + "line": 18, "column": 37 }, "end": { - "line": 3, + "line": 18, "column": 39 } } }, "loc": { "start": { - "line": 3, + "line": 18, "column": 27 }, "end": { - "line": 3, + "line": 18, "column": 39 } } }, "loc": { "start": { - "line": 3, + "line": 18, "column": 27 }, "end": { - "line": 3, + "line": 18, "column": 39 } } @@ -283,11 +283,11 @@ "decorators": [], "loc": { "start": { - "line": 3, + "line": 18, "column": 5 }, "end": { - "line": 3, + "line": 18, "column": 39 } } @@ -376,11 +376,11 @@ "decorators": [], "loc": { "start": { - "line": 4, + "line": 19, "column": 2 }, "end": { - "line": 4, + "line": 19, "column": 2 } } @@ -388,22 +388,22 @@ ], "loc": { "start": { - "line": 1, + "line": 16, "column": 9 }, "end": { - "line": 4, + "line": 19, "column": 2 } } }, "loc": { "start": { - "line": 1, + "line": 16, "column": 1 }, "end": { - "line": 4, + "line": 19, "column": 2 } } @@ -417,11 +417,11 @@ "decorators": [], "loc": { "start": { - "line": 6, + "line": 21, "column": 13 }, "end": { - "line": 6, + "line": 21, "column": 14 } } @@ -436,33 +436,33 @@ "decorators": [], "loc": { "start": { - "line": 6, + "line": 21, "column": 23 }, "end": { - "line": 6, + "line": 21, "column": 24 } } }, "loc": { "start": { - "line": 6, + "line": 21, "column": 23 }, "end": { - "line": 6, + "line": 21, "column": 26 } } }, "loc": { "start": { - "line": 6, + "line": 21, "column": 23 }, "end": { - "line": 6, + "line": 21, "column": 26 } } @@ -477,11 +477,11 @@ "decorators": [], "loc": { "start": { - "line": 7, + "line": 22, "column": 12 }, "end": { - "line": 7, + "line": 22, "column": 20 } } @@ -501,11 +501,11 @@ "decorators": [], "loc": { "start": { - "line": 7, + "line": 22, "column": 12 }, "end": { - "line": 7, + "line": 22, "column": 20 } } @@ -524,33 +524,33 @@ "decorators": [], "loc": { "start": { - "line": 7, + "line": 22, "column": 25 }, "end": { - "line": 7, + "line": 22, "column": 29 } } }, "loc": { "start": { - "line": 7, + "line": 22, "column": 25 }, "end": { - "line": 7, + "line": 22, "column": 31 } } }, "loc": { "start": { - "line": 7, + "line": 22, "column": 25 }, "end": { - "line": 7, + "line": 22, "column": 31 } } @@ -570,11 +570,11 @@ "decorators": [], "loc": { "start": { - "line": 8, + "line": 23, "column": 9 }, "end": { - "line": 8, + "line": 23, "column": 10 } } @@ -585,11 +585,11 @@ "decorators": [], "loc": { "start": { - "line": 8, + "line": 23, "column": 11 }, "end": { - "line": 8, + "line": 23, "column": 19 } } @@ -598,11 +598,11 @@ "optional": false, "loc": { "start": { - "line": 8, + "line": 23, "column": 9 }, "end": { - "line": 8, + "line": 23, "column": 19 } } @@ -611,22 +611,22 @@ "optional": false, "loc": { "start": { - "line": 8, + "line": 23, "column": 9 }, "end": { - "line": 8, + "line": 23, "column": 21 } } }, "loc": { "start": { - "line": 8, + "line": 23, "column": 9 }, "end": { - "line": 8, + "line": 23, "column": 21 } } @@ -634,33 +634,33 @@ ], "loc": { "start": { - "line": 7, + "line": 22, "column": 30 }, "end": { - "line": 9, + "line": 24, "column": 6 } } }, "loc": { "start": { - "line": 7, + "line": 22, "column": 20 }, "end": { - "line": 9, + "line": 24, "column": 6 } } }, "loc": { "start": { - "line": 7, + "line": 22, "column": 20 }, "end": { - "line": 9, + "line": 24, "column": 6 } } @@ -669,11 +669,11 @@ "decorators": [], "loc": { "start": { - "line": 7, + "line": 22, "column": 5 }, "end": { - "line": 9, + "line": 24, "column": 6 } } @@ -762,11 +762,11 @@ "decorators": [], "loc": { "start": { - "line": 10, + "line": 25, "column": 2 }, "end": { - "line": 10, + "line": 25, "column": 2 } } @@ -774,22 +774,22 @@ ], "loc": { "start": { - "line": 6, + "line": 21, "column": 25 }, "end": { - "line": 10, + "line": 25, "column": 2 } } }, "loc": { "start": { - "line": 6, + "line": 21, "column": 7 }, "end": { - "line": 10, + "line": 25, "column": 2 } } @@ -939,9 +939,9 @@ "column": 1 }, "end": { - "line": 11, + "line": 26, "column": 1 } } } -TypeError: 'foo_in_A' is an instance property of 'B' [invalidMemberExpressionFromStaticContext8.ets:8:11] +TypeError: 'foo_in_A' is an instance property of 'A' [invalidMemberExpressionFromStaticContext8.ets:23:11] diff --git a/test/compiler/ets/invalidMemberExpressionFromStaticContext8.ets b/test/compiler/ets/invalidMemberExpressionFromStaticContext8.ets index 53d591bb6..5d71ab3d0 100644 --- a/test/compiler/ets/invalidMemberExpressionFromStaticContext8.ets +++ b/test/compiler/ets/invalidMemberExpressionFromStaticContext8.ets @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 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 A { final foo_in_A() : void {} static static_foo_in_A() : void {} -- Gitee