From 29d3677b2af478ccc656fcca64dae68a5eb3ebf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Mon, 2 Jan 2023 21:49:16 +0100 Subject: [PATCH] Parser - Detect duplicate argument names --- src/parser.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/parser.rs b/src/parser.rs index b5e3bf0..946f2a9 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,3 +1,5 @@ +use std::collections::HashSet; + use crate::{ ast, errors::{ErrorHandler, ParserError}, @@ -251,6 +253,7 @@ impl Parser { let mut params = Vec::new(); if self.expect(&[TokenType::RightParen]).is_none() { + let mut names: HashSet = HashSet::new(); loop { if params.len() >= kind.max_params() { return Err(ParserError::new( @@ -262,7 +265,14 @@ impl Parser { ), )); } - if let TokenType::Identifier(_) = self.peek().token_type { + if let TokenType::Identifier(name) = &self.peek().token_type { + if names.contains(name) { + return Err(ParserError::new( + self.peek(), + &format!("duplicate {} parameter", kind.name()), + )); + } + names.insert(name.to_owned()); params.push(self.advance().clone()); } else { return Err(ParserError::new(self.peek(), "parameter name expected"));