From dbdf0174b4a7947fabb59e275f2ffb5bf494451a Mon Sep 17 00:00:00 2001 From: cdemirer <41021322+cdemirer@users.noreply.github.com> Date: Mon, 4 Jul 2022 12:30:39 +0300 Subject: Do error when for variable conflicts with a variable in scope --- modules/gdscript/gdscript_parser.cpp | 4 ++++ .../tests/scripts/parser/errors/variable_conflicts_for_variable.gd | 4 ++++ .../tests/scripts/parser/errors/variable_conflicts_for_variable.out | 2 ++ .../tests/scripts/parser/errors/variable_conflicts_variable.gd | 3 +++ .../tests/scripts/parser/errors/variable_conflicts_variable.out | 2 ++ 5 files changed, 15 insertions(+) create mode 100644 modules/gdscript/tests/scripts/parser/errors/variable_conflicts_for_variable.gd create mode 100644 modules/gdscript/tests/scripts/parser/errors/variable_conflicts_for_variable.out create mode 100644 modules/gdscript/tests/scripts/parser/errors/variable_conflicts_variable.gd create mode 100644 modules/gdscript/tests/scripts/parser/errors/variable_conflicts_variable.out (limited to 'modules/gdscript') diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index ca430b0f72..80fb9c8415 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -1748,6 +1748,10 @@ GDScriptParser::ForNode *GDScriptParser::parse_for() { SuiteNode *suite = alloc_node(); if (n_for->variable) { + const SuiteNode::Local &local = current_suite->get_local(n_for->variable->name); + if (local.type != SuiteNode::Local::UNDEFINED) { + push_error(vformat(R"(There is already a %s named "%s" declared in this scope.)", local.get_name(), n_for->variable->name), n_for->variable); + } suite->add_local(SuiteNode::Local(n_for->variable, current_function)); } suite->parent_for = n_for; diff --git a/modules/gdscript/tests/scripts/parser/errors/variable_conflicts_for_variable.gd b/modules/gdscript/tests/scripts/parser/errors/variable_conflicts_for_variable.gd new file mode 100644 index 0000000000..409da11051 --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/errors/variable_conflicts_for_variable.gd @@ -0,0 +1,4 @@ +func test(): + var TEST = 1 + for TEST in 2: + pass diff --git a/modules/gdscript/tests/scripts/parser/errors/variable_conflicts_for_variable.out b/modules/gdscript/tests/scripts/parser/errors/variable_conflicts_for_variable.out new file mode 100644 index 0000000000..407f094ca0 --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/errors/variable_conflicts_for_variable.out @@ -0,0 +1,2 @@ +GDTEST_PARSER_ERROR +There is already a variable named "TEST" declared in this scope. diff --git a/modules/gdscript/tests/scripts/parser/errors/variable_conflicts_variable.gd b/modules/gdscript/tests/scripts/parser/errors/variable_conflicts_variable.gd new file mode 100644 index 0000000000..b353fd1288 --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/errors/variable_conflicts_variable.gd @@ -0,0 +1,3 @@ +func test(): + var TEST = 1 + var TEST = 2 diff --git a/modules/gdscript/tests/scripts/parser/errors/variable_conflicts_variable.out b/modules/gdscript/tests/scripts/parser/errors/variable_conflicts_variable.out new file mode 100644 index 0000000000..407f094ca0 --- /dev/null +++ b/modules/gdscript/tests/scripts/parser/errors/variable_conflicts_variable.out @@ -0,0 +1,2 @@ +GDTEST_PARSER_ERROR +There is already a variable named "TEST" declared in this scope. -- cgit v1.2.3