[Bug c++/105321] "non-constant condition" issued for function containing a short-circuited unevaluated non-constant expression

mpolacek at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Wed Apr 20 19:45:51 GMT 2022


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105321

--- Comment #2 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
As you mention, this slightly modified test compiles fine:

bool handle_error();

constexpr int echo(int value, bool yes = false) noexcept
{
    return (!yes || handle_error()), value;
}

static_assert(echo(10) == 10, "");

In the original test we have

<retval> = (void) (VIEW_CONVERT_EXPR<bool>(yes) || handle_error ());,
VIEW_CONVERT_EXPR<int>(value);

which has a COMPOUND_EXPR, so we get to
cxx_eval_constant_expression/COMPOUND_EXPR.  The problem here is that we call 

7044             /* Check that the LHS is constant and then discard it.  */
7045             cxx_eval_constant_expression (ctx, op0,
7046                                           true, non_constant_p,
overflow_p,
7047                                           jump_target);

where lval is always true, so the PARM_DECL 'yes' is not evaluated.

In the working test, we evaluate "!yes" in cxx_eval_unary_expression which
evaluates its operand with

3217   tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
3218                                            non_constant_p, overflow_p);

where lval is false, so the PARM_DECL is evaluated into a constant.


More information about the Gcc-bugs mailing list