diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 3aae7a2ec971e216fc413454e23d7175c6610f8a..33c944adee6f202213c4b983f971e5fb774122a8 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 946e7611eda6860a9524045f088f7a7dd09d57ef..6df1e0310eb87b13b2377de786bc277cb523bf78 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 30da9aa1ce5d3216d0d79c7e9961bf3649ddbe3c..c138f5112c602452c21c235dfa0a800a87a5752b 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 0000000000000000000000000000000000000000..5bc319026e5eb7a5aff2c162ad3c6d83864a21a5 --- /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. */