From f55e880c434e9704924c0cbbe5bde69d0a7cc088 Mon Sep 17 00:00:00 2001 From: damirka Date: Wed, 7 Apr 2021 19:39:53 +0300 Subject: [PATCH 1/3] adds check for already existing functions --- asg/src/error/mod.rs | 7 +++++++ asg/src/program/mod.rs | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/asg/src/error/mod.rs b/asg/src/error/mod.rs index 8dd53d098f..cff66e4116 100644 --- a/asg/src/error/mod.rs +++ b/asg/src/error/mod.rs @@ -172,6 +172,13 @@ impl AsgConvertError { ) } + pub fn duplicate_function_definition(name: &str, span: &Span) -> Self { + Self::new_from_span( + format!("duplicate definition, function with name \"{}\" already exists", 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/program/mod.rs b/asg/src/program/mod.rs index c38ab404e5..868b46e8b3 100644 --- a/asg/src/program/mod.rs +++ b/asg/src/program/mod.rs @@ -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(); From 8ac7440daa87988e6c688c1ddf44f81a200c7b88 Mon Sep 17 00:00:00 2001 From: damirka Date: Wed, 7 Apr 2021 20:20:58 +0300 Subject: [PATCH 2/3] adds test, expects asg error --- compiler/tests/function/duplicate_definition.leo | 7 +++++++ compiler/tests/function/mod.rs | 8 ++++++++ 2 files changed, 15 insertions(+) create mode 100644 compiler/tests/function/duplicate_definition.leo diff --git a/compiler/tests/function/duplicate_definition.leo b/compiler/tests/function/duplicate_definition.leo new file mode 100644 index 0000000000..cddc9ce7f2 --- /dev/null +++ b/compiler/tests/function/duplicate_definition.leo @@ -0,0 +1,7 @@ +function main() { + console.log("{}", 1u8); +} + +function main() { + console.log("{}", 2u8); +} \ No newline at end of file diff --git a/compiler/tests/function/mod.rs b/compiler/tests/function/mod.rs index 6de6cd8153..8dfb865d90 100644 --- a/compiler/tests/function/mod.rs +++ b/compiler/tests/function/mod.rs @@ -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); +} From aa0e5767732e8b55a28d5f0bea73145a4d11228d Mon Sep 17 00:00:00 2001 From: collin Date: Wed, 7 Apr 2021 14:31:44 -0700 Subject: [PATCH 3/3] edit duplicate function message --- asg/src/error/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asg/src/error/mod.rs b/asg/src/error/mod.rs index cff66e4116..7ee1eb62cf 100644 --- a/asg/src/error/mod.rs +++ b/asg/src/error/mod.rs @@ -174,7 +174,7 @@ impl AsgConvertError { pub fn duplicate_function_definition(name: &str, span: &Span) -> Self { Self::new_from_span( - format!("duplicate definition, function with name \"{}\" already exists", name), + format!("a function named \"{}\" already exists in this scope", name), span, ) }