Fixed statement bodies

This commit is contained in:
2026-05-24 14:43:09 +02:00
parent 63cc20107c
commit f21cd5a927
6 changed files with 20 additions and 29 deletions
+1 -1
View File
@@ -103,7 +103,7 @@ impl GenRust for Expression {
if ensure_semicolon {
ctx.expr_ensure_semicolon = true;
if !self.is_soft_return() {
if !self.is_block() {
cg.add(";");
}
}
+7 -3
View File
@@ -95,10 +95,14 @@ impl RustCodegen {
}
}
pub fn ensure_brackets_body(&mut self, ctx: &mut Context, expr: &StatementBody) {
match expr {
StatementBody::Statement(expr) => self.ensure_brackets_expr(ctx, expr),
pub fn ensure_brackets_body(&mut self, ctx: &mut Context, body: &StatementBody) {
match body {
StatementBody::Expression(expr) => self.ensure_brackets_expr(ctx, expr),
StatementBody::Statement(expr) => {
ctx.expr_ensure_semicolon = true;
self.ensure_brackets_expr(ctx, expr);
}
}
}
}
+1 -1
View File
@@ -31,8 +31,8 @@ impl GenRust for StatementBody {
match self {
Self::Expression(expr) => expr.gen_rust(ctx, cg),
Self::Statement(stmt) => {
ctx.expr_ensure_semicolon = true;
stmt.gen_rust(ctx, cg);
cg.add(";");
}
}
}
+2 -2
View File
@@ -84,9 +84,9 @@ pub enum Generic {
}
impl Expression {
pub fn is_soft_return(&self) -> bool {
pub fn is_block(&self) -> bool {
if let Expression::Statement(stmt) = self {
stmt.is_soft_return()
stmt.is_block()
} else {
false
}
+8 -21
View File
@@ -64,28 +64,15 @@ pub struct StatementBranch {
}
impl Statement {
pub fn is_soft_return(&self) -> bool {
pub fn is_block(&self) -> bool {
match self {
Self::Block(_) | Self::Match(_, _) => true,
Self::While(branch) => branch.body.is_soft_return(),
Self::For { body, .. } | Self::Loop(body) | Self::CStyleFor { body, .. } => {
body.is_soft_return()
}
Self::If {
initial,
else_if,
else_branch,
} => {
else_branch
.as_ref()
.map(|v| v.is_soft_return())
.unwrap_or_default()
|| else_if
.last()
.map(|b| b.body.is_soft_return())
.unwrap_or_default()
|| initial.body.is_soft_return()
}
Self::Block(_)
| Self::Match(_, _)
| Self::While(_)
| Self::For { .. }
| Self::Loop(..)
| Self::CStyleFor { .. }
| Self::If { .. } => true,
_ => false,
}
}