diff --git a/ets2panda/compiler/core/ETSCompiler.cpp b/ets2panda/compiler/core/ETSCompiler.cpp index 75fd7d3de505e7bba5f6d34adae7e5d6b9110aeb..c9cd08f04cfaeee81fe493e404600cde859d02b0 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 0a724cf0d7e6f21d80a75d3dfc640b4b1a9d6e4e..7fa5f683506ebccf2e5841f5b6411565277920ed 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