From ce96dd33767c985c01458c3e86c7fe96607eabbe Mon Sep 17 00:00:00 2001 From: Soma Simon Date: Fri, 27 Jun 2025 13:21:05 +0200 Subject: [PATCH] Fix instanceof on this type Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICIDVX Internal issue: #26927 Change-Id: Ie4a7f54f7bcda5c93ad332591718190ada34a8c9 Signed-off-by: Soma Simon --- ets2panda/parser/ETSparser.cpp | 5 +-- ets2panda/parser/ETSparserTypes.cpp | 3 ++ ets2panda/parser/parserImpl.h | 3 +- .../ets/this_type_instanceof_invalid.ets | 35 +++++++++++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 ets2panda/test/ast/parser/ets/this_type_instanceof_invalid.ets diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 3aae7a2ec9..33c944adee 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -1873,8 +1873,9 @@ ir::Expression *ETSParser::ParseExpressionOrTypeAnnotation(lexer::TokenType type [[maybe_unused]] ExpressionParseFlags flags) { if (type == lexer::TokenType::KEYW_INSTANCEOF) { - TypeAnnotationParsingOptions options = - TypeAnnotationParsingOptions::REPORT_ERROR | TypeAnnotationParsingOptions::ANNOTATION_NOT_ALLOW; + TypeAnnotationParsingOptions options = TypeAnnotationParsingOptions::REPORT_ERROR | + TypeAnnotationParsingOptions::ANNOTATION_NOT_ALLOW | + TypeAnnotationParsingOptions::INSTANCEOF; if (Lexer()->GetToken().Type() == lexer::TokenType::LITERAL_NULL) { auto *typeAnnotation = AllocNode(); diff --git a/ets2panda/parser/ETSparserTypes.cpp b/ets2panda/parser/ETSparserTypes.cpp index 946e7611ed..6df1e0310e 100644 --- a/ets2panda/parser/ETSparserTypes.cpp +++ b/ets2panda/parser/ETSparserTypes.cpp @@ -445,10 +445,13 @@ ir::TypeNode *ETSParser::ParseThisType(TypeAnnotationParsingOptions *options) bool parseReturnType = (*options & TypeAnnotationParsingOptions::RETURN_TYPE) != 0; bool isExtensionFunction = (GetContext().Status() & ParserStatus::HAS_RECEIVER) != 0; bool isInUnion = (*options & TypeAnnotationParsingOptions::DISALLOW_UNION) != 0; + bool isInstance = (*options & TypeAnnotationParsingOptions::INSTANCEOF) != 0; if (isExtensionFunction) { if (reportErr && (!IsSimpleReturnThis(Lexer()->GetToken()) || isInUnion)) { LogError(diagnostic::THIS_IN_NON_STATIC_METHOD, {}, startPos); } + } else if (reportErr && (isInstance)) { + LogError(diagnostic::INVALID_RIGHT_INSTANCEOF, {}, startPos); } else if (reportErr && (!allowThisType || !parseReturnType || isArrowFunc)) { LogError(diagnostic::THIS_IN_NON_STATIC_METHOD, {}, startPos); } diff --git a/ets2panda/parser/parserImpl.h b/ets2panda/parser/parserImpl.h index 30da9aa1ce..c138f5112c 100644 --- a/ets2panda/parser/parserImpl.h +++ b/ets2panda/parser/parserImpl.h @@ -63,7 +63,8 @@ enum class TypeAnnotationParsingOptions : uint32_t { ALLOW_DECLARATION_SITE_VARIANCE = 1U << 14U, DISALLOW_UNION = 1U << 15U, POTENTIAL_NEW_ARRAY = 1U << 16U, - ANNOTATION_NOT_ALLOW = 1U << 17U + ANNOTATION_NOT_ALLOW = 1U << 17U, + INSTANCEOF = 1U << 18U }; class ParserImpl { diff --git a/ets2panda/test/ast/parser/ets/this_type_instanceof_invalid.ets b/ets2panda/test/ast/parser/ets/this_type_instanceof_invalid.ets new file mode 100644 index 0000000000..5bc319026e --- /dev/null +++ b/ets2panda/test/ast/parser/ets/this_type_instanceof_invalid.ets @@ -0,0 +1,35 @@ +/* + * 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. + */ + +type F = () => number; + +class C { + a: T; + b: F; + constructor( a: T, b: F) { + this.a = a; + this.b = b; + } + + + foo() { + if (this.a instanceof /* @@ label */this) { + return 0; + } + return 1; + } +} + +/* @@@ label Error SyntaxError: Invalid right-hand side in 'instanceof' expression. */ -- Gitee