From b64c629237c0faacb3a1d9d68b135bc6e97a6319 Mon Sep 17 00:00:00 2001 From: xuxinjie4 Date: Fri, 15 Aug 2025 11:40:55 +0800 Subject: [PATCH] Fix local scope bug Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICT4DF?from=project-issue Signed-off-by: xuxinjie4 --- .../lowering/scopesInit/scopesInitPhase.cpp | 3 +++ .../ets/re-declare_in_local_scope.ets | 22 +++++++++++++++++++ ets2panda/varbinder/scope.cpp | 2 +- 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/ast/compiler/ets/re-declare_in_local_scope.ets diff --git a/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.cpp b/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.cpp index b97fd7aed2..a8cead5fb7 100644 --- a/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.cpp +++ b/ets2panda/compiler/lowering/scopesInit/scopesInitPhase.cpp @@ -516,6 +516,9 @@ std::tuple ScopesInitPhase::AddOrGetVa case ir::VariableDeclaratorFlag::LET: return VarBinder()->NewVarDecl(id->Start(), name); case ir::VariableDeclaratorFlag::VAR: + if (VarBinder()->IsETSBinder()) { + return VarBinder()->NewVarDecl(id->Start(), name); + } return VarBinder()->NewVarDecl(id->Start(), name); case ir::VariableDeclaratorFlag::CONST: return VarBinder()->NewVarDecl(id->Start(), name); diff --git a/ets2panda/test/ast/compiler/ets/re-declare_in_local_scope.ets b/ets2panda/test/ast/compiler/ets/re-declare_in_local_scope.ets new file mode 100644 index 0000000000..ae18b2d15c --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/re-declare_in_local_scope.ets @@ -0,0 +1,22 @@ +/* + * 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. + */ + +{ + var ten = 1; + let ten = 10; +} + +/* @@? 17:9 Error SyntaxError: 'var' keyword is not supported. Use 'let' instead. */ +/* @@? 18:9 Error TypeError: Variable 'ten' has already been declared. */ \ No newline at end of file diff --git a/ets2panda/varbinder/scope.cpp b/ets2panda/varbinder/scope.cpp index 6edef25fd9..6798b4e030 100644 --- a/ets2panda/varbinder/scope.cpp +++ b/ets2panda/varbinder/scope.cpp @@ -233,7 +233,7 @@ Variable *Scope::AddLocalVar(ArenaAllocator *allocator, Decl *newDecl) return scope->InsertBinding(newDecl->Name(), allocator->New(newDecl, varFlags)).first->second; } - return scope->PropagateBinding(allocator, newDecl->Name(), newDecl, varFlags); + return PropagateBinding(allocator, newDecl->Name(), newDecl, varFlags); } Variable *Scope::AddLocalInterfaceVariable(ArenaAllocator *allocator, Decl *newDecl) -- Gitee