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

pull/99/head
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,
len_fn: &'a PyAny,
id_fn: &'a PyAny,
eval_type_fn: &'a PyAny,
origin_ty_fn: &'a PyAny,
args_ty_fn: &'a PyAny,
globals_dict: &'a PyAny,
}
impl Resolver {
@ -87,17 +85,13 @@ impl Resolver {
defs: &[Arc<RwLock<TopLevelDef>>],
primitives: &PrimitiveStore,
) -> 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
.id_fn
.call1((evaluated_ty,))?
.call1((pyty,))?
.extract()?;
let ty_ty_id: u64 = helper
.id_fn
.call1((helper.type_fn.call1((evaluated_ty,))?,))?
.call1((helper.type_fn.call1((pyty,))?,))?
.extract()?;
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;
Ok(Ok((res, true)))
} 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 args: &PyTuple = helper.args_ty_fn.call1((evaluated_ty,))?.cast_as()?;
let origin = helper.origin_ty_fn.call1((pyty,))?;
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)? {
Ok((ty, false)) => ty,
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(),
origin_ty_fn: typings.getattr("get_origin").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(
member.get_item(1)?,
@ -706,8 +698,6 @@ impl SymbolResolver for Resolver {
type_fn: builtins.getattr("type").unwrap(),
origin_ty_fn: typings.getattr("get_origin").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)?;
break;

View File

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

View File

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