Merge pull request #837 from AleoHQ/bug-duplicate-functions

Adds error when function defined more than once
This commit is contained in:
Collin Chin 2021-04-07 14:32:47 -07:00 committed by GitHub
commit bd7ec477c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 1 deletions

View File

@ -172,6 +172,13 @@ impl AsgConvertError {
)
}
pub fn duplicate_function_definition(name: &str, span: &Span) -> Self {
Self::new_from_span(
format!("a function 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

@ -266,7 +266,13 @@ impl<'a> Program<'a> {
asg_function.fill_from_ast(function)?;
functions.insert(name.name.to_string(), asg_function);
let name = name.name.to_string();
if functions.contains_key(&name) {
return Err(AsgConvertError::duplicate_function_definition(&name, &function.span));
}
functions.insert(name, asg_function);
}
let mut circuits = IndexMap::new();

View File

@ -0,0 +1,7 @@
function main() {
console.log("{}", 1u8);
}
function main() {
console.log("{}", 2u8);
}

View File

@ -211,3 +211,11 @@ fn test_array_params_direct_call() {
assert_satisfied(program);
}
#[test]
fn test_duplicate_function_definition() {
let program_string = include_str!("duplicate_definition.leo");
let error = parse_program(program_string).err().unwrap();
expect_asg_error(error);
}