From d95a583ddc6bdb25641060c58147f084581bb43e Mon Sep 17 00:00:00 2001 From: zmw Date: Fri, 5 Sep 2025 14:46:14 +0800 Subject: [PATCH] CTE for instanceof type erase Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICWKGN Description: CTE for instanceof type erase Signed-off-by: zmw --- ets2panda/checker/ets/arithmetic.cpp | 3 +++ .../compiler/ets/instanceof_type_erase.ets | 22 +++++++++++++++++++ ets2panda/util/diagnostic/semantic.yaml | 4 ++++ 3 files changed, 29 insertions(+) create mode 100644 ets2panda/test/ast/compiler/ets/instanceof_type_erase.ets diff --git a/ets2panda/checker/ets/arithmetic.cpp b/ets2panda/checker/ets/arithmetic.cpp index 46ea410c02..491fc8e5df 100644 --- a/ets2panda/checker/ets/arithmetic.cpp +++ b/ets2panda/checker/ets/arithmetic.cpp @@ -1184,6 +1184,9 @@ std::tuple ETSChecker::CheckBinaryOperator(ir::Expression *left, Context().CheckTestSmartCastCondition(operationType); // SUPPRESS_CSA_NEXTLINE(alpha.core.AllocatorETSCheckerHint) checker::Type *rightType = right->Check(this); + if (operationType == lexer::TokenType::KEYW_INSTANCEOF && rightType->IsETSTypeParameter()) { + LogError(diagnostic::INSTANCEOF_TYPE_ERASED, {rightType->AsETSTypeParameter()->Name()}, pos); + } if (right->IsTypeNode()) { rightType = right->AsTypeNode()->GetType(this); diff --git a/ets2panda/test/ast/compiler/ets/instanceof_type_erase.ets b/ets2panda/test/ast/compiler/ets/instanceof_type_erase.ets new file mode 100644 index 0000000000..03b46a6906 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/instanceof_type_erase.ets @@ -0,0 +1,22 @@ +/* + * 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 A { + test(value: Object) { + return value instanceof T // CTE, T is erased + } +} + +/* @@? 18:16 Error TypeError: 'T' is erased when used in instanceof expression. */ diff --git a/ets2panda/util/diagnostic/semantic.yaml b/ets2panda/util/diagnostic/semantic.yaml index 265d476e7a..422e217b9b 100644 --- a/ets2panda/util/diagnostic/semantic.yaml +++ b/ets2panda/util/diagnostic/semantic.yaml @@ -632,6 +632,10 @@ semantic: id: 296 message: "Using the 'instance of' operator with non-object type '{}'" +- name: INSTANCEOF_TYPE_ERASED + id: 402 + message: "{} is erased when used in instanceof expression." + - name: INTERFACE_EXTENDS_CLASS id: 378 message: "Interfaces cannot extend classes, only other interfaces." -- Gitee