From 07d76e9a30c5019b32f668ab2675fe39ab321080 Mon Sep 17 00:00:00 2001 From: anjiaqi Date: Sat, 6 Sep 2025 19:29:49 +0800 Subject: [PATCH] fix null type reference in Namespace Import Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICWPXK Signed-off-by: anjiaqi --- ets2panda/checker/ETSAnalyzer.cpp | 3 +++ ets2panda/checker/ets/helpers.cpp | 3 +-- ets2panda/varbinder/varbinder.h | 30 ++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 80da256ea0..f58ba747b6 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -3123,6 +3123,9 @@ checker::Type *ETSAnalyzer::Check(ir::ImportNamespaceSpecifier *st) const ES2PANDA_UNREACHABLE(); } + auto topScopeCtx = + varbinder::TopScopeContext(checker->VarBinder(), importDecl->Parent()->AsETSModule()->Scope()->AsGlobalScope()); + if (importDecl->IsPureDynamic()) { auto *type = checker->GetImportSpecifierObjectType(importDecl, st->Local()->AsIdentifier())->AsETSObjectType(); checker->SetrModuleObjectTsType(st->Local(), type); diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 77e246ead2..9cbdb70527 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -1857,12 +1857,11 @@ void ETSChecker::SetPropertiesForModuleObject(checker::ETSObjectType *moduleObjT RemoveStatus(CheckerStatus::IN_EXTERNAL); } auto savedProgram = Program(); + auto topScopeCtx = varbinder::TopScopeContext(VarBinder(), program->GlobalScope()); VarBinder()->AsETSBinder()->SetProgram(program); - VarBinder()->AsETSBinder()->ResetTopScope(program->GlobalScope()); program->SetASTChecked(); program->Ast()->Check(this); VarBinder()->AsETSBinder()->SetProgram(savedProgram); - VarBinder()->AsETSBinder()->ResetTopScope(savedProgram->GlobalScope()); } BindingsModuleObjectAddProperty( diff --git a/ets2panda/varbinder/varbinder.h b/ets2panda/varbinder/varbinder.h index ff674f639b..e5e215d743 100644 --- a/ets2panda/varbinder/varbinder.h +++ b/ets2panda/varbinder/varbinder.h @@ -17,6 +17,7 @@ #define ES2PANDA_VARBINDER_VARBINDER_H #include "lexer/token/sourceLocation.h" +#include "scope.h" #include "varbinder/scope.h" namespace ark::es2panda::parser { @@ -176,6 +177,7 @@ public: template friend class LexicalScope; + friend class TopScopeContext; [[nodiscard]] ArenaAllocator *Allocator() const noexcept { @@ -430,5 +432,33 @@ std::tuple VarBinder::NewVarDecl(const lexer::Source ES2PANDA_ASSERT(var != nullptr); return {decl, var}; } + +class TopScopeContext { +public: + TopScopeContext(VarBinder *varbinder, GlobalScope *globalScope) + : varbinder_(varbinder), + prevTopscope_(varbinder->TopScope()), + prevScope_(varbinder->GetScope()), + prevVarScope_(varbinder->VarScope()) + { + varbinder->topScope_ = globalScope; + varbinder->scope_ = globalScope; + varbinder->varScope_ = globalScope; + } + + ~TopScopeContext() + { + varbinder_->topScope_ = prevTopscope_; + varbinder_->scope_ = prevScope_; + varbinder_->varScope_ = prevVarScope_; + } + +private: + VarBinder *varbinder_ {}; + GlobalScope *prevTopscope_ {}; + Scope *prevScope_ {}; + VariableScope *prevVarScope_ {}; +}; + } // namespace ark::es2panda::varbinder #endif -- Gitee