From 824f46ea45ac8410a06b496014c4593ea9308d76 Mon Sep 17 00:00:00 2001 From: Shimenkov Mikhail Date: Mon, 26 May 2025 15:37:01 +0300 Subject: [PATCH] Fix incorrect number literal handling Signed-off-by: Shimenkov Mikhail Change-Id: I61df97977c884944b21a474ab1db332c01a584cf --- .../lowering/ets/constantExpressionLowering.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp b/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp index a608172af..9d109667b 100644 --- a/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp +++ b/ets2panda/compiler/lowering/ets/constantExpressionLowering.cpp @@ -198,8 +198,7 @@ static To CastValTo(const ir::Literal *lit) static bool IsConvertibleToNumericType(const ir::Literal *lit) { - // true if CharLiteral and NumberLiteral - return !(lit->IsStringLiteral() || lit->IsBooleanLiteral() || lit->IsNullLiteral() || lit->IsUndefinedLiteral()); + return lit->IsCharLiteral() || lit->IsNumberLiteral(); } static void LogError(public_lib::Context *context, const diagnostic::DiagnosticKind &diagnostic, @@ -208,6 +207,15 @@ static void LogError(public_lib::Context *context, const diagnostic::DiagnosticK context->diagnosticEngine->LogDiagnostic(diagnostic, diagnosticParams, pos); } +static bool IsCorrectNumberLiteral(const ir::AstNode *lit) +{ + if (!lit->IsNumberLiteral()) { + return false; + } + + return !lit->AsNumberLiteral()->Number().ConversionError(); +} + static bool IsSupportedLiteral(ir::Expression *const node) { if (!node->IsLiteral()) { @@ -215,7 +223,7 @@ static bool IsSupportedLiteral(ir::Expression *const node) } auto literal = node->AsLiteral(); - return literal->IsNumberLiteral() || literal->IsCharLiteral() || literal->IsBooleanLiteral() || + return IsCorrectNumberLiteral(literal) || literal->IsCharLiteral() || literal->IsBooleanLiteral() || literal->IsStringLiteral() || literal->IsUndefinedLiteral() || literal->IsNullLiteral(); } @@ -394,7 +402,7 @@ static ir::PrimitiveType GetRightTypeOfNumberOrCharLiteral(const ir::Literal *li static ir::AstNode *TryToCorrectNumberOrCharLiteral(ir::AstNode *node, public_lib::Context *context) { - if (node->IsNumberLiteral() || node->IsCharLiteral()) { + if (IsCorrectNumberLiteral(node) || node->IsCharLiteral()) { auto lit = node->AsExpression()->AsLiteral(); return CorrectNumberOrCharLiteral(lit, GetRightTypeOfNumberOrCharLiteral(lit), context); } -- Gitee