nac3core: toplevel change class method name handling, cleanup comments

This commit is contained in:
ychenfo 2021-09-12 13:14:46 +08:00
parent 2ac3f9a176
commit 471547855e
5 changed files with 34 additions and 32 deletions

View File

@ -85,7 +85,6 @@ impl TopLevelComposer {
resolver: Option<Arc<Box<dyn SymbolResolver + Send + Sync>>>, resolver: Option<Arc<Box<dyn SymbolResolver + Send + Sync>>>,
mod_path: String, mod_path: String,
) -> Result<(String, DefinitionId), String> { ) -> Result<(String, DefinitionId), String> {
// FIXME: different module same name?
let defined_class_name = &mut self.defined_class_name; let defined_class_name = &mut self.defined_class_name;
let defined_class_method_name = &mut self.defined_class_method_name; let defined_class_method_name = &mut self.defined_class_method_name;
let defined_function_name = &mut self.defined_function_name; let defined_function_name = &mut self.defined_function_name;
@ -603,7 +602,8 @@ impl TopLevelComposer {
unifier, unifier,
primitives_store, primitives_store,
annotation, annotation,
// NOTE: since only class need this, for function, it should be fine // NOTE: since only class need this, for function
// it should be fine to be empty map
HashMap::new(), HashMap::new(),
)?; )?;
@ -651,7 +651,8 @@ impl TopLevelComposer {
unifier, unifier,
primitives_store, primitives_store,
return_annotation, return_annotation,
// NOTE: since only class need this, for function, it should be fine // NOTE: since only class need this, for function
// it should be fine to be empty map
HashMap::new(), HashMap::new(),
)? )?
}; };
@ -896,8 +897,8 @@ impl TopLevelComposer {
FunSignature { args: arg_types, ret: ret_type, vars: method_var_map }.into(), FunSignature { args: arg_types, ret: ret_type, vars: method_var_map }.into(),
)); ));
// NOTE: unify now since function type is not in type annotation define // unify now since function type is not in type annotation define
// which is fine since type within method_type will be subst later // which should be fine since type within method_type will be subst later
unifier.unify(method_dummy_ty, method_type)?; unifier.unify(method_dummy_ty, method_type)?;
} else if let ast::StmtKind::AnnAssign { target, annotation, value: None, .. } = &b.node } else if let ast::StmtKind::AnnAssign { target, annotation, value: None, .. } = &b.node
{ {

View File

@ -44,7 +44,7 @@ impl TopLevelDef {
name, name,
unifier.stringify(*signature, obj_to_name, var_to_name), unifier.stringify(*signature, obj_to_name, var_to_name),
{ {
// NOTE: preserve the order for debug output and test // preserve the order for debug output and test
let mut r = var_id.clone(); let mut r = var_id.clone();
r.sort_unstable(); r.sort_unstable();
r r
@ -123,6 +123,7 @@ impl TopLevelComposer {
} }
pub fn make_class_method_name(mut class_name: String, method_name: &str) -> String { pub fn make_class_method_name(mut class_name: String, method_name: &str) -> String {
class_name.push('.');
class_name.push_str(method_name); class_name.push_str(method_name);
class_name class_name
} }

View File

@ -198,19 +198,19 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s
}"}, }"},
indoc! {"6: Function { indoc! {"6: Function {
name: \"A__init__\", name: \"A.__init__\",
sig: \"fn[[], class4]\", sig: \"fn[[], class4]\",
var_id: [] var_id: []
}"}, }"},
indoc! {"7: Function { indoc! {"7: Function {
name: \"Afun\", name: \"A.fun\",
sig: \"fn[[b=class10], class4]\", sig: \"fn[[b=class10], class4]\",
var_id: [] var_id: []
}"}, }"},
indoc! {"8: Function { indoc! {"8: Function {
name: \"Afoo\", name: \"A.foo\",
sig: \"fn[[a=tvar2, b=tvar3], class4]\", sig: \"fn[[a=tvar2, b=tvar3], class4]\",
var_id: [2, 3] var_id: [2, 3]
}"}, }"},
@ -227,7 +227,7 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s
}"}, }"},
indoc! {"11: Function { indoc! {"11: Function {
name: \"B__init__\", name: \"B.__init__\",
sig: \"fn[[], class4]\", sig: \"fn[[], class4]\",
var_id: [] var_id: []
}"}, }"},
@ -244,13 +244,13 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s
}"}, }"},
indoc! {"14: Function { indoc! {"14: Function {
name: \"C__init__\", name: \"C.__init__\",
sig: \"fn[[], class4]\", sig: \"fn[[], class4]\",
var_id: [] var_id: []
}"}, }"},
indoc! {"15: Function { indoc! {"15: Function {
name: \"Cfun\", name: \"C.fun\",
sig: \"fn[[b=class10], class4]\", sig: \"fn[[b=class10], class4]\",
var_id: [] var_id: []
}"}, }"},
@ -301,13 +301,13 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s
}"}, }"},
indoc! {"6: Function { indoc! {"6: Function {
name: \"Generic_A__init__\", name: \"Generic_A.__init__\",
sig: \"fn[[], class4]\", sig: \"fn[[], class4]\",
var_id: [3] var_id: [3]
}"}, }"},
indoc! {"7: Function { indoc! {"7: Function {
name: \"Generic_Afun\", name: \"Generic_A.fun\",
sig: \"fn[[a=class0], tvar3]\", sig: \"fn[[a=class0], tvar3]\",
var_id: [3] var_id: [3]
}"}, }"},
@ -324,13 +324,13 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s
}"}, }"},
indoc! {"10: Function { indoc! {"10: Function {
name: \"B__init__\", name: \"B.__init__\",
sig: \"fn[[], class4]\", sig: \"fn[[], class4]\",
var_id: [] var_id: []
}"}, }"},
indoc! {"11: Function { indoc! {"11: Function {
name: \"Bfoo\", name: \"B.foo\",
sig: \"fn[[b=tvar2], class4]\", sig: \"fn[[b=tvar2], class4]\",
var_id: [2] var_id: [2]
}"}, }"},
@ -382,13 +382,13 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s
}"}, }"},
indoc! {"7: Function { indoc! {"7: Function {
name: \"A__init__\", name: \"A.__init__\",
sig: \"fn[[v=tvar3], class4]\", sig: \"fn[[v=tvar3], class4]\",
var_id: [2, 3] var_id: [2, 3]
}"}, }"},
indoc! {"8: Function { indoc! {"8: Function {
name: \"Afun\", name: \"A.fun\",
sig: \"fn[[a=tvar2], tvar3]\", sig: \"fn[[a=tvar2], tvar3]\",
var_id: [2, 3] var_id: [2, 3]
}"}, }"},
@ -411,7 +411,7 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s
}"}, }"},
indoc! {"12: Function { indoc! {"12: Function {
name: \"B__init__\", name: \"B.__init__\",
sig: \"fn[[], class4]\", sig: \"fn[[], class4]\",
var_id: [] var_id: []
}"}, }"},
@ -453,13 +453,13 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s
}"}, }"},
indoc! {"6: Function { indoc! {"6: Function {
name: \"A__init__\", name: \"A.__init__\",
sig: \"fn[[a=class5[2->class2, 3->class3], b=class9], class4]\", sig: \"fn[[a=class5[2->class2, 3->class3], b=class9], class4]\",
var_id: [2, 3] var_id: [2, 3]
}"}, }"},
indoc! {"7: Function { indoc! {"7: Function {
name: \"Afun\", name: \"A.fun\",
sig: \"fn[[a=class5[2->class2, 3->class3]], class5[2->class3, 3->class0]]\", sig: \"fn[[a=class5[2->class2, 3->class3]], class5[2->class3, 3->class0]]\",
var_id: [2, 3] var_id: [2, 3]
}"}, }"},
@ -476,19 +476,19 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s
}"}, }"},
indoc! {"10: Function { indoc! {"10: Function {
name: \"B__init__\", name: \"B.__init__\",
sig: \"fn[[], class4]\", sig: \"fn[[], class4]\",
var_id: [] var_id: []
}"}, }"},
indoc! {"11: Function { indoc! {"11: Function {
name: \"Bfoo\", name: \"B.foo\",
sig: \"fn[[b=class9], class9]\", sig: \"fn[[b=class9], class9]\",
var_id: [] var_id: []
}"}, }"},
indoc! {"12: Function { indoc! {"12: Function {
name: \"Bbar\", name: \"B.bar\",
sig: \"fn[[a=class5[2->list[class9], 3->class0]], tuple[class5[2->virtual[class5[2->class9, 3->class0]], 3->class3], class9]]\", sig: \"fn[[a=class5[2->list[class9], 3->class0]], tuple[class5[2->virtual[class5[2->class9, 3->class0]], 3->class3], class9]]\",
var_id: [] var_id: []
}"}, }"},
@ -539,19 +539,19 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s
}"}, }"},
indoc! {"6: Function { indoc! {"6: Function {
name: \"A__init__\", name: \"A.__init__\",
sig: \"fn[[t=tvar2], class4]\", sig: \"fn[[t=tvar2], class4]\",
var_id: [2] var_id: [2]
}"}, }"},
indoc! {"7: Function { indoc! {"7: Function {
name: \"Afun\", name: \"A.fun\",
sig: \"fn[[a=class0, b=tvar2], list[virtual[class10[3->class3]]]]\", sig: \"fn[[a=class0, b=tvar2], list[virtual[class10[3->class3]]]]\",
var_id: [2] var_id: [2]
}"}, }"},
indoc! {"8: Function { indoc! {"8: Function {
name: \"Afoo\", name: \"A.foo\",
sig: \"fn[[c=class14], class4]\", sig: \"fn[[c=class14], class4]\",
var_id: [2] var_id: [2]
}"}, }"},
@ -568,13 +568,13 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s
}"}, }"},
indoc! {"11: Function { indoc! {"11: Function {
name: \"B__init__\", name: \"B.__init__\",
sig: \"fn[[], class4]\", sig: \"fn[[], class4]\",
var_id: [3] var_id: [3]
}"}, }"},
indoc! {"12: Function { indoc! {"12: Function {
name: \"Bfun\", name: \"B.fun\",
sig: \"fn[[a=class0, b=tvar2], list[virtual[class10[3->class3]]]]\", sig: \"fn[[a=class0, b=tvar2], list[virtual[class10[3->class3]]]]\",
var_id: [2, 3] var_id: [2, 3]
}"}, }"},
@ -591,7 +591,7 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s
}"}, }"},
indoc! {"15: Function { indoc! {"15: Function {
name: \"C__init__\", name: \"C.__init__\",
sig: \"fn[[], class4]\", sig: \"fn[[], class4]\",
var_id: [] var_id: []
}"}, }"},

View File

@ -244,7 +244,7 @@ pub fn get_type_from_type_annotation_kinds(
unifier.get_ty(*tvar).as_ref() unifier.get_ty(*tvar).as_ref()
{ {
let ok: bool = { let ok: bool = {
// NOTE: create a temp type var and unify to check compatibility // create a temp type var and unify to check compatibility
let temp = let temp =
unifier.get_fresh_var_with_range(range.borrow().as_slice()); unifier.get_fresh_var_with_range(range.borrow().as_slice());
unifier.unify(temp.0, p).is_ok() unifier.unify(temp.0, p).is_ok()

View File

@ -717,7 +717,7 @@ impl Unifier {
let params = params.iter().map(|(id, v)| { let params = params.iter().map(|(id, v)| {
format!("{}->{}", *id, self.stringify(*v, obj_to_name, var_to_name)) format!("{}->{}", *id, self.stringify(*v, obj_to_name, var_to_name))
}); });
// NOTE: sort to preserve order // sort to preserve order
let mut params = params.sorted(); let mut params = params.sorted();
format!("{}[{}]", name, params.join(", ")) format!("{}[{}]", name, params.join(", "))
} else { } else {