diff --git a/ets2panda/checker/types/ets/etsUnionType.cpp b/ets2panda/checker/types/ets/etsUnionType.cpp index 97b5f76d8bd976d910878cb9f7089e0866f5bf64..334e6e705d4467fe182daecf5a03c18a4db59388 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 0000000000000000000000000000000000000000..30308fa30261c5ed78c72cdfb99454f8be732b32 --- /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 0000000000000000000000000000000000000000..87b5880fb62df4b08aafd156a6326f9eb0be0c1e --- /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"); +}