From patchwork Tue Nov 28 21:29:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1869485 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=fSwVnvsb; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SfwZs3vsjz1ySY for ; Wed, 29 Nov 2023 08:29:36 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 038F6386180E for ; Tue, 28 Nov 2023 21:29:34 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 251A33858409 for ; Tue, 28 Nov 2023 21:29:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 251A33858409 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 251A33858409 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701206961; cv=none; b=ZthX6sfkz7+fma+uaiiblw63QHT2VX7eLtrkQbIfFibBlqjiLF2xfTYuO/pSgSeSkmJxm3wlmRnXOyADW1HlVzhwNawUqDzPwgaDQLMxBRZNZi9yi1r/Auk6yHEgyT3uIPr7stcxCpurBGCwZyLGmNzOcsfKtz0u9ntzr+efsw0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701206961; c=relaxed/simple; bh=D+V2bEjOsF9f+nOM1zoBqzNUtdDz83TuGiaRH6nGCb8=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=gcHFgMWjLgbZVs7SU8VprFj7OAQa/60loPAVA37M3oLGdUldz3oakIxScqsLOVqTetUtDKgS3wBQruke2A1qPXAIA6rBXcWHX/OhIvHke1RWqgBijmZJ65h5pVNZ+zGYmIRmYWeyUmkeiIc9tnPEfS4Teoh6diVlRdNwtxtU8nQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701206959; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ydSCslt6BMp16BwNZxoKJO2jP410HdUiDaQ6tjKhNfg=; b=fSwVnvsbsYEPoiNPYpl4SHKzZiixlfFt97nLPy0mqQiVf0g1bMmunQQO6dMcEPkEl/Qe0v W32GLEMN/dtX19NNj9IDxUOjWb7VGs+RZMbF3CCglEz9BBqUzpnWeB5Nkzpu6ExcVyP15B g+Ik2Wkxc/IoTkgGCQmtq0qacyNrbBQ= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-180-SK15oIigOJWbWtK5-jUOkQ-1; Tue, 28 Nov 2023 16:29:17 -0500 X-MC-Unique: SK15oIigOJWbWtK5-jUOkQ-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-423e684dc6eso1643981cf.1 for ; Tue, 28 Nov 2023 13:29:17 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701206956; x=1701811756; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ydSCslt6BMp16BwNZxoKJO2jP410HdUiDaQ6tjKhNfg=; b=eeq0JUYMWvdLMFg3f6+U8enFGhgc8t32/Ryzfh/I97tFy6BZ5lbvOjeUeO1vgCMwf3 QWrVgiUoTgg/OeqiX5SFjOEAKuO61EqL4UIvlAK70OdHRvJs/QXq9xAdiQTegKeu8B9Y rmf8xwQuWOSv4z44DIPJgQEnZPjG4mJzCpcLMZgrRa08gDpT7ThQe6NKKxyoHu/0A7Dj l+idQK8xaRsZ2X4S12lvgWmMhwK3db8zmugzEAoXro1pMo72X2vuRrAOZ7Vr7fFpZ11B Du6pIeD/DgQJnGX1eNBafkrafcfis7u3rqr8q6dHMUzCpZVUII/AMyzjgOPsWVGaJ0sq 33Yw== X-Gm-Message-State: AOJu0Ywfan97t9fNVt8NB3lgP7KWcFDSvvhLtyBtTHaUP1zg96AVZI2w bFzuNdwVUcXhJ+ag6S4sOyTd2xd/PaGSOUARXPntIvn8ar4/TPGFbBp0/crm/LnBzit5j23DJCQ VGThOo1adYstd9DMW9HuMo3NLYjNYzW+p1v+weGQbpI/VAaIZfIx693Nk/ntdDn+HNbfXuBQEQw == X-Received: by 2002:a05:622a:1d1:b0:423:8b40:5fb with SMTP id t17-20020a05622a01d100b004238b4005fbmr23756779qtw.47.1701206956417; Tue, 28 Nov 2023 13:29:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IEAfZX4M1wgdvuVDVI0K7NkRjMheQ9mts9Ve0kUTEcqvzbtOJtW2ESfyLZ8W5gNlum5eG/ukA== X-Received: by 2002:a05:622a:1d1:b0:423:8b40:5fb with SMTP id t17-20020a05622a01d100b004238b4005fbmr23756761qtw.47.1701206955998; Tue, 28 Nov 2023 13:29:15 -0800 (PST) Received: from jason.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id r22-20020ac87956000000b004238a0bca27sm4885272qtt.4.2023.11.28.13.29.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 13:29:15 -0800 (PST) From: Jason Merrill To: gcc-patches@gcc.gnu.org Cc: Patrick Palka Subject: [pushed] c++: prvalue array decay [PR94264] Date: Tue, 28 Nov 2023 16:29:13 -0500 Message-Id: <20231128212913.288014-1-jason@redhat.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- 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 | 2 +- gcc/cp/call.cc | 2 -- gcc/cp/typeck.cc | 12 +++--------- gcc/testsuite/c-c++-common/array-lit.c | 3 ++- gcc/testsuite/g++.dg/cpp1z/array-prvalue1.C | 7 +++++++ gcc/testsuite/g++.dg/ext/complit17.C | 4 ++++ 6 files changed, 17 insertions(+), 13 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/array-prvalue1.C create mode 100644 gcc/testsuite/g++.dg/ext/complit17.C base-commit: 7eaf95689bf495ab07473951ededa835eb618123 diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 1dbb4471a88..17fdc9789b4 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 81b104f4b40..ae0decd87f1 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 e995fb6ddd7..0839d0a4167 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 % does not extend " diff --git a/gcc/testsuite/c-c++-common/array-lit.c b/gcc/testsuite/c-c++-common/array-lit.c index 6505c2091b4..a6b3adf7cc8 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 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 00000000000..e837d3253a1 --- /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 00000000000..acc80847be3 --- /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" }