1
0
Fork 0
mirror of synced 2025-09-24 04:40:05 +00:00

ban shadowing

This commit is contained in:
schaeff 2018-08-22 13:23:23 +02:00
parent 79c1dffc78
commit 47f1235264
2 changed files with 45 additions and 5 deletions

View file

@ -22,6 +22,13 @@ impl Variable {
}
}
pub fn boolean<S: Into<String>>(id: S) -> Variable {
Variable {
id: id.into(),
_type: Type::Boolean
}
}
pub fn get_type(&self) -> Type {
self._type.clone()
}

View file

@ -94,13 +94,13 @@ pub struct ScopedVariable {
impl Hash for ScopedVariable {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state);
self.id.id.hash(state);
}
}
impl PartialEq for ScopedVariable {
fn eq(&self, other: &ScopedVariable) -> bool {
self.id == other.id
self.id.id == other.id.id
}
}
impl Eq for ScopedVariable {}
@ -229,8 +229,10 @@ impl Checker {
}
}
Statement::Declaration(ref var) => {
self.insert_scope(var.clone());
Ok(TypedStatement::Declaration(var.clone()))
match self.insert_scope(var.clone()) {
true => Ok(TypedStatement::Declaration(var.clone())),
false => Err( Error { message: format!("Duplicate declaration for variable named {}", var.id)})
}
}
Statement::Definition(variable_name, expr) => {
// we create multidef when rhs is a function call to benefit from inference
@ -576,7 +578,6 @@ mod tests {
});
scope.insert(ScopedVariable {
id: Variable::field_element("b"),
level: 0
});
let mut checker = new_with_args(scope, 1, HashSet::new());
@ -1250,4 +1251,36 @@ mod tests {
let mut checker = Checker::new();
assert_eq!(checker.check_program(prog), Err(Error { message: "Only one main function allowed, found 2".to_string() }));
}
#[test]
fn shadowing_with_same_type() {
// field a
// field a
//
// should fail
let mut checker = Checker::new();
let _: Result<TypedStatement<FieldPrime>, Error> = checker.check_statement(&Statement::Declaration(Variable::field_element("a")), &vec![]);
let s2_checked: Result<TypedStatement<FieldPrime>, Error> = checker.check_statement(&Statement::Declaration(Variable::field_element("a")), &vec![]);
assert_eq!(
s2_checked,
Err(Error { message: "Duplicate declaration for variable named a".to_string() })
);
}
#[test]
fn shadowing_with_different_type() {
// field a
// bool a
//
// should fail
let mut checker = Checker::new();
let _: Result<TypedStatement<FieldPrime>, Error> = checker.check_statement(&Statement::Declaration(Variable::field_element("a")), &vec![]);
let s2_checked: Result<TypedStatement<FieldPrime>, Error> = checker.check_statement(&Statement::Declaration(Variable::boolean("a")), &vec![]);
assert_eq!(
s2_checked,
Err(Error { message: "Duplicate declaration for variable named a".to_string() })
);
}
}