From 73dd2097620bffd93b15864d1ba272b36d5fc53f Mon Sep 17 00:00:00 2001 From: Zhelyapov Aleksey Date: Wed, 2 Jul 2025 18:07:31 +0300 Subject: [PATCH] Added warning for enum deprecated conversion Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICJRYX Signed-off-by: Zhelyapov Aleksey --- ets2panda/checker/types/ets/etsEnumType.cpp | 10 +++--- ets2panda/checker/types/typeRelation.h | 1 - .../test/ast/compiler/ets/enumConversions.ets | 35 +++++++++++++++++++ .../test/ast/parser/ets/FixedArray/enum11.ets | 2 ++ ets2panda/test/ast/parser/ets/enum.ets | 2 ++ ets2panda/test/ast/parser/ets/enum11.ets | 2 ++ ets2panda/util/diagnostic/warning.yaml | 4 +++ 7 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/enumConversions.ets diff --git a/ets2panda/checker/types/ets/etsEnumType.cpp b/ets2panda/checker/types/ets/etsEnumType.cpp index 2ff91ba0ca..233a403e05 100644 --- a/ets2panda/checker/types/ets/etsEnumType.cpp +++ b/ets2panda/checker/types/ets/etsEnumType.cpp @@ -61,6 +61,7 @@ void ETSStringEnumType::Cast(TypeRelation *const relation, Type *const target) return; } if (target->IsETSStringType()) { + relation->RaiseError(diagnostic::ENUM_DEPRECATED_CAST, {this, target}, relation->GetNode()->Start()); relation->Result(true); return; } @@ -70,6 +71,7 @@ void ETSStringEnumType::Cast(TypeRelation *const relation, Type *const target) void ETSStringEnumType::CastTarget(TypeRelation *relation, Type *source) { if (source->IsETSStringType()) { + relation->RaiseError(diagnostic::ENUM_DEPRECATED_CAST, {source, this}, relation->GetNode()->Start()); relation->Result(true); return; } @@ -115,6 +117,7 @@ void ETSIntEnumType::Cast(TypeRelation *const relation, Type *const target) return; } if (target->HasTypeFlag(TypeFlag::ETS_NUMERIC) || target->IsBuiltinNumeric()) { + relation->RaiseError(diagnostic::ENUM_DEPRECATED_CAST, {this, target}, relation->GetNode()->Start()); relation->Result(true); return; } @@ -123,11 +126,8 @@ void ETSIntEnumType::Cast(TypeRelation *const relation, Type *const target) void ETSIntEnumType::CastTarget(TypeRelation *relation, Type *source) { - if (source->IsIntType()) { - relation->Result(true); - return; - } - if (source->IsBuiltinNumeric()) { + if (source->IsIntType() || source->IsBuiltinNumeric()) { + relation->RaiseError(diagnostic::ENUM_DEPRECATED_CAST, {source, this}, relation->GetNode()->Start()); relation->Result(true); return; } diff --git a/ets2panda/checker/types/typeRelation.h b/ets2panda/checker/types/typeRelation.h index ac87200fbc..33c32e789c 100644 --- a/ets2panda/checker/types/typeRelation.h +++ b/ets2panda/checker/types/typeRelation.h @@ -312,7 +312,6 @@ public: void RaiseError(const diagnostic::DiagnosticKind &kind, const lexer::SourcePosition &loc) const; void RaiseError(const diagnostic::DiagnosticKind &kind, const util::DiagnosticMessageParams &list, const lexer::SourcePosition &loc) const; - void LogError(const util::DiagnosticMessageParams &list, const lexer::SourcePosition &loc) const; bool Result(bool res) { diff --git a/ets2panda/test/ast/compiler/ets/enumConversions.ets b/ets2panda/test/ast/compiler/ets/enumConversions.ets new file mode 100644 index 0000000000..353ca83e8f --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/enumConversions.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. + */ + +enum E { + a, b, c +} + +enum EE { + a, b, c, d, e +} + +function foo(e: E, i: int) { +// i = e // OK if widening, otherwise - CTE + i = e as int // CTE +// e = i // CTE + e = 1 as E // CTE +// e = E.fromValue(i) // RTE, if i is not equal to one of enum constants, otherwise OK +// e = 1 // OK, 1 can be E.b +// e = 10 // CTE, 10 is out of range +} + +/* @@? 26:7 Warning Warning: Enum cast is deprecated. Cast support from 'E' to 'Int' will be removed. */ +/* @@? 28:7 Warning Warning: Enum cast is deprecated. Cast support from 'Int' to 'E' will be removed. */ diff --git a/ets2panda/test/ast/parser/ets/FixedArray/enum11.ets b/ets2panda/test/ast/parser/ets/FixedArray/enum11.ets index e50e7e0093..44f291da40 100644 --- a/ets2panda/test/ast/parser/ets/FixedArray/enum11.ets +++ b/ets2panda/test/ast/parser/ets/FixedArray/enum11.ets @@ -24,3 +24,5 @@ function main(): void { } /* @@@ label Error TypeError: Enum name 'Color' used in the wrong context */ +/* @@? 21:22 Warning Warning: Enum cast is deprecated. Cast support from 'Color' to 'Int' will be removed. */ +/* @@? 22:13 Warning Warning: Enum cast is deprecated. Cast support from 'Int' to 'Color' will be removed. */ diff --git a/ets2panda/test/ast/parser/ets/enum.ets b/ets2panda/test/ast/parser/ets/enum.ets index 041eb0f42f..3443eb3f6c 100644 --- a/ets2panda/test/ast/parser/ets/enum.ets +++ b/ets2panda/test/ast/parser/ets/enum.ets @@ -65,3 +65,5 @@ function main(): void { arktest.assertTrue(false) } } + +/* @@? 26:10 Warning Warning: Enum cast is deprecated. Cast support from 'Color' to 'Int' will be removed. */ diff --git a/ets2panda/test/ast/parser/ets/enum11.ets b/ets2panda/test/ast/parser/ets/enum11.ets index 0e1d8e35b4..c78d967491 100644 --- a/ets2panda/test/ast/parser/ets/enum11.ets +++ b/ets2panda/test/ast/parser/ets/enum11.ets @@ -24,3 +24,5 @@ function main(): void { } /* @@@ label Error TypeError: Enum name 'Color' used in the wrong context */ +/* @@? 21:22 Warning Warning: Enum cast is deprecated. Cast support from 'Color' to 'Int' will be removed. */ +/* @@? 22:13 Warning Warning: Enum cast is deprecated. Cast support from 'Int' to 'Color' will be removed. */ diff --git a/ets2panda/util/diagnostic/warning.yaml b/ets2panda/util/diagnostic/warning.yaml index 71701fcf70..49f08b5c8e 100644 --- a/ets2panda/util/diagnostic/warning.yaml +++ b/ets2panda/util/diagnostic/warning.yaml @@ -29,6 +29,10 @@ warning: id: 12 message: "Detect duplicate signatures, use '{}{}' to replace" +- name: ENUM_DEPRECATED_CAST + id: 30 + message: "Enum cast is deprecated. Cast support from '{}' to '{}' will be removed." + - name: EXTENSION_MISMATCH id: 25 message: "Not matching extensions! Sourcefile: {}, Manual(used): {}" -- Gitee