Run rustfmt on symbol_table code
This commit is contained in:
parent
93d0a2cd7d
commit
fb31687dea
@ -1,10 +1,13 @@
|
||||
use std::collections::{HashMap, hash_map::Entry};
|
||||
use std::rc::Rc;
|
||||
use std::collections::{hash_map::Entry, HashMap};
|
||||
use std::fmt;
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::tokenizing::Location;
|
||||
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;
|
||||
|
||||
mod resolver;
|
||||
@ -35,19 +38,15 @@ impl Fqsn {
|
||||
for s in strs {
|
||||
scopes.push(Scope::Name(Rc::new(s.to_string())));
|
||||
}
|
||||
Fqsn {
|
||||
scopes
|
||||
Fqsn { scopes }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//TODO eventually this should use ItemId's to avoid String-cloning
|
||||
/// One segment within a scope.
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
|
||||
enum Scope {
|
||||
Name(Rc<String>)
|
||||
Name(Rc<String>),
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
@ -55,20 +54,20 @@ enum Scope {
|
||||
pub enum SymbolError {
|
||||
DuplicateName {
|
||||
prev_name: Fqsn,
|
||||
location: Location
|
||||
location: Location,
|
||||
},
|
||||
DuplicateRecord {
|
||||
type_name: Fqsn,
|
||||
location: Location,
|
||||
member: String,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug)]
|
||||
struct NameSpec<K> {
|
||||
location: Location,
|
||||
kind: K
|
||||
kind: K,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -83,19 +82,22 @@ struct TypeKind;
|
||||
|
||||
/// Keeps track of what names were used in a given namespace.
|
||||
struct NameTable<K> {
|
||||
table: HashMap<Fqsn, NameSpec<K>>
|
||||
table: HashMap<Fqsn, NameSpec<K>>,
|
||||
}
|
||||
|
||||
impl<K> NameTable<K> {
|
||||
fn new() -> Self {
|
||||
Self { table: HashMap::new() }
|
||||
Self {
|
||||
table: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn register(&mut self, name: Fqsn, spec: NameSpec<K>) -> Result<(), SymbolError> {
|
||||
match self.table.entry(name.clone()) {
|
||||
Entry::Occupied(o) => {
|
||||
Err(SymbolError::DuplicateName { prev_name: name, location: o.get().location })
|
||||
},
|
||||
Entry::Occupied(o) => Err(SymbolError::DuplicateName {
|
||||
prev_name: name,
|
||||
location: o.get().location,
|
||||
}),
|
||||
Entry::Vacant(v) => {
|
||||
v.insert(spec);
|
||||
Ok(())
|
||||
@ -138,7 +140,6 @@ impl SymbolTable {
|
||||
/// different ways and populate subtables with information that will be used further in the
|
||||
/// compilation process.
|
||||
pub fn process_ast(&mut self, ast: &ast::AST) -> Result<(), Vec<SymbolError>> {
|
||||
|
||||
let errs = self.populate_name_tables(ast);
|
||||
if !errs.is_empty() {
|
||||
return Err(errs);
|
||||
@ -188,14 +189,27 @@ impl fmt::Display for SymbolSpec {
|
||||
use self::SymbolSpec::*;
|
||||
match self {
|
||||
Func(type_names) => write!(f, "Func({:?})", type_names),
|
||||
DataConstructor { 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),
|
||||
DataConstructor {
|
||||
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"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl SymbolTable {
|
||||
/* note: this adds names for *forward reference* but doesn't actually create any types. solve that problem
|
||||
* later */
|
||||
@ -223,15 +237,24 @@ impl SymbolTable {
|
||||
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![];
|
||||
|
||||
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;
|
||||
if let Err(err) = self.add_single_statement(kind, location, scope_stack) {
|
||||
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 {
|
||||
StatementKind::Declaration(Declaration::FuncDecl(signature, body)) => {
|
||||
let new_scope = Scope::Name(signature.name.clone());
|
||||
@ -247,10 +270,12 @@ impl SymbolTable {
|
||||
scope_stack.pop();
|
||||
output
|
||||
}
|
||||
StatementKind::Declaration(Declaration::TypeDecl { name, body, mutable }) => {
|
||||
self.add_type_members(name, body, mutable, location, scope_stack)
|
||||
}
|
||||
_ => vec![]
|
||||
StatementKind::Declaration(Declaration::TypeDecl {
|
||||
name,
|
||||
body,
|
||||
mutable,
|
||||
}) => self.add_type_members(name, body, mutable, location, scope_stack),
|
||||
_ => vec![],
|
||||
};
|
||||
errors.extend(recursive_errs.into_iter());
|
||||
}
|
||||
@ -259,61 +284,128 @@ impl SymbolTable {
|
||||
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 {
|
||||
StatementKind::Declaration(Declaration::FuncSig(signature)) => {
|
||||
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.types.register(fq_function.clone(), NameSpec { location, kind: TypeKind } )?;
|
||||
self.fq_names.register(
|
||||
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(),
|
||||
spec: SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
StatementKind::Declaration(Declaration::FuncDecl(signature, ..)) => {
|
||||
let fn_name = &signature.name;
|
||||
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.types.register(fq_function.clone(), NameSpec { location, kind: TypeKind } )?;
|
||||
self.fq_names.register(
|
||||
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(),
|
||||
spec: SymbolSpec::Func(vec![]), //TODO does this inner vec need to exist at all?
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
StatementKind::Declaration(Declaration::TypeDecl { name, .. }) => {
|
||||
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, .. }) => {
|
||||
let fq_binding = Fqsn::from_scope_stack(scope_stack, name.clone());
|
||||
self.fq_names.register(fq_binding.clone(), NameSpec { location, kind: NameKind::Binding })?;
|
||||
self.add_symbol(fq_binding, Symbol {
|
||||
self.fq_names.register(
|
||||
fq_binding.clone(),
|
||||
NameSpec {
|
||||
location,
|
||||
kind: NameKind::Binding,
|
||||
},
|
||||
)?;
|
||||
self.add_symbol(
|
||||
fq_binding,
|
||||
Symbol {
|
||||
local_name: name.clone(),
|
||||
spec: SymbolSpec::Binding,
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
StatementKind::Module(ModuleSpecifier { name, .. }) => {
|
||||
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(())
|
||||
}
|
||||
|
||||
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 errors = vec![];
|
||||
|
||||
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) {
|
||||
errors.push(err);
|
||||
} else {
|
||||
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"),
|
||||
};
|
||||
let symbol = Symbol { local_name, spec };
|
||||
@ -335,7 +427,7 @@ impl SymbolTable {
|
||||
type_name: name.clone(),
|
||||
};
|
||||
register(fq_name, spec);
|
||||
},
|
||||
}
|
||||
Variant::TupleStruct(name, items) => {
|
||||
let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone());
|
||||
let spec = SymbolSpec::DataConstructor {
|
||||
@ -344,7 +436,7 @@ impl SymbolTable {
|
||||
type_name: name.clone(),
|
||||
};
|
||||
register(fq_name, spec);
|
||||
},
|
||||
}
|
||||
Variant::Record { name, members } => {
|
||||
let fq_name = Fqsn::from_scope_stack(scope_stack.as_ref(), name.clone());
|
||||
|
||||
@ -358,17 +450,26 @@ impl SymbolTable {
|
||||
location,
|
||||
member: member_name.as_ref().to_string(),
|
||||
});
|
||||
},
|
||||
}
|
||||
//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 {
|
||||
index,
|
||||
type_name: name.clone(),
|
||||
members: members.iter()
|
||||
.map(|(_, _)| (Rc::new("DUMMY_FIELD".to_string()), Rc::new("DUMMY_TYPE_ID".to_string()))).collect()
|
||||
members: members
|
||||
.iter()
|
||||
.map(|(_, _)| {
|
||||
(
|
||||
Rc::new("DUMMY_FIELD".to_string()),
|
||||
Rc::new("DUMMY_TYPE_ID".to_string()),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
};
|
||||
register(fq_name, spec);
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use crate::symbol_table::{SymbolTable, Fqsn, Scope};
|
||||
use crate::ast::*;
|
||||
use crate::symbol_table::{Fqsn, Scope, SymbolTable};
|
||||
use crate::util::ScopeStack;
|
||||
|
||||
type FqsnPrefix = Vec<Scope>;
|
||||
@ -14,7 +14,10 @@ pub struct Resolver<'a> {
|
||||
impl<'a> Resolver<'a> {
|
||||
pub fn new(symbol_table: &'a mut SymbolTable) -> Self {
|
||||
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) {
|
||||
walk_ast(self, ast);
|
||||
@ -24,21 +27,21 @@ impl<'a> Resolver<'a> {
|
||||
let QualifiedName { components, .. } = sym_name;
|
||||
let first_component = &components[0];
|
||||
match self.name_scope_stack.lookup(first_component) {
|
||||
None => {
|
||||
Fqsn {
|
||||
scopes: components.iter()
|
||||
None => Fqsn {
|
||||
scopes: components
|
||||
.iter()
|
||||
.map(|name| Scope::Name(name.clone()))
|
||||
.collect()
|
||||
}
|
||||
.collect(),
|
||||
},
|
||||
Some(fqsn_prefix) => {
|
||||
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);
|
||||
|
||||
Fqsn {
|
||||
scopes: full_name
|
||||
}
|
||||
Fqsn { scopes: full_name }
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -48,7 +51,9 @@ impl<'a> Resolver<'a> {
|
||||
fn qualified_name_in_pattern(&mut self, qualified_name: &QualifiedName) {
|
||||
let fqsn = self.lookup_name_in_scope(qualified_name);
|
||||
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> {
|
||||
//TODO need to un-insert these - maybe need to rethink visitor
|
||||
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 {
|
||||
ImportedNames::All => {
|
||||
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);
|
||||
for member in members.into_iter() {
|
||||
@ -68,20 +80,23 @@ impl<'a> ASTVisitor for Resolver<'a> {
|
||||
let local_name = n.clone();
|
||||
self.name_scope_stack.insert(local_name, member.scopes);
|
||||
}
|
||||
},
|
||||
}
|
||||
ImportedNames::LastOfPath => {
|
||||
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()))
|
||||
.collect();
|
||||
self.name_scope_stack.insert(name.clone(), fqsn_prefix);
|
||||
}
|
||||
ImportedNames::List(ref names) => {
|
||||
let fqsn_prefix: FqsnPrefix = path_components.iter()
|
||||
let fqsn_prefix: FqsnPrefix = path_components
|
||||
.iter()
|
||||
.map(|c| Scope::Name(c.clone()))
|
||||
.collect();
|
||||
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) {
|
||||
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);
|
||||
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) {
|
||||
@ -103,7 +126,9 @@ impl<'a> ASTVisitor for Resolver<'a> {
|
||||
//TODO I think not handling TuplePattern is an oversight
|
||||
TuplePattern(_) => (),
|
||||
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)
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::{Fqsn, Scope};
|
||||
use radix_trie::{Trie, TrieCommon, TrieKey};
|
||||
use super::{Scope, Fqsn};
|
||||
use std::hash::{Hasher, Hash};
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SymbolTrie(Trie<Fqsn, ()>);
|
||||
@ -31,9 +31,13 @@ impl SymbolTrie {
|
||||
pub fn get_children(&self, fqsn: &Fqsn) -> Vec<Fqsn> {
|
||||
let subtrie = match self.0.subtrie(fqsn) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
#![cfg(test)]
|
||||
use super::*;
|
||||
use assert_matches::assert_matches;
|
||||
use crate::util::quick_ast;
|
||||
use assert_matches::assert_matches;
|
||||
|
||||
fn add_symbols(src: &str) -> (SymbolTable, Result<(), Vec<SymbolError>>) {
|
||||
let ast = quick_ast(src);
|
||||
@ -14,7 +14,6 @@ fn make_fqsn(strs: &[&str]) -> Fqsn {
|
||||
Fqsn::from_strs(strs)
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn basic_symbol_table() {
|
||||
let src = "let a = 10; fn b() { 20 }";
|
||||
@ -30,8 +29,16 @@ 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]
|
||||
@ -104,7 +111,11 @@ 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]
|
||||
@ -117,8 +128,16 @@ 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]
|
||||
@ -137,10 +156,30 @@ 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", "second_inner_func", "another_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"
|
||||
]))
|
||||
.is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -178,7 +217,11 @@ 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]
|
||||
@ -204,7 +247,6 @@ fn duplicate_modules() {
|
||||
SymbolError::DuplicateName { prev_name: pn1, ..},
|
||||
] if pn1 == &Fqsn::from_strs(&["a"])
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -229,5 +271,4 @@ fn duplicate_struct_members() {
|
||||
type_name, member, ..},
|
||||
] if type_name == &Fqsn::from_strs(&["Tarak", "Tarak"]) && member == "mets"
|
||||
);
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user