From 4adc271db794232935a8ba203702bdee7bb07318 Mon Sep 17 00:00:00 2001 From: Gabor Aron Takacs Date: Fri, 15 Aug 2025 15:30:52 +0200 Subject: [PATCH] Add diagnostic: is operator is not supported Fixes #28230 internal issue. Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICT8TC Change-Id: I564ba715f4794d03696cec7d97ea088c7bf967f7 Signed-off-by: Gabor Aron Takacs --- ets2panda/parser/ETSparser.cpp | 7 +++++++ .../test/ast/compiler/ets/noIsOperator.ets | 20 +++++++++++++++++++ ets2panda/util/diagnostic/syntax.yaml | 5 +++++ 3 files changed, 32 insertions(+) create mode 100644 ets2panda/test/ast/compiler/ets/noIsOperator.ets diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 1c9fe98ea1..503efea4f0 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -1055,6 +1055,13 @@ ir::TypeNode *ETSParser::ParseTypeReference(TypeAnnotationParsingOptions *option typeRefPart->SetRange({partPos, endPos}); if (!Lexer()->TryEatTokenType(lexer::TokenType::PUNCTUATOR_PERIOD)) { + if (Lexer()->GetToken().KeywordType() == lexer::TokenType::KEYW_IS) { + auto isPos = Lexer()->GetToken().Start(); + Lexer()->NextToken(); // eat 'is' + Lexer()->TryEatTokenType(lexer::TokenType::LITERAL_IDENT); + LogError(diagnostic::ERROR_ARKTS_NO_IS_OPERATOR, {}, isPos); + return AllocBrokenType({partPos, Lexer()->GetToken().End()}); + } break; } diff --git a/ets2panda/test/ast/compiler/ets/noIsOperator.ets b/ets2panda/test/ast/compiler/ets/noIsOperator.ets new file mode 100644 index 0000000000..01aab8be00 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/noIsOperator.ets @@ -0,0 +1,20 @@ +/* + * 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. + */ + +function isString(test: string): test /* @@ label */is string { + return typeof test === "string"; +} + +/* @@@ label Error SyntaxError: 'is' operator is not supported. Use 'instanceof' instead to check whether a variable is instance of a given type and use 'as' operator to cast to the appropriate type! */ diff --git a/ets2panda/util/diagnostic/syntax.yaml b/ets2panda/util/diagnostic/syntax.yaml index 2401da7d0e..315e4e679e 100644 --- a/ets2panda/util/diagnostic/syntax.yaml +++ b/ets2panda/util/diagnostic/syntax.yaml @@ -251,6 +251,11 @@ syntax: message: "'in' operator is not supported. Use 'instanceof' operator to check whether an object is the instance of a class that contains the necessary class member." +- name: ERROR_ARKTS_NO_IS_OPERATOR + id: 169587 + message: "'is' operator is not supported. Use 'instanceof' instead to check whether a variable is instance of a given type\ + \ and use 'as' operator to cast to the appropriate type!" + - name: ERROR_ARKTS_NO_OBJ_LITERAL_TO_DECL_TYPE id: 177354 message: "Using object literals to declare types in place is not supported. Please declare types and interfaces explicitly!" -- Gitee