From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 269043858C52 for ; Thu, 19 Jan 2023 18:02:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 269043858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674151327; 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: in-reply-to:in-reply-to:references:references; bh=0aNn27eFt7qBpyyD9Ntak5Me0hySiWLNWp5RPCDcCGY=; b=OARBF2/cDPu3lUHR0rrOh0v0dVrP6vJtdiOldW/Jp+JLLpDKOiXo2fnDFF3MrUyyNzXpld 8Trcg3Fn3L1PKtpN/MDT8Zyw/3z2uDeP+woFmyBj2iUJHfL5KnDhCirEbQDC+m5tsa9Vrr ehEMoBMA/RspVq2ZtFzUpCYlSXPhakc= Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-454-KUWRdtvUN5GGn_OsPZwyWw-1; Thu, 19 Jan 2023 13:02:06 -0500 X-MC-Unique: KUWRdtvUN5GGn_OsPZwyWw-1 Received: by mail-pf1-f199.google.com with SMTP id p1-20020aa78601000000b0058bca6db4a0so1259838pfn.20 for ; Thu, 19 Jan 2023 10:02:06 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=0aNn27eFt7qBpyyD9Ntak5Me0hySiWLNWp5RPCDcCGY=; b=WKUw15ELWmEQu+cSwWWoWIU2ZJ1c+p9YQC0x5qdR8AFvhWlV3QV6Q5pYLfPMmrG4vd Qr1hED8z9JFJk/gMt77C6PBZg+TVlLX0MJxf9aRl6OpVFmv3R7NHRxm1tlYWOPHhDEvj q3z99ELr6X2yXiB6flXurgLUVSnesypqwWJzeDfvTPGPF5FVdQF/5P3exWer0L+O7PcF PaUyuD+rYzhU13uMBiRvGrpGZ4+3MpFrdTsynPv1KwrGo1HH11v9OIf27YcnnSYvpBoZ 3zgoi3fzOOtrR+3CjK9N9Y1oJvldsyWDfFHJpIyG6k4atI3kVG4RynYv9K9ko/W6pj9+ +X4Q== X-Gm-Message-State: AFqh2krn+M3DdH18KGcS+POmGDscW9zuWHSS650cHzcaw67di9wUZtrf c+/m9Jun52azPKphlrovhQ1PJBk+XZBp0SxybfHfzh0bwmmkgs15DH99Mt+I4aksnu/+8oSL7eH 251RoldAyepJ0ysB0Qg== X-Received: by 2002:a17:90b:3c90:b0:229:32de:981e with SMTP id pv16-20020a17090b3c9000b0022932de981emr12092776pjb.10.1674151325159; Thu, 19 Jan 2023 10:02:05 -0800 (PST) X-Google-Smtp-Source: AMrXdXuib6Hd4UvQpi4lAgszra5KjFQkGmsO3neyiVVYZsG37Kzw7/eeTO7t8evWISjiLqMpE3ygiw== X-Received: by 2002:a17:90b:3c90:b0:229:32de:981e with SMTP id pv16-20020a17090b3c9000b0022932de981emr12092735pjb.10.1674151324605; Thu, 19 Jan 2023 10:02:04 -0800 (PST) Received: from [192.168.1.108] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id q6-20020a05620a0d8600b006fa43e139b5sm24450349qkl.59.2023.01.19.10.02.03 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 19 Jan 2023 10:02:03 -0800 (PST) Message-ID: Date: Thu, 19 Jan 2023 13:02:02 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Subject: Re: [PATCH v2] c++: -Wdangling-reference with reference wrapper [PR107532] To: Marek Polacek Cc: GCC Patches References: <20230118175200.365397-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,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 List-Id: On 1/18/23 20:13, Marek Polacek wrote: > On Wed, Jan 18, 2023 at 04:07:59PM -0500, Jason Merrill wrote: >> On 1/18/23 12:52, Marek Polacek wrote: >>> Here, -Wdangling-reference triggers where it probably shouldn't, causing >>> some grief. The code in question uses a reference wrapper with a member >>> function returning a reference to a subobject of a non-temporary object: >>> >>> const Plane & meta = fm.planes().inner(); >>> >>> I've tried a few approaches, e.g., checking that the member function's >>> return type is the same as the type of the enclosing class (which is >>> the case for member functions returning *this), but that then breaks >>> Wdangling-reference4.C with std::optional. >>> >>> So I figured that perhaps we want to look at the object we're invoking >>> the member function(s) on and see if that is a temporary, as in, don't >>> warn about >>> >>> const Plane & meta = fm.planes().inner(); >>> >>> but do warn about >>> >>> const Plane & meta = FrameMetadata().planes().inner(); >>> >>> It's ugly, but better than asking users to add #pragmas into their code. >> >> Hmm, that doesn't seem right; the former is only OK because Ref is in fact a >> reference-like type. If planes() returned a class that held data, we would >> want to warn. > > Sure, it's always some kind of tradeoff with warnings :/. > >> In this case, we might recognize the reference-like class because it has a >> reference member and a constructor taking the same reference type. > > That occurred to me too, but then I found out that std::reference_wrapper > actually uses T*, not T&, as you say. But here's a patch to do that > (I hope). > >> That wouldn't help with std::reference_wrapper or std::ref_view because they >> have pointer members instead of references, but perhaps loosening the check >> to include that case would make sense? > > Sorry, I don't understand what you mean by loosening the check. I could > hardcode std::reference_wrapper and std::ref_view but I don't think that's > what you meant. Indeed that's not what I meant, but as I was saying in our meeting I think it's worth doing; the compiler has various tweaks to handle specific standard-library classes better. > Surely I cannot _not_ warn for any class that contains a T*. I was thinking if a constructor takes a T& and the class has a T* that would be close enough, though this also wouldn't handle the standard library classes so the benefit is questionable. > Here's the patch so that we have some actual code to discuss... Thanks. > > -- >8 -- > Here, -Wdangling-reference triggers where it probably shouldn't, causing > some grief. The code in question uses a reference wrapper with a member > function returning a reference to a subobject of a non-temporary object: > > const Plane & meta = fm.planes().inner(); > > I've tried a few approaches, e.g., checking that the member function's > return type is the same as the type of the enclosing class (which is > the case for member functions returning *this), but that then breaks > Wdangling-reference4.C with std::optional. > > Perhaps we want to look at the member function's enclosing class > to see if it's a reference wrapper class (meaning, has a reference > member and a constructor taking the same reference type) and don't > warn if so, supposing that the member function returns a reference > to a non-temporary object. > > It's ugly, but better than asking users to add #pragmas into their code. > > PR c++/107532 > > gcc/cp/ChangeLog: > > * call.cc (do_warn_dangling_reference): Don't warn when the > member function comes from a reference wrapper class. Let's factor the new code out into e.g. reference_like_class_p > gcc/testsuite/ChangeLog: > > * g++.dg/warn/Wdangling-reference8.C: New test. > --- > gcc/cp/call.cc | 32 ++++++++ > .../g++.dg/warn/Wdangling-reference8.C | 77 +++++++++++++++++++ > 2 files changed, 109 insertions(+) > create mode 100644 gcc/testsuite/g++.dg/warn/Wdangling-reference8.C > > diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc > index 0780b5840a3..b0670a21240 100644 > --- a/gcc/cp/call.cc > +++ b/gcc/cp/call.cc > @@ -13832,6 +13832,38 @@ do_warn_dangling_reference (tree expr) > if (!(TYPE_REF_OBJ_P (rettype) || std_pair_ref_ref_p (rettype))) > return NULL_TREE; > > + /* An attempt to reduce the number of -Wdangling-reference > + false positives concerning reference wrappers (c++/107532). > + If the enclosing class is a reference-like class, that is, has > + a reference member and a constructor taking the same reference type, > + we suppose that the member function is returning a reference > + to a non-temporary object. */ > + if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl) > + && !DECL_OVERLOADED_OPERATOR_P (fndecl)) > + { > + tree ctx = CP_DECL_CONTEXT (fndecl); > + for (tree fields = TYPE_FIELDS (ctx); > + 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 (ctx))) > + { > + tree args = FUNCTION_FIRST_USER_PARMTYPE (fn); > + if (args > + && same_type_p (TREE_VALUE (args), type) > + && TREE_CHAIN (args) == void_list_node) > + return NULL_TREE; > + } > + } > + } > + > /* Here we're looking to see if any of the arguments is a temporary > initializing a reference parameter. */ > for (int i = 0; i < call_expr_nargs (expr); ++i) > diff --git a/gcc/testsuite/g++.dg/warn/Wdangling-reference8.C b/gcc/testsuite/g++.dg/warn/Wdangling-reference8.C > new file mode 100644 > index 00000000000..4d585891fae > --- /dev/null > +++ b/gcc/testsuite/g++.dg/warn/Wdangling-reference8.C > @@ -0,0 +1,77 @@ > +// PR c++/107532 > +// { dg-do compile { target c++11 } } > +// { dg-options "-Wdangling-reference" } > + > +struct Plane { unsigned int bytesused; }; > + > +// Passes a reference through. Does not change lifetime. > +template > +struct Ref { > + const T& i_; > + Ref(const T & i) : i_(i) {} > + const T & inner(); > +}; > + > +struct FrameMetadata { > + Ref planes() const { return p_; } > + > + Plane p_; > +}; > + > +void bar(const Plane & meta); > +void foo(const FrameMetadata & fm) > +{ > + const Plane & meta = fm.planes().inner(); > + bar(meta); > + const Plane & meta2 = FrameMetadata().planes().inner(); > + bar(meta2); > +} > + > +struct S { > + const S& self () { return *this; } > +} s; > + > +const S& r1 = s.self(); > +const S& r2 = S().self(); // { dg-warning "dangling reference" } > + > +struct D { > +}; > + > +struct C { > + D d; > + Ref get() const { return d; } > +}; > + > +struct B { > + C c; > + const C& get() const { return c; } > + B(); > +}; > + > +struct A { > + B b; > + const B& get() const { return b; } > +}; > + > +void > +g (const A& a) > +{ > + const auto& d1 = a.get().get().get().inner(); > + (void) d1; > + const auto& d2 = A().get().get().get().inner(); > + (void) d2; > + const auto& d3 = A().b.get().get().inner(); > + (void) d3; > + const auto& d4 = a.b.get().get().inner(); > + (void) d4; > + const auto& d5 = a.b.c.get().inner(); > + (void) d5; > + const auto& d6 = A().b.c.get().inner(); > + (void) d6; > + Plane p; > + Ref r(p); > + const auto& d7 = r.inner(); > + (void) d7; > + const auto& d8 = Ref(p).inner(); > + (void) d8; > +} > > base-commit: 8e2c6e7b426b6c9c13076208b2e176d4aa1432f1