Repository navigation
Crash in C++-generating back end: GNU statement expression in short-circuited &&/|| operand
#573
Replies: 3 comments
|
I can reproduce this on current main (f9e48bc), and #574 fixes it in my tests. One detail that may help with the analysis: the crash only happens when an struct O { bool operator&&(int); };
template<typename T> struct wrapper { bool has_value(); };
void test() {
(void)(0 && ({ wrapper<int> value; value; }).has_value());
}Removing only the first line makes it compile. It also fails with Since backing expressions are recorded in every non-preprocessing context in the C++-generating configuration, the missing source sequence entries alone don't seem to explain why the operator declaration is needed. With #574 (691a6cb), the original reproducer and all of the cases above compile, and g++ 15 accepts the generated code. A small point on the test in #574: the comment says the class is instantiated "so the scanner sees the operator names", but declaring the template is enough; the crash happens without |
|
Thanks for the smaller reproducer. The overload declaration changes which folding path handles the expression. Both paths can record a backing expression. scan_logical_operator checks whether the operator name is already in the symbol table before it scans the right operand. With no operator&& declaration, it recognizes 0 && ... immediately and folds it in scan_logical_operator. That path preserves the source-sequence entries when it records the backing expression. With O::operator&& present, the name check conservatively sets might_be_overloaded. Once the right operand has been scanned, the operands still give the operator its built-in meaning. A nonconstant right operand such as the call to has_value() then reaches the later short-circuit fold in do_binary_operation_full. Before the fix, that path removed the right operand’s source-sequence entries and then recorded a backing expression that still referenced the operand. The C++ generator failed when it tried to emit the local declaration. I also traced the local-variable variants. An int initialized from a literal and an aggregate initialized with {false} both took the both-operands-constant path. Initializing either from a function parameter sent it through the same late short-circuit fold as the reproducer. The distinction is whether the right operand folds to a constant on its own. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Issue Summary
The C++-generating back end can crash when the right operand of a
short-circuited
&&or||contains a GNU statement expression with alocal declaration.
Reproducing Source Code
Command-line Options
cpfe-cp -w --g++ --gnu_version=150200 test.cpp
Type of Issue
[Front End] This code SHOULD compile with these options.
Additional Details
When the front end folds
(0 && x)/(1 || x)to a constant(
exprutil.c), it callseliminate_stmt_expr_refs_if_operand_dropped()onthe right operand. That removes the source-sequence entries for that operand's
declarations. But in contexts where constant expressions are recorded
(
curr_expr_kind_is_one_in_which_const_exprs_are_recorded()), the originalexpression is kept as the backing expression, and the C++ generator still
emits it. It then refers to declarations whose entries are gone.
Reproducing (Standard) Configuration(s)
linux-gcc-debug
Reproducing (Non-standard) Configuration
No response
I acknowledge that:
LICENSE.txtfile in my.zipor.tar.gzupload. If I included aLICENSE.txtfile, any tests created using the reproducing source code will be governed by the terms of the license I provided instead.All reactions