meta: Lift return out of conditional statement

This commit is contained in:
David Mak 2023-12-06 11:14:26 +08:00
parent 4dc5dbb856
commit ef04696b02
4 changed files with 25 additions and 25 deletions

View File

@ -475,11 +475,11 @@ impl Nac3 {
if let Err(e) = composer.start_analysis(true) { if let Err(e) = composer.start_analysis(true) {
// report error of __modinit__ separately // report error of __modinit__ separately
if !e.contains("<nac3_synthesized_modinit>") { return if !e.contains("<nac3_synthesized_modinit>") {
return Err(CompileError::new_err(format!( Err(CompileError::new_err(format!(
"compilation failed\n----------\n{}", "compilation failed\n----------\n{}",
e e
))); )))
} else { } else {
let msg = Self::report_modinit( let msg = Self::report_modinit(
&arg_names, &arg_names,
@ -489,10 +489,10 @@ impl Nac3 {
&mut composer.unifier, &mut composer.unifier,
&self.primitive, &self.primitive,
); );
return Err(CompileError::new_err(format!( Err(CompileError::new_err(format!(
"compilation failed\n----------\n{}", "compilation failed\n----------\n{}",
msg.unwrap_or(e) msg.unwrap_or(e)
))); )))
} }
} }
let top_level = Arc::new(composer.make_top_level_context()); let top_level = Arc::new(composer.make_top_level_context());

View File

@ -166,10 +166,10 @@ impl StaticValue for PythonValue {
if ty_id == self.resolver.primitive_ids.option && name == "_nac3_option".into() { if ty_id == self.resolver.primitive_ids.option && name == "_nac3_option".into() {
let obj = self.value.getattr(py, name.to_string().as_str())?; let obj = self.value.getattr(py, name.to_string().as_str())?;
let id = self.resolver.helper.id_fn.call1(py, (&obj,))?.extract(py)?; let id = self.resolver.helper.id_fn.call1(py, (&obj,))?.extract(py)?;
if self.id == self.resolver.primitive_ids.none { return if self.id == self.resolver.primitive_ids.none {
return Ok(None) Ok(None)
} else { } else {
return Ok(Some((id, obj))) Ok(Some((id, obj)))
} }
} }
let def_id = { *self.resolver.pyid_to_def.read().get(&ty_id).unwrap() }; let def_id = { *self.resolver.pyid_to_def.read().get(&ty_id).unwrap() };

View File

@ -1691,7 +1691,7 @@ pub fn gen_expr<'ctx, 'a, G: CodeGenerator>(
&& id == ctx.primitives.option.get_obj_id(&ctx.unifier) && id == ctx.primitives.option.get_obj_id(&ctx.unifier)
{ {
match val { match val {
ValueEnum::Static(v) => match v.get_field("_nac3_option".into(), ctx) { ValueEnum::Static(v) => return match v.get_field("_nac3_option".into(), ctx) {
// if is none, raise exception directly // if is none, raise exception directly
None => { None => {
let err_msg = ctx.gen_string(generator, ""); let err_msg = ctx.gen_string(generator, "");
@ -1723,13 +1723,13 @@ pub fn gen_expr<'ctx, 'a, G: CodeGenerator>(
.get_llvm_type(generator, value.custom.unwrap()) .get_llvm_type(generator, value.custom.unwrap())
.into_pointer_type() .into_pointer_type()
.const_null(); .const_null();
return Ok(Some(ctx.builder.build_load( Ok(Some(ctx.builder.build_load(
ptr, ptr,
"unwrap_none_unreachable_load" "unwrap_none_unreachable_load"
).into())); ).into()))
} }
Some(v) => return Ok(Some(v)), Some(v) => Ok(Some(v)),
} },
ValueEnum::Dynamic(BasicValueEnum::PointerValue(ptr)) => { ValueEnum::Dynamic(BasicValueEnum::PointerValue(ptr)) => {
let not_null = ctx.builder.build_is_not_null(ptr, "unwrap_not_null"); let not_null = ctx.builder.build_is_not_null(ptr, "unwrap_not_null");
ctx.make_assert( ctx.make_assert(

View File

@ -812,17 +812,17 @@ impl<'a> Inferencer<'a> {
{ {
let custom = Some(self.primitives.int64); let custom = Some(self.primitives.int64);
let v: Result<i64, _> = (*val).try_into(); let v: Result<i64, _> = (*val).try_into();
if v.is_ok() { return if v.is_ok() {
return Ok(Located { Ok(Located {
location: args[0].location, location: args[0].location,
custom, custom,
node: ExprKind::Constant { node: ExprKind::Constant {
value: ast::Constant::Int(*val), value: ast::Constant::Int(*val),
kind: kind.clone(), kind: kind.clone(),
}, },
}); })
} else { } else {
return report_error("Integer out of bound", args[0].location) report_error("Integer out of bound", args[0].location)
} }
} }
} }
@ -832,17 +832,17 @@ impl<'a> Inferencer<'a> {
{ {
let custom = Some(self.primitives.uint32); let custom = Some(self.primitives.uint32);
let v: Result<u32, _> = (*val).try_into(); let v: Result<u32, _> = (*val).try_into();
if v.is_ok() { return if v.is_ok() {
return Ok(Located { Ok(Located {
location: args[0].location, location: args[0].location,
custom, custom,
node: ExprKind::Constant { node: ExprKind::Constant {
value: ast::Constant::Int(*val), value: ast::Constant::Int(*val),
kind: kind.clone(), kind: kind.clone(),
}, },
}); })
} else { } else {
return report_error("Integer out of bound", args[0].location) report_error("Integer out of bound", args[0].location)
} }
} }
} }
@ -852,17 +852,17 @@ impl<'a> Inferencer<'a> {
{ {
let custom = Some(self.primitives.uint64); let custom = Some(self.primitives.uint64);
let v: Result<u64, _> = (*val).try_into(); let v: Result<u64, _> = (*val).try_into();
if v.is_ok() { return if v.is_ok() {
return Ok(Located { Ok(Located {
location: args[0].location, location: args[0].location,
custom, custom,
node: ExprKind::Constant { node: ExprKind::Constant {
value: ast::Constant::Int(*val), value: ast::Constant::Int(*val),
kind: kind.clone(), kind: kind.clone(),
}, },
}); })
} else { } else {
return report_error("Integer out of bound", args[0].location) report_error("Integer out of bound", args[0].location)
} }
} }
} }