diff --git a/ets2panda/checker/types/ets/etsUnionType.cpp b/ets2panda/checker/types/ets/etsUnionType.cpp index 7b94a5311209335cc121aade18da8c1fcaf2d8d4..b3939931c2c52435546bc98997d618cffa4e72b2 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 0000000000000000000000000000000000000000..b2371f411fe754fe8267ed1b8b034f7d4827aa72 --- /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 0000000000000000000000000000000000000000..fa9547e23276e432172dfa92ca7e9f31fcb4b237 --- /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 0000000000000000000000000000000000000000..77f73fb89ea2e29570686e2b5d019e28894cfe67 --- /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 0000000000000000000000000000000000000000..b9a975796d1735c7cde2dff05285f2cdcb61bd41 --- /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"); +}