Merge pull request #1203 from AleoHQ/bug/variable-shadowing

[Bugfix] Variable Shadowing
This commit is contained in:
Alessandro Coglio 2021-07-27 17:11:27 -07:00 committed by GitHub
commit 5d4f943183
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 39 additions and 7 deletions

View File

@ -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)
}

View File

@ -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

Binary file not shown.

View File

@ -1,11 +1,13 @@
/*
namespace: Compile
expectation: Fail
input_file: input/integers.in
*/
function main() {
function main(a: u32) -> u32 {
console.log("{}", 1u8);
return 10u32;
}
function main() {

View File

@ -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;
}

View File

@ -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'"

View File

@ -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"