Skip to content

Crash in C++-generating back end: GNU statement expression in short-circuited &&/|| operand #577

Open

Description

@nv-cmeerw

Discussed in #573

Originally posted by jakub-labenski November 20, 2026

Issue Summary

The C++-generating back end can crash when the right operand of a
short-circuited && or || contains a GNU statement expression with a
local declaration.

Reproducing Source Code

template<typename T> struct operators {
  template<typename U> bool operator&&(U);
  template<typename U> bool operator||(U);
};
template<typename T> struct wrapper { bool has_value(); };

void test() {
  operators<int> ops; (void)ops;
  (void)(0 && ({ auto value(wrapper<wrapper<int>>{}); value; }).has_value());
  (void)(1 || ({ auto value(wrapper<wrapper<int>>{}); value; }).has_value());
}

bool nested_short_circuit() {
  return (false && ({ wrapper<int> value; value.has_value(); })) ==
         (true  || ({ struct local { bool value; }; local item{false};
                      item.value; }));
}

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 calls eliminate_stmt_expr_refs_if_operand_dropped() on
the 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 original
expression 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:

  • I have checked open issues and existing discussions before filing this report.
  • The reproducing source code that I have provided does not depend on headers that I have not included with my submission.
  • I have reduced the reproducing source code to the best of my ability.
  • I have selected at least one standard configuration or uploaded a non-standard configuration dump.
  • I understand code shared via this form is NOT confidential.
  • I have permission to share the reproducing source code and grant permission for the project to use the source code (or derivatives of the source code) to create tests.
  • I agree that any tests created using the reproducing source code (or deriatives of it) will be licensed under project license unless I included a LICENSE.txt file in my .zip or .tar.gz upload. If I included a LICENSE.txt file, any tests created using the reproducing source code will be governed by the terms of the license I provided instead.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions