From 2f97522e12728423f339f90bf70c334d382702c3 Mon Sep 17 00:00:00 2001 From: JaDogg Date: Mon, 19 Jun 2023 06:06:41 +0100 Subject: [PATCH] fix(type_checker,tests): handle redefining variables in type_checker --- compiler/src/compiler/type_checker.cpp | 3 +++ compiler/test_data/bug_fixes/redefining_vars.yaka | 7 +++++++ compiler/test_data/bug_fixes/redefining_vars_params.yaka | 7 +++++++ compiler/tests/test_type_checker.cpp | 8 ++++++++ 4 files changed, 25 insertions(+) create mode 100644 compiler/test_data/bug_fixes/redefining_vars.yaka create mode 100644 compiler/test_data/bug_fixes/redefining_vars_params.yaka diff --git a/compiler/src/compiler/type_checker.cpp b/compiler/src/compiler/type_checker.cpp index 262bbdea..f7c9f758 100644 --- a/compiler/src/compiler/type_checker.cpp +++ b/compiler/src/compiler/type_checker.cpp @@ -457,6 +457,9 @@ void type_checker::visit_if_stmt(if_stmt *obj) { } void type_checker::visit_let_stmt(let_stmt *obj) { auto name = obj->name_->token_; + if (scope_.is_defined(name)) { + error(obj->name_, "Redefining a variable is not allowed"); + } auto placeholder = ykobject(obj->data_type_); if (obj->expression_ != nullptr) { obj->expression_->accept(this); diff --git a/compiler/test_data/bug_fixes/redefining_vars.yaka b/compiler/test_data/bug_fixes/redefining_vars.yaka new file mode 100644 index 00000000..69c62436 --- /dev/null +++ b/compiler/test_data/bug_fixes/redefining_vars.yaka @@ -0,0 +1,7 @@ +def a() -> int: + b: int = 1 + b: u8 = 1u8 + return 0 + +def main() -> int: + return 0 diff --git a/compiler/test_data/bug_fixes/redefining_vars_params.yaka b/compiler/test_data/bug_fixes/redefining_vars_params.yaka new file mode 100644 index 00000000..4921bcac --- /dev/null +++ b/compiler/test_data/bug_fixes/redefining_vars_params.yaka @@ -0,0 +1,7 @@ +def a(b: int) -> int: + b: u8 = 1u8 + return 0 + +def main() -> int: + a(1) + return 0 diff --git a/compiler/tests/test_type_checker.cpp b/compiler/tests/test_type_checker.cpp index 4f620b35..8b616110 100644 --- a/compiler/tests/test_type_checker.cpp +++ b/compiler/tests/test_type_checker.cpp @@ -481,4 +481,12 @@ TEST_CASE( "type checker: only 1 argument is allowed for binarydata()") { TEST_SNIPPET("a: Ptr[Const[u8]] = binarydata(1, 2, 3)", "binarydata() builtin expects 1 argument"); +} +TEST_CASE("type checker: Redefining variables in a function different data types") { + TEST_FILE("../test_data/bug_fixes/redefining_vars.yaka", + "Redefining a variable is not allowed"); +} +TEST_CASE("type checker: Redefining variables in a function - params") { + TEST_FILE("../test_data/bug_fixes/redefining_vars_params.yaka", + "Redefining a variable is not allowed"); } \ No newline at end of file