From patchwork Mon Apr 1 16:02:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 87888 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6F3853858D28 for ; Mon, 1 Apr 2024 16:04:52 +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 D489F3858D28 for ; Mon, 1 Apr 2024 16:04:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D489F3858D28 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 D489F3858D28 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=1711987452; cv=none; b=QcXnp7kKsNLZWyNv6c3KYxeCFbM8xriigwog2I1yAl3WcqVAZ+eoMXgzx621Hqx1OGkh4CUr8RrxHWu94akITSnR9EwnMFlx3xCGkZ6/J/Jp087WWiBVxsg4qgnt/Hk0ZMvxc+R4PmJoYkj4h2yZ3WCtJ2lot7ZDm62VB2tTo1g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711987452; c=relaxed/simple; bh=kQTBl8QuRvEuSVVP9q4V0IpLuZhsviCkcur2TLlGcbc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=kWA7cqhrLMWCs/shlsqiU/c+iaBLvGpnuXoPpZ3r8z/+fb/dleVZ9MqJWJiewaTMsQc3wndBpzzE6JcV1SOjmq4w4CR5F2F1EL72YOEJ3nIgMj458BwD74HsnaGdu/FK7dokzJlr05lB6A3NN2QevvbM7IxdinWciVZUL4utLcE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711987445; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=TY89wFaNPnwSVxkIxrNa5D8gKDfWIRTwmXeiLtHBimM=; b=R/34pWcdntnlI5+dnWz5uhymUvzryDGRljMk30Rvuo3i7TdOnEEKcJSvZbyitVRHuYNjDj vvIxHDQjoh++JGNhuT9hts2Fku88v+2oEig8K4qB9z+Fblw1JOaoMyaoqk9TE/S6xymqTd BQPiGFnZQJUO4fnm4aVdm15Je7v1iyc= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-68-06ZUJbL4NQyd52vgDAmwxA-1; Mon, 01 Apr 2024 12:04:04 -0400 X-MC-Unique: 06ZUJbL4NQyd52vgDAmwxA-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-78bc4c5bbf7so433806185a.2 for ; Mon, 01 Apr 2024 09:04:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711987443; x=1712592243; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=TY89wFaNPnwSVxkIxrNa5D8gKDfWIRTwmXeiLtHBimM=; b=UG5AUdJVmVJF/m6tZlH6l0wr30Lo86PKu2+1WSl/1ulnoiZSAU+MAIAsvUSe6k0MjE 6JxOFeQ+11kLsbRfWtKwX8rGugOPBkRABy+BmnoVMl5EcnUPaRuFFsJd/JYBNi5Js1Pq j8zZXwdIg1SE4FKhbljF7ZrxoAoWMmYvrfgTF0dpJStBJVxYh7DQtNidcSsAH068ZPlJ dzmiwJ4lB3F4aZaJjL9UP5WXsjsfedqZEpkssvQYACEf1Bain39yNCd+qoICe1S4SygL sV+yRm2+HAGt9/GLYeUfrIIULAlMVtkBEobaN792F+Zr/26DlWut2Ydu8Go6bIeJe9XW jUHQ== X-Gm-Message-State: AOJu0YzQmFq0sVH1v1Ogu9kqjYq+0wPVojBvpzwWz5iG8mmiwsWRrdy2 tU2kx//meM0C2TFj8GlBli/ucxkBvje22w0wQcGC8OhFWWuJm1oBNkvDlQszki+YNsZcSMZNNRg FvBMLjDuNoWbAJPnIN+mlH1lUJL8pSbaPBmZTEVBwQUpX7tvvq+S83x2KszDOo+0cUs9ExJo1wv HNXaUPL5bSdzHEz71UUbI7/pfYq3LNSrtFrfo= X-Received: by 2002:a05:620a:5636:b0:78a:2b1b:e56b with SMTP id vv22-20020a05620a563600b0078a2b1be56bmr11016325qkn.68.1711987442991; Mon, 01 Apr 2024 09:04:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEPmx/goTGACOizWqUara+VmgtdpnqizlYVAc0CblVfyF38RL52uYlJ4tIWli7bDXL00WmxTQ== X-Received: by 2002:a05:620a:5636:b0:78a:2b1b:e56b with SMTP id vv22-20020a05620a563600b0078a2b1be56bmr11016281qkn.68.1711987442468; Mon, 01 Apr 2024 09:04:02 -0700 (PDT) Received: from jason-thinkpadp1gen4i.rmtusma.csb (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 du46-20020a05620a47ee00b007884505be67sm3556719qkb.31.2024.04.01.09.04.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Apr 2024 09:04:01 -0700 (PDT) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: C++26 returning reference to temporary Date: Mon, 1 Apr 2024 12:02:56 -0400 Message-ID: <20240401160359.506977-1-jason@redhat.com> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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+patchwork=sourceware.org@gcc.gnu.org Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- P2748R5 makes it ill-formed to return a reference to temporary in C++26; implementing this is a simple matter of changing the existing warning to a permerror. For most of the tests I just changed dg-warning to dg-message to accept both; I test the specific diagnostic type in Wreturn-local-addr-5.C. gcc/cp/ChangeLog: * typeck.cc (maybe_warn_about_returning_address_of_local): Permerror in C++26. gcc/testsuite/ChangeLog: * g++.dg/conversion/pr16333.C: Change dg-warning to dg-message. * g++.dg/cpp0x/constexpr-48324.C * g++.dg/other/pr94326.C * g++.dg/warn/Wreturn-local-addr-2.C * g++.old-deja/g++.jason/warning8.C: Likewise. * g++.dg/cpp1y/auto-fn6.C: Check that others don't complain. * g++.dg/warn/Wreturn-local-addr-5.C: Expect error in C++26. --- gcc/cp/typeck.cc | 6 ++++-- gcc/testsuite/g++.dg/conversion/pr16333.C | 2 +- gcc/testsuite/g++.dg/cpp0x/constexpr-48324.C | 2 +- gcc/testsuite/g++.dg/cpp1y/auto-fn6.C | 8 ++++---- gcc/testsuite/g++.dg/other/pr94326.C | 2 +- gcc/testsuite/g++.dg/warn/Wreturn-local-addr-2.C | 4 ++-- gcc/testsuite/g++.dg/warn/Wreturn-local-addr-5.C | 3 ++- gcc/testsuite/g++.old-deja/g++.jason/warning8.C | 2 +- 8 files changed, 16 insertions(+), 13 deletions(-) base-commit: d28ea8e5a70474cf9d28bf0c008092c936ad1358 diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index f5a0a2273be..88ed38e4f30 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -10626,8 +10626,10 @@ maybe_warn_about_returning_address_of_local (tree retval, location_t loc) || TREE_CODE (whats_returned) == TARGET_EXPR) { if (TYPE_REF_P (valtype)) - warning_at (loc, OPT_Wreturn_local_addr, - "returning reference to temporary"); + /* P2748 made this an error in C++26. */ + emit_diagnostic (cxx_dialect >= cxx26 ? DK_PERMERROR : DK_WARNING, + 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"); diff --git a/gcc/testsuite/g++.dg/conversion/pr16333.C b/gcc/testsuite/g++.dg/conversion/pr16333.C index a00bc5c167c..d0049304790 100644 --- a/gcc/testsuite/g++.dg/conversion/pr16333.C +++ b/gcc/testsuite/g++.dg/conversion/pr16333.C @@ -6,5 +6,5 @@ struct X { int a[3]; X foo1 () { return a; } -const X &foo2 () { return a; } // { dg-warning "returning reference to temporary" } +const X &foo2 () { return a; } // { dg-message "returning reference to temporary" } X &foo3 () { return a; } // { dg-error "cannot bind non-const lvalue ref" } diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-48324.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-48324.C index 37ed0e1d163..f53fd4ddbd3 100644 --- a/gcc/testsuite/g++.dg/cpp0x/constexpr-48324.C +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-48324.C @@ -7,7 +7,7 @@ struct S { }; constexpr const int& to_ref(int i) { - return S(i).val; // { dg-warning "reference to temporary" } + return S(i).val; // { dg-message "reference to temporary" } } constexpr int ary[to_ref(98)] = { }; // { dg-error "25:size of array .ary. is not an integral" } diff --git a/gcc/testsuite/g++.dg/cpp1y/auto-fn6.C b/gcc/testsuite/g++.dg/cpp1y/auto-fn6.C index 17ca6f22c50..7fada180841 100644 --- a/gcc/testsuite/g++.dg/cpp1y/auto-fn6.C +++ b/gcc/testsuite/g++.dg/cpp1y/auto-fn6.C @@ -8,11 +8,11 @@ char& g(char); double&& g(double); template auto&& f(T t) -{ return g(t); } // { dg-warning "reference to temporary" } +{ return g(t); } // { dg-message "reference to temporary" } int main() { - ST(); - ST(); - ST(); + ST(); // { dg-message "required from here" } + ST(); // { dg-bogus "required from here" } + ST(); // { dg-bogus "required from here" } } diff --git a/gcc/testsuite/g++.dg/other/pr94326.C b/gcc/testsuite/g++.dg/other/pr94326.C index 4069c038d52..5df72a648fd 100644 --- a/gcc/testsuite/g++.dg/other/pr94326.C +++ b/gcc/testsuite/g++.dg/other/pr94326.C @@ -3,7 +3,7 @@ // { dg-options "-fcompare-debug" } template struct A { - const int &foo() { return 0; } // { dg-warning "returning reference to temporary" } + const int &foo() { return 0; } // { dg-message "returning reference to temporary" } template void bar(_Kt) { foo(); } }; struct B { diff --git a/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-2.C b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-2.C index c483601e6b2..a9f984c153d 100644 --- a/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-2.C +++ b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-2.C @@ -3,9 +3,9 @@ struct Base2 { int m_foo; }; struct Derived2 : public Base2 {}; -const Base2& f8() { return Derived2(); } // { dg-warning "reference to temporary" } +const Base2& f8() { return Derived2(); } // { dg-message "reference to temporary" } struct foo { }; struct bar { foo base; }; -const foo& f9() { return bar().base; } // { dg-warning "reference to temporary" } +const foo& f9() { return bar().base; } // { dg-message "reference to temporary" } diff --git a/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-5.C b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-5.C index 76096279a64..69f1e682363 100644 --- a/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-5.C +++ b/gcc/testsuite/g++.dg/warn/Wreturn-local-addr-5.C @@ -5,4 +5,5 @@ int&& f() { int i = 0; return std::move(i); } // { dg-warning "reference to local variable" } int&& g() { int i = 0; return std::forward(i); } // { dg-warning "reference to local variable" } -int&& h() { long l = 0; return std::forward(l); } // { dg-warning "reference to temporary" } +int&& h() { long l = 0; return std::forward(l); } // { dg-warning "reference to temporary" "" { target { ! c++26 } } } +// { dg-error "reference to temporary" "" { target c++26 } .-1 } diff --git a/gcc/testsuite/g++.old-deja/g++.jason/warning8.C b/gcc/testsuite/g++.old-deja/g++.jason/warning8.C index b5ff3f3de60..23830716ab4 100644 --- a/gcc/testsuite/g++.old-deja/g++.jason/warning8.C +++ b/gcc/testsuite/g++.old-deja/g++.jason/warning8.C @@ -13,5 +13,5 @@ struct B { const B& f () { A a; - return a; // { dg-warning "" } returning reference to temporary + return a; // { dg-message "" } returning reference to temporary }