Compiler: Fix generated invalid expressions in remove_return pass

... when the two branch of a if/else return, and one of the branch has
also non-terminator return in sub conditions

Fixes #8485
This commit is contained in:
Olivier Goffart 2025-05-22 13:45:22 +02:00
parent 30981ca672
commit 18c3ae53d2
2 changed files with 36 additions and 3 deletions

View file

@ -78,11 +78,13 @@ fn process_expression(
}))
}
(te, fe) => {
let has_value = has_value(ty) && (te.has_value() || fe.has_value());
let ty = if has_value { ty } else { &Type::Void };
let te = te.into_return_object(ty, &ctx.ret_ty);
let fe = fe.into_return_object(ty, &ctx.ret_ty);
ExpressionResult::ReturnObject {
has_value: has_value(ty),
has_return_value: has_value(&ctx.ret_ty),
has_value,
has_return_value: self::has_value(&ctx.ret_ty),
value: Expression::Condition {
condition,
true_expr: te.into(),
@ -457,6 +459,17 @@ impl ExpressionResult {
}
}
}
fn has_value(&self) -> bool {
match self {
ExpressionResult::Just(expression) => has_value(&expression.ty()),
ExpressionResult::MaybeReturn { actual_value, .. } => {
actual_value.as_ref().is_some_and(|x| has_value(&x.ty()))
}
ExpressionResult::Return(..) => false,
ExpressionResult::ReturnObject { has_value, .. } => *has_value,
}
}
}
fn codeblock_with_expr(mut pre_statements: Vec<Expression>, expr: Expression) -> Expression {

View file

@ -41,6 +41,25 @@ component BackgroundExpr2 inherits Rectangle {
}
}
export global Issue8485 {
pure public function format_nullable_duration( has-value: bool, precision: int) -> string {
if has-value {
return "";
} else {
if precision == 1 {
return "aaa";
}
if precision == 2 {
return "bbb";
}
return "-";
}
}
}
export component TestCase {
bkg1 := BackgroundExpr { cond: true; }
@ -53,7 +72,8 @@ export component TestCase {
out property <bool> test:
{
return bkg1.background == Colors.blue && bkg2.background == Colors.red
&& bkg3.background == Colors.orange && bkg4.background == Colors.pink && bkg5.background == Colors.blue;
&& bkg3.background == Colors.orange && bkg4.background == Colors.pink && bkg5.background == Colors.blue
&& Issue8485.format_nullable_duration(false, 2) == "bbb";
}