Run rustfmt on symbol_table code

This commit is contained in:
Greg Shuflin 2021-10-21 14:46:42 -07:00
parent 93d0a2cd7d
commit fb31687dea
4 changed files with 671 additions and 500 deletions

View File

@ -1,10 +1,13 @@
use std::collections::{HashMap, hash_map::Entry}; use std::collections::{hash_map::Entry, HashMap};
use std::rc::Rc;
use std::fmt; use std::fmt;
use std::rc::Rc;
use crate::tokenizing::Location;
use crate::ast; use crate::ast;
use crate::ast::{ItemId, TypeBody, Variant, TypeSingletonName, Declaration, Statement, StatementKind, ModuleSpecifier}; use crate::ast::{
Declaration, ItemId, ModuleSpecifier, Statement, StatementKind, TypeBody, TypeSingletonName,
Variant,
};
use crate::tokenizing::Location;
use crate::typechecking::TypeName; use crate::typechecking::TypeName;
mod resolver; mod resolver;
@ -35,19 +38,15 @@ impl Fqsn {
for s in strs { for s in strs {
scopes.push(Scope::Name(Rc::new(s.to_string()))); scopes.push(Scope::Name(Rc::new(s.to_string())));
} }
Fqsn { Fqsn { scopes }
scopes
}
} }
} }
//TODO eventually this should use ItemId's to avoid String-cloning //TODO eventually this should use ItemId's to avoid String-cloning
/// One segment within a scope. /// One segment within a scope.
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)] #[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
enum Scope { enum Scope {
Name(Rc<String>) Name(Rc<String>),
} }
#[allow(dead_code)] #[allow(dead_code)]
@ -55,20 +54,20 @@ enum Scope {
pub enum SymbolError { pub enum SymbolError {
DuplicateName { DuplicateName {
prev_name: Fqsn, prev_name: Fqsn,
location: Location location: Location,
}, },
DuplicateRecord { DuplicateRecord {
type_name: Fqsn, type_name: Fqsn,
location: Location, location: Location,
member: String, member: String,
} },
} }
#[allow(dead_code)] #[allow(dead_code)]
#[derive(Debug)] #[derive(Debug)]
struct NameSpec<K> { struct NameSpec<K> {
location: Location, location: Location,
kind: K kind: K,
} }
#[derive(Debug)] #[derive(Debug)]
@ -83,19 +82,22 @@ struct TypeKind;
/// Keeps track of what names were used in a given namespace. /// Keeps track of what names were used in a given namespace.
struct NameTable<K> { struct NameTable<K> {
table: HashMap<Fqsn, NameSpec<K>> table: HashMap<Fqsn, NameSpec<K>>,
} }
impl<K> NameTable<K> { impl<K> NameTable<K> {
fn new() -> Self { fn new() -> Self {
Self { table: HashMap::new() } Self {
table: HashMap::new(),
}
} }
fn register(&mut self, name: Fqsn, spec: NameSpec<K>) -> Result<(), SymbolError> { fn register(&mut self, name: Fqsn, spec: NameSpec<K>) -> Result<(), SymbolError> {
match self.table.entry(name.clone()) { match self.table.entry(name.clone()) {
Entry::Occupied(o) => { Entry::Occupied(o) => Err(SymbolError::DuplicateName {
Err(SymbolError::DuplicateName { prev_name: name, location: o.get().location }) prev_name: name,
}, location: o.get().location,
}),
Entry::Vacant(v) => { Entry::Vacant(v) => {
v.insert(spec); v.insert(spec);
Ok(()) Ok(())
@ -138,7 +140,6 @@ impl SymbolTable {
/// different ways and populate subtables with information that will be used further in the /// different ways and populate subtables with information that will be used further in the
/// compilation process. /// compilation process.
pub fn process_ast(&mut self, ast: &ast::AST) -> Result<(), Vec<SymbolError>> { pub fn process_ast(&mut self, ast: &ast::AST) -> Result<(), Vec<SymbolError>> {
let errs = self.populate_name_tables(ast); let errs = self.populate_name_tables(ast);
if !errs.is_empty() { if !errs.is_empty() {
return Err(errs); return Err(errs);
@ -188,14 +189,27 @@ impl fmt::Display for SymbolSpec {
use self::SymbolSpec::*; use self::SymbolSpec::*;
match self { match self {
Func(type_names) => write!(f, "Func({:?})", type_names), Func(type_names) => write!(f, "Func({:?})", type_names),
DataConstructor { index, type_name, arity } => write!(f, "DataConstructor(idx: {}, arity: {}, type: {})", index, arity, type_name), DataConstructor {
RecordConstructor { type_name, index, ..} => write!(f, "RecordConstructor(idx: {})(<members> -> {})", index, type_name), index,
type_name,
arity,
} => write!(
f,
"DataConstructor(idx: {}, arity: {}, type: {})",
index, arity, type_name
),
RecordConstructor {
type_name, index, ..
} => write!(
f,
"RecordConstructor(idx: {})(<members> -> {})",
index, type_name
),
Binding => write!(f, "Binding"), Binding => write!(f, "Binding"),
} }
} }
} }
impl SymbolTable { impl SymbolTable {
/* note: this adds names for *forward reference* but doesn't actually create any types. solve that problem /* note: this adds names for *forward reference* but doesn't actually create any types. solve that problem
* later */ * later */
@ -223,15 +237,24 @@ impl SymbolTable {
self.add_from_scope(ast.statements.as_ref(), &mut scope_stack) self.add_from_scope(ast.statements.as_ref(), &mut scope_stack)
} }
fn add_from_scope<'a>(&'a mut self, statements: &[Statement], scope_stack: &mut Vec<Scope>) -> Vec<SymbolError> { fn add_from_scope<'a>(
&'a mut self,
statements: &[Statement],
scope_stack: &mut Vec<Scope>,
) -> Vec<SymbolError> {
let mut errors = vec![]; let mut errors = vec![];
for statement in statements { for statement in statements {
let Statement { id: _, kind, location } = statement; //TODO I'm not sure if I need to do anything with this ID let Statement {
id: _,
kind,
location,
} = statement; //TODO I'm not sure if I need to do anything with this ID
let location = *location; let location = *location;
if let Err(err) = self.add_single_statement(kind, location, scope_stack) { if let Err(err) = self.add_single_statement(kind, location, scope_stack) {
errors.push(err); errors.push(err);
} else { // If there's an error with a name, don't recurse into subscopes of that name } else {
// If there's an error with a name, don't recurse into subscopes of that name
let recursive_errs = match kind { let recursive_errs = match kind {
StatementKind::Declaration(Declaration::FuncDecl(signature, body)) => { StatementKind::Declaration(Declaration::FuncDecl(signature, body)) => {
let new_scope = Scope::Name(signature.name.clone()); let new_scope = Scope::Name(signature.name.clone());
@ -247,10 +270,12 @@ impl SymbolTable {
scope_stack.pop(); scope_stack.pop();
output output
} }
StatementKind::Declaration(Declaration::TypeDecl { name, body, mutable }) => { StatementKind::Declaration(Declaration::TypeDecl {
self.add_type_members(name, body, mutable, location, scope_stack) name,
} body,
_ => vec![] mutable,
}) => self.add_type_members(name, body, mutable, location, scope_stack),
_ => vec![],
}; };
errors.extend(recursive_errs.into_iter()); errors.extend(recursive_errs.into_iter());
} }
@ -259,61 +284,128 @@ impl SymbolTable {
errors errors
} }
fn add_single_statement(&mut self, kind: &StatementKind, location: Location, scope_stack: &[Scope]) -> Result<(), SymbolError> { fn add_single_statement(
&mut self,
kind: &StatementKind,
location: Location,
scope_stack: &[Scope],
) -> Result<(), SymbolError> {
match kind { match kind {
StatementKind::Declaration(Declaration::FuncSig(signature)) => { StatementKind::Declaration(Declaration::FuncSig(signature)) => {
let fq_function = Fqsn::from_scope_stack(scope_stack, signature.name.clone()); let fq_function = Fqsn::from_scope_stack(scope_stack, signature.name.clone());
self.fq_names.register(fq_function.clone(), NameSpec { location, kind: NameKind::Function })?; self.fq_names.register(
self.types.register(fq_function.clone(), NameSpec { location, kind: TypeKind } )?; fq_function.clone(),
NameSpec {
location,
kind: NameKind::Function,
},
)?;
self.types.register(
fq_function.clone(),
NameSpec {
location,
kind: TypeKind,
},
)?;
self.add_symbol(fq_function, Symbol { self.add_symbol(
fq_function,
Symbol {
local_name: signature.name.clone(), local_name: signature.name.clone(),
spec: SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all? spec: SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
}); },
);
} }
StatementKind::Declaration(Declaration::FuncDecl(signature, ..)) => { StatementKind::Declaration(Declaration::FuncDecl(signature, ..)) => {
let fn_name = &signature.name; let fn_name = &signature.name;
let fq_function = Fqsn::from_scope_stack(scope_stack, fn_name.clone()); let fq_function = Fqsn::from_scope_stack(scope_stack, fn_name.clone());
self.fq_names.register(fq_function.clone(), NameSpec { location, kind: NameKind::Function })?; self.fq_names.register(
self.types.register(fq_function.clone(), NameSpec { location, kind: TypeKind } )?; fq_function.clone(),
NameSpec {
location,
kind: NameKind::Function,
},
)?;
self.types.register(
fq_function.clone(),
NameSpec {
location,
kind: TypeKind,
},
)?;
self.add_symbol(fq_function, Symbol { self.add_symbol(
fq_function,
Symbol {
local_name: signature.name.clone(), local_name: signature.name.clone(),
spec: SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all? spec: SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
});
}, },
);
}
StatementKind::Declaration(Declaration::TypeDecl { name, .. }) => { StatementKind::Declaration(Declaration::TypeDecl { name, .. }) => {
let fq_type = Fqsn::from_scope_stack(scope_stack, name.name.clone()); let fq_type = Fqsn::from_scope_stack(scope_stack, name.name.clone());
self.types.register(fq_type, NameSpec { location, kind: TypeKind } )?; self.types.register(
fq_type,
NameSpec {
location,
kind: TypeKind,
}, },
)?;
}
StatementKind::Declaration(Declaration::Binding { name, .. }) => { StatementKind::Declaration(Declaration::Binding { name, .. }) => {
let fq_binding = Fqsn::from_scope_stack(scope_stack, name.clone()); let fq_binding = Fqsn::from_scope_stack(scope_stack, name.clone());
self.fq_names.register(fq_binding.clone(), NameSpec { location, kind: NameKind::Binding })?; self.fq_names.register(
self.add_symbol(fq_binding, Symbol { fq_binding.clone(),
NameSpec {
location,
kind: NameKind::Binding,
},
)?;
self.add_symbol(
fq_binding,
Symbol {
local_name: name.clone(), local_name: name.clone(),
spec: SymbolSpec::Binding, spec: SymbolSpec::Binding,
}); },
);
} }
StatementKind::Module(ModuleSpecifier { name, .. }) => { StatementKind::Module(ModuleSpecifier { name, .. }) => {
let fq_module = Fqsn::from_scope_stack(scope_stack, name.clone()); let fq_module = Fqsn::from_scope_stack(scope_stack, name.clone());
self.fq_names.register(fq_module, NameSpec { location, kind: NameKind::Module })?; self.fq_names.register(
fq_module,
NameSpec {
location,
kind: NameKind::Module,
}, },
)?;
}
_ => (), _ => (),
} }
Ok(()) Ok(())
} }
fn add_type_members(&mut self, type_name: &TypeSingletonName, type_body: &TypeBody, _mutable: &bool, location: Location, scope_stack: &mut Vec<Scope>) -> Vec<SymbolError> { fn add_type_members(
&mut self,
type_name: &TypeSingletonName,
type_body: &TypeBody,
_mutable: &bool,
location: Location,
scope_stack: &mut Vec<Scope>,
) -> Vec<SymbolError> {
let mut member_errors = vec![]; let mut member_errors = vec![];
let mut errors = vec![]; let mut errors = vec![];
let mut register = |fqsn: Fqsn, spec: SymbolSpec| { let mut register = |fqsn: Fqsn, spec: SymbolSpec| {
let name_spec = NameSpec { location, kind: TypeKind }; let name_spec = NameSpec {
location,
kind: TypeKind,
};
if let Err(err) = self.types.register(fqsn.clone(), name_spec) { if let Err(err) = self.types.register(fqsn.clone(), name_spec) {
errors.push(err); errors.push(err);
} else { } else {
let local_name = match spec { let local_name = match spec {
SymbolSpec::DataConstructor { ref type_name, ..} | SymbolSpec::RecordConstructor { ref type_name, .. } => type_name.clone(), SymbolSpec::DataConstructor { ref type_name, .. }
| SymbolSpec::RecordConstructor { ref type_name, .. } => type_name.clone(),
_ => panic!("This should never happen"), _ => panic!("This should never happen"),
}; };
let symbol = Symbol { local_name, spec }; let symbol = Symbol { local_name, spec };
@ -335,7 +427,7 @@ impl SymbolTable {
type_name: name.clone(), type_name: name.clone(),
}; };
register(fq_name, spec); register(fq_name, spec);
}, }
Variant::TupleStruct(name, items) => { Variant::TupleStruct(name, items) => {
let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone()); let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone());
let spec = SymbolSpec::DataConstructor { let spec = SymbolSpec::DataConstructor {
@ -344,7 +436,7 @@ impl SymbolTable {
type_name: name.clone(), type_name: name.clone(),
}; };
register(fq_name, spec); register(fq_name, spec);
}, }
Variant::Record { name, members } => { Variant::Record { name, members } => {
let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone()); let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone());
@ -358,17 +450,26 @@ impl SymbolTable {
location, location,
member: member_name.as_ref().to_string(), member: member_name.as_ref().to_string(),
}); });
}, }
//TODO eventually this should track meaningful locations //TODO eventually this should track meaningful locations
Entry::Vacant(v) => { v.insert(Location::default()); } Entry::Vacant(v) => {
v.insert(Location::default());
}
} }
} }
let spec = SymbolSpec::RecordConstructor { let spec = SymbolSpec::RecordConstructor {
index, index,
type_name: name.clone(), type_name: name.clone(),
members: members.iter() members: members
.map(|(_, _)| (Rc::new("DUMMY_FIELD".to_string()), Rc::new("DUMMY_TYPE_ID".to_string()))).collect() .iter()
.map(|(_, _)| {
(
Rc::new("DUMMY_FIELD".to_string()),
Rc::new("DUMMY_TYPE_ID".to_string()),
)
})
.collect(),
}; };
register(fq_name, spec); register(fq_name, spec);
} }

View File

@ -1,7 +1,7 @@
use std::rc::Rc; use std::rc::Rc;
use crate::symbol_table::{SymbolTable, Fqsn, Scope};
use crate::ast::*; use crate::ast::*;
use crate::symbol_table::{Fqsn, Scope, SymbolTable};
use crate::util::ScopeStack; use crate::util::ScopeStack;
type FqsnPrefix = Vec<Scope>; type FqsnPrefix = Vec<Scope>;
@ -14,7 +14,10 @@ pub struct Resolver<'a> {
impl<'a> Resolver<'a> { impl<'a> Resolver<'a> {
pub fn new(symbol_table: &'a mut SymbolTable) -> Self { pub fn new(symbol_table: &'a mut SymbolTable) -> Self {
let name_scope_stack: ScopeStack<'a, Rc<String>, FqsnPrefix> = ScopeStack::new(None); let name_scope_stack: ScopeStack<'a, Rc<String>, FqsnPrefix> = ScopeStack::new(None);
Self { symbol_table, name_scope_stack } Self {
symbol_table,
name_scope_stack,
}
} }
pub fn resolve(&mut self, ast: &AST) { pub fn resolve(&mut self, ast: &AST) {
walk_ast(self, ast); walk_ast(self, ast);
@ -24,21 +27,21 @@ impl<'a> Resolver<'a> {
let QualifiedName { components, .. } = sym_name; let QualifiedName { components, .. } = sym_name;
let first_component = &components[0]; let first_component = &components[0];
match self.name_scope_stack.lookup(first_component) { match self.name_scope_stack.lookup(first_component) {
None => { None => Fqsn {
Fqsn { scopes: components
scopes: components.iter() .iter()
.map(|name| Scope::Name(name.clone())) .map(|name| Scope::Name(name.clone()))
.collect() .collect(),
}
}, },
Some(fqsn_prefix) => { Some(fqsn_prefix) => {
let mut full_name = fqsn_prefix.clone(); let mut full_name = fqsn_prefix.clone();
let rest_of_name: FqsnPrefix = components[1..].iter().map(|name| Scope::Name(name.clone())).collect(); let rest_of_name: FqsnPrefix = components[1..]
.iter()
.map(|name| Scope::Name(name.clone()))
.collect();
full_name.extend_from_slice(&rest_of_name); full_name.extend_from_slice(&rest_of_name);
Fqsn { Fqsn { scopes: full_name }
scopes: full_name
}
} }
} }
} }
@ -48,7 +51,9 @@ impl<'a> Resolver<'a> {
fn qualified_name_in_pattern(&mut self, qualified_name: &QualifiedName) { fn qualified_name_in_pattern(&mut self, qualified_name: &QualifiedName) {
let fqsn = self.lookup_name_in_scope(qualified_name); let fqsn = self.lookup_name_in_scope(qualified_name);
if self.symbol_table.fqsn_to_symbol.get(&fqsn).is_some() { if self.symbol_table.fqsn_to_symbol.get(&fqsn).is_some() {
self.symbol_table.id_to_fqsn.insert(qualified_name.id.clone(), fqsn); //TODO maybe set this to an explicit value if none? self.symbol_table
.id_to_fqsn
.insert(qualified_name.id.clone(), fqsn); //TODO maybe set this to an explicit value if none?
} }
} }
} }
@ -56,11 +61,18 @@ impl<'a> Resolver<'a> {
impl<'a> ASTVisitor for Resolver<'a> { impl<'a> ASTVisitor for Resolver<'a> {
//TODO need to un-insert these - maybe need to rethink visitor //TODO need to un-insert these - maybe need to rethink visitor
fn import(&mut self, import_spec: &ImportSpecifier) { fn import(&mut self, import_spec: &ImportSpecifier) {
let ImportSpecifier { ref path_components, ref imported_names, .. } = &import_spec; let ImportSpecifier {
ref path_components,
ref imported_names,
..
} = &import_spec;
match imported_names { match imported_names {
ImportedNames::All => { ImportedNames::All => {
let prefix = Fqsn { let prefix = Fqsn {
scopes: path_components.iter().map(|c| Scope::Name(c.clone())).collect() scopes: path_components
.iter()
.map(|c| Scope::Name(c.clone()))
.collect(),
}; };
let members = self.symbol_table.symbol_trie.get_children(&prefix); let members = self.symbol_table.symbol_trie.get_children(&prefix);
for member in members.into_iter() { for member in members.into_iter() {
@ -68,20 +80,23 @@ impl<'a> ASTVisitor for Resolver<'a> {
let local_name = n.clone(); let local_name = n.clone();
self.name_scope_stack.insert(local_name, member.scopes); self.name_scope_stack.insert(local_name, member.scopes);
} }
}, }
ImportedNames::LastOfPath => { ImportedNames::LastOfPath => {
let name = path_components.last().unwrap(); //TODO handle better let name = path_components.last().unwrap(); //TODO handle better
let fqsn_prefix = path_components.iter() let fqsn_prefix = path_components
.iter()
.map(|c| Scope::Name(c.clone())) .map(|c| Scope::Name(c.clone()))
.collect(); .collect();
self.name_scope_stack.insert(name.clone(), fqsn_prefix); self.name_scope_stack.insert(name.clone(), fqsn_prefix);
} }
ImportedNames::List(ref names) => { ImportedNames::List(ref names) => {
let fqsn_prefix: FqsnPrefix = path_components.iter() let fqsn_prefix: FqsnPrefix = path_components
.iter()
.map(|c| Scope::Name(c.clone())) .map(|c| Scope::Name(c.clone()))
.collect(); .collect();
for name in names.iter() { for name in names.iter() {
self.name_scope_stack.insert(name.clone(), fqsn_prefix.clone()); self.name_scope_stack
.insert(name.clone(), fqsn_prefix.clone());
} }
} }
}; };
@ -89,12 +104,20 @@ impl<'a> ASTVisitor for Resolver<'a> {
fn qualified_name(&mut self, qualified_name: &QualifiedName) { fn qualified_name(&mut self, qualified_name: &QualifiedName) {
let fqsn = self.lookup_name_in_scope(qualified_name); let fqsn = self.lookup_name_in_scope(qualified_name);
self.symbol_table.id_to_fqsn.insert(qualified_name.id.clone(), fqsn); self.symbol_table
.id_to_fqsn
.insert(qualified_name.id.clone(), fqsn);
} }
fn named_struct(&mut self, qualified_name: &QualifiedName, _fields: &[ (Rc<String>, Expression) ]) { fn named_struct(
&mut self,
qualified_name: &QualifiedName,
_fields: &[(Rc<String>, Expression)],
) {
let fqsn = self.lookup_name_in_scope(qualified_name); let fqsn = self.lookup_name_in_scope(qualified_name);
self.symbol_table.id_to_fqsn.insert(qualified_name.id.clone(), fqsn); self.symbol_table
.id_to_fqsn
.insert(qualified_name.id.clone(), fqsn);
} }
fn pattern(&mut self, pat: &Pattern) { fn pattern(&mut self, pat: &Pattern) {
@ -103,7 +126,9 @@ impl<'a> ASTVisitor for Resolver<'a> {
//TODO I think not handling TuplePattern is an oversight //TODO I think not handling TuplePattern is an oversight
TuplePattern(_) => (), TuplePattern(_) => (),
Literal(_) | Ignored => (), Literal(_) | Ignored => (),
TupleStruct(name, _) | Record(name, _) | VarOrName(name) => self.qualified_name_in_pattern(name), TupleStruct(name, _) | Record(name, _) | VarOrName(name) => {
self.qualified_name_in_pattern(name)
}
}; };
} }
} }

View File

@ -1,7 +1,7 @@
use super::{Fqsn, Scope};
use radix_trie::{Trie, TrieCommon, TrieKey}; use radix_trie::{Trie, TrieCommon, TrieKey};
use super::{Scope, Fqsn};
use std::hash::{Hasher, Hash};
use std::collections::hash_map::DefaultHasher; use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
#[derive(Debug)] #[derive(Debug)]
pub struct SymbolTrie(Trie<Fqsn, ()>); pub struct SymbolTrie(Trie<Fqsn, ()>);
@ -31,9 +31,13 @@ impl SymbolTrie {
pub fn get_children(&self, fqsn: &Fqsn) -> Vec<Fqsn> { pub fn get_children(&self, fqsn: &Fqsn) -> Vec<Fqsn> {
let subtrie = match self.0.subtrie(fqsn) { let subtrie = match self.0.subtrie(fqsn) {
Some(s) => s, Some(s) => s,
None => return vec![] None => return vec![],
}; };
let output: Vec<Fqsn> = subtrie.keys().filter(|cur_key| **cur_key != *fqsn).cloned().collect(); let output: Vec<Fqsn> = subtrie
.keys()
.filter(|cur_key| **cur_key != *fqsn)
.cloned()
.collect();
output output
} }
} }
@ -47,13 +51,13 @@ mod test {
Fqsn::from_strs(strs) Fqsn::from_strs(strs)
} }
#[test] #[test]
fn test_trie_insertion() { fn test_trie_insertion() {
let mut trie = SymbolTrie::new(); let mut trie = SymbolTrie::new();
trie.insert(&make_fqsn(&["unrelated","thing"])); trie.insert(&make_fqsn(&["unrelated", "thing"]));
trie.insert(&make_fqsn(&["outer","inner"])); trie.insert(&make_fqsn(&["outer", "inner"]));
trie.insert(&make_fqsn(&["outer","inner", "still_inner"])); trie.insert(&make_fqsn(&["outer", "inner", "still_inner"]));
let children = trie.get_children(&make_fqsn(&["outer", "inner"])); let children = trie.get_children(&make_fqsn(&["outer", "inner"]));
assert_eq!(children.len(), 1); assert_eq!(children.len(), 1);

View File

@ -1,7 +1,7 @@
#![cfg(test)] #![cfg(test)]
use super::*; use super::*;
use assert_matches::assert_matches;
use crate::util::quick_ast; use crate::util::quick_ast;
use assert_matches::assert_matches;
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);
@ -14,7 +14,6 @@ fn make_fqsn(strs: &[&str]) -> Fqsn {
Fqsn::from_strs(strs) Fqsn::from_strs(strs)
} }
#[test] #[test]
fn basic_symbol_table() { fn basic_symbol_table() {
let src = "let a = 10; fn b() { 20 }"; let src = "let a = 10; fn b() { 20 }";
@ -30,8 +29,16 @@ 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.types.table.get(&make_fqsn(&["Option", "Some"])).unwrap(); symbols
symbols.types.table.get(&make_fqsn(&["Option", "None"])).unwrap(); .types
.table
.get(&make_fqsn(&["Option", "Some"]))
.unwrap();
symbols
.types
.table
.get(&make_fqsn(&["Option", "None"]))
.unwrap();
} }
#[test] #[test]
@ -104,7 +111,11 @@ 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.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] #[test]
@ -117,8 +128,16 @@ fn inner_func(arg) {
x + inner_func(x) 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
assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func", "inner_func"])).is_some()); .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] #[test]
@ -137,10 +156,30 @@ 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
assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func", "inner_func"])).is_some()); .fq_names
assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func", "second_inner_func"])).is_some()); .table
assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func", "second_inner_func", "another_inner_func"])).is_some()); .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"
]))
.is_some());
} }
#[test] #[test]
@ -178,7 +217,11 @@ 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.fq_names.table.get(&make_fqsn(&["stuff", "item"])).unwrap(); symbols
.fq_names
.table
.get(&make_fqsn(&["stuff", "item"]))
.unwrap();
} }
#[test] #[test]
@ -204,7 +247,6 @@ fn duplicate_modules() {
SymbolError::DuplicateName { prev_name: pn1, ..}, SymbolError::DuplicateName { prev_name: pn1, ..},
] if pn1 == &Fqsn::from_strs(&["a"]) ] if pn1 == &Fqsn::from_strs(&["a"])
); );
} }
#[test] #[test]
@ -229,5 +271,4 @@ fn duplicate_struct_members() {
type_name, member, ..}, type_name, member, ..},
] if type_name == &Fqsn::from_strs(&["Tarak", "Tarak"]) && member == "mets" ] if type_name == &Fqsn::from_strs(&["Tarak", "Tarak"]) && member == "mets"
); );
} }