nac3core: toplevel no duplicate type var too early

This commit is contained in:
ychenfo 2021-08-29 18:19:29 +08:00
parent 236989defc
commit e2a9bdd8bc
4 changed files with 133 additions and 228 deletions

View File

@ -171,8 +171,7 @@ pub fn parse_type_annotation<T>(
} }
let mut subst = HashMap::new(); let mut subst = HashMap::new();
for (var, ty) in izip!(type_vars.iter(), types.iter()) { for (var, ty) in izip!(type_vars.iter(), types.iter()) {
let id = if let TypeEnum::TVar { id, .. } = &*unifier.get_ty(var.1) let id = if let TypeEnum::TVar { id, .. } = &*unifier.get_ty(*var) {
{
*id *id
} else { } else {
unreachable!() unreachable!()

View File

@ -1,6 +1,7 @@
use std::{ use std::{
borrow::BorrowMut, borrow::BorrowMut,
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
iter::FromIterator,
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
sync::Arc, sync::Arc,
}; };
@ -37,10 +38,7 @@ pub enum TopLevelDef {
// object ID used for TypeEnum // object ID used for TypeEnum
object_id: DefinitionId, object_id: DefinitionId,
/// type variables bounded to the class. /// type variables bounded to the class.
/// the first field in the tuple is the var_id of the type_vars: Vec<Type>,
///original typevar defined in the top level and returned
/// by the symbol resolver
type_vars: Vec<(u32, Type)>,
// class fields // class fields
fields: Vec<(String, Type)>, fields: Vec<(String, Type)>,
// class methods, pointing to the corresponding function definition. // class methods, pointing to the corresponding function definition.
@ -88,8 +86,8 @@ pub struct TopLevelComposer {
pub unifier: Unifier, pub unifier: Unifier,
// primitive store // primitive store
pub primitives_ty: PrimitiveStore, pub primitives_ty: PrimitiveStore,
// keyword list to prevent same custom def class name // keyword list to prevent same user-defined name
pub keyword_list: Vec<String>, pub keyword_list: HashSet<String>,
} }
impl Default for TopLevelComposer { impl Default for TopLevelComposer {
@ -121,7 +119,7 @@ impl TopLevelComposer {
unifier: primitives.1, unifier: primitives.1,
// class_method_to_def_id: Default::default(), // class_method_to_def_id: Default::default(),
// to_be_analyzed_class: Default::default(), // to_be_analyzed_class: Default::default(),
keyword_list: vec![ keyword_list: HashSet::from_iter(vec![
"Generic".into(), "Generic".into(),
"virtual".into(), "virtual".into(),
"list".into(), "list".into(),
@ -132,7 +130,7 @@ impl TopLevelComposer {
"bool".into(), "bool".into(),
"none".into(), "none".into(),
"None".into(), "None".into(),
], ]),
} }
} }
@ -200,6 +198,9 @@ impl TopLevelComposer {
let mut class_method_index_offset = 0; let mut class_method_index_offset = 0;
for b in body { for b in body {
if let ast::StmtKind::FunctionDef { name: method_name, .. } = &b.node { if let ast::StmtKind::FunctionDef { name: method_name, .. } = &b.node {
if self.keyword_list.contains(name) {
return Err("cannot use keyword as a method name".into());
}
let global_class_method_name = let global_class_method_name =
Self::make_class_method_name(class_name.clone(), method_name); Self::make_class_method_name(class_name.clone(), method_name);
if !defined_class_method_name.insert(global_class_method_name.clone()) { if !defined_class_method_name.insert(global_class_method_name.clone()) {
@ -259,6 +260,9 @@ impl TopLevelComposer {
} }
ast::StmtKind::FunctionDef { name, .. } => { ast::StmtKind::FunctionDef { name, .. } => {
if self.keyword_list.contains(name) {
return Err("cannot use keyword as a top level function name".into());
}
let fun_name = name.to_string(); let fun_name = name.to_string();
if !defined_function_name.insert(name.to_string()) { if !defined_function_name.insert(name.to_string()) {
return Err("duplicate top level function define".into()); return Err("duplicate top level function define".into());
@ -285,8 +289,6 @@ impl TopLevelComposer {
} }
/// step 1, analyze the type vars associated with top level class /// step 1, analyze the type vars associated with top level class
/// note that we make a duplicate of the type var returned by symbol resolver
/// since one top level type var may be used at multiple places
fn analyze_top_level_class_type_var(&mut self) -> Result<(), String> { fn analyze_top_level_class_type_var(&mut self) -> Result<(), String> {
let def_list = &self.definition_ast_list; let def_list = &self.definition_ast_list;
let temp_def_list = self.extract_def_list(); let temp_def_list = self.extract_def_list();
@ -373,16 +375,6 @@ impl TopLevelComposer {
return Err("expect unique type variables".into()); return Err("expect unique type variables".into());
} }
// NOTE: create a copy of all type vars for the type vars associated with class
let type_vars = type_vars
.into_iter()
.map(|x| {
// must be type var here after previous check
let dup = duplicate_type_var(unifier, x);
(dup.2, dup.0)
})
.collect_vec();
// add to TopLevelDef // add to TopLevelDef
class_def_type_vars.extend(type_vars); class_def_type_vars.extend(type_vars);
} }
@ -400,6 +392,7 @@ impl TopLevelComposer {
/// put Self class into the ancestors list. We only allow single inheritance /// put Self class into the ancestors list. We only allow single inheritance
fn analyze_top_level_class_bases(&mut self) -> Result<(), String> { fn analyze_top_level_class_bases(&mut self) -> Result<(), String> {
let temp_def_list = self.extract_def_list(); let temp_def_list = self.extract_def_list();
let unifier = self.unifier.borrow_mut();
for (class_def, class_ast) in self.definition_ast_list.iter_mut() { for (class_def, class_ast) in self.definition_ast_list.iter_mut() {
let mut class_def = class_def.write(); let mut class_def = class_def.write();
let (class_bases, class_ancestors, class_resolver, class_id, class_type_vars) = { let (class_bases, class_ancestors, class_resolver, class_id, class_type_vars) = {
@ -446,53 +439,49 @@ impl TopLevelComposer {
let base_ty = parse_ast_to_type_annotation_kinds( let base_ty = parse_ast_to_type_annotation_kinds(
class_resolver, class_resolver,
&temp_def_list, &temp_def_list,
self.unifier.borrow_mut(), unifier,
&self.primitives_ty, &self.primitives_ty,
b, b,
)?; )?;
if let TypeAnnotation::CustomClassKind { id, .. } = &base_ty { if let TypeAnnotation::CustomClassKind { id, .. } = &base_ty {
// check to prevent cyclic base class // TODO: change a way to check to prevent cyclic base class
let all_base = Self::get_all_base(*id, &temp_def_list); let all_base = Self::get_all_base(*id, &temp_def_list);
if all_base.contains(&class_id) { if all_base.contains(&class_id) {
return Err("cyclic base detected".into()); return Err("cyclic base detected".into());
} }
// find the intersection of type vars occured // NOTE: type vars occured in the base type annotation must be
// in the base class type parameter // a subset of the class decalred type annotation
// and the type vars occured in the class generic declaration let base_type_var_ids = get_type_var_contained_in_type_annotation(&base_ty)
let type_var_occured_in_base = .into_iter()
get_type_var_contained_in_type_annotation(&base_ty); .map(|x| {
for type_ann in type_var_occured_in_base { if let TypeAnnotation::TypeVarKind(ty) = x {
if let TypeAnnotation::TypeVarKind(id, ty) = type_ann { get_var_id(ty, unifier)
for (ty_id, class_typvar_ty) in class_type_vars.iter() {
// if they refer to the same top level defined
// type var, we unify them together
if id == *ty_id {
// assert to make sure
assert!(matches!(
self.unifier.get_ty(ty).as_ref(),
TypeEnum::TVar { .. }
));
self.unifier.unify(ty, *class_typvar_ty)?;
}
}
} else { } else {
unreachable!("must be type var annotation") unreachable!("must be type var")
} }
})
.collect::<Result<HashSet<_>, _>>()?;
let class_generic_type_var_ids = class_type_vars
.iter()
.map(|x| get_var_id(*x, unifier))
.collect::<Result<HashSet<_>, _>>()?;
if class_generic_type_var_ids.is_superset(&base_type_var_ids) {
// TODO: this base confirmed
// in what order to push all the ancestors?
// class_ancestors.push(base_ty);
} else {
return Err("base class generic type parameter must be declared".into());
} }
class_ancestors.push(base_ty);
} else { } else {
return Err("class base declaration can only be custom class".into()); return Err("class base declaration can only be custom class".into());
} }
} }
// TODO: ancestors should include all bases, need to rewrite
// push self to the ancestors // push self to the ancestors
class_ancestors.push(make_self_type_annotation( class_ancestors.push(make_self_type_annotation(&temp_def_list, class_id)?)
&temp_def_list,
class_id
)?)
} }
Ok(()) Ok(())
} }
@ -514,6 +503,7 @@ impl TopLevelComposer {
unifier, unifier,
primitives, primitives,
&mut type_var_to_concrete_def, &mut type_var_to_concrete_def,
&self.keyword_list,
)? )?
} }
@ -533,6 +523,7 @@ impl TopLevelComposer {
/// step 4, after class methods are done /// step 4, after class methods are done
fn analyze_top_level_function(&mut self) -> Result<(), String> { fn analyze_top_level_function(&mut self) -> Result<(), String> {
let def_list = &self.definition_ast_list; let def_list = &self.definition_ast_list;
let keyword_list = &self.keyword_list;
let temp_def_list = self.extract_def_list(); let temp_def_list = self.extract_def_list();
let unifier = self.unifier.borrow_mut(); let unifier = self.unifier.borrow_mut();
let primitives_store = &self.primitives_ty; let primitives_store = &self.primitives_ty;
@ -553,18 +544,19 @@ impl TopLevelComposer {
let resolver = resolver.unwrap(); let resolver = resolver.unwrap();
let resolver = resolver.deref(); let resolver = resolver.deref();
// occured type vars should not be handled separately
let mut occured_type_var: HashMap<u32, Type> = HashMap::new();
let mut function_var_map: HashMap<u32, Type> = HashMap::new(); let mut function_var_map: HashMap<u32, Type> = HashMap::new();
let arg_types = { let arg_types = {
// make sure no duplicate parameter // make sure no duplicate parameter
let mut defined_paramter_name: HashSet<String> = HashSet::new(); let mut defined_paramter_name: HashSet<String> = HashSet::new();
let have_unique_fuction_parameter_name = args let have_unique_fuction_parameter_name = args.args.iter().all(|x| {
.args defined_paramter_name.insert(x.node.arg.clone())
.iter() && keyword_list.contains(&x.node.arg)
.all(|x| defined_paramter_name.insert(x.node.arg.clone())); && "self" != x.node.arg
});
if !have_unique_fuction_parameter_name { if !have_unique_fuction_parameter_name {
return Err("top level function have duplicate parameter name".into()); return Err("top level function must have unique parameter names \
and names thould not be the same as the keywords"
.into());
} }
args.args args.args
@ -591,43 +583,21 @@ impl TopLevelComposer {
let type_vars_within = let type_vars_within =
get_type_var_contained_in_type_annotation(&type_annotation) get_type_var_contained_in_type_annotation(&type_annotation)
.into_iter() .into_iter()
.map(|x| { .map(|x| -> Result<(u32, Type), String> {
if let TypeAnnotation::TypeVarKind(id, ty) = x { if let TypeAnnotation::TypeVarKind(ty) = x {
// assert here to make sure the ty is TypeEnum::TVar Ok((get_var_id(ty, unifier)?, ty))
assert!(matches!(
unifier.get_ty(ty).as_ref(),
TypeEnum::TVar { .. }
));
(id, ty)
} else { } else {
unreachable!("must be type var annotation kind") unreachable!("must be type var annotation kind")
} }
}) })
.collect_vec(); .collect::<Result<Vec<_>, _>>()?;
for (top_level_var_id, ty) in type_vars_within { for (id, ty) in type_vars_within {
if let Some(occured_ty) = if let Some(prev_ty) = function_var_map.insert(id, ty) {
occured_type_var.get(&top_level_var_id) // if already have the type inserted, make sure they are the same thing
{ assert_eq!(prev_ty, ty);
// if already occured, we unify this two duplicated
// type var of the same top level type var
unifier.unify(ty, *occured_ty)?;
} else {
// if not, put it into the occured type var hashmap,
// since parse_ast_to_type_var already duplicated it
// we do not need to duplicate it again
occured_type_var.insert(top_level_var_id, ty);
// the type var map to it self
if let TypeEnum::TVar { id: self_id, .. } =
unifier.get_ty(ty).as_ref()
{
function_var_map.insert(*self_id, ty);
} else {
unreachable!("must be type var");
}
} }
} }
// NOTE: get the actual ty after handling the type vars, really? why?
let ty = get_type_from_type_annotation_kinds( let ty = get_type_from_type_annotation_kinds(
temp_def_list.as_ref(), temp_def_list.as_ref(),
unifier, unifier,
@ -662,24 +632,21 @@ impl TopLevelComposer {
let type_vars_within = let type_vars_within =
get_type_var_contained_in_type_annotation(&return_ty_annotation) get_type_var_contained_in_type_annotation(&return_ty_annotation)
.into_iter() .into_iter()
.map(|x| { .map(|x| -> Result<(u32, Type), String> {
if let TypeAnnotation::TypeVarKind(id, ty) = x { if let TypeAnnotation::TypeVarKind(ty) = x {
(id, ty) Ok((get_var_id(ty, unifier)?, ty))
} else { } else {
unreachable!("must be type var here") unreachable!("must be type var here")
} }
}) })
.collect_vec(); .collect::<Result<Vec<_>, _>>()?;
for (top_level_var_id, ty) in type_vars_within { for (id, ty) in type_vars_within {
if let Some(existing_ty) = occured_type_var.get(&top_level_var_id) { if let Some(prev_ty) = function_var_map.insert(id, ty) {
// should not return err here // if already have the type inserted, make sure they are the same thing
unifier.unify(ty, *existing_ty)?; assert_eq!(prev_ty, ty);
} else {
occured_type_var.insert(top_level_var_id, ty);
} }
} }
// NOTE: get the actual ty after handling the type vars, really? why?
let return_ty = get_type_from_type_annotation_kinds( let return_ty = get_type_from_type_annotation_kinds(
&temp_def_list, &temp_def_list,
unifier, unifier,
@ -715,6 +682,7 @@ impl TopLevelComposer {
unifier: &mut Unifier, unifier: &mut Unifier,
primitives: &PrimitiveStore, primitives: &PrimitiveStore,
type_var_to_concrete_def: &mut HashMap<Type, TypeAnnotation>, type_var_to_concrete_def: &mut HashMap<Type, TypeAnnotation>,
keyword_list: &HashSet<String>,
) -> Result<(), String> { ) -> Result<(), String> {
let mut class_def = class_def.write(); let mut class_def = class_def.write();
let ( let (
@ -763,13 +731,10 @@ impl TopLevelComposer {
let (method_dummy_ty, ..) = let (method_dummy_ty, ..) =
Self::get_class_method_def_info(class_methods_def, name)?; Self::get_class_method_def_info(class_methods_def, name)?;
// handle var map, to create a new copy of type var // the method var map can surely include the class's generic parameters
// while tracking the type var associated with class
// from the duplicated type var's var_id to themselves
// include the class type vars like the type var context of the method
let mut method_var_map: HashMap<u32, Type> = class_type_vars_def let mut method_var_map: HashMap<u32, Type> = class_type_vars_def
.iter() .iter()
.map(|(_, ty)| { .map(|ty| {
if let TypeEnum::TVar { id, .. } = unifier.get_ty(*ty).as_ref() { if let TypeEnum::TVar { id, .. } = unifier.get_ty(*ty).as_ref() {
(*id, *ty) (*id, *ty)
} else { } else {
@ -778,18 +743,17 @@ impl TopLevelComposer {
}) })
.collect(); .collect();
// from the def_id of top level type vars to duplicated type vars
// also include the class type var like the type var context of the method
let mut occured_type_vars: HashMap<u32, Type> =
class_type_vars_def.iter().map(|(id, ty)| (*id, *ty)).collect();
let arg_types: Vec<FuncArg> = { let arg_types: Vec<FuncArg> = {
// check method parameters cannot have same name // check method parameters cannot have same name
let mut defined_paramter_name: HashSet<String> = HashSet::new(); let mut defined_paramter_name: HashSet<String> = HashSet::new();
let have_unique_fuction_parameter_name = let have_unique_fuction_parameter_name = args.args.iter().all(|x| {
args.args.iter().all(|x| defined_paramter_name.insert(x.node.arg.clone())); defined_paramter_name.insert(x.node.arg.clone())
&& !keyword_list.contains(&x.node.arg)
});
if !have_unique_fuction_parameter_name { if !have_unique_fuction_parameter_name {
return Err("class method have duplicate parameter name".into()); return Err("class method must have unique parameter names \
and names thould not be the same as the keywords"
.into());
} }
let mut result = Vec::new(); let mut result = Vec::new();
@ -817,26 +781,11 @@ impl TopLevelComposer {
get_type_var_contained_in_type_annotation(&type_ann); get_type_var_contained_in_type_annotation(&type_ann);
// handle the class type var and the method type var // handle the class type var and the method type var
for type_var_within in type_vars_within { for type_var_within in type_vars_within {
if let TypeAnnotation::TypeVarKind(top_level_id, ty) = if let TypeAnnotation::TypeVarKind(ty) = type_var_within {
type_var_within let id = get_var_id(ty, unifier)?;
{ if let Some(prev_ty) = method_var_map.insert(id, ty) {
if let Some(duped_ty) = occured_type_vars.get(&top_level_id) { // if already in the list, make sure they are the same?
// if already occured, not matter if it is class typevar or method typevar, just unify assert_eq!(prev_ty, ty);
unifier.unify(ty, *duped_ty)?;
} else {
// if not insert them to the occured_type_vars and the method_varmap
// note that the content to insert is different
occured_type_vars.insert(top_level_id, ty);
method_var_map.insert(
if let TypeEnum::TVar { id, .. } =
unifier.get_ty(ty).as_ref()
{
*id
} else {
unreachable!("must be type var")
},
ty,
);
} }
} else { } else {
unreachable!("must be type var annotation"); unreachable!("must be type var annotation");
@ -893,24 +842,15 @@ impl TopLevelComposer {
get_type_var_contained_in_type_annotation(&annotation); get_type_var_contained_in_type_annotation(&annotation);
// handle the class type var and the method type var // handle the class type var and the method type var
for type_var_within in type_vars_within { for type_var_within in type_vars_within {
if let TypeAnnotation::TypeVarKind(top_id, ty) = type_var_within { if let TypeAnnotation::TypeVarKind(ty) = type_var_within {
if let Some(duped_ty) = occured_type_vars.get(&top_id) { if let TypeAnnotation::TypeVarKind(ty) = type_var_within {
// if already occured, not matter if it is class typevar or method typevar, just unify let id = get_var_id(ty, unifier)?;
unifier.unify(ty, *duped_ty)?; if let Some(prev_ty) = method_var_map.insert(id, ty) {
// if already in the list, make sure they are the same?
assert_eq!(prev_ty, ty);
}
} else { } else {
// if not insert them to the occured_type_vars and the method_varmap unreachable!("must be type var annotation");
// note that the content to insert is different
occured_type_vars.insert(top_id, ty);
method_var_map.insert(
if let TypeEnum::TVar { id, .. } =
unifier.get_ty(ty).as_ref()
{
*id
} else {
unreachable!("must be type var")
},
ty,
);
} }
} else { } else {
unreachable!("must be type var annotation"); unreachable!("must be type var annotation");
@ -966,16 +906,12 @@ impl TopLevelComposer {
get_type_var_contained_in_type_annotation(&annotation); get_type_var_contained_in_type_annotation(&annotation);
// handle the class type var and the method type var // handle the class type var and the method type var
for type_var_within in type_vars_within { for type_var_within in type_vars_within {
if let TypeAnnotation::TypeVarKind(top_level_id, ty) = if let TypeAnnotation::TypeVarKind(t) = type_var_within
type_var_within
{ {
if let Some(duped_ty) = if !class_type_vars_def.contains(&t) {
occured_type_vars.get(&top_level_id) return Err("class fields can only use type \
{ vars declared as class generic type vars"
// if already occured, not matter if it is class typevar or method typevar, just unify .into());
unifier.unify(ty, *duped_ty)?;
} else {
return Err("this type var is not available inside this function".into());
} }
} else { } else {
unreachable!("must be type var annotation"); unreachable!("must be type var annotation");

View File

@ -3,25 +3,17 @@ use super::*;
#[derive(Clone)] #[derive(Clone)]
pub enum TypeAnnotation { pub enum TypeAnnotation {
PrimitiveKind(Type), PrimitiveKind(Type),
// we use type vars kind at // we use type vars kind at params to represent self type
// params to represent self type
CustomClassKind { CustomClassKind {
id: DefinitionId, id: DefinitionId,
// can not be type var, others are all fine // params can also be type var
// TODO: can also be type var?
params: Vec<TypeAnnotation>, params: Vec<TypeAnnotation>,
}, },
// can only be CustomClassKind // can only be CustomClassKind
VirtualKind(Box<TypeAnnotation>), VirtualKind(Box<TypeAnnotation>),
// the first u32 refers to the var_id of the TypeVarKind(Type),
// TVar returned by the symbol resolver,
// this is used to handle type vars
// associated with class/functions
// since when associating we create a copy of type vars
TypeVarKind(u32, Type),
} }
/// if is typevar, this function will make a copy of it
pub fn parse_ast_to_type_annotation_kinds<T>( pub fn parse_ast_to_type_annotation_kinds<T>(
resolver: &Box<dyn SymbolResolver + Send + Sync>, resolver: &Box<dyn SymbolResolver + Send + Sync>,
top_level_defs: &[Arc<RwLock<TopLevelDef>>], top_level_defs: &[Arc<RwLock<TopLevelDef>>],
@ -52,16 +44,8 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
Err("function cannot be used as a type".into()) Err("function cannot be used as a type".into())
} }
} else if let Some(ty) = resolver.get_symbol_type(unifier, primitives, id) { } else if let Some(ty) = resolver.get_symbol_type(unifier, primitives, id) {
if let TypeEnum::TVar { id, .. } = unifier.get_ty(ty).as_ref() { if let TypeEnum::TVar { .. } = unifier.get_ty(ty).as_ref() {
// NOTE: always create a new one here Ok(TypeAnnotation::TypeVarKind(ty))
// and later unify if needed
// but record the var_id of the original type var
// returned by symbol resolver
Ok(TypeAnnotation::TypeVarKind(
// this id is the id of the top level type var
*id,
duplicate_type_var(unifier, ty).0,
))
} else { } else {
Err("not a type variable identifier".into()) Err("not a type variable identifier".into())
} }
@ -98,35 +82,34 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
.ok_or_else(|| "unknown class name".to_string())?; .ok_or_else(|| "unknown class name".to_string())?;
let def = top_level_defs[obj_id.0].read(); let def = top_level_defs[obj_id.0].read();
if let TopLevelDef::Class { type_vars, .. } = &*def { if let TopLevelDef::Class { type_vars, .. } = &*def {
let param_type_infos = if let ast::ExprKind::Tuple { elts, .. } = &slice.node { let param_type_infos = {
elts.iter() let params_ast = if let ast::ExprKind::Tuple { elts, .. } = &slice.node {
.map(|v| { elts.iter().collect_vec()
} else {
vec![slice.as_ref()]
};
if type_vars.len() != params_ast.len() {
return Err(format!(
"expect {} type parameters but got {}",
type_vars.len(),
params_ast.len()
));
}
params_ast
.into_iter()
.map(|x| {
parse_ast_to_type_annotation_kinds( parse_ast_to_type_annotation_kinds(
resolver, resolver,
top_level_defs, top_level_defs,
unifier, unifier,
primitives, primitives,
v, x,
) )
}) })
.collect::<Result<Vec<_>, _>>()? .collect::<Result<Vec<_>, _>>()?
} else {
vec![parse_ast_to_type_annotation_kinds(
resolver,
top_level_defs,
unifier,
primitives,
slice,
)?]
}; };
if type_vars.len() != param_type_infos.len() {
return Err(format!( // allow type var in class generic application list
"expect {} type parameters but got {}",
type_vars.len(),
param_type_infos.len()
));
}
// NOTE: allow type var in class generic application list
Ok(TypeAnnotation::CustomClassKind { id: obj_id, params: param_type_infos }) Ok(TypeAnnotation::CustomClassKind { id: obj_id, params: param_type_infos })
} else { } else {
Err("function cannot be used as a type".into()) Err("function cannot be used as a type".into())
@ -140,7 +123,6 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
} }
} }
/// if is typeannotation::tvar, this function will NOT make a copy of it
pub fn get_type_from_type_annotation_kinds( pub fn get_type_from_type_annotation_kinds(
top_level_defs: &[Arc<RwLock<TopLevelDef>>], top_level_defs: &[Arc<RwLock<TopLevelDef>>],
unifier: &mut Unifier, unifier: &mut Unifier,
@ -169,11 +151,11 @@ pub fn get_type_from_type_annotation_kinds(
) )
}) })
.collect::<Result<Vec<_>, _>>()?; .collect::<Result<Vec<_>, _>>()?;
// NOTE: cannot directy subst type var here? need to subst types in fields/methods?
let subst = type_vars let subst = type_vars
.iter() .iter()
.map(|x| { .map(|x| {
if let TypeEnum::TVar { id, .. } = unifier.get_ty(x.1).as_ref() { if let TypeEnum::TVar { id, .. } = unifier.get_ty(*x).as_ref() {
// this is for the class generic application, // this is for the class generic application,
// we only need the information for the copied type var // we only need the information for the copied type var
// associated with the class // associated with the class
@ -205,8 +187,7 @@ pub fn get_type_from_type_annotation_kinds(
unreachable!("should be class def here") unreachable!("should be class def here")
} }
} }
TypeAnnotation::PrimitiveKind(ty) => Ok(*ty), TypeAnnotation::PrimitiveKind(ty) | TypeAnnotation::TypeVarKind(ty) => Ok(*ty),
TypeAnnotation::TypeVarKind(_, ty) => Ok(*ty),
TypeAnnotation::VirtualKind(ty) => { TypeAnnotation::VirtualKind(ty) => {
let ty = get_type_from_type_annotation_kinds( let ty = get_type_from_type_annotation_kinds(
top_level_defs, top_level_defs,
@ -219,21 +200,6 @@ pub fn get_type_from_type_annotation_kinds(
} }
} }
/// the first return is the duplicated type \
/// the second return is the var_id of the duplicated type \
/// the third return is the var_id of the original type
pub fn duplicate_type_var(unifier: &mut Unifier, type_var: Type) -> (Type, u32, u32) {
let ty = unifier.get_ty(type_var);
if let TypeEnum::TVar { id, range, .. } = ty.as_ref() {
let range = range.borrow();
let range = range.as_slice();
let dup = unifier.get_fresh_var_with_range(range);
(dup.0, dup.1, *id)
} else {
unreachable!("must be type var here to be duplicated");
}
}
/// given an def id, return a type annotation of self \ /// given an def id, return a type annotation of self \
/// ```python /// ```python
/// class A(Generic[T, V]): /// class A(Generic[T, V]):
@ -254,7 +220,7 @@ pub fn duplicate_type_var(unifier: &mut Unifier, type_var: Type) -> (Type, u32,
/// and unify them with the class generic `T`, `V` /// and unify them with the class generic `T`, `V`
pub fn make_self_type_annotation( pub fn make_self_type_annotation(
top_level_defs: &[Arc<RwLock<TopLevelDef>>], top_level_defs: &[Arc<RwLock<TopLevelDef>>],
def_id: DefinitionId def_id: DefinitionId,
) -> Result<TypeAnnotation, String> { ) -> Result<TypeAnnotation, String> {
let obj_def = let obj_def =
top_level_defs.get(def_id.0).ok_or_else(|| "invalid definition id".to_string())?; top_level_defs.get(def_id.0).ok_or_else(|| "invalid definition id".to_string())?;
@ -264,12 +230,7 @@ pub fn make_self_type_annotation(
if let TopLevelDef::Class { type_vars, .. } = obj_def { if let TopLevelDef::Class { type_vars, .. } = obj_def {
Ok(TypeAnnotation::CustomClassKind { Ok(TypeAnnotation::CustomClassKind {
id: def_id, id: def_id,
params: type_vars params: type_vars.iter().map(|ty| TypeAnnotation::TypeVarKind(*ty)).collect_vec(),
.iter()
// NOTE: here the var_id also points to the var_id of
// the top level defined type var's var id
.map(|(var_id, ty)| TypeAnnotation::TypeVarKind(*var_id, *ty))
.collect_vec(),
}) })
} else { } else {
unreachable!("must be top level class def here") unreachable!("must be top level class def here")
@ -277,7 +238,7 @@ pub fn make_self_type_annotation(
} }
/// get all the occurences of type vars contained in a type annotation /// get all the occurences of type vars contained in a type annotation
/// e.g. `A[int, B[T], V]` => [T, V] /// e.g. `A[int, B[T], V, virtual[C[G]]]` => [T, V, G]
/// this function will not make a duplicate of type var /// this function will not make a duplicate of type var
pub fn get_type_var_contained_in_type_annotation(ann: &TypeAnnotation) -> Vec<TypeAnnotation> { pub fn get_type_var_contained_in_type_annotation(ann: &TypeAnnotation) -> Vec<TypeAnnotation> {
let mut result: Vec<TypeAnnotation> = Vec::new(); let mut result: Vec<TypeAnnotation> = Vec::new();
@ -295,3 +256,12 @@ pub fn get_type_var_contained_in_type_annotation(ann: &TypeAnnotation) -> Vec<Ty
} }
result result
} }
/// get the var_id of a given TVar type
pub fn get_var_id(var_ty: Type, unifier: &mut Unifier) -> Result<u32, String> {
if let TypeEnum::TVar { id, .. } = unifier.get_ty(var_ty).as_ref() {
Ok(*id)
} else {
Err("not type var".to_string())
}
}

View File

@ -175,7 +175,7 @@ impl TestEnvironment {
RwLock::new(TopLevelDef::Class { RwLock::new(TopLevelDef::Class {
name: "Foo".to_string(), name: "Foo".to_string(),
object_id: DefinitionId(5), object_id: DefinitionId(5),
type_vars: vec![(id, v0)], type_vars: vec![v0],
fields: [("a".into(), v0)].into(), fields: [("a".into(), v0)].into(),
methods: Default::default(), methods: Default::default(),
ancestors: Default::default(), ancestors: Default::default(),