From 815f30d7f615f01c8d22c44f47cb3cb43483301c Mon Sep 17 00:00:00 2001 From: Vsevolod Pukhov Date: Mon, 21 Jul 2025 14:10:19 +0300 Subject: [PATCH] Simplify reference narrowing codegen Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICNT2P Signed-off-by: Vsevolod Pukhov --- ets2panda/compiler/core/ETSCompiler.cpp | 8 +- ets2panda/compiler/core/ETSGen.cpp | 130 +++++++++--------- ets2panda/compiler/core/ETSGen.h | 3 +- ets2panda/compiler/scripts/signatures.yaml | 6 - ets2panda/test/runtime/ets/UncheckedCasts.ets | 5 + ...e_initialization_with_generic_field_02.ets | 2 +- ets2panda/test/unit/union_emit_test.cpp | 5 - 7 files changed, 79 insertions(+), 80 deletions(-) diff --git a/ets2panda/compiler/core/ETSCompiler.cpp b/ets2panda/compiler/core/ETSCompiler.cpp index 75fd7d3de5..c9cd08f04c 100644 --- a/ets2panda/compiler/core/ETSCompiler.cpp +++ b/ets2panda/compiler/core/ETSCompiler.cpp @@ -828,9 +828,13 @@ void ETSCompiler::Compile(const ir::Identifier *expr) const } if (smartType->IsETSReferenceType()) { + // #28038: there is a hidden bug in LoadVar when a smart-cast was applied on a previous store + etsg->SetAccumulatorType(expr->Variable()->TsType()); + // #28038: incorrect smart-cast interferes with broken `union as undefined` conversion + auto fixupTarget = smartType->IsETSUndefinedType() ? etsg->GetAccumulatorType() : smartType; // In case when smart cast type of identifier differs from initial variable type perform cast if required - if (!etsg->Checker()->AsETSChecker()->Relation()->IsSupertypeOf(smartType, etsg->GetAccumulatorType())) { - etsg->CastToReftype(expr, smartType, false); + if (!etsg->Checker()->AsETSChecker()->Relation()->IsSupertypeOf(fixupTarget, etsg->GetAccumulatorType())) { + etsg->CastToReftype(expr, fixupTarget, false); } } else if (smartType->IsETSPrimitiveType()) { etsg->ApplyConversionCast(expr, smartType); diff --git a/ets2panda/compiler/core/ETSGen.cpp b/ets2panda/compiler/core/ETSGen.cpp index 0a724cf0d7..7fa5f68350 100644 --- a/ets2panda/compiler/core/ETSGen.cpp +++ b/ets2panda/compiler/core/ETSGen.cpp @@ -606,6 +606,8 @@ void ETSGen::TestIsInstanceType(const ir::AstNode *const node, std::tuple