From 1c5e68aca9be8032a08556d5fc91a2b058e096c4 Mon Sep 17 00:00:00 2001 From: ychenfo Date: Sun, 21 Nov 2021 05:07:36 +0800 Subject: [PATCH] nac3artiq/nac3core: remove forwardref type annotation support for unstable python API --- nac3artiq/src/symbol_resolver.rs | 18 ++++-------------- nac3core/src/symbol_resolver.rs | 5 +---- nac3core/src/toplevel/type_annotation.rs | 13 +++---------- 3 files changed, 8 insertions(+), 28 deletions(-) diff --git a/nac3artiq/src/symbol_resolver.rs b/nac3artiq/src/symbol_resolver.rs index 722f410..454f178 100644 --- a/nac3artiq/src/symbol_resolver.rs +++ b/nac3artiq/src/symbol_resolver.rs @@ -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>], primitives: &PrimitiveStore, ) -> PyResult> { - // 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; diff --git a/nac3core/src/symbol_resolver.rs b/nac3core/src/symbol_resolver.rs index 9f9c161..1b1c146 100644 --- a/nac3core/src/symbol_resolver.rs +++ b/nac3core/src/symbol_resolver.rs @@ -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( 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()) } diff --git a/nac3core/src/toplevel/type_annotation.rs b/nac3core/src/toplevel/type_annotation.rs index 7d6523a..c6482d9 100644 --- a/nac3core/src/toplevel/type_annotation.rs +++ b/nac3core/src/toplevel/type_annotation.rs @@ -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( }; 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( // 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( // 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( 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()) }