Skip to content

Generated C++ drops or crashes on a braced cast to a reference-to-array type #572

Description

@daveedvdv-nvidia

Issue Summary

A braced functional-notation cast to a reference-to-array type, like RA{1, 2} with using RA = int(&&)[2], isn't reproduced correctly in generated C++:

  • For an rvalue reference to an array, the type is dropped and only the braced list is generated, so f(RA{1, 2}) becomes f({1, 2}). The generated code then has a different meaning or is ill-formed (template argument deduction fails, auto &&r = {1, 2} needs std::initializer_list, and decltype(({1, 2})) is a GNU statement expression).
  • For an lvalue reference to a const array (using CA = const int(&)[2]), the C++ generator stops with an internal error.

The front end itself accepts all of these. This came up in PR #544, where a value-category check like is_rref<decltype((RA{1, 2}))> can't be used in a test because of it.

Reproducing Source Code

using RA = int(&&)[2];
using CA = const int(&)[2];
template<class T> constexpr int size(T&&) { return sizeof(T); }

static_assert(size(RA{1, 2}) == 8, "");                // 1
static_assert(sizeof(decltype((RA{1, 2}))) == 8, "");  // 2
void f() { auto &&r = RA{1, 2}; (void)r; }             // 3
static_assert(size(CA{1, 2}) == 8, "");                // 4

Command-line Options

cpfe-cp --c++17 --gen_c_file_name t.int.c t.cpp

Type of Issue

[Back End] There is a problem with the generated C++ code.

Additional Details

GCC and Clang accept the source, as does cpfe --c++17 (without C++ generation).

With lines 1 to 3, the generated C++ is:

static_assert((size({1, 2}) == 8), "");
static_assert((sizeof(decltype(({1, 2}))) == (8)), "");
void f() { auto &&r = {1, 2}; (void)r; }

which GCC rejects (no matching function for call to 'size(<brace-enclosed initializer list>)', statement-expressions are not allowed outside functions, and deducing from brace-enclosed initializer list requires '#include <initializer_list>'). A braced cast to the array type itself (using A = int[2]; A{1, 2}) is generated correctly, and so is an initializer like int (&&k)[2] = RA{1, 2}; (where {1, 2} happens to mean the same thing).

Line 4 (and likewise int n = size(CA{1, 2}); or const auto &r = CA{1, 2};) gives:

"t.cpp", line 8 (col. 1): internal error: assertion failed:
          gen_initializer_constant: ran out of fields (cp_gen_be.c, line 8688
          in gen_initializer_constant)

(so to see the output for lines 1 to 3, remove line 4).

Both problems already occur on main, without PR #544. They show up in the edg_x86_64_cp and edg_x86_32_cp test configurations, which compile the generated C++.

Reproducing (Standard) Configuration(s)

linux-gcc-debug

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions