From 2a775d822e8d1116ff260a57c0ea6efa1ea873ad Mon Sep 17 00:00:00 2001 From: David Mak Date: Fri, 29 Sep 2023 17:49:09 +0800 Subject: [PATCH] core: Demote dead code into a stdout warning --- nac3core/src/codegen/mod.rs | 8 +++----- nac3core/src/typecheck/function_check.rs | 2 +- nac3standalone/demo/src/dead_code_issue118.py | 8 ++++++++ 3 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 nac3standalone/demo/src/dead_code_issue118.py diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index 8d2e39f2..9006a59c 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -1,4 +1,5 @@ use crate::{ + codegen::stmt::gen_block, symbol_resolver::{StaticValue, SymbolResolver}, toplevel::{TopLevelContext, TopLevelDef}, typecheck::{ @@ -160,7 +161,7 @@ pub struct CodeGenContext<'ctx, 'a> { impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { pub fn is_terminated(&self) -> bool { - self.builder.get_insert_block().unwrap().get_terminator().is_some() + self.builder.get_insert_block().and_then(|bb| bb.get_terminator()).is_some() } } @@ -854,10 +855,7 @@ pub fn gen_func<'ctx, G: CodeGenerator>( ) -> Result<(Builder<'ctx>, Module<'ctx>, FunctionValue<'ctx>), (Builder<'ctx>, String)> { let body = task.body.clone(); gen_func_impl(context, generator, registry, builder, module, task, |generator, ctx| { - for stmt in body.iter() { - generator.gen_stmt(ctx, stmt)?; - } - Ok(()) + gen_block(generator, ctx, body.iter()) }) } diff --git a/nac3core/src/typecheck/function_check.rs b/nac3core/src/typecheck/function_check.rs index c2dc884d..2c4b28b3 100644 --- a/nac3core/src/typecheck/function_check.rs +++ b/nac3core/src/typecheck/function_check.rs @@ -295,7 +295,7 @@ impl<'a> Inferencer<'a> { let mut ret = false; for stmt in block { if ret { - return Err(format!("dead code at {:?}", stmt.location)); + println!("warning: dead code at {:?}\n", stmt.location) } if self.check_stmt(stmt, defined_identifiers)? { ret = true; diff --git a/nac3standalone/demo/src/dead_code_issue118.py b/nac3standalone/demo/src/dead_code_issue118.py new file mode 100644 index 00000000..c1f62225 --- /dev/null +++ b/nac3standalone/demo/src/dead_code_issue118.py @@ -0,0 +1,8 @@ +def f(): + return + return + +def run() -> int32: + f() + + return 0 \ No newline at end of file