core: Demote dead code into a stdout warning

This commit is contained in:
David Mak 2023-09-29 17:49:09 +08:00
parent 1659c3e724
commit 2a775d822e
3 changed files with 12 additions and 6 deletions

View File

@ -1,4 +1,5 @@
use crate::{ use crate::{
codegen::stmt::gen_block,
symbol_resolver::{StaticValue, SymbolResolver}, symbol_resolver::{StaticValue, SymbolResolver},
toplevel::{TopLevelContext, TopLevelDef}, toplevel::{TopLevelContext, TopLevelDef},
typecheck::{ typecheck::{
@ -160,7 +161,7 @@ pub struct CodeGenContext<'ctx, 'a> {
impl<'ctx, 'a> CodeGenContext<'ctx, 'a> { impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
pub fn is_terminated(&self) -> bool { 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)> { ) -> Result<(Builder<'ctx>, Module<'ctx>, FunctionValue<'ctx>), (Builder<'ctx>, String)> {
let body = task.body.clone(); let body = task.body.clone();
gen_func_impl(context, generator, registry, builder, module, task, |generator, ctx| { gen_func_impl(context, generator, registry, builder, module, task, |generator, ctx| {
for stmt in body.iter() { gen_block(generator, ctx, body.iter())
generator.gen_stmt(ctx, stmt)?;
}
Ok(())
}) })
} }

View File

@ -295,7 +295,7 @@ impl<'a> Inferencer<'a> {
let mut ret = false; let mut ret = false;
for stmt in block { for stmt in block {
if ret { 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)? { if self.check_stmt(stmt, defined_identifiers)? {
ret = true; ret = true;

View File

@ -0,0 +1,8 @@
def f():
return
return
def run() -> int32:
f()
return 0