Skip to content

Commit

Permalink
Prevent var def in child scope when name already taken by parent scope
Browse files Browse the repository at this point in the history
Fixes #275
  • Loading branch information
cburgdorf committed Mar 19, 2021
1 parent 7d2e6f5 commit e55cbbf
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions analyzer/src/namespace/scopes.rs
Expand Up @@ -306,6 +306,9 @@ impl BlockScope {

/// Add a variable to the block scope.
pub fn add_var(&mut self, name: &str, typ: FixedSize) -> Result<(), SemanticError> {
if self.get_variable_def(name).is_some() {
return Err(SemanticError::already_defined());
}
match self.variable_defs.entry(name.to_owned()) {
Entry::Occupied(_) => Err(SemanticError::already_defined()),
Entry::Vacant(e) => {
Expand Down
1 change: 1 addition & 0 deletions compiler/tests/compile_errors.rs
Expand Up @@ -20,6 +20,7 @@ use std::fs;
case("duplicate_method_in_contract.fe", "AlreadyDefined"),
case("duplicate_struct_in_module.fe", "AlreadyDefined"),
case("duplicate_typedef_in_module.fe", "AlreadyDefined"),
case("duplicate_var_in_child_scope.fe", "AlreadyDefined"),
case("duplicate_var_in_contract_method.fe", "AlreadyDefined"),
case("external_call_type_error.fe", "TypeError"),
case("external_call_wrong_number_of_params.fe", "WrongNumberOfParams"),
Expand Down
@@ -0,0 +1,6 @@
contract Foo:
pub def bar():
my_array: u256[3]
sum: u256 = 0
for i in my_array:
sum: u256 = 0
13 changes: 13 additions & 0 deletions newsfragments/317.bugfix.md
Expand Up @@ -37,3 +37,16 @@ struct SomeStruct:
some_field: u8
some_field: u8
```


Prevent variable definition in child scope when name already taken in parent scope.

Example that now produces a compile time error:

```
pub def bar():
my_array: u256[3]
sum: u256 = 0
for i in my_array:
sum: u256 = 0
```

0 comments on commit e55cbbf

Please sign in to comment.