nac3artiq: add checking to prevent user to decorate class constructor with rpc decorator

This commit is contained in:
z78078 2022-07-11 13:40:10 +08:00
parent 5048977a6b
commit 049981ab39
1 changed files with 3 additions and 12 deletions

View File

@ -394,20 +394,11 @@ impl Nac3 {
let class_obj = module.getattr(py, &class_name).unwrap(); let class_obj = module.getattr(py, &class_name).unwrap();
for stmt in body.iter() { for stmt in body.iter() {
if let StmtKind::FunctionDef { name, decorator_list, .. } = &stmt.node { if let StmtKind::FunctionDef { name, decorator_list, .. } = &stmt.node {
if let Some(location) = decorator_list if decorator_list.iter().any(|decorator| matches!(decorator.node, ExprKind::Name { id, .. } if id == "rpc".into())) {
.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 name == &"__init__".into() { if name == &"__init__".into() {
return Err(CompileError::new_err(format!( return Err(CompileError::new_err(format!(
"compilation failed\n----------\nClass {} Constructor __init__ function should not decorated with rpc decorator (at {})", "compilation failed\n----------\nThe constructor of class {} should not be decorated with rpc decorator (at {})",
class_name, location class_name, stmt.location
))); )));
} }
rpc_ids.push((Some((class_obj.clone(), *name)), def_id)); rpc_ids.push((Some((class_obj.clone(), *name)), def_id));