diff --git a/schala-lang/language/src/symbol_table.rs b/schala-lang/language/src/symbol_table.rs index 2ab5c69..0eb9a0e 100644 --- a/schala-lang/language/src/symbol_table.rs +++ b/schala-lang/language/src/symbol_table.rs @@ -7,7 +7,7 @@ use std::fmt::Write; use crate::schala::SourceMapHandle; use crate::source_map::{SourceMap, LineNumber}; use crate::ast; -use crate::ast::{ItemId, TypeBody, TypeSingletonName, Signature, Statement, StatementKind}; +use crate::ast::{ItemId, TypeBody, TypeSingletonName, Signature, Statement, StatementKind, ModuleSpecifier}; use crate::typechecking::TypeName; @@ -191,8 +191,6 @@ impl SymbolTable { fn add_symbols_from_scope<'a>(&'a mut self, statements: &Vec, scope_name_stack: &mut Vec) -> Result<(), String> { use self::ast::Declaration::*; - //Err(format!("Duplicate definition: {}. It's already defined at {}", name, line_number)) - let mut seen_identifiers = DuplicateNameTrackTable::new(); let mut seen_modules = DuplicateNameTrackTable::new(); @@ -213,7 +211,7 @@ impl SymbolTable { name: signature.name.clone(), }); let output = self.add_symbols_from_scope(body, scope_name_stack); - let _ = scope_name_stack.pop(); + scope_name_stack.pop(); output? }, TypeDecl { name, body, mutable } => { @@ -229,8 +227,13 @@ impl SymbolTable { _ => () } }, - Statement { kind: StatementKind::Module(mod_spec), id } => { - () + Statement { kind: StatementKind::Module(ModuleSpecifier { name, contents}), id } => { + seen_modules.try_register(&name, &id, &self.source_map_handle.borrow()) + .map_err(|line| format!("Duplicate module definition: {}. It's already defined at {}", name, line))?; + scope_name_stack.push(ScopeSegment { name: name.clone() }); + let output = self.add_symbols_from_scope(contents, scope_name_stack); + scope_name_stack.pop(); + output? }, _ => () } diff --git a/schala-lang/language/src/symbol_table/test.rs b/schala-lang/language/src/symbol_table/test.rs index 96c147a..11c2d2d 100644 --- a/schala-lang/language/src/symbol_table/test.rs +++ b/schala-lang/language/src/symbol_table/test.rs @@ -156,16 +156,22 @@ inner_func(x) } #[test] -fn modules() { +fn duplicate_modules() { let source = r#" - - module a { + module q { + fn foo() { 4 } } module a { + fn foo() { 334 } + } + module a { + fn foo() { 256.1 } } "#; let (_, output) = add_symbols_from_source(source); - //assert!(output.unwrap_err().contains("Duplicate")) + let output = output.unwrap_err(); + assert!(output.contains("Duplicate module")); + assert!(output.contains("already defined at 5")); }