c++: prvalue array decay [PR94264]
commit305a2686c99bf9b57490419f25c79f6fb3ae0feb
authorJason Merrill <jason@redhat.com>
Tue, 28 Nov 2023 18:54:47 +0000 (28 13:54 -0500)
committerJason Merrill <jason@redhat.com>
Tue, 28 Nov 2023 21:29:19 +0000 (28 16:29 -0500)
treed6002e2cc5253350245349cd3d8e19767cd72d6e
parent3d104d93a7011146b0870ab160613147adb8d9b3
c++: prvalue array decay [PR94264]

My change for PR53220 made array to pointer decay for prvalue arrays
ill-formed to catch well-defined C code that produces a dangling pointer in
C++ due to the shorter lifetime of compound literals.  This wasn't really
correct, but wasn't a problem until C++17 added prvalue arrays, at which
point it started rejecting valid C++ code.

I wanted to make sure that we still diagnose the problematic code;
-Wdangling-pointer covers the array-lit.c case, but I needed to extend
-Wreturn-local-addr to handle the return case.

PR c++/94264
PR c++/53220

gcc/c/ChangeLog:

* c-typeck.cc (array_to_pointer_conversion): Adjust -Wc++-compat
diagnostic.

gcc/cp/ChangeLog:

* call.cc (convert_like_internal): Remove obsolete comment.
* typeck.cc (decay_conversion): Allow array prvalue.
(maybe_warn_about_returning_address_of_local): Check
for returning pointer to temporary.

gcc/testsuite/ChangeLog:

* c-c++-common/array-lit.c: Adjust.
* g++.dg/cpp1z/array-prvalue1.C: New test.
* g++.dg/ext/complit17.C: New test.
gcc/c/c-typeck.cc
gcc/cp/call.cc
gcc/cp/typeck.cc
gcc/testsuite/c-c++-common/array-lit.c
gcc/testsuite/g++.dg/cpp1z/array-prvalue1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/complit17.C [new file with mode: 0644]