From ebd25af38b5d61b41245e5a33404d8263b8b9b83 Mon Sep 17 00:00:00 2001 From: z78078 Date: Thu, 7 Jul 2022 10:36:25 +0800 Subject: [PATCH] nac3standalone: allow classes without explicit init (#221) Reviewed-on: https://git.m-labs.hk/M-Labs/nac3/pulls/304 Co-authored-by: z78078 Co-committed-by: z78078 --- nac3artiq/src/lib.rs | 4 ++-- nac3core/src/toplevel/composer.rs | 3 ++- nac3core/src/toplevel/test.rs | 26 +++++++++++++++++++++----- nac3standalone/src/main.rs | 2 +- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/nac3artiq/src/lib.rs b/nac3artiq/src/lib.rs index 855b13317..1a58bb7e5 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/composer.rs b/nac3core/src/toplevel/composer.rs index 0feb46224..16f508a7a 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)) } diff --git a/nac3core/src/toplevel/test.rs b/nac3core/src/toplevel/test.rs index e74c21db4..49400711e 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 + a = A() + "}; + "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(), true).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 3d93d1da8..cc50fb638 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);