nac3core: top level fix function/methods none return type

This commit is contained in:
ychenfo 2021-08-31 15:41:48 +08:00
parent 98d032b72a
commit 1ae6acc061
2 changed files with 77 additions and 63 deletions

View File

@ -678,11 +678,10 @@ impl TopLevelComposer {
.collect::<Result<Vec<_>, _>>()?
};
let return_ty = {
if let Some(returns) = returns {
let return_ty_annotation = {
let return_annotation = returns
.as_ref()
.ok_or_else(|| "function return type needed".to_string())?
.as_ref();
let return_annotation = returns.as_ref();
parse_ast_to_type_annotation_kinds(
resolver,
&temp_def_list,
@ -710,13 +709,16 @@ impl TopLevelComposer {
}
}
let return_ty = get_type_from_type_annotation_kinds(
get_type_from_type_annotation_kinds(
&temp_def_list,
unifier,
primitives_store,
&return_ty_annotation,
)?;
)?
} else {
primitives_store.none
}
};
let function_ty = unifier.add_ty(TypeEnum::TFunc(
FunSignature { args: arg_types, ret: return_ty, vars: function_var_map }
.into(),
@ -883,10 +885,8 @@ impl TopLevelComposer {
let ret_type = {
if name != "__init__" {
let result = returns
.as_ref()
.ok_or_else(|| "method return type annotation needed".to_string())?
.as_ref();
if let Some(result) = returns {
let result = result.as_ref();
let annotation = parse_ast_to_type_annotation_kinds(
class_resolver,
temp_def_list,
@ -914,6 +914,16 @@ impl TopLevelComposer {
let dummy_return_type = unifier.get_fresh_var().0;
type_var_to_concrete_def.insert(dummy_return_type, annotation.clone());
dummy_return_type
} else {
// if do not have return annotation, return none
// for uniform handling, still use type annoatation
let dummy_return_type = unifier.get_fresh_var().0;
type_var_to_concrete_def.insert(
dummy_return_type,
TypeAnnotation::PrimitiveKind(primitives.none),
);
dummy_return_type
}
} else {
// if is the "__init__" function, the return type is self
let dummy_return_type = unifier.get_fresh_var().0;

View File

@ -56,6 +56,10 @@ impl SymbolResolver for Resolver {
def fun(self):
self.b = self.b + 3.0
"},
indoc! {"
def foo(a: float):
a + 1.0
"}
]
)]