From df9df84879d8fba769ab204479e35fe36e525d80 Mon Sep 17 00:00:00 2001 From: z78078 Date: Thu, 7 Jul 2022 15:50:56 +0800 Subject: [PATCH 01/10] update function analyze_single_class_methods_fields in file nac3core/src/toplevel/composer.rs to throw error "class constructor __init__ (at ) must not be decorated with rpc" when the constructor __init__ is decorated with decorator rpc --- nac3core/src/toplevel/composer.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index 16f508a..027777f 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -1067,12 +1067,27 @@ impl TopLevelComposer { let mut defined_fields: HashSet<_> = HashSet::new(); for b in class_body_ast { match &b.node { - ast::StmtKind::FunctionDef { args, returns, name, .. } => { + ast::StmtKind::FunctionDef { args, returns, name, decorator_list, .. } => { let (method_dummy_ty, method_id) = Self::get_class_method_def_info(class_methods_def, *name)?; let mut method_var_map: HashMap = HashMap::new(); + // This part check if __init__ method decorate with inappropriate decorator (rpc) + if name == &"__init__".into() { + if let Some(_) = decorator_list + .iter() + .find(|ast::Located { node, .. }| match node { + &ast::ExprKind::Name { id, .. } => id == "rpc".into(), + _ => false + }) { + return Err(format!( + "class {} constructor {} (at {}) must not be decorated with rpc", + _class_name, name, b.location + )); + } + } + let arg_types: Vec = { // check method parameters cannot have same name let mut defined_parameter_name: HashSet<_> = HashSet::new(); -- 2.44.1 From 6cba19a50b22dfc6263ee36d7f3e24a3f8c9b25e Mon Sep 17 00:00:00 2001 From: z78078 Date: Fri, 8 Jul 2022 12:59:06 +0800 Subject: [PATCH 02/10] nac3artiq check if constructor __init__ use rpc decorator --- nac3artiq/src/lib.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index 1a58bb7..a7a07b7 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -393,6 +393,20 @@ impl Nac3 { let class_obj = module.getattr(py, name.to_string()).unwrap(); for stmt in body.iter() { if let StmtKind::FunctionDef { name, decorator_list, .. } = &stmt.node { + // checking: constructor __init__ must not be decorated with rpc + if name == &"__init__".into() { + for decorator in decorator_list.iter() { + if let ast::Located { location, node: ExprKind::Name { id, .. }, .. } = decorator { + if id == &"rpc".into() { + return Err(CompileError::new_err(format!( + "compilation failed\n----------\nConstructor __init__ function should not decorated with rpc decorator (at {})", + location + ))); + } + } + } + } + if decorator_list.iter().any(|decorator| matches!(decorator.node, ExprKind::Name { id, .. } if id == "rpc".into())) { rpc_ids.push((Some((class_obj.clone(), *name)), def_id)); } -- 2.44.1 From 90d7243bf5ff12dd76796157f7659b57c2fbf490 Mon Sep 17 00:00:00 2001 From: z78078 Date: Fri, 8 Jul 2022 14:24:05 +0800 Subject: [PATCH 03/10] nac3artiq check if constructor __init__ use rpc decorator --- nac3artiq/src/lib.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index a7a07b7..8037170 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -393,21 +393,21 @@ impl Nac3 { let class_obj = module.getattr(py, name.to_string()).unwrap(); for stmt in body.iter() { if let StmtKind::FunctionDef { name, decorator_list, .. } = &stmt.node { - // checking: constructor __init__ must not be decorated with rpc - if name == &"__init__".into() { - for decorator in decorator_list.iter() { - if let ast::Located { location, node: ExprKind::Name { id, .. }, .. } = decorator { - if id == &"rpc".into() { - return Err(CompileError::new_err(format!( - "compilation failed\n----------\nConstructor __init__ function should not decorated with rpc decorator (at {})", - location - ))); - } + if let Some(location) = decorator_list + .iter() + .find_map(|ast::Located { location, node, .. } | { + match node { + ExprKind::Name { id, .. } if id == &"rpc".into() => Some(location), + _ => None, } + }) + { + if name == &"__init__".into() { + return Err(CompileError::new_err(format!( + "compilation failed\n----------\nConstructor __init__ function should not decorated with rpc decorator (at {})", + location + ))); } - } - - if decorator_list.iter().any(|decorator| matches!(decorator.node, ExprKind::Name { id, .. } if id == "rpc".into())) { rpc_ids.push((Some((class_obj.clone(), *name)), def_id)); } } -- 2.44.1 From 74d39be59c0d79dbeafa0fdf17475c79e2007ac0 Mon Sep 17 00:00:00 2001 From: z78078 Date: Fri, 8 Jul 2022 17:11:50 +0800 Subject: [PATCH 04/10] add comment to the code block --- nac3artiq/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index 8037170..d949882 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -402,6 +402,7 @@ impl Nac3 { } }) { + // This part check if __init__ method decorate with inappropriate decorator (rpc) if name == &"__init__".into() { return Err(CompileError::new_err(format!( "compilation failed\n----------\nConstructor __init__ function should not decorated with rpc decorator (at {})", -- 2.44.1 From cc99085403a22d56ff01c6be6a9ad47e818cc33a Mon Sep 17 00:00:00 2001 From: z78078 Date: Fri, 8 Jul 2022 17:12:53 +0800 Subject: [PATCH 05/10] move the changes to nac3artiq --- nac3core/src/toplevel/composer.rs | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index 027777f..94491d1 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -1073,21 +1073,6 @@ impl TopLevelComposer { let mut method_var_map: HashMap = HashMap::new(); - // This part check if __init__ method decorate with inappropriate decorator (rpc) - if name == &"__init__".into() { - if let Some(_) = decorator_list - .iter() - .find(|ast::Located { node, .. }| match node { - &ast::ExprKind::Name { id, .. } => id == "rpc".into(), - _ => false - }) { - return Err(format!( - "class {} constructor {} (at {}) must not be decorated with rpc", - _class_name, name, b.location - )); - } - } - let arg_types: Vec = { // check method parameters cannot have same name let mut defined_parameter_name: HashSet<_> = HashSet::new(); -- 2.44.1 From 5e32724c91917dcd9d33eb080af6df98e4642de3 Mon Sep 17 00:00:00 2001 From: z78078 Date: Fri, 8 Jul 2022 17:14:41 +0800 Subject: [PATCH 06/10] move the changes to nac3artiq --- nac3core/src/toplevel/composer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index 94491d1..7c53074 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -1067,7 +1067,7 @@ impl TopLevelComposer { let mut defined_fields: HashSet<_> = HashSet::new(); for b in class_body_ast { match &b.node { - ast::StmtKind::FunctionDef { args, returns, name, decorator_list, .. } => { + ast::StmtKind::FunctionDef { args, returns, name, .. }=> { let (method_dummy_ty, method_id) = Self::get_class_method_def_info(class_methods_def, *name)?; -- 2.44.1 From cf45d7666899cf36200185e8ec9f4c1f4eb57d6c Mon Sep 17 00:00:00 2001 From: z78078 Date: Fri, 8 Jul 2022 17:15:49 +0800 Subject: [PATCH 07/10] remove unnecesary changes --- nac3core/src/toplevel/composer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index 7c53074..16f508a 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -1067,7 +1067,7 @@ impl TopLevelComposer { let mut defined_fields: HashSet<_> = HashSet::new(); for b in class_body_ast { match &b.node { - ast::StmtKind::FunctionDef { args, returns, name, .. }=> { + ast::StmtKind::FunctionDef { args, returns, name, .. } => { let (method_dummy_ty, method_id) = Self::get_class_method_def_info(class_methods_def, *name)?; -- 2.44.1 From 11af619455285c41188b09071abced3dfb8398c9 Mon Sep 17 00:00:00 2001 From: z78078 Date: Fri, 8 Jul 2022 17:26:40 +0800 Subject: [PATCH 08/10] nac3artiq: add class name to the error message when rpc decorator found in __init__ function --- nac3artiq/src/lib.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index d949882..c0eac07 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -390,7 +390,8 @@ impl Nac3 { } } StmtKind::ClassDef { name, body, .. } => { - let class_obj = module.getattr(py, name.to_string()).unwrap(); + let class_name = name.to_string(); + let class_obj = module.getattr(py, &class_name).unwrap(); for stmt in body.iter() { if let StmtKind::FunctionDef { name, decorator_list, .. } = &stmt.node { if let Some(location) = decorator_list @@ -405,8 +406,8 @@ impl Nac3 { // This part check if __init__ method decorate with inappropriate decorator (rpc) if name == &"__init__".into() { return Err(CompileError::new_err(format!( - "compilation failed\n----------\nConstructor __init__ function should not decorated with rpc decorator (at {})", - location + "compilation failed\n----------\nClass {} Constructor __init__ function should not decorated with rpc decorator (at {})", + class_name, location ))); } rpc_ids.push((Some((class_obj.clone(), *name)), def_id)); -- 2.44.1 From 5048977a6b3c7a9c1661daaa7025e93316127955 Mon Sep 17 00:00:00 2001 From: z78078 Date: Fri, 8 Jul 2022 17:45:23 +0800 Subject: [PATCH 09/10] nac3artiq remove unecessary space --- nac3artiq/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index c0eac07..a12c766 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -396,7 +396,7 @@ impl Nac3 { if let StmtKind::FunctionDef { name, decorator_list, .. } = &stmt.node { if let Some(location) = decorator_list .iter() - .find_map(|ast::Located { location, node, .. } | { + .find_map(|ast::Located { location, node, .. }| { match node { ExprKind::Name { id, .. } if id == &"rpc".into() => Some(location), _ => None, -- 2.44.1 From 049981ab39d6b46d6880df8c435fc15788fde768 Mon Sep 17 00:00:00 2001 From: z78078 Date: Mon, 11 Jul 2022 13:40:10 +0800 Subject: [PATCH 10/10] nac3artiq: add checking to prevent user to decorate class constructor with rpc decorator --- nac3artiq/src/lib.rs | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index a12c766..4417a68 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -394,20 +394,11 @@ impl Nac3 { let class_obj = module.getattr(py, &class_name).unwrap(); for stmt in body.iter() { if let StmtKind::FunctionDef { name, decorator_list, .. } = &stmt.node { - if let Some(location) = decorator_list - .iter() - .find_map(|ast::Located { location, node, .. }| { - match node { - ExprKind::Name { id, .. } if id == &"rpc".into() => Some(location), - _ => None, - } - }) - { - // This part check if __init__ method decorate with inappropriate decorator (rpc) + if decorator_list.iter().any(|decorator| matches!(decorator.node, ExprKind::Name { id, .. } if id == "rpc".into())) { if name == &"__init__".into() { return Err(CompileError::new_err(format!( - "compilation failed\n----------\nClass {} Constructor __init__ function should not decorated with rpc decorator (at {})", - class_name, location + "compilation failed\n----------\nThe constructor of class {} should not be decorated with rpc decorator (at {})", + class_name, stmt.location ))); } rpc_ids.push((Some((class_obj.clone(), *name)), def_id)); -- 2.44.1