diff --git a/ets2panda/checker/types/ets/etsEnumType.cpp b/ets2panda/checker/types/ets/etsEnumType.cpp index 2ff91ba0ca4a748b24dd7df25ed23dc97d4d1dab..233a403e0501e8a662fba2b7bd2ad9c056b0df62 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 ac87200fbce06c222e7618258bcee72f2aa1a2a8..33c32e789cfada5d83580cd839f5aacf80fa48e5 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 0000000000000000000000000000000000000000..353ca83e8fccc42181af47b2b18dd800dfbe3530 --- /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 e50e7e009395b1ad9816c3aeb1a38b4d4f9cf1e6..44f291da40da477a7b0108f54aa3a5b88ac41c43 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 041eb0f42f3c0f4e6ed37c784e39c8e5d6da8582..3443eb3f6c38ac0318e3b1f6bdbc74e170d7ec40 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 0e1d8e35b438b8899285779527accddc1d6a604d..c78d967491c138158f757858b21bd5b205dc833c 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 71701fcf70a2e8489969e376178d119bbe0b13d3..49f08b5c8e8b958c210840575bc304cb9f7333e1 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): {}"