forked from M-Labs/nac3
symbol_resolver: fixed type variable handling
This commit is contained in:
parent
de8b67b605
commit
d46a4b2d38
|
@ -22,19 +22,22 @@ pub enum SymbolValue {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait SymbolResolver {
|
pub trait SymbolResolver {
|
||||||
|
// get type of type variable identifier or top-level function type
|
||||||
fn get_symbol_type(
|
fn get_symbol_type(
|
||||||
&self,
|
&self,
|
||||||
unifier: &mut Unifier,
|
unifier: &mut Unifier,
|
||||||
primitives: &PrimitiveStore,
|
primitives: &PrimitiveStore,
|
||||||
str: &str,
|
str: &str,
|
||||||
) -> Option<Type>;
|
) -> Option<Type>;
|
||||||
fn get_identifier_def(&self, str: &str) -> DefinitionId;
|
// get the top-level definition of identifiers
|
||||||
|
fn get_identifier_def(&self, str: &str) -> Option<DefinitionId>;
|
||||||
fn get_symbol_value(&self, str: &str) -> Option<SymbolValue>;
|
fn get_symbol_value(&self, str: &str) -> Option<SymbolValue>;
|
||||||
fn get_symbol_location(&self, str: &str) -> Option<Location>;
|
fn get_symbol_location(&self, str: &str) -> Option<Location>;
|
||||||
// handle function call etc.
|
// handle function call etc.
|
||||||
}
|
}
|
||||||
|
|
||||||
impl dyn SymbolResolver {
|
impl dyn SymbolResolver {
|
||||||
|
// convert type annotation into type
|
||||||
pub fn parse_type_annotation<T>(
|
pub fn parse_type_annotation<T>(
|
||||||
&self,
|
&self,
|
||||||
top_level: &TopLevelContext,
|
top_level: &TopLevelContext,
|
||||||
|
@ -52,6 +55,7 @@ impl dyn SymbolResolver {
|
||||||
"None" => Ok(primitives.none),
|
"None" => Ok(primitives.none),
|
||||||
x => {
|
x => {
|
||||||
let obj_id = self.get_identifier_def(x);
|
let obj_id = self.get_identifier_def(x);
|
||||||
|
if let Some(obj_id) = obj_id {
|
||||||
let defs = top_level.definitions.read();
|
let defs = top_level.definitions.read();
|
||||||
let def = defs[obj_id.0].read();
|
let def = defs[obj_id.0].read();
|
||||||
if let TopLevelDef::Class { fields, methods, type_vars, .. } = &*def {
|
if let TopLevelDef::Class { fields, methods, type_vars, .. } = &*def {
|
||||||
|
@ -76,6 +80,17 @@ impl dyn SymbolResolver {
|
||||||
} else {
|
} else {
|
||||||
Err("Cannot use function name as type".into())
|
Err("Cannot use function name as type".into())
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// it could be a type variable
|
||||||
|
let ty = self
|
||||||
|
.get_symbol_type(unifier, primitives, x)
|
||||||
|
.ok_or_else(|| "Cannot use function name as type".to_owned())?;
|
||||||
|
if let TypeEnum::TVar { .. } = &*unifier.get_ty(ty) {
|
||||||
|
Ok(ty)
|
||||||
|
} else {
|
||||||
|
Err(format!("Unknown type annotation {}", x))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Subscript { value, slice, .. } => {
|
Subscript { value, slice, .. } => {
|
||||||
|
@ -95,7 +110,9 @@ impl dyn SymbolResolver {
|
||||||
vec![self.parse_type_annotation(top_level, unifier, primitives, slice)?]
|
vec![self.parse_type_annotation(top_level, unifier, primitives, slice)?]
|
||||||
};
|
};
|
||||||
|
|
||||||
let obj_id = self.get_identifier_def(id);
|
let obj_id = self
|
||||||
|
.get_identifier_def(id)
|
||||||
|
.ok_or_else(|| format!("Unknown type annotation {}", id))?;
|
||||||
let defs = top_level.definitions.read();
|
let defs = top_level.definitions.read();
|
||||||
let def = defs[obj_id.0].read();
|
let def = defs[obj_id.0].read();
|
||||||
if let TopLevelDef::Class { fields, methods, type_vars, .. } = &*def {
|
if let TopLevelDef::Class { fields, methods, type_vars, .. } = &*def {
|
||||||
|
|
|
@ -28,8 +28,8 @@ impl SymbolResolver for Resolver {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_identifier_def(&self, id: &str) -> DefinitionId {
|
fn get_identifier_def(&self, id: &str) -> Option<DefinitionId> {
|
||||||
self.id_to_def.get(id).cloned().unwrap()
|
self.id_to_def.get(id).cloned()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue