From 79f41082a67e4d50d8b859eed76e47e9773929af Mon Sep 17 00:00:00 2001 From: yp9522 Date: Wed, 25 Jun 2025 17:35:29 +0800 Subject: [PATCH] =?UTF-8?q?stringsnum=E5=90=8C=E4=B8=80=E5=8C=96=E5=8F=8A?= =?UTF-8?q?=E9=9A=90=E5=BC=8F=E8=BD=AC=E6=8D=A2=201=20Issue:https://gitee.?= =?UTF-8?q?com/openharmony/arkcompiler=5Fets=5Ffrontend/issues/ICHSB4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yp9522 --- ets2panda/checker/types/ets/etsUnionType.cpp | 15 +++++- .../ets/enum_stringEnum_as_string.ets | 31 ++++++++++++ .../compiler/ets/enum_stringEnum_as_union.ets | 28 +++++++++++ .../runtime/ets/enum_stringEnum_as_string.ets | 34 +++++++++++++ .../runtime/ets/enum_stringEnum_as_union.ets | 48 +++++++++++++++++++ 5 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 ets2panda/test/ast/compiler/ets/enum_stringEnum_as_string.ets create mode 100644 ets2panda/test/ast/compiler/ets/enum_stringEnum_as_union.ets create mode 100644 ets2panda/test/runtime/ets/enum_stringEnum_as_string.ets create mode 100644 ets2panda/test/runtime/ets/enum_stringEnum_as_union.ets diff --git a/ets2panda/checker/types/ets/etsUnionType.cpp b/ets2panda/checker/types/ets/etsUnionType.cpp index 7b94a53112..b3939931c2 100644 --- a/ets2panda/checker/types/ets/etsUnionType.cpp +++ b/ets2panda/checker/types/ets/etsUnionType.cpp @@ -194,15 +194,26 @@ void ETSUnionType::CastTarget(TypeRelation *relation, Type *source) RelationTarget(relation, source, relFn); } +static bool IsStringEnumtypeOf(Type *const super, Type *const sub) +{ + if (!super->IsETSStringType() || sub == nullptr) { + return false; + } + if (sub->IsETSStringEnumType()) { + return true; + } + return false; +} + static std::optional TryMergeTypes(TypeRelation *relation, Type *const t1, Type *const t2) { auto *const checker = relation->GetChecker()->AsETSChecker(); auto *const never = checker->GetGlobalTypesHolder()->GlobalETSNeverType(); - if (relation->IsSupertypeOf(t1, t2) || t2 == never) { + if (relation->IsSupertypeOf(t1, t2) || IsStringEnumtypeOf(t1, t2) || t2 == never) { return t1; } - if (relation->IsSupertypeOf(t2, t1) || t1 == never) { + if (relation->IsSupertypeOf(t2, t1) || IsStringEnumtypeOf(t2, t1) || t1 == never) { return t2; } return std::nullopt; diff --git a/ets2panda/test/ast/compiler/ets/enum_stringEnum_as_string.ets b/ets2panda/test/ast/compiler/ets/enum_stringEnum_as_string.ets new file mode 100644 index 0000000000..b2371f411f --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/enum_stringEnum_as_string.ets @@ -0,0 +1,31 @@ +/* + * 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 StringEnum { + A = "A", + B = "B", + C = "C" +} + +enum IntEnum { + A = 1, + B = 2, + C = 3 +} + +let a: string = StringEnum.A; +let b: string = /* @@ label */IntEnum.A; + +/* @@@ label Error TypeError: Type 'IntEnum' cannot be assigned to type 'String' */ diff --git a/ets2panda/test/ast/compiler/ets/enum_stringEnum_as_union.ets b/ets2panda/test/ast/compiler/ets/enum_stringEnum_as_union.ets new file mode 100644 index 0000000000..fa9547e232 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/enum_stringEnum_as_union.ets @@ -0,0 +1,28 @@ +/* + * 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 StringEnum { + A = "A", + B = "B", + C = "C" +} + +type UTA = string | StringEnum | number; + +let a: UTA = StringEnum.A; + +let b: string = /* @@ label */a; + +/* @@@ label Error TypeError: Type 'String|Double' cannot be assigned to type 'String' */ diff --git a/ets2panda/test/runtime/ets/enum_stringEnum_as_string.ets b/ets2panda/test/runtime/ets/enum_stringEnum_as_string.ets new file mode 100644 index 0000000000..77f73fb89e --- /dev/null +++ b/ets2panda/test/runtime/ets/enum_stringEnum_as_string.ets @@ -0,0 +1,34 @@ +/* + * 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 StringEnum { + A = "A", + B = "B", + C = "C" +} + +interface I { + a: string; +} + +function foo(a: string): string { + return a; +} + +let a: string = StringEnum.A; +assertEQ(a, "A"); +let i: I = { a: StringEnum.B }; +assertEQ(i.a, "B"); +assertEQ(foo(StringEnum.C), "C"); diff --git a/ets2panda/test/runtime/ets/enum_stringEnum_as_union.ets b/ets2panda/test/runtime/ets/enum_stringEnum_as_union.ets new file mode 100644 index 0000000000..b9a975796d --- /dev/null +++ b/ets2panda/test/runtime/ets/enum_stringEnum_as_union.ets @@ -0,0 +1,48 @@ +/* + * 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 StringEnum { + A = "A", + B = "B", + C = "C" +} + +class C { + public a: string | StringEnum = StringEnum.A; +} + +interface I { + a: string | StringEnum; +} + +type UTA = string | StringEnum | number; + +function foo(a: string | StringEnum): string { + return a; +} + +function main() { + let a: UTA = StringEnum.A; + let b: string | number = a; + assertEQ(a, b); + assertEQ(b, "A"); + assertEQ(foo(StringEnum.B), "B"); + // class + let c: C = new C(); + assertEQ(c.a, "A"); + // interface + let i: I = { a: StringEnum.C }; + assertEQ(i.a, "C"); +} -- Gitee