From dcd6ff6a932ebb77636ee2c6adeebe0e0de7e60c Mon Sep 17 00:00:00 2001 From: abdul124 Date: Thu, 13 Jun 2024 15:41:42 +0800 Subject: [PATCH] Fixing Changes --- nac3artiq/demo/support_class_attr_issue102.py | 32 ------ nac3core/src/toplevel/builtins.rs | 4 - nac3core/src/toplevel/composer.rs | 97 +------------------ nac3core/src/toplevel/helper.rs | 1 - nac3core/src/toplevel/mod.rs | 4 - .../src/typecheck/type_inferencer/test.rs | 4 - 6 files changed, 1 insertion(+), 141 deletions(-) delete mode 100644 nac3artiq/demo/support_class_attr_issue102.py diff --git a/nac3artiq/demo/support_class_attr_issue102.py b/nac3artiq/demo/support_class_attr_issue102.py deleted file mode 100644 index b6d9653..0000000 --- a/nac3artiq/demo/support_class_attr_issue102.py +++ /dev/null @@ -1,32 +0,0 @@ -from min_artiq import * -from numpy import int32 - -@nac3 -class DemoTest: - ATTR1: KernelInvariant[int32] = 16 # Kernel Access - ATTR2: int32 = 16 # Host only - - -@nac3 -class Demo: - core: KernelInvariant[Core] - ATTR3: int32 = 16 # Host only - ATTR4: KernelInvariant[int32] = 16 # Kernel Access - - def __init__(self): - self.core = Core() - - @kernel - def run(self): - # Should Work - print_int32(self.ATTR4) - #print_int32(Demo.ATTR4) - #print_int32(DemoTest.ATTR1) - - # Should NOT Work - #print_int32(self.ATTR3) - #print_int32(DemoTest.ATTR2) - - -if __name__ == "__main__": - Demo().run() \ No newline at end of file diff --git a/nac3core/src/toplevel/builtins.rs b/nac3core/src/toplevel/builtins.rs index 295a400..5602617 100644 --- a/nac3core/src/toplevel/builtins.rs +++ b/nac3core/src/toplevel/builtins.rs @@ -87,7 +87,6 @@ pub fn get_exn_constructor( object_id: DefinitionId(class_id), type_vars: Vec::default(), fields: exception_fields, - attr: Vec::default(), methods: vec![("__init__".into(), signature, DefinitionId(cons_id))], ancestors: vec![ TypeAnnotation::CustomClass { id: DefinitionId(class_id), params: Vec::default() }, @@ -445,7 +444,6 @@ pub fn get_builtins(unifier: &mut Unifier, primitives: &PrimitiveStore) -> Built object_id: PRIMITIVE_DEF_IDS.exception, type_vars: Vec::default(), fields: exception_fields, - attr: Vec::default(), methods: Vec::default(), ancestors: vec![], constructor: None, @@ -472,7 +470,6 @@ pub fn get_builtins(unifier: &mut Unifier, primitives: &PrimitiveStore) -> Built object_id: PRIMITIVE_DEF_IDS.option, type_vars: vec![option_ty_var], fields: vec![], - attr: Vec::default(), methods: vec![ ("is_some".into(), is_some_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.option.0 + 1)), ("is_none".into(), is_some_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.option.0 + 2)), @@ -563,7 +560,6 @@ pub fn get_builtins(unifier: &mut Unifier, primitives: &PrimitiveStore) -> Built object_id: PRIMITIVE_DEF_IDS.ndarray, type_vars: vec![ndarray_dtype_ty, ndarray_ndims_ty], fields: Vec::default(), - attr: Vec::default(), methods: vec![ ("copy".into(), ndarray_copy_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 1)), ("fill".into(), ndarray_fill_ty.0, DefinitionId(PRIMITIVE_DEF_IDS.ndarray.0 + 2)), diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index 9913a8d..c95def8 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -1051,7 +1051,6 @@ impl TopLevelComposer { object_id, ancestors, fields, - attr, methods, resolver, type_vars, @@ -1070,11 +1069,10 @@ impl TopLevelComposer { class_body_ast, _class_ancestor_def, class_fields_def, - class_attr_def, class_methods_def, class_type_vars_def, class_resolver, - ) = (*object_id, *name, bases, body, ancestors, fields, attr, methods, type_vars, resolver); + ) = (*object_id, *name, bases, body, ancestors, fields, methods, type_vars, resolver); let class_resolver = class_resolver.as_ref().unwrap(); let class_resolver = class_resolver.as_ref(); @@ -1354,99 +1352,6 @@ impl TopLevelComposer { ])) } } - ast::StmtKind::AnnAssign { target, annotation, value: Some(boxed_expr), .. } => { - // Class attributes are set as immutable regardless - // Check if Kernel or KernelInvariant to allow access in Kernel - if let ast::ExprKind::Name {id: attr, .. } = &target.node { - if defined_fields.insert(attr.to_string()){ - let (annotation, mutable) = match &annotation.node { - ast::ExprKind::Subscript { slice, .. } => (slice, false), - _ if core_config.kernel_ann.is_none() => (annotation, false), - _ => continue, - }; - - let field_type; - match &**boxed_expr { - ast::Located {location: _, custom: _, node: ExprKind::Constant { value: v, kind: _ }} => { - match v { - // Constant::Bool(i) => {field_type = unifier.get_fresh_literal(vec![SymbolValue::Bool(*i)], std::option::Option::Some(*loc));} - // Constant::Str(i) => {field_type = unifier.get_fresh_literal(vec![SymbolValue::Str(i.to_string())], std::option::Option::Some(*loc));} - // Constant::Int(i) => {field_type = unifier.get_fresh_literal(vec![SymbolValue::I64((*i).try_into().unwrap())], std::option::Option::Some(*loc));} - // Constant::Float(i) => {field_type = unifier.get_fresh_literal(vec![SymbolValue::Double(*i)], std::option::Option::Some(*loc));} - // _ => {println!("Unknown type"); field_type = unifier.get_dummy_var().0} - Constant::Bool(_) => {field_type = unifier.get_dummy_var().0;} - Constant::Str(_) => {field_type = unifier.get_dummy_var().0;} - Constant::Int(_) => {field_type = unifier.get_dummy_var().0;} - Constant::Float(_) => {field_type = unifier.get_dummy_var().0;} - _ => { - return Err(HashSet::from([ - format!( - "unsupported statement in class definition body (at {})", - b.location - ), - ])) - } - } - } - _ => { - return Err(HashSet::from([ - format!( - "unsupported statement in class definition body (at {})", - b.location - ), - ])) - } - } - - class_attr_def.push((*attr, field_type, mutable)); - - let parsed_annotation = parse_ast_to_type_annotation_kinds( - class_resolver, - temp_def_list, - unifier, - primitives, - annotation.as_ref(), - vec![(class_id, class_type_vars_def.clone())].into_iter().collect(), - )?; - // find type vars within this return type annotation - let type_vars_within = - get_type_var_contained_in_type_annotation(&parsed_annotation); - // handle the class type var and the method type var - for type_var_within in type_vars_within { - let TypeAnnotation::TypeVar(t) = type_var_within else { - unreachable!("must be type var annotation") - }; - - if !class_type_vars_def.contains(&t) { - return Err(HashSet::from([ - format!( - "class fields can only use type \ - vars over which the class is generic (at {})", - annotation.location - ), - ])) - } - } - type_var_to_concrete_def.insert(field_type, parsed_annotation); - - - } else { - return Err(HashSet::from([ - format!( - "same class fields `{}` defined twice (at {})", - attr, target.location - ), - ])) - } - } else { - return Err(HashSet::from([ - format!( - "unsupported statement type in class definition body (at {})", - target.location - ), - ])) - } - } ast::StmtKind::Assign { .. } // we don't class attributes | ast::StmtKind::Expr { value: _, .. } // typically a docstring; ignoring all expressions matches CPython behavior | ast::StmtKind::Pass { .. } => {} diff --git a/nac3core/src/toplevel/helper.rs b/nac3core/src/toplevel/helper.rs index 5e34052..e212ac8 100644 --- a/nac3core/src/toplevel/helper.rs +++ b/nac3core/src/toplevel/helper.rs @@ -280,7 +280,6 @@ impl TopLevelComposer { object_id: obj_id, type_vars: Vec::default(), fields: Vec::default(), - attr: Vec::default(), methods: Vec::default(), ancestors: Vec::default(), constructor, diff --git a/nac3core/src/toplevel/mod.rs b/nac3core/src/toplevel/mod.rs index 369c219..e38207f 100644 --- a/nac3core/src/toplevel/mod.rs +++ b/nac3core/src/toplevel/mod.rs @@ -99,10 +99,6 @@ pub enum TopLevelDef { /// /// Name and type is mutable. fields: Vec<(StrRef, Type, bool)>, - /// Class Attributes - /// - /// Same as fields, but are immutable - attr: Vec<(StrRef, Type, bool)>, /// Class methods, pointing to the corresponding function definition. methods: Vec<(StrRef, Type, DefinitionId)>, /// Ancestor classes, including itself. diff --git a/nac3core/src/typecheck/type_inferencer/test.rs b/nac3core/src/typecheck/type_inferencer/test.rs index f036403..e6fe800 100644 --- a/nac3core/src/typecheck/type_inferencer/test.rs +++ b/nac3core/src/typecheck/type_inferencer/test.rs @@ -289,7 +289,6 @@ impl TestEnvironment { object_id: DefinitionId(i), type_vars: Default::default(), fields: Default::default(), - attr: Default::default(), methods: Default::default(), ancestors: Default::default(), resolver: None, @@ -332,7 +331,6 @@ impl TestEnvironment { object_id: DefinitionId(defs + 1), type_vars: vec![v0], fields: [("a".into(), v0, true)].into(), - attr: Default::default(), methods: Default::default(), ancestors: Default::default(), resolver: None, @@ -370,7 +368,6 @@ impl TestEnvironment { object_id: DefinitionId(defs + 2), type_vars: Default::default(), fields: [("a".into(), int32, true), ("b".into(), fun, true)].into(), - attr: Default::default(), methods: Default::default(), ancestors: Default::default(), resolver: None, @@ -402,7 +399,6 @@ impl TestEnvironment { object_id: DefinitionId(defs + 3), type_vars: Default::default(), fields: [("a".into(), bool, true), ("b".into(), fun, false)].into(), - attr: Default::default(), methods: Default::default(), ancestors: Default::default(), resolver: None,