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 6DE213858CDB for ; Thu, 3 Nov 2022 19:02:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6DE213858CDB 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=1667502150; 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: in-reply-to:in-reply-to:references:references; bh=YytlGEhqyz5gsFze9yJPPy3mw74eaONW6K2c8+ZsGGs=; b=Uiy05PXDwySHeyTiJ+a11OoZCzyQ7OIY59IPFAnvFKH6Jp26bCZry8B4Ye8+RrIPq+qJxl DcpyEM0+ymRDo3rR0kPRCZIuERCHtk4P9Qfg3JYLrxDBBNdVirQlJuGikOFMRoJ7mhwVUF 43NigfYgvDnUlVYkLRU6YX3Zs3HNCts= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-604-3z5B1mgnMOSKs-XVZajobg-1; Thu, 03 Nov 2022 15:02:29 -0400 X-MC-Unique: 3z5B1mgnMOSKs-XVZajobg-1 Received: by mail-qt1-f198.google.com with SMTP id s14-20020a05622a1a8e00b00397eacd9c1aso2449356qtc.21 for ; Thu, 03 Nov 2022 12:02:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YytlGEhqyz5gsFze9yJPPy3mw74eaONW6K2c8+ZsGGs=; b=fU6Me3xDak8PFOgf0p5Hqem8Mrj8p5CjkzwQH/YD7d3pi82vjJkggZjhsR9VimoByj BqHeW3BXnpHFgXsS8PPtktSZxuktIrQ/q344pEpxiO/ir8VhWue2TuucXtLnM7wTeyWQ T503KOnZIIh3RSNoTGPjytaJsJf4STNmEjmtQqruaqFqjDtKFNteT8FEghy2Jf3nhRb4 q21KA6ReVYZB+VJewVtRRhLwBeRiz06pZ+RAwF10zPWKbR9x6nqGSTNhhDVdlImvDg6j 0ZR1X7goD+pSbgrosYKAmsytUYFW4n6RzP+aWzxdFPpSgF0EZYiRBhQIonovlbX7HlKX oP4g== X-Gm-Message-State: ACrzQf3RYjeaFcqViQDKyYd20aYI3TNXYcdSXyidLK59PK9hR5bU8Gde FP81ZS6+P23Jidl+jLYJTy8ST3vpqnDAsqU7SQvLWWooS/vlbkXpVb0gPaf7/t52Eit49Cf+c7D JSOc+dAwYHPuOZ7IIUg== X-Received: by 2002:a05:620a:662:b0:6f8:82a6:d6ea with SMTP id a2-20020a05620a066200b006f882a6d6eamr22666917qkh.133.1667502148403; Thu, 03 Nov 2022 12:02:28 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4GEsETldSYVjiDiHtto4S59/ornLrLCu4qjfCbjHDEHiop7fA1nXG5FrcJGXGJIMwZGR3a+A== X-Received: by 2002:a05:620a:662:b0:6f8:82a6:d6ea with SMTP id a2-20020a05620a066200b006f882a6d6eamr22666894qkh.133.1667502148146; Thu, 03 Nov 2022 12:02:28 -0700 (PDT) Received: from redhat.com (2603-7000-9500-2e39-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:2e39::1db4]) by smtp.gmail.com with ESMTPSA id s10-20020a05620a254a00b006bb8b5b79efsm1200280qko.129.2022.11.03.12.02.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Nov 2022 12:02:27 -0700 (PDT) Date: Thu, 3 Nov 2022 15:02:25 -0400 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: Re: [PATCH] c++: Quash -Wdangling-reference for member operator* [PR107488] Message-ID: References: <20221101220652.588178-1-polacek@redhat.com> <7db5d94d-f75e-9387-bb43-3ccec6f7fdb3@redhat.com> MIME-Version: 1.0 In-Reply-To: <7db5d94d-f75e-9387-bb43-3ccec6f7fdb3@redhat.com> User-Agent: Mutt/2.2.7 (2022-08-07) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-6.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 Thu, Nov 03, 2022 at 02:54:12PM -0400, Jason Merrill wrote: > On 11/1/22 18:06, Marek Polacek wrote: > > -Wdangling-reference complains here: > > > > std::vector v = ...; > > std::vector::const_iterator it = v.begin(); > > while (it != v.end()) { > > const int &r = *it++; // warning > > } > > > > because it sees a call to > > __gnu_cxx::__normal_iterator >::operator* > > which returns a reference and its argument is a TARGET_EXPR representing > > the result of > > __gnu_cxx::__normal_iterator >::operator++ > > But 'r' above refers to one of the int elements of the vector 'v', not > > to a temporary object. Therefore the warning is a false positive. > > > > I suppose code like the above is relatively common (the warning broke > > cppunit-1.15.1 and a few other projects), so presumably it makes sense > > to suppress the warning when it comes to member operator*. In this case > > it's defined as > > > > reference > > operator*() const _GLIBCXX_NOEXCEPT > > { return *_M_current; } > > > > and I'm guessing a lot of member operator* are like that, at least when > > it comes to iterators. I've looked at _Fwd_list_iterator, > > _Fwd_list_const_iterator, __shared_ptr_access, _Deque_iterator, > > istream_iterator, etc, and they're all like that, so adding #pragmas > > would be quite tedious. :/ > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > OK. Thanks. > It also occurred to me that we should avoid warning if the reference we're > initializing is a non-const lvalue reference, which can't bind to a > temporary. Yup; amusingly I noticed that too while working with the reduced version of the testcase, which I deliberately didn't end up using, and which reduced to 'int&' rather than 'const int&'. > Maybe also if the function returns a non-const lvalue reference. Ok. Expect a patch soon. Marek