From 2fd9da101f35e55222399d770b16dce5f9d5b96c Mon Sep 17 00:00:00 2001 From: Daveed Vandevoorde Date: Fri, 20 Nov 2026 13:15:02 +0000 Subject: [PATCH] Fix braced casts to references to aggregates [GH #572] --- src/cp_gen_be.c | 20 +++++- src/expr.c | 30 +++++++- .../gh-572-array-ref-braced-cast.txt | 32 +++++++++ tests/expectations/edg_x86_32_cp/cwg.log | 1 - tests/expectations/edg_x86_32_cp/imported.log | 2 - tests/expectations/edg_x86_64_cp/cwg.log | 1 - tests/expectations/edg_x86_64_cp/imported.log | 2 - .../tests/changes/.GH572.rto/default.1.1.txt | 1 + .../tests/changes/.GH572.rto/default.2.1.txt | 1 + tests/tests/changes/GH572.sft.cpp | 70 +++++++++++++++++++ .../cwg/.cwg2941.rto/edg_x86_32_cp.1.1.txt | 8 --- .../cwg/.cwg2941.rto/edg_x86_64_cp.1.1.txt | 8 --- .../.initlist89.rto/edg_x86_32_cp.1.1.txt | 8 --- .../.initlist89.rto/edg_x86_64_cp.1.1.txt | 8 --- .../.array-conv12.rto/edg_x86_32_cp.1.1.txt | 8 --- .../.array-conv12.rto/edg_x86_64_cp.1.1.txt | 8 --- 16 files changed, 149 insertions(+), 59 deletions(-) create mode 100644 src/pending_changes/gh-572-array-ref-braced-cast.txt create mode 100644 tests/tests/changes/.GH572.rto/default.1.1.txt create mode 100644 tests/tests/changes/.GH572.rto/default.2.1.txt create mode 100644 tests/tests/changes/GH572.sft.cpp delete mode 100644 tests/tests/cwg/.cwg2941.rto/edg_x86_32_cp.1.1.txt delete mode 100644 tests/tests/cwg/.cwg2941.rto/edg_x86_64_cp.1.1.txt delete mode 100644 tests/tests/imported/gnu/cpp/cpp0x/.initlist89.rto/edg_x86_32_cp.1.1.txt delete mode 100644 tests/tests/imported/gnu/cpp/cpp0x/.initlist89.rto/edg_x86_64_cp.1.1.txt delete mode 100644 tests/tests/imported/gnu/cpp/cpp2a/.array-conv12.rto/edg_x86_32_cp.1.1.txt delete mode 100644 tests/tests/imported/gnu/cpp/cpp2a/.array-conv12.rto/edg_x86_64_cp.1.1.txt diff --git a/src/cp_gen_be.c b/src/cp_gen_be.c index 52e8dd0a3b..10714d690d 100644 --- a/src/cp_gen_be.c +++ b/src/cp_gen_be.c @@ -8539,6 +8539,15 @@ which constant is the value. /* Figure out the kind of aggregate so we can track the type as we work through constants. */ type = skip_typerefs(type); + if (type_is(type, tk_pointer) && type->variant.pointer.is_reference) { + a_type_ptr referenced_type = skip_typerefs(type_pointed_to(type)); + if (type_is(referenced_type, tk_array) || + is_immediate_class_type(referenced_type)) { + /* The aggregate initializes the object bound to the reference, as in + "R{1, 2}" with R being "const int(&)[2]". */ + type = referenced_type; + } /* if */ + } /* if */ switch (type->kind) { case tk_array: #if GNU_VECTOR_TYPES_ALLOWED @@ -23545,9 +23554,11 @@ when possible. /* Constant (simple or aggregate). */ con = dip->variant.constant.ptr; if (is_reference_type(init_entity_type) && + !con->explicit_cast_applied && !((assoc_expr != NULL && is_glvalue_node(assoc_expr)) || is_reference_type(con->type))) { - /* A reference initialized by a prvalue. */ + /* A reference initialized by a prvalue (other than by a cast like + "R{1, 2}", which is put out with the reference type). */ a_type_ptr referred_to_type = type_pointed_to(init_entity_type); if (is_array_type(referred_to_type) || (is_class_struct_union_type(referred_to_type) && @@ -24068,7 +24079,12 @@ Output the initializer, if any, for the indicated variable. } /* if */ } else { dip = expr->variant.init.dynamic_init; - init_type = expr->type; + /* As in gen_temp_init, use the type of a braced cast as written + (e.g., a reference type, as in "R{1, 2}"). */ + init_type = expr->variant.init.source.type; + if (init_type == NULL) { + init_type = expr->type; + } /* if */ goto handle_dynamic_init; } /* if */ } else if (con->explicit_parentheses_on_aggregate && diff --git a/src/expr.c b/src/expr.c index 358c42d016..b095b6edcc 100644 --- a/src/expr.c +++ b/src/expr.c @@ -30795,18 +30795,42 @@ previously-scanned braced initializer. } /* if */ expr = expr_node_from_operand(result); if (expr != NULL) { + an_expr_node_ptr temp_expr = expr; /* Record source form information in the associated IL entries if needed. */ expr->is_brace_notation_cast = TRUE; - if (expr->kind == (an_expr_node_kind)enk_temp_init) { - a_dynamic_init_ptr dip = expr->variant.init.dynamic_init; + if (is_any_reference_type(type_cast_to) && is_array_type(expr->type) && + is_operation_node(expr) && + node_operator_is(expr, eok_lvalue_adjust)) { + /* A reference bound to an array temporary, as in "R{1, 2}" with R + being "int(&&)[2]", is "*" applied to the decayed temporary (under + the eok_lvalue_adjust). Record the cast on the temporary. */ + an_expr_node_ptr opnd = expr->variant.operation.operands; + if (is_operation_node(opnd) && node_operator_is(opnd, eok_indirect)) { + opnd = opnd->variant.operation.operands; + if (is_operation_node(opnd) && + node_operator_is(opnd, eok_array_to_pointer)) { + temp_expr = opnd->variant.operation.operands; + } /* if */ + } /* if */ + } /* if */ + if (node_is(temp_expr, enk_temp_init)) { + a_dynamic_init_ptr dip = temp_expr->variant.init.dynamic_init; dip->is_explicit_cast = TRUE; dip->is_braced_initializer = TRUE; if (dip->kind == (a_dynamic_init_kind)dik_constant || dip->kind == (a_dynamic_init_kind)dik_nonconstant_aggregate) { dip->variant.constant.ptr->explicit_cast_applied = TRUE; } /* if */ - expr->variant.init.source.type = type_cast_to; + temp_expr->variant.init.source.type = type_cast_to; + } /* if */ + if (is_rvalue_reference_type(type_cast_to) && + !is_function_type(type_pointed_to(type_cast_to)) && + is_expression_operand(result) && is_an_lvalue(result)) { + /* The result of a cast to an rvalue reference to an object type is + an xvalue, but binding the reference produced an lvalue (e.g., for + an array temporary, as in "R{1, 2}" with R being "int(&&)[2]"). */ + conv_rvalue_reference_result_to_xvalue(result); } /* if */ } /* if */ #if EXTRA_SOURCE_POSITIONS_IN_IL diff --git a/src/pending_changes/gh-572-array-ref-braced-cast.txt b/src/pending_changes/gh-572-array-ref-braced-cast.txt new file mode 100644 index 0000000000..9b075effae --- /dev/null +++ b/src/pending_changes/gh-572-array-ref-braced-cast.txt @@ -0,0 +1,32 @@ +[GH #572] +Braced casts to references to aggregates + +The C++-generating back end dropped the type of a braced functional-notation +cast to a reference to an array, so the generated code had a different +meaning or was rejected, and it issued an internal error for such a cast to a +reference to a const array or to a class, and for a braced initializer of +such a reference. Also, when such a cast to a reference to an array was the +constant initializer of a variable, it was generated as a compound literal +(a GNU extension). For example, with --c++17: + + struct S { int a, b; }; + using RA = int(&&)[2]; + using CA = const int(&)[2]; + template int size(T&&) { return sizeof(T); } + int n1 = size(RA{1, 2}); // Was generated as "size({1, 2})". + int n2 = size(CA{1, 2}); // Internal error. + const S &r{1, 2}; // Internal error. + CA c = CA{1, 2}; // Was generated as "(const int [2]){1, 2}". + +Such casts and initializers are now generated as written. + +Also, the front end treated a braced functional-notation cast to an rvalue +reference to an array as an lvalue rather than an xvalue. For example, with +"RA" as above, "decltype((RA{1, 2}))" was "int(&)[2]" instead of +"int(&&)[2]", and in + + int h(const int (&)[2]); + int h(const int (&&)[2]); + int n3 = h(RA{1, 2}); + +the first "h" was selected. The second one is now selected. diff --git a/tests/expectations/edg_x86_32_cp/cwg.log b/tests/expectations/edg_x86_32_cp/cwg.log index ccb7c6856e..ac31661ea9 100644 --- a/tests/expectations/edg_x86_32_cp/cwg.log +++ b/tests/expectations/edg_x86_32_cp/cwg.log @@ -1,3 +1,2 @@ cwg2242/test.mft.cpp:fn:-DTEST_NUMBER=1:CATASTROPHE: cwg2845b.sft.cpp:cp:-DTEST_NUMBER=1:FAIL: -cwg2941.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:ABORT: diff --git a/tests/expectations/edg_x86_32_cp/imported.log b/tests/expectations/edg_x86_32_cp/imported.log index 68a94244ff..1c3ccb0aaa 100644 --- a/tests/expectations/edg_x86_32_cp/imported.log +++ b/tests/expectations/edg_x86_32_cp/imported.log @@ -780,7 +780,6 @@ gnu/cpp/cpp0x/initlist25/test.mft.cpp:rp:-DTEST_NUMBER=1 --c++11:FAIL(COMPILE): gnu/cpp/cpp0x/initlist48.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL: gnu/cpp/cpp0x/initlist53.sft.cpp:rp:-DTEST_NUMBER=1 --c++11:FAIL(COMPILE): gnu/cpp/cpp0x/initlist54.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL: -gnu/cpp/cpp0x/initlist89.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:ABORT: gnu/cpp/cpp0x/lambda/lambda-eh2.sft.cpp:rp:-DTEST_NUMBER=1 --c++11:FAIL(COMPILE): gnu/cpp/cpp0x/lambda/lambda-err3.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS: gnu/cpp/cpp0x/lambda/lambda-ice16.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS: @@ -859,7 +858,6 @@ gnu/cpp/cpp23/attr-assume11.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS: gnu/cpp/cpp23/explicit-obj-default1.sft.cpp:rp:-DTEST_NUMBER=1 --c++23:FAIL(COMPILE): gnu/cpp/cpp23/ext-floating14.sft.cpp:rp:-DTEST_NUMBER=1 --c++23:FAIL(COMPILE): gnu/cpp/cpp23/size_t-literals.sft.cpp:fp:-DTEST_NUMBER=1 --c++23:FAIL: -gnu/cpp/cpp2a/array-conv12.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:ABORT: gnu/cpp/cpp2a/concepts-alias6.sft.cpp:fn:-DTEST_NUMBER=1 --c++20:PASS: gnu/cpp/cpp2a/concepts-cmath.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL: gnu/cpp/cpp2a/concepts-explicit-inst3.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL: diff --git a/tests/expectations/edg_x86_64_cp/cwg.log b/tests/expectations/edg_x86_64_cp/cwg.log index ccb7c6856e..ac31661ea9 100644 --- a/tests/expectations/edg_x86_64_cp/cwg.log +++ b/tests/expectations/edg_x86_64_cp/cwg.log @@ -1,3 +1,2 @@ cwg2242/test.mft.cpp:fn:-DTEST_NUMBER=1:CATASTROPHE: cwg2845b.sft.cpp:cp:-DTEST_NUMBER=1:FAIL: -cwg2941.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:ABORT: diff --git a/tests/expectations/edg_x86_64_cp/imported.log b/tests/expectations/edg_x86_64_cp/imported.log index ff1491cf6a..ab54fa12f2 100644 --- a/tests/expectations/edg_x86_64_cp/imported.log +++ b/tests/expectations/edg_x86_64_cp/imported.log @@ -101,7 +101,6 @@ gnu/cpp/cpp0x/gen-attrs-5.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL: gnu/cpp/cpp0x/gen-attrs-74.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS: gnu/cpp/cpp0x/initlist-value4.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS: gnu/cpp/cpp0x/initlist123.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:FAIL: -gnu/cpp/cpp0x/initlist89.sft.cpp:fp:-DTEST_NUMBER=1 --c++11:ABORT: gnu/cpp/cpp0x/lambda/lambda-err3.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS: gnu/cpp/cpp0x/lambda/lambda-ice16.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS: gnu/cpp/cpp0x/lambda/lambda-ice27.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS: @@ -129,7 +128,6 @@ gnu/cpp/cpp1z/class-deduction70.sft.cpp:fn:-DTEST_NUMBER=1:PASS: gnu/cpp/cpp1z/constexpr-if15.sft.cpp:fn:-DTEST_NUMBER=1 --c++17:PASS: gnu/cpp/cpp1z/fold6.sft.cpp:fn:-DTEST_NUMBER=1 --c++17:PASS: gnu/cpp/cpp23/attr-assume11.sft.cpp:fn:-DTEST_NUMBER=1 --c++11:PASS: -gnu/cpp/cpp2a/array-conv12.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:ABORT: gnu/cpp/cpp2a/concepts-alias6.sft.cpp:fn:-DTEST_NUMBER=1 --c++20:PASS: gnu/cpp/cpp2a/concepts-explicit-inst3.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL: gnu/cpp/cpp2a/concepts-lambda15.sft.cpp:fp:-DTEST_NUMBER=1 --c++20:FAIL: diff --git a/tests/tests/changes/.GH572.rto/default.1.1.txt b/tests/tests/changes/.GH572.rto/default.1.1.txt new file mode 100644 index 0000000000..6cbd97ece6 --- /dev/null +++ b/tests/tests/changes/.GH572.rto/default.1.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=1 --c++11 Test_name.c diff --git a/tests/tests/changes/.GH572.rto/default.2.1.txt b/tests/tests/changes/.GH572.rto/default.2.1.txt new file mode 100644 index 0000000000..d7c18a04c5 --- /dev/null +++ b/tests/tests/changes/.GH572.rto/default.2.1.txt @@ -0,0 +1 @@ +fe_only -DTEST_NUMBER=2 --c++17 Test_name.c diff --git a/tests/tests/changes/GH572.sft.cpp b/tests/tests/changes/GH572.sft.cpp new file mode 100644 index 0000000000..c60275fadb --- /dev/null +++ b/tests/tests/changes/GH572.sft.cpp @@ -0,0 +1,70 @@ +//type:fp +//options:--c++11:--c++17 +//remark:Braced casts to references to aggregates +// +// In generated C++, a braced functional-notation cast to a reference to an +// array, like R{1, 2} with "using R = int(&&)[2]", lost its type (becoming +// just "{1, 2}"), and one to a reference to a const array or to a class +// caused an internal error, as did a braced initializer for such a +// reference. Such a cast to a reference to an array that was the constant +// initializer of a variable became a compound literal (a GNU extension). +// Also, a braced cast to an rvalue reference to an array was an lvalue +// rather than an xvalue. + +struct S { int a, b; }; +using RA = int(&&)[2]; +using CA = const int(&)[2]; +using CRA = const int(&&)[2]; +using RS = S&&; +using CS = const S&; +using RSA = S(&&)[2]; +template constexpr int size(T&&) { return sizeof(T); } +template struct same { static const bool value = false; }; +template struct same { static const bool value = true; }; +int i, j; + +static_assert(size(RA{1, 2}) == 2 * sizeof(int), ""); +static_assert(size(CA{1, 2}) == 2 * sizeof(int), ""); +static_assert(size(CRA{1, 2}) == 2 * sizeof(int), ""); +static_assert(size(RS{1, 2}) == sizeof(S), ""); +static_assert(size(CS{1, 2}) == sizeof(S), ""); +static_assert(size(RSA{{1, 2}, {3, 4}}) == 2 * sizeof(S), ""); +static_assert(sizeof(decltype((RA{1, 2}))) == 2 * sizeof(int), ""); +static_assert(same::value, ""); +static_assert(same::value, ""); +static_assert(same::value, ""); +static_assert(same::value, ""); +static_assert(same::value, ""); +static_assert(same::value, ""); +constexpr int h(const int (&)[2]) { return 1; } +constexpr int h(const int (&&)[2]) { return 2; } +static_assert(h(RA{1, 2}) == 2 && h(CA{1, 2}) == 1, ""); +template constexpr bool is_lvalue(T&&) { return same::value; } +static_assert(!is_lvalue(RA{1, 2}) && is_lvalue(CA{1, 2}), ""); +template constexpr bool cast_is_lvalue() { + return is_lvalue(T{1, 2}); +} +static_assert(!cast_is_lvalue() && cast_is_lvalue(), ""); + +const int (&r1)[2]{1, 2}; +const S &r2{1, 2}; +auto &&r3 = RA{1, 2}; +CA r4 = CA{1, 2}; +const S &r5 = RS{1, 2}; +#if __cplusplus >= 201402L +decltype(auto) r6 = RA{1, 2}; +decltype(auto) r7 = CA{1, 2}; +decltype(auto) r8 = RS{1, 2}; +static_assert(same::value, ""); +static_assert(same::value, ""); +static_assert(same::value, ""); +#endif +void g(const int (&)[2]); +void f() { + auto &&a = RA{1, 2}; + auto &&b = CA{i, j}; + int n = size(RA{i, j}) + size(CS{i, j}) + size(RA{}); + g(CA{1, 2}); + g(RA{i, j}); + (void)a; (void)b; (void)n; +} diff --git a/tests/tests/cwg/.cwg2941.rto/edg_x86_32_cp.1.1.txt b/tests/tests/cwg/.cwg2941.rto/edg_x86_32_cp.1.1.txt deleted file mode 100644 index 6b83b3232c..0000000000 --- a/tests/tests/cwg/.cwg2941.rto/edg_x86_32_cp.1.1.txt +++ /dev/null @@ -1,8 +0,0 @@ -fe_only -DTEST_NUMBER=1 -A --c++20 Test_name.c -"Test_name.c", line 12 (col. 12): internal error: assertion failed: gen_initializer_constant: ran out of fields (cp_gen_be.c, line in gen_initializer_constant) - - -1 catastrophic error detected in the compilation of "Test_name.c". -Compilation aborted. -Aborted -eccp: front end returned an exit status of 134 diff --git a/tests/tests/cwg/.cwg2941.rto/edg_x86_64_cp.1.1.txt b/tests/tests/cwg/.cwg2941.rto/edg_x86_64_cp.1.1.txt deleted file mode 100644 index 6b83b3232c..0000000000 --- a/tests/tests/cwg/.cwg2941.rto/edg_x86_64_cp.1.1.txt +++ /dev/null @@ -1,8 +0,0 @@ -fe_only -DTEST_NUMBER=1 -A --c++20 Test_name.c -"Test_name.c", line 12 (col. 12): internal error: assertion failed: gen_initializer_constant: ran out of fields (cp_gen_be.c, line in gen_initializer_constant) - - -1 catastrophic error detected in the compilation of "Test_name.c". -Compilation aborted. -Aborted -eccp: front end returned an exit status of 134 diff --git a/tests/tests/imported/gnu/cpp/cpp0x/.initlist89.rto/edg_x86_32_cp.1.1.txt b/tests/tests/imported/gnu/cpp/cpp0x/.initlist89.rto/edg_x86_32_cp.1.1.txt deleted file mode 100644 index fc5c6a799f..0000000000 --- a/tests/tests/imported/gnu/cpp/cpp0x/.initlist89.rto/edg_x86_32_cp.1.1.txt +++ /dev/null @@ -1,8 +0,0 @@ -fe_only -DTEST_NUMBER=1 --gnu_version=999999 -tused -e 200 --no_wrap --no_wrap_diagnostics --c++11 Test_name.c -"Test_name.c", line 6 (col. 13): internal error: assertion failed: gen_initializer_constant: ran out of fields (cp_gen_be.c, line in gen_initializer_constant) - - -1 catastrophic error detected in the compilation of "Test_name.c". -Compilation aborted. -Aborted -eccp: front end returned an exit status of 134 diff --git a/tests/tests/imported/gnu/cpp/cpp0x/.initlist89.rto/edg_x86_64_cp.1.1.txt b/tests/tests/imported/gnu/cpp/cpp0x/.initlist89.rto/edg_x86_64_cp.1.1.txt deleted file mode 100644 index fc5c6a799f..0000000000 --- a/tests/tests/imported/gnu/cpp/cpp0x/.initlist89.rto/edg_x86_64_cp.1.1.txt +++ /dev/null @@ -1,8 +0,0 @@ -fe_only -DTEST_NUMBER=1 --gnu_version=999999 -tused -e 200 --no_wrap --no_wrap_diagnostics --c++11 Test_name.c -"Test_name.c", line 6 (col. 13): internal error: assertion failed: gen_initializer_constant: ran out of fields (cp_gen_be.c, line in gen_initializer_constant) - - -1 catastrophic error detected in the compilation of "Test_name.c". -Compilation aborted. -Aborted -eccp: front end returned an exit status of 134 diff --git a/tests/tests/imported/gnu/cpp/cpp2a/.array-conv12.rto/edg_x86_32_cp.1.1.txt b/tests/tests/imported/gnu/cpp/cpp2a/.array-conv12.rto/edg_x86_32_cp.1.1.txt deleted file mode 100644 index b288271c2d..0000000000 --- a/tests/tests/imported/gnu/cpp/cpp2a/.array-conv12.rto/edg_x86_32_cp.1.1.txt +++ /dev/null @@ -1,8 +0,0 @@ -fe_only -DTEST_NUMBER=1 --gnu_version=999999 -tused -e 200 --no_wrap --no_wrap_diagnostics --c++20 Test_name.c -"Test_name.c", line 11 (col. 7): internal error: assertion failed: gen_initializer_constant: ran out of fields (cp_gen_be.c, line in gen_initializer_constant) - - -1 catastrophic error detected in the compilation of "Test_name.c". -Compilation aborted. -Aborted -eccp: front end returned an exit status of 134 diff --git a/tests/tests/imported/gnu/cpp/cpp2a/.array-conv12.rto/edg_x86_64_cp.1.1.txt b/tests/tests/imported/gnu/cpp/cpp2a/.array-conv12.rto/edg_x86_64_cp.1.1.txt deleted file mode 100644 index b288271c2d..0000000000 --- a/tests/tests/imported/gnu/cpp/cpp2a/.array-conv12.rto/edg_x86_64_cp.1.1.txt +++ /dev/null @@ -1,8 +0,0 @@ -fe_only -DTEST_NUMBER=1 --gnu_version=999999 -tused -e 200 --no_wrap --no_wrap_diagnostics --c++20 Test_name.c -"Test_name.c", line 11 (col. 7): internal error: assertion failed: gen_initializer_constant: ran out of fields (cp_gen_be.c, line in gen_initializer_constant) - - -1 catastrophic error detected in the compilation of "Test_name.c". -Compilation aborted. -Aborted -eccp: front end returned an exit status of 134