Skip to content

Commit 90d2de4

Browse files
committed
fix: add a check for multiple definitions of the type alias
1 parent 568b462 commit 90d2de4

2 files changed

Lines changed: 34 additions & 21 deletions

File tree

src/ast.rs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,35 @@ impl Scope {
618618
/// There are any undefined aliases.
619619
pub fn insert_alias(&mut self, name: AliasName, ty: AliasedType) -> Result<(), Error> {
620620
let resolved_ty = self.resolve(&ty)?;
621-
self.aliases.insert(name, resolved_ty);
622-
Ok(())
621+
622+
match self.aliases.entry(name.clone()) {
623+
Entry::Occupied(_) => Err(Error::RedefinedAlias(name)),
624+
Entry::Vacant(entry) => {
625+
entry.insert(resolved_ty);
626+
Ok(())
627+
}
628+
}
629+
// self.aliases.insert(name, resolved_ty);
630+
// Ok(())
631+
}
632+
633+
/// Insert a custom function into the global map.
634+
///
635+
/// ## Errors
636+
///
637+
/// The function has already been defined.
638+
pub fn insert_function(
639+
&mut self,
640+
name: FunctionName,
641+
function: CustomFunction,
642+
) -> Result<(), Error> {
643+
match self.functions.entry(name.clone()) {
644+
Entry::Occupied(_) => Err(Error::FunctionRedefined(name)),
645+
Entry::Vacant(entry) => {
646+
entry.insert(function);
647+
Ok(())
648+
}
649+
}
623650
}
624651

625652
/// Insert a parameter into the global map.
@@ -671,25 +698,6 @@ impl Scope {
671698
)
672699
}
673700

674-
/// Insert a custom function into the global map.
675-
///
676-
/// ## Errors
677-
///
678-
/// The function has already been defined.
679-
pub fn insert_function(
680-
&mut self,
681-
name: FunctionName,
682-
function: CustomFunction,
683-
) -> Result<(), Error> {
684-
match self.functions.entry(name.clone()) {
685-
Entry::Occupied(_) => Err(Error::FunctionRedefined(name)),
686-
Entry::Vacant(entry) => {
687-
entry.insert(function);
688-
Ok(())
689-
}
690-
}
691-
}
692-
693701
/// Get the definition of a custom function.
694702
pub fn get_function(&self, name: &FunctionName) -> Option<&CustomFunction> {
695703
self.functions.get(name)

src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ pub enum Error {
428428
ExpressionNotConstant,
429429
IntegerOutOfBounds(UIntType),
430430
UndefinedVariable(Identifier),
431+
RedefinedAlias(AliasName),
431432
UndefinedAlias(AliasName),
432433
VariableReuseInPattern(Identifier),
433434
WitnessReused(WitnessName),
@@ -546,6 +547,10 @@ impl fmt::Display for Error {
546547
f,
547548
"Variable `{identifier}` is not defined"
548549
),
550+
Error::RedefinedAlias(identifier) => write!(
551+
f,
552+
"Type alias `{identifier}` was defined multiple times"
553+
),
549554
Error::UndefinedAlias(identifier) => write!(
550555
f,
551556
"Type alias `{identifier}` is not defined"

0 commit comments

Comments
 (0)