Bootstrapped and regtested on x86-64-pc-linux-gnu, does this look OK for trunk?
-- >8 -- Here we deem the array-to-pointer conversions in both calls as invalid, but we fail to issue a diagnostic for the second call, ultimately because cp_build_c_cast doesn't replay errors from build_const_cast_1. This means the second call get silently discarded leading to wrong/unexpected code. This patch fixes this issue. I'm not sure if we want to accept these conversions in the first place (that's PR94264 or at least related to it), but at least we're more consistent now. PR c++/112658 PR c++/94264 gcc/cp/ChangeLog: * typeck.cc (cp_build_c_cast): If we're committed to a const_cast and the result is erroneous, call build_const_cast_1 a second time to issue errors. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/initlist-array20.C: New test. --- gcc/cp/typeck.cc | 4 +++- gcc/testsuite/g++.dg/cpp0x/initlist-array20.C | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist-array20.C diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index e995fb6ddd7..b112bea4d1e 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -9209,6 +9209,8 @@ cp_build_c_cast (location_t loc, tree type, tree expr, maybe_warn_about_useless_cast (loc, type, value, complain); maybe_warn_about_cast_ignoring_quals (loc, type, complain); } + else if (complain & tf_error) + build_const_cast_1 (loc, type, value, tf_error, &valid_p); return result; } @@ -9244,7 +9246,7 @@ cp_build_c_cast (location_t loc, tree type, tree expr, to succeed. */ if (!same_type_p (non_reference (type), non_reference (result_type))) { - result = build_const_cast_1 (loc, type, result, false, &valid_p); + result = build_const_cast_1 (loc, type, result, tf_none, &valid_p); gcc_assert (valid_p); } return result; diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-array20.C b/gcc/testsuite/g++.dg/cpp0x/initlist-array20.C new file mode 100644 index 00000000000..967b67023f3 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist-array20.C @@ -0,0 +1,10 @@ +// PR c++/112658 +// { dg-do compile { target c++11 } } + +void f(int*); + +int main() { + using array = int[]; + f(array{42}); // { dg-error "address of temporary array" } + f((int*)array{42}); // { dg-error "address of temporary array" } +} -- 2.43.0.rc1