add check and error handling for duplicate function inputs

This commit is contained in:
collin 2020-11-16 16:27:37 -08:00
parent 848adfa6dd
commit b59ac9c055
4 changed files with 22 additions and 19 deletions

View File

@ -15,7 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{FunctionInputVariableType, SymbolTable, Type, TypeError};
use leo_ast::{FunctionInput, Identifier};
use leo_ast::{FunctionInput, Identifier, Span};
use serde::{Deserialize, Serialize};
@ -46,6 +46,16 @@ impl FunctionInputType {
}
}
///
/// Return the `Span` of the current function input.
///
pub fn span(&self) -> &Span {
match self {
FunctionInputType::InputKeyword(identifier) => &identifier.span,
FunctionInputType::Variable(variable) => &variable.span,
}
}
///
/// Return a new `FunctionInputType` from a given `FunctionInput`.
///

View File

@ -74,16 +74,4 @@ impl TypeAssertionError {
Self::new_from_span(message, membership.span())
}
///
/// Mismatched array type dimensions.
///
pub fn array_dimensions(dimensions1: &[usize], dimensions2: &[usize], span: &Span) -> Self {
let message = format!(
"Expected array with dimensions `{:?}`, found array with dimensions `{:?}`.",
dimensions1, dimensions2
);
Self::new_from_span(message, span)
}
}

View File

@ -43,10 +43,10 @@ impl VariableTableError {
}
///
/// Attempted to lookup a variable name that does not exist in the table.
/// Attempted to define two function inputs with the same name.
///
pub fn undefined_variable_name(name: &str, span: &Span) -> Self {
let message = format!("Cannot find variable `{}` in this scope.", name);
pub fn duplicate_function_input(name: &str, span: &Span) -> Self {
let message = format!("Duplicate function input `{}`found in function signature.", name);
Self::new_from_span(message, span.clone())
}

View File

@ -50,11 +50,16 @@ impl VariableTable {
///
pub fn insert_function_inputs(&mut self, function_inputs: &[FunctionInputType]) -> Result<(), VariableTableError> {
for input in function_inputs {
let input_name = input.identifier().name.clone();
let input_name = &input.identifier().name;
let input_type = input.type_();
let input_span = input.span();
// TODO (collinc97) throw an error for duplicate function input names.
self.insert(input_name, input_type);
// Check for duplicate function input names.
let duplicate = self.insert(input_name.clone(), input_type);
if duplicate.is_some() {
return Err(VariableTableError::duplicate_function_input(input_name, input_span));
}
}
Ok(())
}