From bb991e7d4bacdf266112d3f5e92b96127970b523 Mon Sep 17 00:00:00 2001 From: yp9522 Date: Thu, 7 Aug 2025 15:38:31 +0800 Subject: [PATCH] string|stringenum normalization & implicit conversion Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICSJ3J Signed-off-by: yp9522 --- ets2panda/checker/types/ets/etsUnionType.cpp | 15 ++++- .../runtime/ets/enum_stringEnum_as_union.ets | 48 ++++++++++++++++ .../ets/implicit_stringEnum_as_string.ets | 57 +++++++++++++++++++ 3 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 ets2panda/test/runtime/ets/enum_stringEnum_as_union.ets create mode 100644 ets2panda/test/runtime/ets/implicit_stringEnum_as_string.ets diff --git a/ets2panda/checker/types/ets/etsUnionType.cpp b/ets2panda/checker/types/ets/etsUnionType.cpp index 97b5f76d8b..334e6e705d 100644 --- a/ets2panda/checker/types/ets/etsUnionType.cpp +++ b/ets2panda/checker/types/ets/etsUnionType.cpp @@ -245,15 +245,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/runtime/ets/enum_stringEnum_as_union.ets b/ets2panda/test/runtime/ets/enum_stringEnum_as_union.ets new file mode 100644 index 0000000000..30308fa302 --- /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; + arktest.assertEQ(a, b); + arktest.assertEQ(b, "A"); + arktest.assertEQ(foo(StringEnum.B), "B"); + // class + let c: C = new C(); + arktest.assertEQ(c.a, "A"); + // interface + let i: I = { a: StringEnum.C }; + arktest.assertEQ(i.a, "C"); +} diff --git a/ets2panda/test/runtime/ets/implicit_stringEnum_as_string.ets b/ets2panda/test/runtime/ets/implicit_stringEnum_as_string.ets new file mode 100644 index 0000000000..87b5880fb6 --- /dev/null +++ b/ets2panda/test/runtime/ets/implicit_stringEnum_as_string.ets @@ -0,0 +1,57 @@ +/* + * 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 { + X = "X", + Y = "Y", + Z = "Z" +} + +interface TestInterface { + prop: string; +} + +class TestClass { + public name: string = StringEnum.X; +} + +function processString(str: string): string { + return str; +} + +function concatStrings(a: string, b: string): string { + return a + b; +} + +function main() { + let directAssignment: string = StringEnum.Y; + arktest.assertEQ(directAssignment, "Y"); + + let obj: TestInterface = { prop: StringEnum.Z }; + arktest.assertEQ(obj.prop, "Z"); + + let instance: TestClass = new TestClass(); + arktest.assertEQ(instance.name, "X"); + + arktest.assertEQ(processString(StringEnum.Y), "Y"); + + let result: string = concatStrings("Prefix", StringEnum.X); + arktest.assertEQ(result, "PrefixX"); + + let stringArray: string[] = [StringEnum.X, StringEnum.Y, StringEnum.Z]; + arktest.assertEQ(stringArray[0], "X"); + arktest.assertEQ(stringArray[1], "Y"); + arktest.assertEQ(stringArray[2], "Z"); +} -- Gitee