From patchwork Sat Jan 20 02:18:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 189700 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp1400772dyb; Fri, 19 Jan 2024 18:19:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IHzyaOXemo7ncZDfXfT/KHc35/Qq+chE7cnr/Pr2FLJ0s0eO9PD+BmAbD0MkuKspkL9i/kd X-Received: by 2002:a05:6214:f0f:b0:681:86fe:6fc with SMTP id gw15-20020a0562140f0f00b0068186fe06fcmr1192275qvb.116.1705717181034; Fri, 19 Jan 2024 18:19:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705717181; cv=pass; d=google.com; s=arc-20160816; b=RtuX3EhzUGpbDRovWK4P4zJvv3e9vzJC+OQP475Wt2zpOdQFrnsVUGv9/O60az/9Wt Niy7mHbbOiX6zb48iTVhzHO+IfVUK69rLXXXcu1Ga7b5TUb3Tkm+zo8kVrOAjeO46pnI KtMAYyF5KO9hjsq1zNoeALEuGdXAqtIbdJ5F7U752w2iZuSnd3/d/fds1WN7Uooh5v8/ qulawj26pR8glpzC5YgoBOQ8XSGKlNdVm95G/MOEeiQVu5lhlgrTnCHHRd9SVIpUMO9p MHzTnNC2G17yaI3V/JNFGf+XFZA8RF3V2dUU5wshTUmtQGs1z27dCqkzkyHaBZ5Adt1s 05Kg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=iPM8wpVJ32doUxVMsqvCkRuszJRI7Uk/8j1aTPAJSk0=; fh=J677JrSZa3KgVhVVqCcMc7el77KX6MCMccbTCcPSdHk=; b=FMzGkFXLkGRYMzKG9KAzSudAccc/OOpW2rbjfS1eoKogSaHBEwo8h6xBJLaAgkMD9v HEybu5xX8tYE5h0SQgKlsH9nBKTKYrWyNWle1RxBbgf5k9IpolhF8T6PwmV15j8APkSh 09TiwQn2v7UthuzxmBdNzqLa47Cz6hcRjgDcLPazj9ae/i8dnvCKtIGCN2wNHFTJav41 OakYM8ho4ReteDjvCZKjk58Kor0oyKOgWshTcEBBTCp7Hes4p/FTv6p3DX6Q6JwZ5irD Q1IZ0W1k8L3lOoa5ROdLBA1xthkW4r5tLK5dsk5oJTKMCbLmLGFqrk4/Elbe9dSzI2TY 1RnA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cyrCMOdA; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id u10-20020a0cdd0a000000b00681954c39d2si657901qvk.50.2024.01.19.18.19.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 18:19:41 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=cyrCMOdA; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A86DB3857C6F for ; Sat, 20 Jan 2024 02:19:40 +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 70C0A3858419 for ; Sat, 20 Jan 2024 02:18:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 70C0A3858419 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 70C0A3858419 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=1705717115; cv=none; b=e9977R0OrcgWC5urHvsXA12CuJFlXojBKJqbKudJsqITtWntxr4hk208CcZITBn13vtmEzDyGSahT2X09AxWMz4GSwtWaj45bE7i/ToDkWtR0dNh3FD+O63s9YRmqIX9BDqkDkaTpV6Ypr5Iy4dAiaBr5RNRv5P8Lr5iLoTymbE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705717115; c=relaxed/simple; bh=OVz+JzQUCnHZ/+9ma/jAEbkeJ7QaNZ/WMTux6Pc88NA=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=vjAyRAsFOMDAnXNWa7QzbEU99H/2BWj+bE4BqYXVnM/8/kFTcMd1YCisBsHxV7D7vClL/30KZ4octUsKsYxKvoPWuoNLfljacsREfWCPBHGf5lf2x/FI95UBTkA56k6x3GNgrfWFtsEXEiaHoz2qlvgJi15N7OYHgRRvUKqsTmM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705717113; 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=iPM8wpVJ32doUxVMsqvCkRuszJRI7Uk/8j1aTPAJSk0=; b=cyrCMOdARUrEgVEPTvYlvBQKwRbu/t+KROjQKS3Q5Gulodi9bm3nbfvtsXNuhV5P6uFpOm f6O8S19fk6y7AY5/Wg0k2po1ZcueoXyF9k6rKOkk9WEX2oDz85wCPWAEzY/ptXkgHVaa9F GVHzNfjeor1ar3VUADsgiCwRh+RAxBE= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-274-EeKpNeCoPbe4geplVUQcgA-1; Fri, 19 Jan 2024 21:18:30 -0500 X-MC-Unique: EeKpNeCoPbe4geplVUQcgA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B08303C0C4A3; Sat, 20 Jan 2024 02:18:29 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.9.190]) by smtp.corp.redhat.com (Postfix) with ESMTP id 874D451D5; Sat, 20 Jan 2024 02:18:29 +0000 (UTC) From: Marek Polacek To: GCC Patches , libstdc++ , Jason Merrill Subject: [PATCH] c++: -Wdangling-reference and lambda false warning [PR109640] Date: Fri, 19 Jan 2024 21:18:23 -0500 Message-ID: <20240120021823.81839-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.6 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=unavailable 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788574098791935549 X-GMAIL-MSGID: 1788574098791935549 Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- -Wdangling-reference checks if a function receives a temporary as its argument, and only warns if any of the arguments was a temporary. But we should not warn when the temporary represents a lambda or we generate false positives as in the attached testcases. PR c++/113256 PR c++/111607 PR c++/109640 gcc/cp/ChangeLog: * call.cc (do_warn_dangling_reference): Don't warn if the temporary is of lambda type. gcc/testsuite/ChangeLog: * g++.dg/warn/Wdangling-reference14.C: New test. * g++.dg/warn/Wdangling-reference15.C: New test. * g++.dg/warn/Wdangling-reference16.C: New test. --- gcc/cp/call.cc | 9 ++++-- .../g++.dg/warn/Wdangling-reference14.C | 22 +++++++++++++ .../g++.dg/warn/Wdangling-reference15.C | 31 +++++++++++++++++++ .../g++.dg/warn/Wdangling-reference16.C | 13 ++++++++ 4 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference14.C create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference15.C create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference16.C base-commit: 615e25c82de97acc17ab438f88d6788cf7ffe1d6 diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 1f5ff417c81..77f51bacce3 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -14123,7 +14123,10 @@ do_warn_dangling_reference (tree expr, bool arg_p) tree e = expr; while (handled_component_p (e)) e = TREE_OPERAND (e, 0); - if (!reference_like_class_p (TREE_TYPE (e))) + tree type = TREE_TYPE (e); + /* If the temporary represents a lambda, we don't really know + what's going on here. */ + if (!reference_like_class_p (type) && !LAMBDA_TYPE_P (type)) return expr; } @@ -14180,10 +14183,10 @@ do_warn_dangling_reference (tree expr, bool arg_p) initializing this reference parameter. */ if (do_warn_dangling_reference (arg, /*arg_p=*/true)) return expr; - /* Don't warn about member function like: + /* Don't warn about member functions like: std::any a(...); S& s = a.emplace({0}, 0); - which constructs a new object and returns a reference to it, but + which construct a new object and return a reference to it, but we still want to detect: struct S { const S& self () { return *this; } }; const S& s = S().self(); diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference14.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference14.C new file mode 100644 index 00000000000..92b38a965e0 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference14.C @@ -0,0 +1,22 @@ +// PR c++/113256 +// { dg-do compile { target c++14 } } +// { dg-options "-Wdangling-reference" } + +#include +#include + +template auto bind(M T::* pm, A) +{ + return [=]( auto&& x ) -> M const& { return x.*pm; }; +} + +template struct arg {}; + +arg<1> _1; + +int main() +{ + std::pair pair; + int const& x = bind( &std::pair::first, _1 )( pair ); // { dg-bogus "dangling reference" } + assert( &x == &pair.first ); +} diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference15.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference15.C new file mode 100644 index 00000000000..c39577db64a --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference15.C @@ -0,0 +1,31 @@ +// PR c++/111607 +// { dg-do compile { target c++20 } } +// { dg-options "-Wdangling-reference" } + +#include + +struct S { + constexpr S(int i_) : i(i_) {} + S(S const &) = delete; + S & operator=(S const &) = delete; + S(S &&) = delete; + S & operator=(S &&) = delete; + int i; +}; + +struct A { + S s{0}; +}; + +using V = std::variant; + +consteval auto f(V const & v) { + auto const & s = std::visit([](auto const & v) -> S const & { return v.s; }, v); // { dg-bogus "dangling reference" } + return s.i; +} + +int main() { + constexpr V a{std::in_place_type}; + constexpr auto i = f(a); + return i; +} diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference16.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference16.C new file mode 100644 index 00000000000..91996922291 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference16.C @@ -0,0 +1,13 @@ +// PR c++/109640 +// { dg-do compile { target c++11 } } +// { dg-options "-Wdangling-reference" } + +bool +fn0 () +{ + int a; + int&& i = [](int& r) -> int&& { return static_cast(r); }(a); // { dg-bogus "dangling reference" } + auto const l = [](int& r) -> int&& { return static_cast(r); }; + int&& j = l(a); + return &i == &j; +}