variable shadow error

This commit is contained in:
gluaxspeed 2021-07-27 13:39:34 -07:00
parent 070735fe9a
commit 745f6c2275
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 { pub fn index_into_non_tuple(name: &str, span: &Span) -> Self {
Self::new_from_span(format!("failed to index into non-tuple '{}'", name), span) 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() { for variable in variables.iter() {
scope let mut variables = scope.variables.borrow_mut();
.variables let var_name = variable.borrow().name.name.to_string();
.borrow_mut() if variables.contains_key(&var_name) {
.insert(variable.borrow().name.name.to_string(), *variable); return Err(AsgConvertError::duplicate_variable_definition(
&var_name,
&statement.span,
));
}
variables.insert(var_name, *variable);
} }
let statement = scope let statement = scope

Binary file not shown.

View File

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