nac3artiq/nac3core: remove forwardref type annotation support for unstable python API

This commit is contained in:
ychenfo 2021-11-21 05:07:36 +08:00
parent dab06bdb58
commit 1c5e68aca9
3 changed files with 8 additions and 28 deletions

View File

@ -40,10 +40,8 @@ struct PythonHelper<'a> {
type_fn: &'a PyAny, type_fn: &'a PyAny,
len_fn: &'a PyAny, len_fn: &'a PyAny,
id_fn: &'a PyAny, id_fn: &'a PyAny,
eval_type_fn: &'a PyAny,
origin_ty_fn: &'a PyAny, origin_ty_fn: &'a PyAny,
args_ty_fn: &'a PyAny, args_ty_fn: &'a PyAny,
globals_dict: &'a PyAny,
} }
impl Resolver { impl Resolver {
@ -87,17 +85,13 @@ impl Resolver {
defs: &[Arc<RwLock<TopLevelDef>>], defs: &[Arc<RwLock<TopLevelDef>>],
primitives: &PrimitiveStore, primitives: &PrimitiveStore,
) -> PyResult<Result<(Type, bool), String>> { ) -> PyResult<Result<(Type, bool), String>> {
// eval_type use only globals_dict should be fine
let evaluated_ty = helper
.eval_type_fn
.call1((pyty, helper.globals_dict, helper.globals_dict)).unwrap();
let ty_id: u64 = helper let ty_id: u64 = helper
.id_fn .id_fn
.call1((evaluated_ty,))? .call1((pyty,))?
.extract()?; .extract()?;
let ty_ty_id: u64 = helper let ty_ty_id: u64 = helper
.id_fn .id_fn
.call1((helper.type_fn.call1((evaluated_ty,))?,))? .call1((helper.type_fn.call1((pyty,))?,))?
.extract()?; .extract()?;
if ty_id == self.primitive_ids.int || ty_id == self.primitive_ids.int32 { if ty_id == self.primitive_ids.int || ty_id == self.primitive_ids.int32 {
@ -188,8 +182,8 @@ impl Resolver {
let res = unifier.get_fresh_var_with_range(&constraint_types).0; let res = unifier.get_fresh_var_with_range(&constraint_types).0;
Ok(Ok((res, true))) Ok(Ok((res, true)))
} else if ty_ty_id == self.primitive_ids.generic_alias.0 || ty_ty_id == self.primitive_ids.generic_alias.1 { } else if ty_ty_id == self.primitive_ids.generic_alias.0 || ty_ty_id == self.primitive_ids.generic_alias.1 {
let origin = helper.origin_ty_fn.call1((evaluated_ty,))?; let origin = helper.origin_ty_fn.call1((pyty,))?;
let args: &PyTuple = helper.args_ty_fn.call1((evaluated_ty,))?.cast_as()?; let args: &PyTuple = helper.args_ty_fn.call1((pyty,))?.cast_as()?;
let origin_ty = match self.get_pyty_obj_type(origin, helper, unifier, defs, primitives)? { let origin_ty = match self.get_pyty_obj_type(origin, helper, unifier, defs, primitives)? {
Ok((ty, false)) => ty, Ok((ty, false)) => ty,
Ok((_, true)) => return Ok(Err("instantiated type does not take type parameters".into())), Ok((_, true)) => return Ok(Err("instantiated type does not take type parameters".into())),
@ -657,8 +651,6 @@ impl SymbolResolver for Resolver {
type_fn: builtins.getattr("type").unwrap(), type_fn: builtins.getattr("type").unwrap(),
origin_ty_fn: typings.getattr("get_origin").unwrap(), origin_ty_fn: typings.getattr("get_origin").unwrap(),
args_ty_fn: typings.getattr("get_args").unwrap(), args_ty_fn: typings.getattr("get_args").unwrap(),
globals_dict: obj.getattr("__dict__").unwrap(),
eval_type_fn: typings.getattr("_eval_type").unwrap(),
}; };
sym_ty = self.get_obj_type( sym_ty = self.get_obj_type(
member.get_item(1)?, member.get_item(1)?,
@ -706,8 +698,6 @@ impl SymbolResolver for Resolver {
type_fn: builtins.getattr("type").unwrap(), type_fn: builtins.getattr("type").unwrap(),
origin_ty_fn: typings.getattr("get_origin").unwrap(), origin_ty_fn: typings.getattr("get_origin").unwrap(),
args_ty_fn: typings.getattr("get_args").unwrap(), args_ty_fn: typings.getattr("get_args").unwrap(),
globals_dict: obj.getattr("__dict__").unwrap(),
eval_type_fn: typings.getattr("_eval_type").unwrap(),
}; };
sym_value = self.get_obj_value(val, &helper, ctx)?; sym_value = self.get_obj_value(val, &helper, ctx)?;
break; break;

View File

@ -13,7 +13,7 @@ use crate::{
use crate::{location::Location, typecheck::typedef::TypeEnum}; use crate::{location::Location, typecheck::typedef::TypeEnum};
use inkwell::values::BasicValueEnum; use inkwell::values::BasicValueEnum;
use itertools::{chain, izip}; use itertools::{chain, izip};
use nac3parser::ast::{Constant::Str, Expr, StrRef}; use nac3parser::ast::{Expr, StrRef};
use parking_lot::RwLock; use parking_lot::RwLock;
#[derive(Clone, PartialEq, Debug)] #[derive(Clone, PartialEq, Debug)]
@ -235,12 +235,9 @@ pub fn parse_type_annotation<T>(
match &expr.node { match &expr.node {
Name { id, .. } => name_handling(id, unifier), Name { id, .. } => name_handling(id, unifier),
Constant { value: Str(id), .. } => name_handling(&id.clone().into(), unifier),
Subscript { value, slice, .. } => { Subscript { value, slice, .. } => {
if let Name { id, .. } = &value.node { if let Name { id, .. } = &value.node {
subscript_name_handle(id, slice, unifier) subscript_name_handle(id, slice, unifier)
} else if let Constant { value: Str(id), .. } = &value.node {
subscript_name_handle(&id.clone().into(), slice, unifier)
} else { } else {
Err("unsupported type expression".into()) Err("unsupported type expression".into())
} }

View File

@ -1,7 +1,6 @@
use std::cell::RefCell; use std::cell::RefCell;
use crate::typecheck::typedef::TypeVarMeta; use crate::typecheck::typedef::TypeVarMeta;
use ast::Constant::Str;
use super::*; use super::*;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -164,12 +163,10 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
}; };
match &expr.node { match &expr.node {
ast::ExprKind::Name { id, .. } => name_handle(id, unifier, locked), ast::ExprKind::Name { id, .. } => name_handle(id, unifier, locked),
ast::ExprKind::Constant { value: Str(id), .. } => name_handle(&id.clone().into(), unifier, locked),
// virtual // virtual
ast::ExprKind::Subscript { value, slice, .. } ast::ExprKind::Subscript { value, slice, .. }
if { if {
matches!(&value.node, ast::ExprKind::Name { id, .. } if id == &"virtual".into()) || matches!(&value.node, ast::ExprKind::Name { id, .. } if id == &"virtual".into())
matches!(&value.node, ast::ExprKind::Constant { value: Str(id), .. } if id == "virtual")
} => } =>
{ {
let def = parse_ast_to_type_annotation_kinds( let def = parse_ast_to_type_annotation_kinds(
@ -189,8 +186,7 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
// list // list
ast::ExprKind::Subscript { value, slice, .. } ast::ExprKind::Subscript { value, slice, .. }
if { if {
matches!(&value.node, ast::ExprKind::Name { id, .. } if id == &"list".into()) || matches!(&value.node, ast::ExprKind::Name { id, .. } if id == &"list".into())
matches!(&value.node, ast::ExprKind::Constant { value: Str(id), .. } if id == "list")
} => } =>
{ {
let def_ann = parse_ast_to_type_annotation_kinds( let def_ann = parse_ast_to_type_annotation_kinds(
@ -207,8 +203,7 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
// tuple // tuple
ast::ExprKind::Subscript { value, slice, .. } ast::ExprKind::Subscript { value, slice, .. }
if { if {
matches!(&value.node, ast::ExprKind::Name { id, .. } if id == &"tuple".into()) || matches!(&value.node, ast::ExprKind::Name { id, .. } if id == &"tuple".into())
matches!(&value.node, ast::ExprKind::Constant { value: Str(id), .. } if id == "tuple")
} => } =>
{ {
if let ast::ExprKind::Tuple { elts, .. } = &slice.node { if let ast::ExprKind::Tuple { elts, .. } = &slice.node {
@ -235,8 +230,6 @@ pub fn parse_ast_to_type_annotation_kinds<T>(
ast::ExprKind::Subscript { value, slice, .. } => { ast::ExprKind::Subscript { value, slice, .. } => {
if let ast::ExprKind::Name { id, .. } = &value.node { if let ast::ExprKind::Name { id, .. } = &value.node {
class_name_handle(id, slice, unifier, locked) class_name_handle(id, slice, unifier, locked)
} else if let ast::ExprKind::Constant { value: Str(id), .. } = &value.node {
class_name_handle(&id.clone().into(), slice, unifier, locked)
} else { } else {
Err("unsupported expression type for class name".into()) Err("unsupported expression type for class name".into())
} }