From bed01725c1c6a7a463a9b42082ef91a5406588ec Mon Sep 17 00:00:00 2001 From: liushuyu Date: Mon, 5 Sep 2022 17:56:03 -0600 Subject: backend: correctly formulate the exit condition ... ... previously the exit condition was treated the same as the loop condition (which is the inverse condition of the exit condition). Now this is corrected. --- gcc/rust/backend/rust-compile-expr.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'gcc') diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index e13c08c..660ad09 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -727,8 +727,11 @@ CompileExpr::visit (HIR::WhileLoopExpr &expr) tree condition = CompileExpr::Compile (expr.get_predicate_expr ().get (), ctx); + tree exit_condition + = fold_build1_loc (expr.get_locus ().gcc_location (), TRUTH_NOT_EXPR, + boolean_type_node, condition); tree exit_expr - = ctx->get_backend ()->exit_expression (condition, expr.get_locus ()); + = ctx->get_backend ()->exit_expression (exit_condition, expr.get_locus ()); ctx->add_statement (exit_expr); tree code_block_stmt -- cgit v1.1 From 6839ad40eebf982f149452cc294ab387350abcef Mon Sep 17 00:00:00 2001 From: liushuyu Date: Tue, 6 Sep 2022 22:41:17 -0600 Subject: testsuite: add loop condition execution test --- .../rust/execute/torture/loop-condition-eval.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 gcc/testsuite/rust/execute/torture/loop-condition-eval.rs (limited to 'gcc') diff --git a/gcc/testsuite/rust/execute/torture/loop-condition-eval.rs b/gcc/testsuite/rust/execute/torture/loop-condition-eval.rs new file mode 100644 index 0000000..0089659 --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/loop-condition-eval.rs @@ -0,0 +1,21 @@ +// { dg-output "1\n" } +pub fn test() -> u64 { + let mut n = 113383; // #20 in https://oeis.org/A006884 + while n != 1 { + n = if n % 2 == 0 { n / 2 } else { 3 * n + 1 }; + } + n +} + +pub fn test_1() -> u64 { + test() +} + +extern "C" { + fn printf(fmt: *const i8, ...); +} + +fn main() -> i32 { + unsafe { printf("%lu\n" as *const str as *const i8, test_1()) } + 0 +} -- cgit v1.1