From 842f28149b10202ea849b77754eac99568107581 Mon Sep 17 00:00:00 2001 From: Jakub Labenski Date: Fri, 20 Nov 2026 13:44:33 +0100 Subject: [PATCH] Preserve GNU statement expressions in folded logical operands. [GH #577] --- src/exprutil.c | 33 ++++++------ .../gh-577-short-circuit-statement-expr.txt | 12 +++++ .../default.1.1.txt | 1 + .../default.2.1.txt | 1 + ...h-577-short-circuit-statement-expr.sft.cpp | 53 +++++++++++++++++++ 5 files changed, 83 insertions(+), 17 deletions(-) create mode 100644 src/pending_changes/gh-577-short-circuit-statement-expr.txt create mode 100644 tests/tests/regressions/.gh-577-short-circuit-statement-expr.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.gh-577-short-circuit-statement-expr.rto/default.2.1.txt create mode 100644 tests/tests/regressions/gh-577-short-circuit-statement-expr.sft.cpp diff --git a/src/exprutil.c b/src/exprutil.c index 0f4577b011..ec79386de2 100644 --- a/src/exprutil.c +++ b/src/exprutil.c @@ -15708,29 +15708,28 @@ of a subscript operation). operand_2->variant.constant.folded_statement_expression; } /* if */ } /* if */ - } else if (op == eok_lor && constexpr_enabled && + } else if ((op == eok_lor || op == eok_land) && constexpr_enabled && !expr_stack->possible_rescan_context && is_constant_operand(operand_1) && constant_bool_value_known_at_compile_time( &operand_1->variant.constant) && - !op_is_false_constant(operand_1)) { - /* (1 || nonconstant) produces true in C++11. */ - eliminate_stmt_expr_refs_if_operand_dropped(operand_2); - discard_operand(operand_2); - make_integer_constant_operand(result, (a_host_large_integer)1); - cast_operand(result_type, result, /*is_implicit_cast=*/TRUE); - did_not_fold = FALSE; - } else if (op == eok_land && constexpr_enabled && - !expr_stack->possible_rescan_context && - is_constant_operand(operand_1) && - constant_bool_value_known_at_compile_time( - &operand_1->variant.constant) && - op_is_false_constant(operand_1)) { - /* (0 && nonconstant) produces false in C++11. */ - eliminate_stmt_expr_refs_if_operand_dropped(operand_2); + op_is_false_constant(operand_1) == (op == eok_land)) { + /* The left operand determines the value of the operation. Preserve + statement-expression entries if the right operand will appear in + the backing expression. */ + a_boolean record_backing_expr = + curr_expr_kind_is_one_in_which_const_exprs_are_recorded(); + if (!record_backing_expr) { + eliminate_stmt_expr_refs_if_operand_dropped(operand_2); + } /* if */ discard_operand(operand_2); - make_integer_constant_operand(result, (a_host_large_integer)0); + make_integer_constant_operand(result, + (a_host_large_integer)(op == eok_lor)); cast_operand(result_type, result, /*is_implicit_cast=*/TRUE); + if (record_backing_expr) { + result->variant.constant.folded_statement_expression = + expr_stack->statement_expression_seen; + } /* if */ did_not_fold = FALSE; #if GNU_EXTENSIONS_ALLOWED } else if ((op == eok_pdiff || op == eok_eq || op == eok_ne) && diff --git a/src/pending_changes/gh-577-short-circuit-statement-expr.txt b/src/pending_changes/gh-577-short-circuit-statement-expr.txt new file mode 100644 index 0000000000..d78b428e68 --- /dev/null +++ b/src/pending_changes/gh-577-short-circuit-statement-expr.txt @@ -0,0 +1,12 @@ +[GH #577] +C++-generating back end: Abort on GNU statement expressions in logical operands + +The C++-generating back end could abort with a failed assertion in +check_for_and_take_source_seq_entry when the right operand of a +short-circuited && or || contained a GNU statement expression with a local +declaration. For example, with --gnu_version=150200: + + struct S { bool operator&&(int); }; + bool f(int n) { + return false && ({ int i = n; i; }); // Previously triggered an abort. + } diff --git a/tests/tests/regressions/.gh-577-short-circuit-statement-expr.rto/default.1.1.txt b/tests/tests/regressions/.gh-577-short-circuit-statement-expr.rto/default.1.1.txt new file mode 100644 index 0000000000..71984170d0 --- /dev/null +++ b/tests/tests/regressions/.gh-577-short-circuit-statement-expr.rto/default.1.1.txt @@ -0,0 +1 @@ +cpfe -DTEST_NUMBER=1 --clang --clang_version=220100 Test_name.c diff --git a/tests/tests/regressions/.gh-577-short-circuit-statement-expr.rto/default.2.1.txt b/tests/tests/regressions/.gh-577-short-circuit-statement-expr.rto/default.2.1.txt new file mode 100644 index 0000000000..adae8def43 --- /dev/null +++ b/tests/tests/regressions/.gh-577-short-circuit-statement-expr.rto/default.2.1.txt @@ -0,0 +1 @@ +cpfe -DTEST_NUMBER=2 --g++ --gnu_version=150200 Test_name.c diff --git a/tests/tests/regressions/gh-577-short-circuit-statement-expr.sft.cpp b/tests/tests/regressions/gh-577-short-circuit-statement-expr.sft.cpp new file mode 100644 index 0000000000..66c7fb83d5 --- /dev/null +++ b/tests/tests/regressions/gh-577-short-circuit-statement-expr.sft.cpp @@ -0,0 +1,53 @@ +//type: cp +//options:--clang --clang_version=220100:--g++ --gnu_version=150200 + +bool builtin_operators(bool b) { + // With no overloads declared yet, the scanner knows from the constant + // left operands that both initializers skip the right operand. + bool left_false = false && ({ int value = b; value; }); + bool left_true = true || ({ int value = b; value; }); + // The final statement expression executes when b is true. + return (left_false == left_true) != + (b && ({ int value = b; value; })); +} + +// These declarations make the scanner consider overloading when it sees +// either operator name. +struct operators { + bool operator&&(int); + bool operator||(int); +}; + +struct wrapper { + bool has_value(); +}; + +void test() { + // Both expressions use built-in operators. The calls to has_value keep + // the right operands nonconstant, so the compiler short-circuits during + // binary folding. The C++ generator still needs both local declarations. + (void)(0 && ({ + wrapper value; + value; + }).has_value()); + (void)(1 || ({ + wrapper value; + value; + }).has_value()); +} + +bool nested_short_circuit(int i) { + // The parameter keeps the right operand of && nonconstant. The right + // operand of || folds on its own. The enclosing comparison folds both + // results and the C++ generator still needs the declarations in both blocks. + return (false && ({ + int value = i; + value; + })) == (true || ({ + struct local { + bool value; + }; + local item{false}; + item.value; + })); +}