Disallow duplicated function definitions.

This commit is contained in:
Stephen Chung
2021-01-03 20:54:08 +08:00
parent 103af43f68
commit e2a47b2a65
5 changed files with 51 additions and 16 deletions

View File

@@ -2585,7 +2585,7 @@ fn parse_stmt(
// fn ...
#[cfg(not(feature = "no_function"))]
Token::Fn if !settings.is_global => Err(PERR::WrongFnDefinition.into_err(settings.pos)),
Token::Fn if !settings.is_global => Err(PERR::FnWrongDefinition.into_err(settings.pos)),
#[cfg(not(feature = "no_function"))]
Token::Fn | Token::Private => {
@@ -2621,13 +2621,20 @@ fn parse_stmt(
let func = parse_fn(input, &mut new_state, lib, access, settings, _comments)?;
lib.insert(
// Qualifiers (none) + function name + number of arguments.
calc_script_fn_hash(empty(), &func.name, func.params.len()).unwrap(),
func,
);
// Qualifiers (none) + function name + number of arguments.
let hash = calc_script_fn_hash(empty(), &func.name, func.params.len()).unwrap();
Ok(Stmt::Noop(settings.pos))
if lib.contains_key(&hash) {
return Err(PERR::FnDuplicatedDefinition(
func.name.into_owned(),
func.params.len(),
)
.into_err(pos));
}
lib.insert(hash, func);
Ok(Stmt::Noop(pos))
}
(_, pos) => Err(PERR::MissingToken(