From 15c0a7374050cded41bf00eaa90be07d7f1c6055 Mon Sep 17 00:00:00 2001 From: Makhnev Petr <51853996+i582@users.noreply.github.com> Date: Thu, 26 Jan 2023 14:31:35 +0400 Subject: [PATCH] checker: fixed error pointer for "redefinition of key iteration variable" and "redefinition of value iteration variable" errors (#17121) --- vlib/v/checker/tests/for_in_key_redefinition.out | 7 +++++++ vlib/v/checker/tests/for_in_key_redefinition.vv | 6 ++++++ vlib/v/checker/tests/for_in_value_redefinition.out | 7 +++++++ vlib/v/checker/tests/for_in_value_redefinition.vv | 6 ++++++ vlib/v/parser/for.v | 6 ++++-- 5 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 vlib/v/checker/tests/for_in_key_redefinition.out create mode 100644 vlib/v/checker/tests/for_in_key_redefinition.vv create mode 100644 vlib/v/checker/tests/for_in_value_redefinition.out create mode 100644 vlib/v/checker/tests/for_in_value_redefinition.vv diff --git a/vlib/v/checker/tests/for_in_key_redefinition.out b/vlib/v/checker/tests/for_in_key_redefinition.out new file mode 100644 index 0000000000..fd973fbe3f --- /dev/null +++ b/vlib/v/checker/tests/for_in_key_redefinition.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/for_in_key_redefinition.vv:4:5: error: redefinition of key iteration variable `value` + 2 | value := 2 + 3 | + 4 | for value, _ in arr { + | ~~~~~ + 5 | println(value) + 6 | } diff --git a/vlib/v/checker/tests/for_in_key_redefinition.vv b/vlib/v/checker/tests/for_in_key_redefinition.vv new file mode 100644 index 0000000000..bda6b35735 --- /dev/null +++ b/vlib/v/checker/tests/for_in_key_redefinition.vv @@ -0,0 +1,6 @@ +arr := [1,2,3] +value := 2 + +for value, _ in arr { + println(value) +} diff --git a/vlib/v/checker/tests/for_in_value_redefinition.out b/vlib/v/checker/tests/for_in_value_redefinition.out new file mode 100644 index 0000000000..150ea1f7eb --- /dev/null +++ b/vlib/v/checker/tests/for_in_value_redefinition.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/for_in_value_redefinition.vv:4:8: error: redefinition of value iteration variable `value` + 2 | value := 2 + 3 | + 4 | for _, value in arr { + | ~~~~~ + 5 | println(value) + 6 | } diff --git a/vlib/v/checker/tests/for_in_value_redefinition.vv b/vlib/v/checker/tests/for_in_value_redefinition.vv new file mode 100644 index 0000000000..eceab27b2e --- /dev/null +++ b/vlib/v/checker/tests/for_in_value_redefinition.vv @@ -0,0 +1,6 @@ +arr := [1,2,3] +value := 2 + +for _, value in arr { + println(value) +} diff --git a/vlib/v/parser/for.v b/vlib/v/parser/for.v index 843cbed200..53a30e8247 100644 --- a/vlib/v/parser/for.v +++ b/vlib/v/parser/for.v @@ -121,10 +121,12 @@ fn (mut p Parser) for_stmt() ast.Stmt { val_var_pos) } if p.scope.known_var(key_var_name) { - return p.error('redefinition of key iteration variable `${key_var_name}`') + return p.error_with_pos('redefinition of key iteration variable `${key_var_name}`', + key_var_pos) } if p.scope.known_var(val_var_name) { - return p.error('redefinition of value iteration variable `${val_var_name}`') + return p.error_with_pos('redefinition of value iteration variable `${val_var_name}`', + val_var_pos) } p.scope.register(ast.Var{ name: key_var_name