From patchwork Fri Nov 11 20:22:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 18989 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp948734wru; Fri, 11 Nov 2022 12:23:14 -0800 (PST) X-Google-Smtp-Source: AA0mqf5YJtcaD3kQiex65xKCO0x38czWdlE4y0wa6iYMCp2XhIkOalPa2ZdFvoAINMz7RYPXEcxw X-Received: by 2002:a05:6402:3806:b0:461:6b61:81ae with SMTP id es6-20020a056402380600b004616b6181aemr2956340edb.62.1668198194817; Fri, 11 Nov 2022 12:23:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668198194; cv=none; d=google.com; s=arc-20160816; b=K4qrY7CSmgUWBsB1Kx4xMWVbymZSJD5rf1RG0aTwAN/b0HVOQ58jRBPwfLcjnlq3OC Xw8G1ACQ6j/t7IXohLtSKToINT7xHGeOGZBvfzvVnxmeyzItdgLxhoms0LGoCEhR1bH0 1VYPcg9tQn17NtwGQG/0ZupZROMJbEG6DTUMPithk/D+N9KHjgQGTiNQCorgun8I2LlH ClGGftw+wewgyiXE9TsZpiKA42SP8VtHV9veCADlkYCboPVtErdDjuJxD2O+T8OO0UJS aYj8hxZ/lyKyvQmSPD8ESvzVeyCbuJFwgyzEt2bfF2TT5gKc6ZG7Uyv0owvz3Kyfs/JU q17w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=tVwaigk2AUeC5aRetxIgEh6sRV+NMrSslRy9a5CeDBs=; b=h1CcxSpM5HoVcaiqXoK0fOuyHB6rDx0n93XDwvrSN84YMZGPsq1qx81uBKivHNLez5 pWrj0+wT9yj1aUzMnhBKyk9GAyv1vZA+CFxfWA4Hk0dTpTmnt/6ygCTpr6EUW7MXy26X 0GXm6L2BDKPBhT1WpWwO0nhVo6xN9/cClVkUxnAY1idHkJWMyKQilZVhu8VNUhBc4ygc q3z11FsxUrGc8DZUok/xEbzF9mP/qGutJD4hP9xXDFmEpx4JO+0+LX6wqCFQ3dGJ0ZVT jRdn1jLXTzes565sRe5aZ8KOZkhMQCRSGUZ+kx6jzW9AnOBj4nuw39ICOdz1Z5oYd1sW gcgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=jLZtlCyJ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id ds5-20020a170907724500b0078d4962a32asi3202809ejc.961.2022.11.11.12.23.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Nov 2022 12:23:14 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=jLZtlCyJ; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C67613858434 for ; Fri, 11 Nov 2022 20:23:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C67613858434 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668198193; bh=tVwaigk2AUeC5aRetxIgEh6sRV+NMrSslRy9a5CeDBs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=jLZtlCyJ26bibS4WTdd9T6YrVDZO5Jrkc+uAMEfLbq7jHsr8P+QaPKmiZz3UXC9d1 U6+Ml0S7P3m8sJ0/jryysCgPG6z0BYjeu0F9ewriW+UHvFWFVvyuZjloT00lKe6ink NUa05FbqI0faTcfxtznPqHQbtnDwSd2KU0BYhPFI= 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 D09823858D20 for ; Fri, 11 Nov 2022 20:22:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D09823858D20 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-88-s29sjlcoPaiyxkx28fRPOw-1; Fri, 11 Nov 2022 15:22:28 -0500 X-MC-Unique: s29sjlcoPaiyxkx28fRPOw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 477C43C021A3 for ; Fri, 11 Nov 2022 20:22:28 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.33.235]) by smtp.corp.redhat.com (Postfix) with ESMTP id 29BC763A56; Fri, 11 Nov 2022 20:22:28 +0000 (UTC) To: Jason Merrill , GCC Patches Subject: [PATCH] c++: Disable -Wdangling-reference when initing T& Date: Fri, 11 Nov 2022 15:22:26 -0500 Message-Id: <20221111202226.103649-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 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_H2, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749232590489549501?= X-GMAIL-MSGID: =?utf-8?q?1749232590489549501?= Non-const lvalue references can't bind to a temporary, so the warning should not be emitted if we're initializing something of that type. I'm not disabling the warning when the function itself returns a non-const lvalue reference, that would regress at least const int &r = std::any_cast(std::any()); in Wdangling-reference2.C where the any_cast returns an int&. Unfortunately, this patch means we'll stop diagnosing int& fn(int&& x) { return static_cast(x); } void test () { int &r = fn(4); } where there's a genuine dangling reference. OTOH, the patch should suppress false positives with iterators, like: auto &candidate = *candidates.begin (); and arguably that's more important than detecting some relatively obscure cases. It's probably not worth it making the warning more complicated by, for instance, not warning when a fn returns 'int&' but takes 'const int&' (because then it can't return its argument). Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? gcc/cp/ChangeLog: * call.cc (maybe_warn_dangling_reference): Don't warn when initializing a non-const lvalue reference. gcc/testsuite/ChangeLog: * g++.dg/cpp23/elision4.C: Remove dg-warning. * g++.dg/warn/Wdangling-reference1.C: Turn dg-warning into dg-bogus. * g++.dg/warn/Wdangling-reference7.C: New test. --- gcc/cp/call.cc | 10 ++++++++-- gcc/testsuite/g++.dg/cpp23/elision4.C | 4 ++-- gcc/testsuite/g++.dg/warn/Wdangling-reference1.C | 4 ++-- gcc/testsuite/g++.dg/warn/Wdangling-reference7.C | 16 ++++++++++++++++ 4 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference7.C base-commit: 0a7b437ca71e2721e9bcf070762fc54ef7991aeb diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index bd3b64a7e26..ef618d5c485 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -13679,8 +13679,14 @@ maybe_warn_dangling_reference (const_tree decl, tree init) { if (!warn_dangling_reference) return; - if (!(TYPE_REF_OBJ_P (TREE_TYPE (decl)) - || std_pair_ref_ref_p (TREE_TYPE (decl)))) + tree type = TREE_TYPE (decl); + /* Only warn if what we're initializing has type T&& or const T&, or + std::pair. (A non-const lvalue reference can't + bind to a temporary.) */ + if (!((TYPE_REF_OBJ_P (type) + && (TYPE_REF_IS_RVALUE (type) + || CP_TYPE_CONST_P (TREE_TYPE (type)))) + || std_pair_ref_ref_p (type))) return; /* Don't suppress the diagnostic just because the call comes from a system header. If the DECL is not in a system header, or if diff --git a/gcc/testsuite/g++.dg/cpp23/elision4.C b/gcc/testsuite/g++.dg/cpp23/elision4.C index d39053ad741..77dcffcdaad 100644 --- a/gcc/testsuite/g++.dg/cpp23/elision4.C +++ b/gcc/testsuite/g++.dg/cpp23/elision4.C @@ -34,6 +34,6 @@ T& temporary2(T&& x) { return static_cast(x); } void test () { - int& r1 = temporary1 (42); // { dg-warning "dangling reference" } - int& r2 = temporary2 (42); // { dg-warning "dangling reference" } + int& r1 = temporary1 (42); + int& r2 = temporary2 (42); } diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference1.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference1.C index 97c81ee716c..1718c28165e 100644 --- a/gcc/testsuite/g++.dg/warn/Wdangling-reference1.C +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference1.C @@ -139,6 +139,6 @@ struct Y { // x1 = Y::operator int&& (&TARGET_EXPR ) int&& x1 = Y(); // { dg-warning "dangling reference" } int&& x2 = Y{}; // { dg-warning "dangling reference" } -int& x3 = Y(); // { dg-warning "dangling reference" } -int& x4 = Y{}; // { dg-warning "dangling reference" } +int& x3 = Y(); // { dg-bogus "dangling reference" } +int& x4 = Y{}; // { dg-bogus "dangling reference" } const int& t1 = Y().foo(10); // { dg-warning "dangling reference" } diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference7.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference7.C new file mode 100644 index 00000000000..4b0de2d8670 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference7.C @@ -0,0 +1,16 @@ +// { dg-do compile { target c++11 } } +// { dg-options "-Wdangling-reference" } + +int& ref(const int&); +int&& rref(const int&); + +void +g () +{ + const int& r1 = ref (1); // { dg-warning "dangling reference" } + int& r2 = ref (2); // { dg-bogus "dangling reference" } + auto& r3 = ref (3); // { dg-bogus "dangling reference" } + int&& r4 = rref (4); // { dg-warning "dangling reference" } + auto&& r5 = rref (5); // { dg-warning "dangling reference" } + const int&& r6 = rref (6); // { dg-warning "dangling reference" } +}