From 745f6c2275e4815576edf5312cb17deb850b7dba Mon Sep 17 00:00:00 2001 From: gluaxspeed Date: Tue, 27 Jul 2021 13:39:34 -0700 Subject: [PATCH] variable shadow error --- asg/src/error/mod.rs | 7 +++++++ asg/src/statement/definition.rs | 14 ++++++++++---- grammar/FORMAT_ABNF_GRAMMAR.md | Bin 3660 -> 3522 bytes .../function/duplicate_definition_fail.leo | 6 ++++-- .../statements/duplicate_variable.leo | 12 ++++++++++++ .../duplicate_definition_fail.leo.out | 2 +- .../statements/duplicate_variable.leo.out | 5 +++++ 7 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 tests/compiler/statements/duplicate_variable.leo create mode 100644 tests/expectations/compiler/compiler/statements/duplicate_variable.leo.out diff --git a/asg/src/error/mod.rs b/asg/src/error/mod.rs index f3d9533ffa..fd045ff26d 100644 --- a/asg/src/error/mod.rs +++ b/asg/src/error/mod.rs @@ -192,6 +192,13 @@ impl AsgConvertError { ) } + pub fn duplicate_variable_definition(name: &str, span: &Span) -> Self { + Self::new_from_span( + format!("a variable named \"{}\" already exists in this scope", name), + span, + ) + } + pub fn index_into_non_tuple(name: &str, span: &Span) -> Self { Self::new_from_span(format!("failed to index into non-tuple '{}'", name), span) } diff --git a/asg/src/statement/definition.rs b/asg/src/statement/definition.rs index 4e2ce3cd83..f340052409 100644 --- a/asg/src/statement/definition.rs +++ b/asg/src/statement/definition.rs @@ -130,10 +130,16 @@ impl<'a> FromAst<'a, leo_ast::DefinitionStatement> for &'a Statement<'a> { } for variable in variables.iter() { - scope - .variables - .borrow_mut() - .insert(variable.borrow().name.name.to_string(), *variable); + let mut variables = scope.variables.borrow_mut(); + let var_name = variable.borrow().name.name.to_string(); + if variables.contains_key(&var_name) { + return Err(AsgConvertError::duplicate_variable_definition( + &var_name, + &statement.span, + )); + } + + variables.insert(var_name, *variable); } let statement = scope diff --git a/grammar/FORMAT_ABNF_GRAMMAR.md b/grammar/FORMAT_ABNF_GRAMMAR.md index cc88f9fda16e7bedcda279922d09d5f7c877fcbe..e1283d1b8e4ae75d83359369ed97e06b1f676631 100644 GIT binary patch delta 425 zcmX>jb4YqZ$iy%oMy|;-nKUO}$e3)!*fseWW6NY8CJ(S+`(!2Ny2*=}t0v2_L;*!= zSz0HvuuhWXVgNy1Gzb)%yqi@WsLqgGaS|H~kipMpJK2xTW%3lZAUvA5*=1px*no_1 z_VCFY*i$BpaRg1y;)t7kiUZ^taZY1?E`|g!Ok_x6$YV&G9LrhD2@~U*?8|LD`5%wK zWF0OZL6|61CD3Jw-11=A8ZJH*J%*EIxJ4%0abnja3zSttQ6G%eY~#rs8a delta 568 zcmX>keMV+N2qW*rU|&{V1}+BPi3=w(@=i8n>;{s%8C!v*J(DL)N^|lirZ}JwKXW~h z>}9S7lI$$e5b4RuEFk62SthH3O#+z)#JXq@E;o5Ht2#4~H~Ax*;^YggEKpM(vf2Vo zk!5oQl8J1=M4EM$O%};4HmF&`?BPIDGTBps u32 { + console.log("{}", 1u8); + return 10u32; } function main() { diff --git a/tests/compiler/statements/duplicate_variable.leo b/tests/compiler/statements/duplicate_variable.leo new file mode 100644 index 0000000000..3271a58d7a --- /dev/null +++ b/tests/compiler/statements/duplicate_variable.leo @@ -0,0 +1,12 @@ +/* +namespace: Compile +expectation: Fail +input_file: inputs/dummy.in +*/ + +function main(k: bool) -> bool { + let x = 1u8; + let x = true; + + return k == true; +} \ No newline at end of file diff --git a/tests/expectations/compiler/compiler/function/duplicate_definition_fail.leo.out b/tests/expectations/compiler/compiler/function/duplicate_definition_fail.leo.out index 3b1bf075b9..7b1e418ef4 100644 --- a/tests/expectations/compiler/compiler/function/duplicate_definition_fail.leo.out +++ b/tests/expectations/compiler/compiler/function/duplicate_definition_fail.leo.out @@ -2,4 +2,4 @@ namespace: Compile expectation: Fail outputs: - - " --> compiler-test:8:1\n |\n 8 | function main() {\n 9 | ...\n 10 | }\n | ^\n |\n = a function named \"main\" already exists in this scope" + - " --> compiler-test:6:12\n |\n 6 | return 10u32; \n | ^^^^^\n |\n = unexpected type, expected: '()', received: 'u32'" diff --git a/tests/expectations/compiler/compiler/statements/duplicate_variable.leo.out b/tests/expectations/compiler/compiler/statements/duplicate_variable.leo.out new file mode 100644 index 0000000000..a313c9602a --- /dev/null +++ b/tests/expectations/compiler/compiler/statements/duplicate_variable.leo.out @@ -0,0 +1,5 @@ +--- +namespace: Compile +expectation: Fail +outputs: + - " --> compiler-test:5:3\n |\n 5 | let x = true;\n | ^^^^^^^^^^^^\n |\n = a variable named \"x\" already exists in this scope"