diff options
author | Jason Merrill <jason@redhat.com> | 2023-11-28 13:54:47 -0500 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2023-11-28 16:29:19 -0500 |
commit | 305a2686c99bf9b57490419f25c79f6fb3ae0feb (patch) | |
tree | d6002e2cc5253350245349cd3d8e19767cd72d6e /gcc | |
parent | 3d104d93a7011146b0870ab160613147adb8d9b3 (diff) | |
download | gcc-305a2686c99bf9b57490419f25c79f6fb3ae0feb.zip gcc-305a2686c99bf9b57490419f25c79f6fb3ae0feb.tar.gz gcc-305a2686c99bf9b57490419f25c79f6fb3ae0feb.tar.bz2 |
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.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/c/c-typeck.cc | 2 | ||||
-rw-r--r-- | gcc/cp/call.cc | 2 | ||||
-rw-r--r-- | gcc/cp/typeck.cc | 12 | ||||
-rw-r--r-- | gcc/testsuite/c-c++-common/array-lit.c | 3 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp1z/array-prvalue1.C | 7 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/ext/complit17.C | 4 |
6 files changed, 17 insertions, 13 deletions
diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 1dbb447..17fdc97 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -1748,7 +1748,7 @@ array_to_pointer_conversion (location_t loc, tree exp) if (!TREE_READONLY (decl) && !TREE_STATIC (decl)) warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat, "converting an array compound literal to a pointer " - "is ill-formed in C++"); + "leads to a dangling pointer in C++"); } adr = build_unary_op (loc, ADDR_EXPR, exp, true); diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 81b104f..ae0decd 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -8578,8 +8578,6 @@ convert_like_internal (conversion *convs, tree expr, tree fn, int argnum, array = finish_compound_literal (array, new_ctor, complain); /* This is dubious now, should be blessed by P2752. */ DECL_MERGEABLE (TARGET_EXPR_SLOT (array)) = true; - /* Take the address explicitly rather than via decay_conversion - to avoid the error about taking the address of a temporary. */ array = cp_build_addr_expr (array, complain); } else diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index e995fb6..0839d0a 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -2534,15 +2534,6 @@ decay_conversion (tree exp, return error_mark_node; } - /* Don't let an array compound literal decay to a pointer. It can - still be used to initialize an array or bind to a reference. */ - if (TREE_CODE (exp) == TARGET_EXPR) - { - if (complain & tf_error) - error_at (loc, "taking address of temporary array"); - return error_mark_node; - } - ptrtype = build_pointer_type (TREE_TYPE (type)); if (VAR_P (exp)) @@ -10535,6 +10526,9 @@ maybe_warn_about_returning_address_of_local (tree retval, location_t loc) if (TYPE_REF_P (valtype)) warning_at (loc, OPT_Wreturn_local_addr, "returning reference to temporary"); + else if (TYPE_PTR_P (valtype)) + warning_at (loc, OPT_Wreturn_local_addr, + "returning pointer to temporary"); else if (is_std_init_list (valtype)) warning_at (loc, OPT_Winit_list_lifetime, "returning temporary %<initializer_list%> does not extend " diff --git a/gcc/testsuite/c-c++-common/array-lit.c b/gcc/testsuite/c-c++-common/array-lit.c index 6505c20..a6b3adf 100644 --- a/gcc/testsuite/c-c++-common/array-lit.c +++ b/gcc/testsuite/c-c++-common/array-lit.c @@ -1,10 +1,11 @@ /* { dg-options "-std=c99 -Wc++-compat -Werror" { target c } } */ +/* { dg-options "-Werror=dangling-pointer=1" { target c++ } } */ /* { dg-prune-output "treated as errors" } */ #include <stdio.h> int main() { - for (int *p = (int[]){ 1, 2, 3, 0 }; /* { dg-error "array" } */ + for (int *p = (int[]){ 1, 2, 3, 0 }; /* { dg-error "array|temporary" } */ *p; ++p) { printf("%d\n", *p); } diff --git a/gcc/testsuite/g++.dg/cpp1z/array-prvalue1.C b/gcc/testsuite/g++.dg/cpp1z/array-prvalue1.C new file mode 100644 index 0000000..e837d32 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/array-prvalue1.C @@ -0,0 +1,7 @@ +// PR c++/94264 +// { dg-do compile { target c++17 } } + +int main() { + using T = int[]; + T{1, 2} == nullptr; +} diff --git a/gcc/testsuite/g++.dg/ext/complit17.C b/gcc/testsuite/g++.dg/ext/complit17.C new file mode 100644 index 0000000..acc8084 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/complit17.C @@ -0,0 +1,4 @@ +// PR c++/53220 +// { dg-options "" } + +int* f() { return (int[]){42}; } // { dg-warning "pointer to temporary" } |