From ad35f135a3d9b4bc0417e934a4d721f480d0e01b Mon Sep 17 00:00:00 2001 From: Mingyang Date: Wed, 23 Jul 2025 16:00:54 +0800 Subject: [PATCH] Fix nested var redeclaration crash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICOB1Z Reason: FindLocal only searches the current scope, so inner `var x` couldn’t see an existing outer `let x` or function‐scoped `var x`, causing AddBinding to return nullptr and trigger an assertion failure. Description: In `Scope::AddDecl`, when declaration is var-declaration, add `Find(decl->Name(), ResolveBindingOptions::BINDINGS).variable` to perform a full scope‐chain lookup. This ensures redeclarations throw an error instead of crashing. Tests: ninja tests passed tests/tests-u-runner/runner.sh --ets-cts --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --ets-func-tests --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --astchecker --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --ets-runtime --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --parser --no-js --show-progress --build-dir x64.release --processes=all passed Signed-off-by: Mingyang --- .../compiler/ets/nested_var_declaration.ets | 24 +++++++++++++++++++ ets2panda/varbinder/varbinder.h | 4 ++++ 2 files changed, 28 insertions(+) create mode 100644 ets2panda/test/ast/compiler/ets/nested_var_declaration.ets diff --git a/ets2panda/test/ast/compiler/ets/nested_var_declaration.ets b/ets2panda/test/ast/compiler/ets/nested_var_declaration.ets new file mode 100644 index 0000000000..fc663fbc08 --- /dev/null +++ b/ets2panda/test/ast/compiler/ets/nested_var_declaration.ets @@ -0,0 +1,24 @@ +/* + * 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. + */ + +function main() { + let x = 1; + { + var x = 1; + } +} + +/* @@? 19:13 Error SyntaxError: 'var' keyword is not supported. Use 'let' instead. */ +/* @@? 19:13 Error TypeError: Variable 'x' has already been declared. */ diff --git a/ets2panda/varbinder/varbinder.h b/ets2panda/varbinder/varbinder.h index ff674f639b..65ca0bc652 100644 --- a/ets2panda/varbinder/varbinder.h +++ b/ets2panda/varbinder/varbinder.h @@ -427,6 +427,10 @@ std::tuple VarBinder::NewVarDecl(const lexer::Source var = scope_->FindLocal(decl->Name(), ResolveBindingOptions::BINDINGS); } + if (var == nullptr && decl->IsVarDecl()) { + var = scope_->Find(decl->Name(), ResolveBindingOptions::BINDINGS).variable; + } + ES2PANDA_ASSERT(var != nullptr); return {decl, var}; } -- Gitee