Compare commits

...

2 Commits

Author SHA1 Message Date
234823c51a nac3standalone: added typevar test 2022-03-18 16:52:52 +08:00
b97c016629 nac3core: fixed test breakage 2022-03-18 16:52:28 +08:00
4 changed files with 56 additions and 2 deletions

View File

@ -1193,9 +1193,18 @@ impl TopLevelComposer {
unreachable!("must be type var annotation");
}
}
get_type_from_type_annotation_kinds(temp_def_list, unifier, primitives, &annotation)?
let dummy_return_type = unifier.get_dummy_var().0;
type_var_to_concrete_def.insert(dummy_return_type, annotation.clone());
dummy_return_type
} else {
primitives.none
// if do not have return annotation, return none
// for uniform handling, still use type annoatation
let dummy_return_type = unifier.get_dummy_var().0;
type_var_to_concrete_def.insert(
dummy_return_type,
TypeAnnotation::Primitive(primitives.none),
);
dummy_return_type
}
};

View File

@ -105,6 +105,8 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
Ok(TypeAnnotation::CustomClass { id: obj_id, params: vec![] })
} else if let Ok(ty) = resolver.get_symbol_type(unifier, top_level_defs, primitives, *id) {
if let TypeEnum::TVar { .. } = unifier.get_ty(ty).as_ref() {
let var = unifier.get_fresh_var(Some(*id), Some(expr.location)).0;
unifier.unify(var, ty).unwrap();
Ok(TypeAnnotation::TypeVar(ty))
} else {
Err(format!("`{}` is not a valid type annotation (at {})", id, expr.location))

View File

@ -0,0 +1,40 @@
from typing import TypeVar, Generic
@extern
def output_int32(x: int32):
...
class A:
def __init__(self):
pass
def foo(self):
output_int32(1)
class B:
def __init__(self):
pass
def foo(self):
output_int32(2)
T = TypeVar("T", A, B)
class C(Generic[T]):
x: T
def __init__(self, x: T):
self.x = x
def foo(self):
self.x.foo()
def run() -> int32:
insta = A()
inst = C(insta)
inst.foo()
return 0

View File

@ -62,6 +62,9 @@ fn main() {
let parser_result = parser::parse_program(&program, file_name.into()).unwrap();
for stmt in parser_result.into_iter() {
if matches!(stmt.node, StmtKind::Import { .. } | StmtKind::ImportFrom { .. }) {
continue;
}
if let StmtKind::Assign { targets, value, .. } = &stmt.node {
fn handle_typevar_definition(
var: &Expr,