Add test for duplicate types in symbol table

This commit is contained in:
Greg Shuflin 2021-10-29 00:28:05 -07:00
parent b2d9622feb
commit 30fbc9a721
2 changed files with 34 additions and 54 deletions

View File

@ -211,7 +211,6 @@ impl SymbolTable {
spec, spec,
def_id, def_id,
}); });
println!("In add_symbol(), adding: {:?}", symbol);
self.symbol_trie.insert(&fqsn); self.symbol_trie.insert(&fqsn);
self.fqsn_to_symbol.insert(fqsn, symbol.clone()); self.fqsn_to_symbol.insert(fqsn, symbol.clone());
self.id_to_symbol.insert(*id, symbol); self.id_to_symbol.insert(*id, symbol);

View File

@ -1,12 +1,13 @@
#![cfg(test)] #![cfg(test)]
use super::*;
use crate::util::quick_ast;
use assert_matches::assert_matches; use assert_matches::assert_matches;
use super::*;
use crate::{tokenizing::Location, util::quick_ast};
fn add_symbols(src: &str) -> (SymbolTable, Result<(), Vec<SymbolError>>) { fn add_symbols(src: &str) -> (SymbolTable, Result<(), Vec<SymbolError>>) {
let ast = quick_ast(src); let ast = quick_ast(src);
let mut symbol_table = SymbolTable::new(); 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); let result = symbol_table.process_ast(&ast, &mut type_context);
(symbol_table, result) (symbol_table, result)
} }
@ -30,16 +31,8 @@ fn basic_symbol_table() {
let (symbols, _) = add_symbols(src); let (symbols, _) = add_symbols(src);
symbols.types.table.get(&make_fqsn(&["Option"])).unwrap(); symbols.types.table.get(&make_fqsn(&["Option"])).unwrap();
symbols symbols.types.table.get(&make_fqsn(&["Option", "Some"])).unwrap();
.types symbols.types.table.get(&make_fqsn(&["Option", "None"])).unwrap();
.table
.get(&make_fqsn(&["Option", "Some"]))
.unwrap();
symbols
.types
.table
.get(&make_fqsn(&["Option", "None"]))
.unwrap();
} }
#[test] #[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] #[test]
fn no_variable_definition_duplicates_in_function() { fn no_variable_definition_duplicates_in_function() {
let source = r#" let source = r#"
@ -112,11 +125,7 @@ fn dont_falsely_detect_duplicates() {
let (symbols, _) = add_symbols(source); let (symbols, _) = add_symbols(source);
assert!(symbols.fq_names.table.get(&make_fqsn(&["a"])).is_some()); assert!(symbols.fq_names.table.get(&make_fqsn(&["a"])).is_some());
assert!(symbols assert!(symbols.fq_names.table.get(&make_fqsn(&["some_func", "a"])).is_some());
.fq_names
.table
.get(&make_fqsn(&["some_func", "a"]))
.is_some());
} }
#[test] #[test]
@ -129,16 +138,8 @@ fn inner_func(arg) {
x + inner_func(x) x + inner_func(x)
}"#; }"#;
let (symbols, _) = add_symbols(source); let (symbols, _) = add_symbols(source);
assert!(symbols assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func"])).is_some());
.fq_names assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func", "inner_func"])).is_some());
.table
.get(&make_fqsn(&["outer_func"]))
.is_some());
assert!(symbols
.fq_names
.table
.get(&make_fqsn(&["outer_func", "inner_func"]))
.is_some());
} }
#[test] #[test]
@ -157,29 +158,13 @@ fn second_inner_func() {
inner_func(x) inner_func(x)
}"#; }"#;
let (symbols, _) = add_symbols(source); 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 assert!(symbols
.fq_names .fq_names
.table .table
.get(&make_fqsn(&["outer_func"])) .get(&make_fqsn(&["outer_func", "second_inner_func", "another_inner_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"
]))
.is_some()); .is_some());
} }
@ -218,11 +203,7 @@ fn item()
let (symbols, _) = add_symbols(source); let (symbols, _) = add_symbols(source);
symbols.fq_names.table.get(&make_fqsn(&["stuff"])).unwrap(); 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(&["item"])).unwrap();
symbols symbols.fq_names.table.get(&make_fqsn(&["stuff", "item"])).unwrap();
.fq_names
.table
.get(&make_fqsn(&["stuff", "item"]))
.unwrap();
} }
#[test] #[test]