Skip to content

Commit

Permalink
fix(type_checker,tests): handle redefining variables in type_checker
Browse files Browse the repository at this point in the history
  • Loading branch information
JaDogg committed Jun 19, 2023
1 parent 3da613c commit 2f97522
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions compiler/src/compiler/type_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions compiler/test_data/bug_fixes/redefining_vars.yaka
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def a() -> int:
b: int = 1
b: u8 = 1u8
return 0

def main() -> int:
return 0
7 changes: 7 additions & 0 deletions compiler/test_data/bug_fixes/redefining_vars_params.yaka
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def a(b: int) -> int:
b: u8 = 1u8
return 0

def main() -> int:
a(1)
return 0
8 changes: 8 additions & 0 deletions compiler/tests/test_type_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit 2f97522

Please sign in to comment.