From 84011743f91c4030472e93de7bd39442a46ded80 Mon Sep 17 00:00:00 2001 From: z78078 Date: Wed, 6 Jul 2022 14:10:37 +0800 Subject: [PATCH 1/7] add argument allow_no_constructor when calling the function register_top_level --- nac3artiq/src/lib.rs | 4 ++-- nac3standalone/src/main.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index 855b133..1a58bb7 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -371,7 +371,7 @@ impl Nac3 { }); let (name, def_id, ty) = composer - .register_top_level(stmt.clone(), Some(resolver.clone()), path.clone()) + .register_top_level(stmt.clone(), Some(resolver.clone()), path.clone(), false) .map_err(|e| { CompileError::new_err(format!( "compilation failed\n----------\n{}", @@ -451,7 +451,7 @@ impl Nac3 { }); let resolver = Arc::new(Resolver(inner_resolver.clone())) as Arc; let (_, def_id, _) = composer - .register_top_level(synthesized.pop().unwrap(), Some(resolver.clone()), "".into()) + .register_top_level(synthesized.pop().unwrap(), Some(resolver.clone()), "".into(), false) .unwrap(); let fun_signature = diff --git a/nac3standalone/src/main.rs b/nac3standalone/src/main.rs index 3d93d1d..cc50fb6 100644 --- a/nac3standalone/src/main.rs +++ b/nac3standalone/src/main.rs @@ -208,7 +208,7 @@ fn main() { if module == &Some("__future__".into()) && names.len() == 1 && names[0].name == "annotations".into() => (), _ => { let (name, def_id, ty) = - composer.register_top_level(stmt, Some(resolver.clone()), "__main__".into()).unwrap(); + composer.register_top_level(stmt, Some(resolver.clone()), "__main__".into(), true).unwrap(); internal_resolver.add_id_def(name, def_id); if let Some(ty) = ty { internal_resolver.add_id_type(name, ty); -- 2.42.0 From 7f816bb01cc348203dedc50c58b0648b6ab8301d Mon Sep 17 00:00:00 2001 From: z78078 Date: Wed, 6 Jul 2022 14:11:03 +0800 Subject: [PATCH 2/7] modified the tests upon the changes --- nac3core/src/toplevel/test.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/nac3core/src/toplevel/test.rs b/nac3core/src/toplevel/test.rs index e74c21d..ab0decb 100644 --- a/nac3core/src/toplevel/test.rs +++ b/nac3core/src/toplevel/test.rs @@ -116,7 +116,7 @@ fn test_simple_register(source: Vec<&str>) { let ast = parse_program(s, Default::default()).unwrap(); let ast = ast[0].clone(); - composer.register_top_level(ast, None, "".into()).unwrap(); + composer.register_top_level(ast, None, "".into(), false).unwrap(); } } @@ -163,7 +163,7 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s let ast = ast[0].clone(); let (id, def_id, ty) = - composer.register_top_level(ast, Some(resolver.clone()), "".into()).unwrap(); + composer.register_top_level(ast, Some(resolver.clone()), "".into(), false).unwrap(); internal_resolver.add_id_def(id, def_id); if let Some(ty) = ty { internal_resolver.add_id_type(id, ty); @@ -515,7 +515,7 @@ fn test_analyze(source: Vec<&str>, res: Vec<&str>) { let ast = ast[0].clone(); let (id, def_id, ty) = { - match composer.register_top_level(ast, Some(resolver.clone()), "".into()) { + match composer.register_top_level(ast, Some(resolver.clone()), "".into(), false) { Ok(x) => x, Err(msg) => { if print { @@ -699,7 +699,7 @@ fn test_inference(source: Vec<&str>, res: Vec<&str>) { let ast = ast[0].clone(); let (id, def_id, ty) = { - match composer.register_top_level(ast, Some(resolver.clone()), "".into()) { + match composer.register_top_level(ast, Some(resolver.clone()), "".into(), false) { Ok(x) => x, Err(msg) => { if print { -- 2.42.0 From 8c26f186b54a82e684fb45a72ca8216a493ebd35 Mon Sep 17 00:00:00 2001 From: z78078 Date: Wed, 6 Jul 2022 14:11:38 +0800 Subject: [PATCH 3/7] add parameter allow_no_constructor to the function register_top_level --- nac3core/src/toplevel/composer.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index 0feb462..16f508a 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -164,6 +164,7 @@ impl TopLevelComposer { ast: ast::Stmt<()>, resolver: Option>, mod_path: String, + allow_no_constructor: bool, ) -> Result<(StrRef, DefinitionId, Option), String> { let defined_names = &mut self.defined_names; match &ast.node { @@ -298,7 +299,7 @@ impl TopLevelComposer { self.definition_ast_list.push((def, Some(ast))); } - let result_ty = if contains_constructor { Some(constructor_ty) } else { None }; + let result_ty = if allow_no_constructor || contains_constructor { Some(constructor_ty) } else { None }; Ok((class_name, DefinitionId(class_def_id), result_ty)) } -- 2.42.0 From 8ee10802c99a7a4596db025179ac4585bfcc2830 Mon Sep 17 00:00:00 2001 From: z78078 Date: Wed, 6 Jul 2022 16:14:22 +0800 Subject: [PATCH 4/7] update/add the arguments on the register_top_level --- nac3artiq/src/lib.rs | 4 ++-- nac3core/src/toplevel/test.rs | 26 +++++++++++++++++++++----- nac3standalone/src/main.rs | 2 +- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index 855b133..1a58bb7 100644 --- a/nac3artiq/src/lib.rs +++ b/nac3artiq/src/lib.rs @@ -371,7 +371,7 @@ impl Nac3 { }); let (name, def_id, ty) = composer - .register_top_level(stmt.clone(), Some(resolver.clone()), path.clone()) + .register_top_level(stmt.clone(), Some(resolver.clone()), path.clone(), false) .map_err(|e| { CompileError::new_err(format!( "compilation failed\n----------\n{}", @@ -451,7 +451,7 @@ impl Nac3 { }); let resolver = Arc::new(Resolver(inner_resolver.clone())) as Arc; let (_, def_id, _) = composer - .register_top_level(synthesized.pop().unwrap(), Some(resolver.clone()), "".into()) + .register_top_level(synthesized.pop().unwrap(), Some(resolver.clone()), "".into(), false) .unwrap(); let fun_signature = diff --git a/nac3core/src/toplevel/test.rs b/nac3core/src/toplevel/test.rs index e74c21d..8480d54 100644 --- a/nac3core/src/toplevel/test.rs +++ b/nac3core/src/toplevel/test.rs @@ -105,7 +105,7 @@ impl SymbolResolver for Resolver { def __init__(self): self.c: int32 = 4 self.a: bool = True - "} + "}, ]; "register" )] @@ -116,10 +116,26 @@ fn test_simple_register(source: Vec<&str>) { let ast = parse_program(s, Default::default()).unwrap(); let ast = ast[0].clone(); - composer.register_top_level(ast, None, "".into()).unwrap(); + composer.register_top_level(ast, None, "".into(), false).unwrap(); } } +#[test_case( + indoc! {" + class A: + def foo(self): + pass + "}; + "register" +)] +fn test_simple_register_without_constructor(source: &str) { + let mut composer: TopLevelComposer = Default::default(); + let ast = parse_program(source, Default::default()).unwrap(); + let ast = ast[0].clone(); + composer.register_top_level(ast, None, "".into(), false).unwrap(); +} + + #[test_case( vec![ indoc! {" @@ -163,7 +179,7 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s let ast = ast[0].clone(); let (id, def_id, ty) = - composer.register_top_level(ast, Some(resolver.clone()), "".into()).unwrap(); + composer.register_top_level(ast, Some(resolver.clone()), "".into(), false).unwrap(); internal_resolver.add_id_def(id, def_id); if let Some(ty) = ty { internal_resolver.add_id_type(id, ty); @@ -515,7 +531,7 @@ fn test_analyze(source: Vec<&str>, res: Vec<&str>) { let ast = ast[0].clone(); let (id, def_id, ty) = { - match composer.register_top_level(ast, Some(resolver.clone()), "".into()) { + match composer.register_top_level(ast, Some(resolver.clone()), "".into(), false) { Ok(x) => x, Err(msg) => { if print { @@ -699,7 +715,7 @@ fn test_inference(source: Vec<&str>, res: Vec<&str>) { let ast = ast[0].clone(); let (id, def_id, ty) = { - match composer.register_top_level(ast, Some(resolver.clone()), "".into()) { + match composer.register_top_level(ast, Some(resolver.clone()), "".into(), false) { Ok(x) => x, Err(msg) => { if print { diff --git a/nac3standalone/src/main.rs b/nac3standalone/src/main.rs index 3d93d1d..cc50fb6 100644 --- a/nac3standalone/src/main.rs +++ b/nac3standalone/src/main.rs @@ -208,7 +208,7 @@ fn main() { if module == &Some("__future__".into()) && names.len() == 1 && names[0].name == "annotations".into() => (), _ => { let (name, def_id, ty) = - composer.register_top_level(stmt, Some(resolver.clone()), "__main__".into()).unwrap(); + composer.register_top_level(stmt, Some(resolver.clone()), "__main__".into(), true).unwrap(); internal_resolver.add_id_def(name, def_id); if let Some(ty) = ty { internal_resolver.add_id_type(name, ty); -- 2.42.0 From 29525e222a7c7f83d5f470dd5c763114d4f4ee97 Mon Sep 17 00:00:00 2001 From: z78078 Date: Wed, 6 Jul 2022 16:15:04 +0800 Subject: [PATCH 5/7] add parameter to the function register_top_level --- nac3core/src/toplevel/composer.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index 0feb462..16f508a 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -164,6 +164,7 @@ impl TopLevelComposer { ast: ast::Stmt<()>, resolver: Option>, mod_path: String, + allow_no_constructor: bool, ) -> Result<(StrRef, DefinitionId, Option), String> { let defined_names = &mut self.defined_names; match &ast.node { @@ -298,7 +299,7 @@ impl TopLevelComposer { self.definition_ast_list.push((def, Some(ast))); } - let result_ty = if contains_constructor { Some(constructor_ty) } else { None }; + let result_ty = if allow_no_constructor || contains_constructor { Some(constructor_ty) } else { None }; Ok((class_name, DefinitionId(class_def_id), result_ty)) } -- 2.42.0 From 2be5d96635fb45a5d870b47b37900154033cc3f1 Mon Sep 17 00:00:00 2001 From: z78078 Date: Wed, 6 Jul 2022 16:23:31 +0800 Subject: [PATCH 6/7] remove space --- nac3core/src/toplevel/test.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/nac3core/src/toplevel/test.rs b/nac3core/src/toplevel/test.rs index 8480d54..3f6a985 100644 --- a/nac3core/src/toplevel/test.rs +++ b/nac3core/src/toplevel/test.rs @@ -135,7 +135,6 @@ fn test_simple_register_without_constructor(source: &str) { composer.register_top_level(ast, None, "".into(), false).unwrap(); } - #[test_case( vec![ indoc! {" -- 2.42.0 From f9b7f288705ae3717354d7ee0b6bf414600acc49 Mon Sep 17 00:00:00 2001 From: z78078 Date: Thu, 7 Jul 2022 10:07:48 +0800 Subject: [PATCH 7/7] correct parameter of the function call --- nac3core/src/toplevel/test.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nac3core/src/toplevel/test.rs b/nac3core/src/toplevel/test.rs index 3f6a985..4940071 100644 --- a/nac3core/src/toplevel/test.rs +++ b/nac3core/src/toplevel/test.rs @@ -125,6 +125,7 @@ fn test_simple_register(source: Vec<&str>) { class A: def foo(self): pass + a = A() "}; "register" )] @@ -132,7 +133,7 @@ fn test_simple_register_without_constructor(source: &str) { let mut composer: TopLevelComposer = Default::default(); let ast = parse_program(source, Default::default()).unwrap(); let ast = ast[0].clone(); - composer.register_top_level(ast, None, "".into(), false).unwrap(); + composer.register_top_level(ast, None, "".into(), true).unwrap(); } #[test_case( -- 2.42.0