From 30fbc9a721d48376d7aa7111486b1a86dc6bc1d0 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Fri, 29 Oct 2021 00:28:05 -0700 Subject: [PATCH] Add test for duplicate types in symbol table --- schala-lang/language/src/symbol_table/mod.rs | 1 - schala-lang/language/src/symbol_table/test.rs | 87 ++++++++----------- 2 files changed, 34 insertions(+), 54 deletions(-) diff --git a/schala-lang/language/src/symbol_table/mod.rs b/schala-lang/language/src/symbol_table/mod.rs index 29aa210..7d83fa1 100644 --- a/schala-lang/language/src/symbol_table/mod.rs +++ b/schala-lang/language/src/symbol_table/mod.rs @@ -211,7 +211,6 @@ impl SymbolTable { spec, def_id, }); - println!("In add_symbol(), adding: {:?}", symbol); self.symbol_trie.insert(&fqsn); self.fqsn_to_symbol.insert(fqsn, symbol.clone()); self.id_to_symbol.insert(*id, symbol); diff --git a/schala-lang/language/src/symbol_table/test.rs b/schala-lang/language/src/symbol_table/test.rs index 4489628..d25c005 100644 --- a/schala-lang/language/src/symbol_table/test.rs +++ b/schala-lang/language/src/symbol_table/test.rs @@ -1,12 +1,13 @@ #![cfg(test)] -use super::*; -use crate::util::quick_ast; use assert_matches::assert_matches; +use super::*; +use crate::{tokenizing::Location, util::quick_ast}; + fn add_symbols(src: &str) -> (SymbolTable, Result<(), Vec>) { let ast = quick_ast(src); let mut symbol_table = SymbolTable::new(); - let mut type_context = crate::type_inference::TypeContext::new(); + let mut type_context = crate::type_inference::TypeContext::new(); let result = symbol_table.process_ast(&ast, &mut type_context); (symbol_table, result) } @@ -30,16 +31,8 @@ fn basic_symbol_table() { let (symbols, _) = add_symbols(src); symbols.types.table.get(&make_fqsn(&["Option"])).unwrap(); - symbols - .types - .table - .get(&make_fqsn(&["Option", "Some"])) - .unwrap(); - symbols - .types - .table - .get(&make_fqsn(&["Option", "None"])) - .unwrap(); + symbols.types.table.get(&make_fqsn(&["Option", "Some"])).unwrap(); + symbols.types.table.get(&make_fqsn(&["Option", "None"])).unwrap(); } #[test] @@ -76,6 +69,26 @@ fn no_variable_definition_duplicates() { ); } +#[test] +fn no_type_definition_duplicates() { + let source = r#" + let x = 9 + type Food = Japchae | Burrito | Other + type Food = GoodJapchae | Breadfruit + "#; + let (_, output) = add_symbols(source); + let errs = output.unwrap_err(); + let err = &errs[0]; + + match err { + SymbolError::DuplicateName { location, prev_name } => { + assert_eq!(prev_name, &Fqsn::from_strs(&["Food"])); + assert_eq!(location, &Location { line_num: 2, char_num: 2 }); + } + _ => panic!(), + } +} + #[test] fn no_variable_definition_duplicates_in_function() { let source = r#" @@ -112,11 +125,7 @@ fn dont_falsely_detect_duplicates() { let (symbols, _) = add_symbols(source); assert!(symbols.fq_names.table.get(&make_fqsn(&["a"])).is_some()); - assert!(symbols - .fq_names - .table - .get(&make_fqsn(&["some_func", "a"])) - .is_some()); + assert!(symbols.fq_names.table.get(&make_fqsn(&["some_func", "a"])).is_some()); } #[test] @@ -129,16 +138,8 @@ fn inner_func(arg) { x + inner_func(x) }"#; let (symbols, _) = add_symbols(source); - assert!(symbols - .fq_names - .table - .get(&make_fqsn(&["outer_func"])) - .is_some()); - assert!(symbols - .fq_names - .table - .get(&make_fqsn(&["outer_func", "inner_func"])) - .is_some()); + assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func"])).is_some()); + assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func", "inner_func"])).is_some()); } #[test] @@ -157,29 +158,13 @@ fn second_inner_func() { inner_func(x) }"#; let (symbols, _) = add_symbols(source); + assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func"])).is_some()); + assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func", "inner_func"])).is_some()); + assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func", "second_inner_func"])).is_some()); assert!(symbols .fq_names .table - .get(&make_fqsn(&["outer_func"])) - .is_some()); - assert!(symbols - .fq_names - .table - .get(&make_fqsn(&["outer_func", "inner_func"])) - .is_some()); - assert!(symbols - .fq_names - .table - .get(&make_fqsn(&["outer_func", "second_inner_func"])) - .is_some()); - assert!(symbols - .fq_names - .table - .get(&make_fqsn(&[ - "outer_func", - "second_inner_func", - "another_inner_func" - ])) + .get(&make_fqsn(&["outer_func", "second_inner_func", "another_inner_func"])) .is_some()); } @@ -218,11 +203,7 @@ fn item() let (symbols, _) = add_symbols(source); symbols.fq_names.table.get(&make_fqsn(&["stuff"])).unwrap(); symbols.fq_names.table.get(&make_fqsn(&["item"])).unwrap(); - symbols - .fq_names - .table - .get(&make_fqsn(&["stuff", "item"])) - .unwrap(); + symbols.fq_names.table.get(&make_fqsn(&["stuff", "item"])).unwrap(); } #[test]