Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/cp_gen_be.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) &&
Expand Down Expand Up @@ -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 &&
Expand Down
37 changes: 34 additions & 3 deletions src/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -30795,18 +30795,49 @@ 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 */
} else if (is_any_reference_type(type_cast_to) &&
is_operation_node(expr) &&
node_operator_is(expr, eok_lvalue)) {
/* In a template, a reference bound to a temporary of dependent type,
as in "R<T>{x, x}" with R<T> being "const S<T>&", is the temporary
under an eok_lvalue. Record the cast on the temporary. */
temp_expr = expr->variant.operation.operands;
} /* 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
Expand Down
44 changes: 44 additions & 0 deletions src/pending_changes/gh-572-array-ref-braced-cast.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[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<class T> 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.

Similarly, in configurations that generate templates from their prototype
instantiations (rather than from their source text), the type of a braced
cast to a reference to a class of dependent type was dropped. For example,

template<class T> struct S2 { T x, y; };
template<class T> using R = const S2<T>&;
template<class T> void f(T x) {
auto &&r = R<T>{x, x}; // Was generated as "auto &&r = {x, x};".
}

Such a cast is now generated as written as well.

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.
1 change: 0 additions & 1 deletion tests/expectations/edg_x86_32_cp/cwg.log
Original file line number Diff line number Diff line change
@@ -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:
2 changes: 0 additions & 2 deletions tests/expectations/edg_x86_32_cp/imported.log
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion tests/expectations/edg_x86_64_cp/cwg.log
Original file line number Diff line number Diff line change
@@ -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:
2 changes: 0 additions & 2 deletions tests/expectations/edg_x86_64_cp/imported.log
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions tests/tests/changes/.GH572.rto/default.1.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=1 --c++11 Test_name.c
1 change: 1 addition & 0 deletions tests/tests/changes/.GH572.rto/default.2.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fe_only -DTEST_NUMBER=2 --c++17 Test_name.c
82 changes: 82 additions & 0 deletions tests/tests/changes/GH572.sft.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//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. In a template generated from its prototype
// instantiation (rather than from its source text), a braced cast to a
// reference to a class of dependent type also lost its type.

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<class T> constexpr int size(T&&) { return sizeof(T); }
template<class T, class U> struct same { static const bool value = false; };
template<class T> struct same<T, T> { 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<decltype((RA{1, 2})), RA>::value, "");
static_assert(same<decltype((RA{i, j})), RA>::value, "");
static_assert(same<decltype((CA{1, 2})), CA>::value, "");
static_assert(same<decltype((CRA{1, 2})), CRA>::value, "");
static_assert(same<decltype((RS{1, 2})), RS>::value, "");
static_assert(same<decltype((RSA{{1, 2}, {3, 4}})), RSA>::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<class T> constexpr bool is_lvalue(T&&) { return same<T, T&>::value; }
static_assert(!is_lvalue(RA{1, 2}) && is_lvalue(CA{1, 2}), "");
template<class T> constexpr bool cast_is_lvalue() {
return is_lvalue(T{1, 2});
}
static_assert(!cast_is_lvalue<RA>() && cast_is_lvalue<CA>(), "");

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<decltype(r6), RA>::value, "");
static_assert(same<decltype(r7), CA>::value, "");
static_assert(same<decltype(r8), RS>::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;
}

template<class T> struct ST { T x, y; };
template<class T> using RT = const ST<T>&;
template<class T> using RRT = ST<T>&&;
template<class T> int ft(T x) {
auto &&r = RT<T>{x, x};
const ST<T> &s = RRT<T>{x, x};
return r.x + s.y + RT<T>{x, x}.x + size(RT<T>{x, x});
}
int nt = ft(1);
8 changes: 0 additions & 8 deletions tests/tests/cwg/.cwg2941.rto/edg_x86_32_cp.1.1.txt

This file was deleted.

8 changes: 0 additions & 8 deletions tests/tests/cwg/.cwg2941.rto/edg_x86_64_cp.1.1.txt

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading