Skip to content

Commit

Permalink
Don't allow the user to define the same field name in a class more th…
Browse files Browse the repository at this point in the history
…an once.
  • Loading branch information
ltratt committed Jun 10, 2020
1 parent 5e00d91 commit 4d6ba76
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lang_tests/instance_fields_overlap2.som
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"
VM:
status: error
stderr:
...instance_fields_overlap2.som', line 11, column 9:
| a a |
Field 'a' has already been defined in this class.
"

instance_field_overlap2 = (
| a a |
)
16 changes: 15 additions & 1 deletion src/lib/compiler/ast_to_instrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,21 @@ impl<'a, 'input> Compiler<'a, 'input> {
};
for var in ast_inst_vars {
let vars_len = inst_vars.len();
inst_vars.insert(lexer.span_str(*var).to_owned(), vars_len);
let n = lexer.span_str(*var).to_owned();
match inst_vars.entry(n) {
hash_map::Entry::Occupied(_) => {
return Err(vec![(
*var,
format!(
"Field '{}' has already been defined in this class.",
self.lexer.span_str(*var)
),
)])
}
hash_map::Entry::Vacant(e) => {
e.insert(vars_len);
}
}
}
self.vars_stack.push(inst_vars);

Expand Down

0 comments on commit 4d6ba76

Please sign in to comment.