From patchwork Fri Mar 17 20:29:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 66532 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 69D4E3850206 for ; Fri, 17 Mar 2023 20:29:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 69D4E3850206 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679084987; bh=X7OCKiGFtZizdybadomO13jeGV6ZtcNmXZnAa11ABIY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=iPXjj5+QX6pCuv48ftke7gHd+dUqBhg73zOwOOOUMrvcy2TvtCzxzOYr4QirdeHGD vGgtgCRbCD9Ve7JBBhefgu6WPR4gN8ZwjVKKSQ3bA2wHoKe3TWtVebI5Rxy24vc8nl tHYY8fkiYUNAh8cdGcTxzO10bDX+wch6xUZMBvCQ= 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 B9666385085D for ; Fri, 17 Mar 2023 20:29:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B9666385085D Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-596-oaMMUq1RMO-qT4Aieu71VQ-1; Fri, 17 Mar 2023 16:29:12 -0400 X-MC-Unique: oaMMUq1RMO-qT4Aieu71VQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 242D496DC81 for ; Fri, 17 Mar 2023 20:29:12 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.10.187]) by smtp.corp.redhat.com (Postfix) with ESMTP id 00B8F492B00; Fri, 17 Mar 2023 20:29:11 +0000 (UTC) To: Jason Merrill , GCC Patches Subject: [PATCH] c++: further -Wdangling-reference refinement [PR107532] Date: Fri, 17 Mar 2023 16:29:08 -0400 Message-Id: <20230317202908.42800-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Based on , it seems like we should treat *any* class with a reference member as a reference wrapper. This simplifies the code so I'm happy to make that change. The patch, however, does not suppress the warning in int i = 42; auto const& v = std::get<0>(std::tuple(i)); Since reference_like_class_p already checks for std::pair maybe it could also check for std::tuple. I don't know if we want to make that change in GCC 13, or move -Wdangling-reference to -Wextra for GCC 13 and perhaps move it back to -Wall in GCC 14. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/107532 gcc/cp/ChangeLog: * call.cc (reference_like_class_p): Don't look for a constructor. gcc/testsuite/ChangeLog: * g++.dg/warn/Wdangling-reference11.C: New test. --- gcc/cp/call.cc | 35 +++++++------------ .../g++.dg/warn/Wdangling-reference11.C | 23 ++++++++++++ 2 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference11.C base-commit: ae7190e345a8d80310835cb83b3b41ef2aeb0d37 diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index c01e7b82457..00d56a157b6 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -13781,8 +13781,9 @@ std_pair_ref_ref_p (tree t) /* Return true if a class CTYPE is either std::reference_wrapper or std::ref_view, or a reference wrapper class. We consider a class - a reference wrapper class if it has a reference member and a - constructor taking the same reference type. */ + a reference wrapper class if it has a reference member. We no + longer check that it has a constructor taking the same reference type + since that approach still generated too many false positives. */ static bool reference_like_class_p (tree ctype) @@ -13798,31 +13799,19 @@ reference_like_class_p (tree ctype) if (decl_in_std_namespace_p (tdecl)) { tree name = DECL_NAME (tdecl); - return (name - && (id_equal (name, "reference_wrapper") - || id_equal (name, "span") - || id_equal (name, "ref_view"))); + if (name + && (id_equal (name, "reference_wrapper") + || id_equal (name, "span") + || id_equal (name, "ref_view"))) + return true; } for (tree fields = TYPE_FIELDS (ctype); fields; fields = DECL_CHAIN (fields)) - { - if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields)) - continue; - tree type = TREE_TYPE (fields); - if (!TYPE_REF_P (type)) - continue; - /* OK, the field is a reference member. Do we have a constructor - taking its type? */ - for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (ctype))) - { - tree args = FUNCTION_FIRST_USER_PARMTYPE (fn); - if (args - && same_type_p (TREE_VALUE (args), type) - && TREE_CHAIN (args) == void_list_node) - return true; - } - } + if (TREE_CODE (fields) == FIELD_DECL + && !DECL_ARTIFICIAL (fields) + && TYPE_REF_P (TREE_TYPE (fields))) + return true; return false; } diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference11.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference11.C new file mode 100644 index 00000000000..667618e7196 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference11.C @@ -0,0 +1,23 @@ +// PR c++/107532 +// { dg-do compile { target c++11 } } +// { dg-options "-Wdangling-reference" } + +struct R +{ + int& r; + int& get() { return r; } + int&& rget() { return static_cast(r); } +}; + +int main() +{ + int i = 42; + int& l = R{i}.get(); // { dg-bogus "dangling reference" } + int const& cl = R{i}.get(); // { dg-bogus "dangling reference" } + int&& r = R{i}.rget(); // { dg-bogus "dangling reference" } + int const&& cr = R{i}.rget(); // { dg-bogus "dangling reference" } + (void) l; + (void) r; + (void) cr; + (void) cl; +}