diff --git a/ets2panda/checker/types/ets/etsEnumType.cpp b/ets2panda/checker/types/ets/etsEnumType.cpp index 233a403e0501e8a662fba2b7bd2ad9c056b0df62..db31d63d6278ff1f18f586957345e15b9ad4eed0 100644 --- a/ets2panda/checker/types/ets/etsEnumType.cpp +++ b/ets2panda/checker/types/ets/etsEnumType.cpp @@ -26,6 +26,28 @@ Type *ETSEnumType::GetBaseEnumElementType(ETSChecker *checker) return checker->MaybeUnboxType(SuperType()->TypeArguments()[0]); } +static void SetGenerateValueOfFlag(TypeRelation *relation) +{ + if (!relation->GetNode()->TsType()->IsETSUnionType()) { + relation->GetNode()->AddAstNodeFlags(ir::AstNodeFlags::GENERATE_VALUE_OF); + return; + } + + if (relation->GetNode()->IsConditionalExpression()) { + auto consequent = relation->GetNode()->AsConditionalExpression()->Consequent(); + auto alternate = relation->GetNode()->AsConditionalExpression()->Alternate(); + if (consequent->TsType()->IsETSEnumType()) { + consequent->AddAstNodeFlags(ir::AstNodeFlags::GENERATE_VALUE_OF); + } + if (alternate->TsType()->IsETSEnumType()) { + alternate->AddAstNodeFlags(ir::AstNodeFlags::GENERATE_VALUE_OF); + } + return; + } + + ES2PANDA_UNREACHABLE(); +} + bool ETSStringEnumType::AssignmentSource(TypeRelation *relation, Type *target) { bool result = false; @@ -34,7 +56,7 @@ bool ETSStringEnumType::AssignmentSource(TypeRelation *relation, Type *target) } else if (target->IsETSStringType()) { result = true; if (relation->GetNode() != nullptr) { - relation->GetNode()->AddAstNodeFlags(ir::AstNodeFlags::GENERATE_VALUE_OF); + SetGenerateValueOfFlag(relation); } } else if (target->IsETSUnionType()) { auto &unionConstituentTypes = target->AsETSUnionType()->ConstituentTypes(); @@ -54,30 +76,6 @@ void ETSStringEnumType::AssignmentTarget(TypeRelation *relation, Type *source) relation->IsIdenticalTo(this, source) ? relation->Result(true) : relation->Result(false); } -void ETSStringEnumType::Cast(TypeRelation *const relation, Type *const target) -{ - if (relation->IsIdenticalTo(this, target)) { - relation->Result(true); - return; - } - if (target->IsETSStringType()) { - relation->RaiseError(diagnostic::ENUM_DEPRECATED_CAST, {this, target}, relation->GetNode()->Start()); - relation->Result(true); - return; - } - conversion::Forbidden(relation); -} - -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; - } - conversion::Forbidden(relation); -} - bool ETSIntEnumType::AssignmentSource(TypeRelation *relation, Type *target) { bool result = false; @@ -87,11 +85,11 @@ bool ETSIntEnumType::AssignmentSource(TypeRelation *relation, Type *target) result = true; } else if (target->IsBuiltinNumeric()) { result = true; - relation->GetNode()->AddAstNodeFlags(ir::AstNodeFlags::GENERATE_VALUE_OF); + SetGenerateValueOfFlag(relation); } } else if (target->HasTypeFlag(TypeFlag::ETS_NUMERIC)) { result = true; - relation->GetNode()->AddAstNodeFlags(ir::AstNodeFlags::GENERATE_VALUE_OF); + SetGenerateValueOfFlag(relation); } else if (target->IsETSUnionType()) { auto &unionConstituentTypes = target->AsETSUnionType()->ConstituentTypes(); for (auto *constituentType : unionConstituentTypes) { @@ -110,28 +108,4 @@ void ETSIntEnumType::AssignmentTarget(TypeRelation *relation, Type *source) relation->IsIdenticalTo(this, source) ? relation->Result(true) : relation->Result(false); } -void ETSIntEnumType::Cast(TypeRelation *const relation, Type *const target) -{ - if (relation->IsIdenticalTo(this, target)) { - relation->Result(true); - return; - } - if (target->HasTypeFlag(TypeFlag::ETS_NUMERIC) || target->IsBuiltinNumeric()) { - relation->RaiseError(diagnostic::ENUM_DEPRECATED_CAST, {this, target}, relation->GetNode()->Start()); - relation->Result(true); - return; - } - conversion::Forbidden(relation); -} - -void ETSIntEnumType::CastTarget(TypeRelation *relation, Type *source) -{ - if (source->IsIntType() || source->IsBuiltinNumeric()) { - relation->RaiseError(diagnostic::ENUM_DEPRECATED_CAST, {source, this}, relation->GetNode()->Start()); - relation->Result(true); - return; - } - conversion::Forbidden(relation); -} - } // namespace ark::es2panda::checker diff --git a/ets2panda/checker/types/ets/etsEnumType.h b/ets2panda/checker/types/ets/etsEnumType.h index 3005b78053a406f8ef707e2410d7d7cc090593a4..636b92e55da5cb1f3ade0df34adfe98bcfe427f9 100644 --- a/ets2panda/checker/types/ets/etsEnumType.h +++ b/ets2panda/checker/types/ets/etsEnumType.h @@ -137,8 +137,6 @@ public: bool AssignmentSource(TypeRelation *relation, Type *target) override; void AssignmentTarget(TypeRelation *relation, Type *source) override; - void Cast(TypeRelation *relation, Type *target) override; - void CastTarget(TypeRelation *relation, Type *source) override; }; class ETSStringEnumType : public ETSEnumType { @@ -158,8 +156,6 @@ public: bool AssignmentSource(TypeRelation *relation, Type *target) override; void AssignmentTarget(TypeRelation *relation, Type *source) override; - void Cast(TypeRelation *relation, Type *target) override; - void CastTarget(TypeRelation *relation, Type *source) override; }; } // namespace ark::es2panda::checker diff --git a/ets2panda/compiler/lowering/ets/enumPostCheckLowering.cpp b/ets2panda/compiler/lowering/ets/enumPostCheckLowering.cpp index abd8401d58e559c36a883568ff437cd53e8fd8ca..29f8b130f87d7c0e72561ca6e6a19d71bbc27875 100644 --- a/ets2panda/compiler/lowering/ets/enumPostCheckLowering.cpp +++ b/ets2panda/compiler/lowering/ets/enumPostCheckLowering.cpp @@ -43,89 +43,6 @@ static ir::ClassDeclaration *FindEnclosingClass(ir::AstNode *ast) ES2PANDA_UNREACHABLE(); } -static std::string TypeToString(checker::Type *type) -{ - std::stringstream ss; - type->ToString(ss); - return ss.str(); -} - -static util::StringView TypeAnnotationToString(ir::ETSTypeReference *typeAnnotation, public_lib::Context *ctx) -{ - std::stringstream ss; - std::function typeAnnoToStringImpl = [&ss, &typeAnnoToStringImpl](ir::AstNode *node) -> void { - if (node->IsIdentifier()) { - ss << node->AsIdentifier()->ToString() << "."; - } - node->Iterate(typeAnnoToStringImpl); - }; - typeAnnotation->Iterate(typeAnnoToStringImpl); - std::string res = ss.str(); - res.erase(res.size() - 1); - return util::UString {res, ctx->Allocator()}.View(); -} - -static bool IsValidEnumCasting(checker::Type *type, EnumCastType castType) -{ - switch (castType) { - case EnumCastType::NONE: { - return false; - } - case EnumCastType::CAST_TO_STRING: { - return type->IsETSStringEnumType(); - } - case EnumCastType::CAST_TO_INT: { - return type->IsETSIntEnumType(); - } - case EnumCastType::CAST_TO_INT_ENUM: - case EnumCastType::CAST_TO_STRING_ENUM: { - return true; - } - default: { - ES2PANDA_UNREACHABLE(); - } - } -} - -static EnumCastType NeedHandleEnumCasting(ir::TSAsExpression *node) -{ - auto type = node->TsType(); - EnumCastType castType = EnumCastType::NONE; - if (type == nullptr) { - return castType; - } - if (type->IsETSStringType()) { - castType = EnumCastType::CAST_TO_STRING; - } else if (type->HasTypeFlag(checker::TypeFlag::ETS_NUMERIC) || type->IsBuiltinNumeric()) { - castType = EnumCastType::CAST_TO_INT; - } else if (type->IsETSEnumType()) { - castType = type->IsETSIntEnumType() ? EnumCastType::CAST_TO_INT_ENUM : EnumCastType::CAST_TO_STRING_ENUM; - } else { - return castType; - } - auto expr = node->Expr(); - if (expr->TsType()->IsETSUnionType()) { - for (auto *ct : expr->TsType()->AsETSUnionType()->ConstituentTypes()) { - if (ct->IsETSEnumType() && IsValidEnumCasting(ct, castType)) { - return castType; - } - } - } - return IsValidEnumCasting(expr->TsType(), castType) ? castType : EnumCastType::NONE; -} - -static ir::CallExpression *CallStaticEnumMethod(public_lib::Context *ctx, parser::ETSParser *parser, - std::string_view className, std::string_view methodName, - ir::Expression *argument) -{ - auto classId = parser->CreateExpression(className); - auto methodId = ctx->AllocNode(methodName, ctx->Allocator()); - auto callee = ctx->AllocNode(classId, methodId, ir::MemberExpressionKind::PROPERTY_ACCESS, - false, false); - ArenaVector callArguments({argument}, ctx->Allocator()->Adapter()); - return ctx->AllocNode(callee, std::move(callArguments), nullptr, false); -} - static ir::CallExpression *CallInstanceEnumMethod(public_lib::Context *ctx, std::string_view methodName, ir::Expression *thisArg) { @@ -166,164 +83,6 @@ static ir::CallExpression *CreateCallInstanceEnumExpression(public_lib::Context return callExpr; } -[[nodiscard]] static ir::ETSTypeReference *MakeTypeReference(public_lib::Context *ctx, const std::string &name) -{ - auto allocator = ctx->Allocator(); - auto *const ident = ctx->AllocNode(util::UString(name, allocator).View(), allocator); - auto *const referencePart = ctx->AllocNode(ident, allocator); - return ctx->AllocNode(referencePart, allocator); -} - -static ir::IfStatement *CreateEnumIfStatement(public_lib::Context *ctx, ir::Identifier *ident, - const std::string &enumName, ir::Statement *consequent) -{ - auto enumType = MakeTypeReference(ctx, enumName); - auto clonedIdent = ident->Clone(ctx->Allocator(), nullptr); - auto ifTestExpr = ctx->AllocNode(clonedIdent, enumType, lexer::TokenType::KEYW_INSTANCEOF); - return ctx->AllocNode(ifTestExpr, consequent, nullptr); -} - -ir::Statement *EnumPostCheckLoweringPhase::CreateStatement(const std::string &src, ir::Expression *ident, - ir::Expression *init) -{ - std::vector nodes; - if (ident != nullptr) { - nodes.push_back(ident->Clone(context_->Allocator(), nullptr)); - } - - if (init != nullptr) { - nodes.push_back(init->Clone(context_->Allocator(), nullptr)); - } - - auto statements = parser_->CreateFormattedStatements(src, nodes); - if (!statements.empty()) { - return *statements.begin(); - } - - return nullptr; -} - -ir::Expression *EnumPostCheckLoweringPhase::HandleEnumTypeCasting(checker::Type *type, ir::Expression *expr, - ir::TSAsExpression *tsAsExpr) -{ - ir::Expression *transformedExpr = nullptr; - // Generate fromValue call; - if (type->IsETSEnumType()) { - auto exprType = expr->TsType(); - if (exprType->IsETSEnumType() || exprType->IsETSAnyType() || - (exprType->IsETSObjectType() && exprType->AsETSObjectType()->IsGlobalETSObjectType())) { - return expr; - } - auto name = TypeAnnotationToString(tsAsExpr->TypeAnnotation()->AsETSTypeReference(), context_); - transformedExpr = GenerateFromValueCall(expr, name); - } else { - transformedExpr = CallInstanceEnumMethod(context_, checker::ETSEnumType::VALUE_OF_METHOD_NAME, expr); - } - transformedExpr->SetRange(expr->Range()); - transformedExpr->SetParent(expr->Parent()); - return transformedExpr; -} - -// CC-OFFNXT(huge_method,G.FUD.05) -void EnumPostCheckLoweringPhase::CreateStatementForUnionConstituentType(EnumCastType castType, ir::Identifier *ident, - checker::Type *type, - ir::TSAsExpression *tsAsExpr, - ArenaVector &statements) -{ - auto createInstanceOfStatement = [this, &statements, &ident, &type](ir::Expression *callExpr) { - auto consequent = CreateStatement("@@I1 = @@E2;", ident, callExpr); - auto ifStatement = CreateEnumIfStatement(context_, ident, TypeToString(type), consequent); - auto prevStatement = statements.back(); - if (prevStatement != nullptr && prevStatement->IsIfStatement()) { - prevStatement->AsIfStatement()->SetAlternate(ifStatement); - } - statements.push_back(ifStatement); - }; - switch (castType) { - case EnumCastType::CAST_TO_STRING: { - if (type->IsETSStringEnumType()) { - auto callExpr = CallInstanceEnumMethod(context_, checker::ETSEnumType::VALUE_OF_METHOD_NAME, - ident->Clone(context_->Allocator(), nullptr)->AsExpression()); - callExpr->SetRange(tsAsExpr->Expr()->Range()); - createInstanceOfStatement(callExpr); - } - break; - } - case EnumCastType::CAST_TO_INT: { - if (type->IsETSIntEnumType()) { - auto callExpr = CallInstanceEnumMethod(context_, checker::ETSEnumType::VALUE_OF_METHOD_NAME, - ident->Clone(context_->Allocator(), nullptr)->AsExpression()); - callExpr->SetRange(tsAsExpr->Expr()->Range()); - createInstanceOfStatement(callExpr); - } - break; - } - case EnumCastType::CAST_TO_INT_ENUM: { - // int and Boxed Int can be casted to int enum - if (type->IsIntType() || (type->IsETSObjectType() && - type->AsETSObjectType()->HasObjectFlag(checker::ETSObjectFlags::BUILTIN_INT))) { - auto name = TypeAnnotationToString(tsAsExpr->TypeAnnotation()->AsETSTypeReference(), context_); - auto callExpr = - GenerateFromValueCall(ident->Clone(context_->Allocator(), nullptr)->AsExpression(), name); - callExpr->SetRange(tsAsExpr->Expr()->Range()); - createInstanceOfStatement(callExpr); - } - break; - } - case EnumCastType::CAST_TO_STRING_ENUM: { - if (type->IsETSStringType()) { - auto name = TypeAnnotationToString(tsAsExpr->TypeAnnotation()->AsETSTypeReference(), context_); - auto callExpr = - GenerateFromValueCall(ident->Clone(context_->Allocator(), nullptr)->AsExpression(), name); - callExpr->SetRange(tsAsExpr->Expr()->Range()); - createInstanceOfStatement(callExpr); - } - break; - } - default: { - ES2PANDA_UNREACHABLE(); - } - } -} - -ir::Expression *EnumPostCheckLoweringPhase::HandleUnionTypeForCalls(checker::ETSUnionType *unionType, - ir::Expression *expr, ir::TSAsExpression *tsAsExpr, - EnumCastType castType) -{ - /* - * For given union type: number | string | Enum | otherTypes, this method generate instanceof trees to ensuring - * all union constituent types are handled correctly with enum related casting. - */ - auto *const allocator = context_->Allocator(); - auto *genSymIdent = Gensym(allocator); - ArenaVector statements(allocator->Adapter()); - const std::string createSrc = "let @@I1 = @@E2"; - statements.push_back(CreateStatement(createSrc, genSymIdent, expr)); - for (auto type : unionType->ConstituentTypes()) { - CreateStatementForUnionConstituentType(castType, genSymIdent, type, tsAsExpr, statements); - } - statements.push_back(CreateStatement("@@I1", genSymIdent, nullptr)); - auto block = context_->AllocNode(std::move(statements)); - return block; -} - -ir::AstNode *EnumPostCheckLoweringPhase::GenerateEnumCasting(ir::TSAsExpression *node, EnumCastType castType) -{ - auto expr = node->Expr(); - if (expr->TsType()->IsETSUnionType()) { - auto newExpr = HandleUnionTypeForCalls(expr->TsType()->AsETSUnionType(), node->Expr(), node, castType); - node->SetExpr(newExpr); - } else { - auto newExpr = HandleEnumTypeCasting(node->TsType(), node->Expr(), node); - node->SetExpr(newExpr); - } - node->SetTsType(nullptr); - auto *scope = NearestScope(node); - auto bscope = varbinder::LexicalScope::Enter(varbinder_, scope); - CheckLoweredNode(varbinder_, checker_, node); - return node; -} - static auto *InlineValueOf(ir::MemberExpression *enumMemberRef, ArenaAllocator *allocator) { auto key = enumMemberRef->Property()->AsIdentifier()->Name().Utf8(); @@ -355,14 +114,6 @@ ir::AstNode *EnumPostCheckLoweringPhase::GenerateValueOfCall(ir::AstNode *const return callExpr; } -ir::Expression *EnumPostCheckLoweringPhase::GenerateFromValueCall(ir::Expression *const node, util::StringView name) -{ - auto *callExpr = - CallStaticEnumMethod(context_, parser_, name.Utf8(), checker::ETSEnumType::FROM_VALUE_METHOD_NAME, node); - callExpr->SetRange(node->Range()); - return callExpr; -} - ir::SwitchStatement *EnumPostCheckLoweringPhase::GenerateGetOrdinalCallForSwitch(ir::SwitchStatement *const node) { node->AddAstNodeFlags(ir::AstNodeFlags::RECHECK); @@ -413,13 +164,6 @@ bool EnumPostCheckLoweringPhase::PerformForModule(public_lib::Context *ctx, pars if (node->HasAstNodeFlags(ir::AstNodeFlags::GENERATE_VALUE_OF)) { return GenerateValueOfCall(node); } - if (node->IsTSAsExpression()) { - auto castFlag = NeedHandleEnumCasting(node->AsTSAsExpression()); - if (castFlag == EnumCastType::NONE) { - return node; - } - return GenerateEnumCasting(node->AsTSAsExpression(), castFlag); - } if (node->IsSwitchStatement() && (node->AsSwitchStatement()->Discriminant()->TsType() != nullptr) && node->AsSwitchStatement()->Discriminant()->TsType()->IsETSEnumType()) { return GenerateGetOrdinalCallForSwitch(node->AsSwitchStatement()); diff --git a/ets2panda/compiler/lowering/ets/enumPostCheckLowering.h b/ets2panda/compiler/lowering/ets/enumPostCheckLowering.h index 1ce716fe85b21f515640551bfc42b09b63874693..f1b6e64dd04fcd1b9adea116c9a24126cad84f76 100644 --- a/ets2panda/compiler/lowering/ets/enumPostCheckLowering.h +++ b/ets2panda/compiler/lowering/ets/enumPostCheckLowering.h @@ -39,16 +39,8 @@ public: bool PerformForModule(public_lib::Context *ctx, parser::Program *program) override; private: - ir::Statement *CreateStatement(const std::string &src, ir::Expression *ident, ir::Expression *init); - void CreateStatementForUnionConstituentType(EnumCastType castType, ir::Identifier *ident, checker::Type *type, - ir::TSAsExpression *tsAsExpr, ArenaVector &statements); ir::SwitchStatement *GenerateGetOrdinalCallForSwitch(ir::SwitchStatement *const node); - ir::AstNode *GenerateEnumCasting(ir::TSAsExpression *node, EnumCastType castType); ir::AstNode *GenerateValueOfCall(ir::AstNode *const node); - ir::Expression *GenerateFromValueCall(ir::Expression *const node, util::StringView name); - ir::Expression *HandleEnumTypeCasting(checker::Type *type, ir::Expression *expr, ir::TSAsExpression *tsAsExpr); - ir::Expression *HandleUnionTypeForCalls(checker::ETSUnionType *unionType, ir::Expression *expr, - ir::TSAsExpression *tsAsExpr, EnumCastType castType); public_lib::Context *context_ {nullptr}; parser::ETSParser *parser_ {nullptr}; diff --git a/ets2panda/scripts/arkui.properties b/ets2panda/scripts/arkui.properties index 596d66d7752f3b3d26f0170ef32fb0eff678208d..36f68bd8c7d1d1eb3bbcb6c93917263188cc55b9 100644 --- a/ets2panda/scripts/arkui.properties +++ b/ets2panda/scripts/arkui.properties @@ -1,3 +1,3 @@ ARKUI_DEV_REPO=https://gitee.com/rri_opensource/koala_projects.git -ARKUI_DEV_BRANCH=panda_rev_9-array-length-int +ARKUI_DEV_BRANCH=panda_rev_9-enum-conversion-remove-support ARKUI_DEST=koala-sig diff --git a/ets2panda/test/ast/compiler/ets/enumConversions.ets b/ets2panda/test/ast/compiler/ets/enumConversions.ets index 353ca83e8fccc42181af47b2b18dd800dfbe3530..be4189be715b7bf082f60d2f7010949681f322dc 100644 --- a/ets2panda/test/ast/compiler/ets/enumConversions.ets +++ b/ets2panda/test/ast/compiler/ets/enumConversions.ets @@ -13,23 +13,167 @@ * limitations under the License. */ -enum E { +enum INT_ENUM_1 { a, b, c } -enum EE { - a, b, c, d, e +enum INT_ENUM_2 { + 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 +enum STR_ENUM_1 { + a = "a", b = "b" } -/* @@? 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. */ +enum STR_ENUM_2 { + b = "d", c = "e" +} + +function int_enum_conversions(int_enum_1: INT_ENUM_1, int_enum_2: INT_ENUM_2, i: int, l: long, s: short, n: number, + d: double) +{ + // ----------------------------------------------------------------------------------------------------------------- + // CTE: + // ----------------------------------------------------------------------------------------------------------------- + i = int_enum_1 as int + l = int_enum_1 as long + s = int_enum_1 as short + n = int_enum_1 as number + d = int_enum_1 as double + + int_enum_1 = i as INT_ENUM_1 + int_enum_1 = l as INT_ENUM_1 + int_enum_1 = s as INT_ENUM_1 + int_enum_1 = n as INT_ENUM_1 + int_enum_1 = d as INT_ENUM_1 + + int_enum_2 = int_enum_1 + int_enum_2 = int_enum_1 as INT_ENUM_2 + + // Narrowing: + int_enum_1 = i + int_enum_1 = l + int_enum_1 = s + int_enum_1 = 1 + s = int_enum_1 // CTE!!! + + // Incompatible types: + int_enum_1 = n + int_enum_1 = d + int_enum_1 = 1.0 + int_enum_1 = "a" + int_enum_1 = INT_ENUM_1.fromValue(l) + int_enum_1 = INT_ENUM_1.fromValue(1.0) + int_enum_1 = INT_ENUM_1.fromValue("a") + int_enum_1 = INT_ENUM_1.fromValue(n) + int_enum_1 = INT_ENUM_1.fromValue(d) + // ----------------------------------------------------------------------------------------------------------------- + // OK: + // ----------------------------------------------------------------------------------------------------------------- + // RTE, if value is not equal to one of enum constants, otherwise OK: + int_enum_1 = int_enum_1 + int_enum_1 = INT_ENUM_1.fromValue(i) + int_enum_1 = INT_ENUM_1.fromValue(s) // Implicit widening + int_enum_1 = INT_ENUM_1.fromValue(Short.toInt(s)) + int_enum_1 = INT_ENUM_1.fromValue(Long.toInt(l)) + int_enum_1 = INT_ENUM_1.fromValue(Double.toInt(n)) + int_enum_1 = INT_ENUM_1.fromValue(Double.toInt(d)) + int_enum_1 = INT_ENUM_1.fromValue(1) + int_enum_1 = INT_ENUM_1.fromValue(10) + int_enum_1 = INT_ENUM_1.fromValue(Double.toInt(1.0)) + int_enum_1 = INT_ENUM_1.fromValue(int_enum_2) + + // Widening: + i = int_enum_1 + l = int_enum_1 + n = int_enum_1 + d = int_enum_1 + // ----------------------------------------------------------------------------------------------------------------- +} + +function str_enum_conversions(str_enum_1: STR_ENUM_1, str_enum_2: STR_ENUM_2, s: string) { + // ----------------------------------------------------------------------------------------------------------------- + // CTE: + // ----------------------------------------------------------------------------------------------------------------- + s = str_enum_1 as string + str_enum_1 = s as STR_ENUM_1 + + str_enum_2 = str_enum_1 + str_enum_2 = str_enum_1 as STR_ENUM_2 + + // Narrowing: + str_enum_1 = s + str_enum_1 = "a" + + // Incompatible types: + str_enum_1 = 1 + str_enum_1 = 1.0 + str_enum_1 = STR_ENUM_1.fromValue(1) + str_enum_1 = STR_ENUM_1.fromValue(1.0) + // ----------------------------------------------------------------------------------------------------------------- + // OK: + // ----------------------------------------------------------------------------------------------------------------- + // RTE, if value is not equal to one of enum constants, otherwise OK: + str_enum_1 = str_enum_1 + str_enum_1 = STR_ENUM_1.fromValue(s) + str_enum_1 = STR_ENUM_1.fromValue("a") + str_enum_1 = STR_ENUM_1.fromValue("x") + str_enum_1 = STR_ENUM_1.fromValue(str_enum_2) + + // Widening: + s = str_enum_1 + // ----------------------------------------------------------------------------------------------------------------- +} + +/* @@? 38:9 Error TypeError: Cannot cast type 'INT_ENUM_1' to 'Int' */ +/* @@? 39:9 Error TypeError: Cannot cast type 'INT_ENUM_1' to 'Long' */ +/* @@? 40:9 Error TypeError: Cannot cast type 'INT_ENUM_1' to 'Short' */ +/* @@? 41:9 Error TypeError: Cannot cast type 'INT_ENUM_1' to 'Double' */ +/* @@? 42:9 Error TypeError: Cannot cast type 'INT_ENUM_1' to 'Double' */ + +/* @@? 44:18 Error TypeError: Cannot cast type 'Int' to 'INT_ENUM_1' */ +/* @@? 45:18 Error TypeError: Cannot cast type 'Long' to 'INT_ENUM_1' */ +/* @@? 46:18 Error TypeError: Cannot cast type 'Short' to 'INT_ENUM_1' */ +/* @@? 47:18 Error TypeError: Cannot cast type 'Double' to 'INT_ENUM_1' */ +/* @@? 48:18 Error TypeError: Cannot cast type 'Double' to 'INT_ENUM_1' */ + +/* @@? 50:18 Error TypeError: Type 'INT_ENUM_1' cannot be assigned to type 'INT_ENUM_2' */ +/* @@? 51:18 Error TypeError: Cannot cast type 'INT_ENUM_1' to 'INT_ENUM_2' */ + +/* @@? 54:18 Error TypeError: Type 'Int' cannot be assigned to type 'INT_ENUM_1' */ +/* @@? 55:18 Error TypeError: Type 'Long' cannot be assigned to type 'INT_ENUM_1' */ +/* @@? 56:18 Error TypeError: Type 'Short' cannot be assigned to type 'INT_ENUM_1' */ +/* @@? 57:18 Error TypeError: Type 'Int' cannot be assigned to type 'INT_ENUM_1' */ + +// TypeError expected for line 58, but did not reported. See #27649 for more info. + +/* @@? 61:18 Error TypeError: Type 'Double' cannot be assigned to type 'INT_ENUM_1' */ +/* @@? 62:18 Error TypeError: Type 'Double' cannot be assigned to type 'INT_ENUM_1' */ +/* @@? 63:18 Error TypeError: Type 'Double' cannot be assigned to type 'INT_ENUM_1' */ +/* @@? 64:18 Error TypeError: Type '"a"' cannot be assigned to type 'INT_ENUM_1' */ +/* @@? 65:18 Error TypeError: No matching call signature for fromValue(Long) */ +/* @@? 65:39 Error TypeError: Type 'Long' is not compatible with type 'Int' at index 1 */ +/* @@? 66:18 Error TypeError: No matching call signature for fromValue(Double) */ +/* @@? 66:39 Error TypeError: Type 'Double' is not compatible with type 'Int' at index 1 */ +/* @@? 67:18 Error TypeError: No matching call signature for fromValue("a") */ +/* @@? 67:39 Error TypeError: Type '"a"' is not compatible with type 'Int' at index 1 */ +/* @@? 68:18 Error TypeError: No matching call signature for fromValue(Double) */ +/* @@? 68:39 Error TypeError: Type 'Double' is not compatible with type 'Int' at index 1 */ +/* @@? 69:18 Error TypeError: No matching call signature for fromValue(Double) */ +/* @@? 69:39 Error TypeError: Type 'Double' is not compatible with type 'Int' at index 1 */ + +/* @@? 98:9 Error TypeError: Cannot cast type 'STR_ENUM_1' to 'String' */ +/* @@? 99:18 Error TypeError: Cannot cast type 'String' to 'STR_ENUM_1' */ + +/* @@? 101:18 Error TypeError: Type 'STR_ENUM_1' cannot be assigned to type 'STR_ENUM_2' */ +/* @@? 102:18 Error TypeError: Cannot cast type 'STR_ENUM_1' to 'STR_ENUM_2' */ + +/* @@? 105:18 Error TypeError: Type 'String' cannot be assigned to type 'STR_ENUM_1' */ +/* @@? 106:18 Error TypeError: Type '"a"' cannot be assigned to type 'STR_ENUM_1' */ + +/* @@? 109:18 Error TypeError: Type 'Int' cannot be assigned to type 'STR_ENUM_1' */ +/* @@? 110:18 Error TypeError: Type 'Double' cannot be assigned to type 'STR_ENUM_1' */ +/* @@? 111:18 Error TypeError: No matching call signature for fromValue(Int) */ +/* @@? 111:39 Error TypeError: Type 'Int' is not compatible with type 'String' at index 1 */ +/* @@? 112:18 Error TypeError: No matching call signature for fromValue(Double) */ +/* @@? 112:39 Error TypeError: Type 'Double' is not compatible with type 'String' at index 1 */ diff --git a/ets2panda/test/ast/compiler/ets/local_enum02.ets b/ets2panda/test/ast/compiler/ets/local_enum02.ets index 43b006318a74b9acfae22a330d6f9e6b6214b542..4d3edcf233960f5c0954ae8a22f57a353015c250 100644 --- a/ets2panda/test/ast/compiler/ets/local_enum02.ets +++ b/ets2panda/test/ast/compiler/ets/local_enum02.ets @@ -19,11 +19,8 @@ function main() { { enum Foo {Baz, Bar} - arktest.assertEQ(Foo.Bar as int, 1) + arktest.assertEQ(Foo.Bar, 1) } - arktest.assertEQ(Foo.Bar as int, 0) + arktest.assertEQ(Foo.Bar, 0) } - -/* @@? 21:7 Error SyntaxError: Illegal start of ENUM expression. */ -/* @@? 22:24 Warning Warning: Enum cast is deprecated. Cast support from 'Foo' to 'Int' will be removed. */ diff --git a/ets2panda/test/ast/parser/ets/FixedArray/enum11.ets b/ets2panda/test/ast/parser/ets/FixedArray/enum11.ets index 44f291da40da477a7b0108f54aa3a5b88ac41c43..2036d75b477b4471cee9e2bb79473462283884a2 100644 --- a/ets2panda/test/ast/parser/ets/FixedArray/enum11.ets +++ b/ets2panda/test/ast/parser/ets/FixedArray/enum11.ets @@ -23,6 +23,7 @@ function main(): void { let ordinalFail: int = /* @@ label */Color as int; } -/* @@@ 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. */ +/* @@? 21:22 Error TypeError: Cannot cast type 'Color' to 'Int' */ +/* @@? 22:13 Error TypeError: Cannot cast type 'Int' to 'Color' */ +/* @@@ label Error TypeError: Enum name 'Color' used in the wrong context */ +/* @@@ label Error TypeError: Cannot cast type 'Color' to 'Int' */ diff --git a/ets2panda/test/ast/parser/ets/enum.ets b/ets2panda/test/ast/parser/ets/enum.ets index 3443eb3f6c38ac0318e3b1f6bdbc74e170d7ec40..6b1532347b4a879658779f36b0b8a5c4df7a5da5 100644 --- a/ets2panda/test/ast/parser/ets/enum.ets +++ b/ets2panda/test/ast/parser/ets/enum.ets @@ -66,4 +66,6 @@ function main(): void { } } -/* @@? 26:10 Warning Warning: Enum cast is deprecated. Cast support from 'Color' to 'Int' will be removed. */ +/* @@? 26:10 Error TypeError: Cannot cast type 'Color' to 'Int' */ +/* @@? 38:22 Error TypeError: Cannot cast type 'Color' to 'Int' */ +/* @@? 40:29 Error TypeError: Cannot cast type 'Color' to 'Int' */ diff --git a/ets2panda/test/ast/parser/ets/enum11.ets b/ets2panda/test/ast/parser/ets/enum11.ets index c78d967491c138158f757858b21bd5b205dc833c..3f0620cd37772ee74fa47def2420d52b21d2f038 100644 --- a/ets2panda/test/ast/parser/ets/enum11.ets +++ b/ets2panda/test/ast/parser/ets/enum11.ets @@ -23,6 +23,7 @@ function main(): void { let ordinalFail: int = /* @@ label */Color as int; } -/* @@@ 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. */ +/* @@? 21:22 Error TypeError: Cannot cast type 'Color' to 'Int' */ +/* @@? 22:13 Error TypeError: Cannot cast type 'Int' to 'Color' */ +/* @@@ label Error TypeError: Enum name 'Color' used in the wrong context */ +/* @@@ label Error TypeError: Cannot cast type 'Color' to 'Int' */ diff --git a/ets2panda/test/runtime/ets/Enum.ets b/ets2panda/test/runtime/ets/Enum.ets index b2826bde7fc7a61153249a3e8590346bbf707de5..c46ef47840b3d5d623a77eb793b257ac35905c0b 100644 --- a/ets2panda/test/runtime/ets/Enum.ets +++ b/ets2panda/test/runtime/ets/Enum.ets @@ -23,7 +23,7 @@ class A { } function colorToInt(c: Color): int { - return c as int; + return c; } function main(): void { @@ -35,9 +35,9 @@ function main(): void { arktest.assertEQ(red, red2) arktest.assertEQ(Color.Red, red2) - let red_int: int = red as int; + let red_int: int = red; arktest.assertEQ(red_int, 0) - arktest.assertEQ(red_int, Color.Red as int) + arktest.assertEQ(red_int, Color.Red) let a: A = new A(red); arktest.assertEQ(a.c, Color.Red) diff --git a/ets2panda/test/runtime/ets/Enum2.ets b/ets2panda/test/runtime/ets/Enum2.ets index 2a2af8c723716ab6889797fefccb4e0563411429..3c1ec26a58c7850f352995b65016f83f57e5cffa 100644 --- a/ets2panda/test/runtime/ets/Enum2.ets +++ b/ets2panda/test/runtime/ets/Enum2.ets @@ -21,9 +21,9 @@ class ColorEnumDelegate { public static values(): Color[] { return ColorEnumDelegate.values_; } - public static ordinal(x: Color): int { return x as int; } + public static ordinal(x: Color): int { return x; } - public static toString(x: Color): String { return ColorEnumDelegate.names_[x as int]; } + public static toString(x: Color): String { return ColorEnumDelegate.names_[x]; } public static valueOf(name: String): Color { for (let i = 0; i < ColorEnumDelegate.values_.length; i++) { diff --git a/ets2panda/test/runtime/ets/Enum3.ets b/ets2panda/test/runtime/ets/Enum3.ets index 150f7e76c6e6928ef87ffbb4a3b84972834b9a4b..779a5a59cdf9e873dfc838bcf74ae07fc46af497 100644 --- a/ets2panda/test/runtime/ets/Enum3.ets +++ b/ets2panda/test/runtime/ets/Enum3.ets @@ -48,17 +48,16 @@ function main(): void { } catch (e) {} let one: int = 1; - let green = one as Color; + let green = Color.fromValue(one); arktest.assertEQ(green, Color.Green) try { - let x = 5 as Color; + let x = Color.fromValue(5); arktest.assertTrue( false) } catch (e: Error) { arktest.assertTrue( (e as Object).toString().startsWith("Error: No enum Color with value 5")) } - arktest.assertEQ(2 as Color as int, 2) - arktest.assertEQ(Color.Blue as int as Color, Color.Blue) - arktest.assertEQ((Color.Red as int + 1) as Color, (Color.Blue as int - 1) as Color) + arktest.assertEQ(Color.fromValue(2), 2) + arktest.assertEQ(Color.fromValue(Color.Red + 1), Color.fromValue(Color.Blue - 1)) } diff --git a/ets2panda/test/runtime/ets/Enum4.ets b/ets2panda/test/runtime/ets/Enum4.ets index bfac7dcebc85a435ecec9896a98e2b461b0d6625..b37797f5c4e59c86db596d32c20d1a50206c4d8d 100644 --- a/ets2panda/test/runtime/ets/Enum4.ets +++ b/ets2panda/test/runtime/ets/Enum4.ets @@ -44,7 +44,7 @@ function main(): void { let red1: Color = Color.Red; let red2: Color = Color.Green; - arktest.assertEQ(red2 as int, 0) + arktest.assertEQ(red2, 0) arktest.assertEQ(red2.valueOf(), 0) arktest.assertEQ(red2.getName(), "Green") arktest.assertEQ(red2.toString(), "0") @@ -62,12 +62,12 @@ function main(): void { //cons.print( "Ordinal (as int): Color.Red = " + Color.Red as int + ", = Red = " + red1 as int + ", ValueOf(Red) = " + red2 as int + "\n") //cons.print( "ToString: Color.Red = '" + Color.Red.toString() + "', = Red = '" + red1.toString() + "', ValueOf(Red) = '" + red2.toString() + "'\n") - arktest.assertEQ(red1 as int, 2) + arktest.assertEQ(red1, 2) arktest.assertEQ(red1.valueOf(), 2) arktest.assertEQ(red1.getName(), "Red") arktest.assertEQ(red1.toString(), "2") - arktest.assertEQ(red2 as int, 2) + arktest.assertEQ(red2, 2) arktest.assertEQ(red2.valueOf(), 2) arktest.assertEQ(red2.getName(), "Red") arktest.assertEQ(red2.toString(), "2") @@ -77,8 +77,8 @@ function main(): void { arktest.assertEQ(red2, red1) let ord: int = 3; - blue = ord as Color; - arktest.assertEQ(blue as int, 3) + blue = Color.fromValue(ord); + arktest.assertEQ(blue, 3) arktest.assertEQ(blue.valueOf(), 3) arktest.assertEQ(blue.getName(), "Blue") arktest.assertEQ(blue.toString(), "3") @@ -91,22 +91,21 @@ function main(): void { } catch (e) {} ord = 0; - let green: Color = ord as Color; + let green: Color = Color.fromValue(ord); //cons.print("Green = " + green as int + ", Color.Green = " + Color.Green as int + "\n") arktest.assertEQ(green, Color.Green) try { - let x = 5 as Color; + let x = Color.fromValue(5); arktest.assertTrue( false) } catch (e: Error) { arktest.assertTrue( (e as Object).toString().startsWith("Error: No enum Color with value 5")) } - arktest.assertEQ( 2 as Color as int, 2) - arktest.assertEQ( Color.Blue as int as Color, Color.Blue) - arktest.assertEQ( (Color.Green as int + 2) as Color, (Color.Blue as int - 1) as Color) + arktest.assertEQ( Color.fromValue(2), 2) + arktest.assertEQ( Color.fromValue(Color.Green + 2), Color.fromValue(Color.Blue - 1)) try { - arktest.assertEQ((Color.Green as int + 1) as Color, Color.getValueOf("Red")) + arktest.assertEQ(Color.fromValue(Color.Green + 1), Color.getValueOf("Red")) } catch (e) { } } diff --git a/ets2panda/test/runtime/ets/Enum5.ets b/ets2panda/test/runtime/ets/Enum5.ets index c765271af61c591276d4cd6e795e06b582498f6c..ea29e96be4062c6ff98ae39d4222d632e92a9d63 100644 --- a/ets2panda/test/runtime/ets/Enum5.ets +++ b/ets2panda/test/runtime/ets/Enum5.ets @@ -42,7 +42,7 @@ function main(): void { let red1: Color = Color.Red; let red2: Color = Color.Green; - arktest.assertEQ(red2 as string, "red") + arktest.assertEQ(red2, "red") arktest.assertEQ(red2.valueOf(), "red") arktest.assertEQ(red2.getName(), "Green") arktest.assertEQ(red2.toString(), "red") @@ -53,12 +53,12 @@ function main(): void { arktest.assertTrue( (e as Object).toString().startsWith("No enum constant Color.Red")) } - arktest.assertEQ(red1 as string, "blue") + arktest.assertEQ(red1, "blue") arktest.assertEQ(red1.valueOf(), "blue") arktest.assertEQ(red1.getName(), "Red") arktest.assertEQ(red1.toString(), "blue") - arktest.assertEQ(red2 as string, "blue") + arktest.assertEQ(red2, "blue") arktest.assertEQ(red2.valueOf(), "blue") arktest.assertEQ(red2.getName(), "Red") arktest.assertEQ(red2.toString(), "blue") @@ -68,8 +68,8 @@ function main(): void { arktest.assertEQ(red2, red1) let ord: string = "green"; - blue = ord as Color; - arktest.assertEQ(blue as string, "green") + blue = Color.fromValue(ord); + arktest.assertEQ(blue, "green") arktest.assertEQ(blue.valueOf(), "green") arktest.assertEQ(blue.getName(), "Blue") arktest.assertEQ(blue.toString(), "green") @@ -82,20 +82,19 @@ function main(): void { } catch (e) {} ord = "red"; - let green: Color = ord as Color; + let green: Color = Color.fromValue(ord); arktest.assertEQ( green, Color.Green) try { - let x = "a" as Color; + let x = Color.fromValue("a"); arktest.assertTrue( false) } catch (e: Error) { arktest.assertTrue( (e as Object).toString().startsWith("Error: No enum Color with value a")) } - arktest.assertEQ( "red" as Color as string, "red") - arktest.assertEQ( Color.Blue as string as Color, Color.Blue) + arktest.assertEQ( Color.fromValue("red"), "red") try { - arktest.assertEQ( (Color.Green as string) as Color, Color.getValueOf("red")) + arktest.assertEQ( Color.fromValue(Color.Green), Color.getValueOf("red")) } catch (e) { } } diff --git a/ets2panda/test/runtime/ets/enumConversions.ets b/ets2panda/test/runtime/ets/enumConversions.ets new file mode 100644 index 0000000000000000000000000000000000000000..60122f7b9c7a8b7207518dd0a10fec66884134d5 --- /dev/null +++ b/ets2panda/test/runtime/ets/enumConversions.ets @@ -0,0 +1,98 @@ +/* + * 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 INT_ENUM_1 { + a, b, c, d, e, f, g, h, k +} + +enum INT_ENUM_2 { + b, c, d, e +} + +enum STR_ENUM_1 { + a = "a", b = "b", c = "c" +} + +enum STR_ENUM_2 { + b = "b", c = "c" +} + +function int_enum_conversions(int_enum_1: INT_ENUM_1, int_enum_2: INT_ENUM_2, i: int, l: long, s: short, n: number, + d: double) +{ + int_enum_1 = int_enum_1 + let new_enum_1 = int_enum_1 as INT_ENUM_1 + let new_enum_2: (INT_ENUM_1 | number) = int_enum_1 as INT_ENUM_1 + arktest.assertEQ(int_enum_1, INT_ENUM_1.a); + + int_enum_1 = INT_ENUM_1.fromValue(int_enum_2) + arktest.assertEQ(int_enum_1, INT_ENUM_1.b); + + int_enum_1 = INT_ENUM_1.fromValue(i) + arktest.assertEQ(int_enum_1, INT_ENUM_1.c); + + int_enum_1 = INT_ENUM_1.fromValue(Long.toInt(l)) + arktest.assertEQ(int_enum_1, INT_ENUM_1.d); + + int_enum_1 = INT_ENUM_1.fromValue(s) + arktest.assertEQ(int_enum_1, INT_ENUM_1.e); + + int_enum_1 = INT_ENUM_1.fromValue(Double.toInt(n)) + arktest.assertEQ(int_enum_1, INT_ENUM_1.f); + + int_enum_1 = INT_ENUM_1.fromValue(Double.toInt(d)) + arktest.assertEQ(int_enum_1, INT_ENUM_1.g); + + int_enum_1 = INT_ENUM_1.fromValue(1) + arktest.assertEQ(int_enum_1, INT_ENUM_1.b); + + int_enum_1 = INT_ENUM_1.fromValue(Double.toInt(0.0)) + arktest.assertEQ(int_enum_1, INT_ENUM_1.a); + + // Widening: + i = int_enum_1 + arktest.assertEQ(i, 0); + l = int_enum_1 + arktest.assertEQ(l, 0); + n = int_enum_1 + arktest.assertEQ(n, 0); + d = int_enum_1 + arktest.assertEQ(d, 0); +} + +function str_enum_conversions(str_enum_1: STR_ENUM_1, str_enum_2: STR_ENUM_2, s: string) { + str_enum_1 = str_enum_1 + let new_enum_1 = str_enum_1 as STR_ENUM_1 + let new_enum_2: (STR_ENUM_1 | number) = str_enum_1 as STR_ENUM_1 + arktest.assertEQ(str_enum_1, STR_ENUM_1.a); + + str_enum_1 = STR_ENUM_1.fromValue(str_enum_2) + arktest.assertEQ(str_enum_1, STR_ENUM_1.b); + + str_enum_1 = STR_ENUM_1.fromValue(s) + arktest.assertEQ(str_enum_1, STR_ENUM_1.c); + + str_enum_1 = STR_ENUM_1.fromValue("a") + arktest.assertEQ(str_enum_1, STR_ENUM_1.a); + + // Widening: + s = str_enum_1 + arktest.assertEQ(s, "a"); +} + +function main(): void { + int_enum_conversions(INT_ENUM_1.a, INT_ENUM_2.c, 2, 3, 4, 5.0, 6.0) + str_enum_conversions(STR_ENUM_1.a, STR_ENUM_2.b, "c") +} diff --git a/ets2panda/test/runtime/ets/enum_as_expression_cast.ets b/ets2panda/test/runtime/ets/enum_as_expression_cast.ets index 18c270aa302b3b4d1afc5d737d4230b9cf39a3b6..7d5d97ec16dc9a81d36ff99eac1b775d9170e7c4 100644 --- a/ets2panda/test/runtime/ets/enum_as_expression_cast.ets +++ b/ets2panda/test/runtime/ets/enum_as_expression_cast.ets @@ -20,9 +20,9 @@ enum E { function main() { arktest.assertEQ(E.A.toString(), "100") // E.A.toString() - arktest.assertEQ(E.A as int, 100) // cast(E.A) to int, E.A is a index + arktest.assertEQ(E.A, 100) // cast(E.A) to int, E.A is a index arktest.assertEQ(E.B.toString(), "200") // E.B.toString() arktest.assertEQ(Int.toDouble(E.B), 200) // cast(E.B) to number, E.B is a index - arktest.assertEQ(E.A.valueOf() as int, 100) // get value of E.A + arktest.assertEQ(E.A.valueOf(), 100) // get value of E.A arktest.assertEQ(Int.toDouble(E.B.valueOf()), 200) // get value of E.B } \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/enum_conditional_expression.ets b/ets2panda/test/runtime/ets/enum_conditional_expression.ets index 4b4dd493ef23b3f6cc0946fe8c14a4d05f5afa72..36ce7158d8a0692ee77ab8f74da6ae0802f65e6e 100644 --- a/ets2panda/test/runtime/ets/enum_conditional_expression.ets +++ b/ets2panda/test/runtime/ets/enum_conditional_expression.ets @@ -23,5 +23,5 @@ function main(){ const isPauseReq:boolean = true; const onPausePolicy: OnPause = OnPause.Reset const onPause: OnPause = isPauseReq ? onPausePolicy : OnPause.Nothing; - arktest.assertEQ(onPause as int, 1) + arktest.assertEQ(onPause, 1) } \ No newline at end of file diff --git a/ets2panda/test/runtime/ets/enum_namespace.ets b/ets2panda/test/runtime/ets/enum_namespace.ets index 3d7fea8a818038e2d0a271711b9d6cd3f877dde4..587d4279bb5f880e0c2665f4727851cd8f9c78fb 100644 --- a/ets2panda/test/runtime/ets/enum_namespace.ets +++ b/ets2panda/test/runtime/ets/enum_namespace.ets @@ -36,6 +36,6 @@ function foo(a : boolean) : NSOuter.NSInner.XXXType | number | string { function main() { const res : NSOuter.NSInner.XXXType = foo(true) as NSOuter.NSInner.XXXType arktest.assertEQ(res, NSOuter.NSInner.XXXType.TAP) - const res1 : NSOuter.NSInner.XXXType = 1 as NSOuter.NSInner.XXXType + const res1 : NSOuter.NSInner.XXXType = NSOuter.NSInner.XXXType.fromValue(1) arktest.assertEQ(res1, NSOuter.NSInner.XXXType.PAT) } diff --git a/ets2panda/test/runtime/ets/enum_union.ets b/ets2panda/test/runtime/ets/enum_union.ets index f3d736c628d5cd2fc82b33ad229d4fa20cc6525c..50054b42b43c336da3df8176216c89ea00c520fd 100644 --- a/ets2panda/test/runtime/ets/enum_union.ets +++ b/ets2panda/test/runtime/ets/enum_union.ets @@ -37,16 +37,16 @@ function foo1(b : boolean) : int | Color | string | ColorInt { } function testOptional(b ?: ColorInt | int | string) : int { - const v1 : int = b ? b as int : 0; + const v1 : int = b ? (b as ColorInt) : 0; return v1; } function main() { const v1: Color = foo(false) as Color - const v2: ColorInt = foo(true) as ColorInt - const v3: string = foo(false) as string - const v4: int = foo1(false) as int - const v5: Color = foo1(true) as Color + const v2: ColorInt = ColorInt.fromValue(foo(true) as int) + const v3: string = foo(false) as Color + const v4: int = foo1(false) as ColorInt + const v5: Color = Color.fromValue(foo1(true) as string) const v6: ColorInt = foo1(false) as ColorInt arktest.assertEQ(v1, Color.Red) diff --git a/ets2panda/test/runtime/ets/import_self_head_tests/A/test.ets b/ets2panda/test/runtime/ets/import_self_head_tests/A/test.ets index bf88298a2c9c3f065d5d5fb4e755a0091a1e0791..05319718fb88ca661f5f49b6439901ee03acc857 100644 --- a/ets2panda/test/runtime/ets/import_self_head_tests/A/test.ets +++ b/ets2panda/test/runtime/ets/import_self_head_tests/A/test.ets @@ -21,6 +21,6 @@ namespace NS { } let cc:NS.Color = NS.Color.RED; -arktest.assertEQ(cc as Int, 1); +arktest.assertEQ(cc, 1); export default NS diff --git a/ets2panda/test/runtime/ets/local_enum01.ets b/ets2panda/test/runtime/ets/local_enum01.ets index 6054274fadd1fa7e7b403ca97886161b18b3b4dc..4f071a4f5758a06ff9c3b9fd5929c92eb636fc74 100644 --- a/ets2panda/test/runtime/ets/local_enum01.ets +++ b/ets2panda/test/runtime/ets/local_enum01.ets @@ -16,6 +16,6 @@ enum Foo {Bar, Baz} function main() { - arktest.assertEQ(Foo.Bar as int, 0) - arktest.assertEQ(Foo.Baz as int, 1) + arktest.assertEQ(Foo.Bar, 0) + arktest.assertEQ(Foo.Baz, 1) } diff --git a/ets2panda/test/runtime/ets/overload_declaration/Enum2.ets b/ets2panda/test/runtime/ets/overload_declaration/Enum2.ets index 9d25d2fc0ace2fe06bdbc1c7b724ed6d80ac36d3..576d5ab0b277be0b911e92b5446320b30201cb92 100644 --- a/ets2panda/test/runtime/ets/overload_declaration/Enum2.ets +++ b/ets2panda/test/runtime/ets/overload_declaration/Enum2.ets @@ -21,9 +21,9 @@ class ColorEnumDelegate { public static values(): Color[] { return ColorEnumDelegate.values_; } - public static ordinal(x: Color): int { return x as int; } + public static ordinal(x: Color): int { return x; } - public static toString(x: Color): String { return ColorEnumDelegate.names_[x as int]; } + public static toString(x: Color): String { return ColorEnumDelegate.names_[x]; } public static valueOf1(name: String): Color { for (let i = 0; i < ColorEnumDelegate.values_.length; i++) { diff --git a/ets2panda/util/diagnostic/warning.yaml b/ets2panda/util/diagnostic/warning.yaml index 59820b70e1b65d13f7b71458dd71c7034672e399..5784d63e12c4c341f7b5660abde2db89aea0200f 100644 --- a/ets2panda/util/diagnostic/warning.yaml +++ b/ets2panda/util/diagnostic/warning.yaml @@ -33,10 +33,6 @@ 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): {}" @@ -123,4 +119,5 @@ graveyard: - 19 - 27 - 28 +- 30 # See ets_frontend/ets2panda/util/diagnostic/README.md before contributing.