Compare commits
2 Commits
14a5c7981e
...
234823c51a
Author | SHA1 | Date | |
---|---|---|---|
234823c51a | |||
b97c016629 |
@ -1193,9 +1193,18 @@ impl TopLevelComposer {
|
|||||||
unreachable!("must be type var annotation");
|
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 {
|
} 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
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -105,6 +105,8 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
|
|||||||
Ok(TypeAnnotation::CustomClass { id: obj_id, params: vec![] })
|
Ok(TypeAnnotation::CustomClass { id: obj_id, params: vec![] })
|
||||||
} else if let Ok(ty) = resolver.get_symbol_type(unifier, top_level_defs, primitives, *id) {
|
} 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() {
|
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))
|
Ok(TypeAnnotation::TypeVar(ty))
|
||||||
} else {
|
} else {
|
||||||
Err(format!("`{}` is not a valid type annotation (at {})", id, expr.location))
|
Err(format!("`{}` is not a valid type annotation (at {})", id, expr.location))
|
||||||
|
40
nac3standalone/demo/src/typevar.py
Normal file
40
nac3standalone/demo/src/typevar.py
Normal 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
|
||||||
|
|
@ -62,6 +62,9 @@ fn main() {
|
|||||||
let parser_result = parser::parse_program(&program, file_name.into()).unwrap();
|
let parser_result = parser::parse_program(&program, file_name.into()).unwrap();
|
||||||
|
|
||||||
for stmt in parser_result.into_iter() {
|
for stmt in parser_result.into_iter() {
|
||||||
|
if matches!(stmt.node, StmtKind::Import { .. } | StmtKind::ImportFrom { .. }) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if let StmtKind::Assign { targets, value, .. } = &stmt.node {
|
if let StmtKind::Assign { targets, value, .. } = &stmt.node {
|
||||||
fn handle_typevar_definition(
|
fn handle_typevar_definition(
|
||||||
var: &Expr,
|
var: &Expr,
|
||||||
|
Loading…
Reference in New Issue
Block a user