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.129.124]) by sourceware.org (Postfix) with ESMTPS id D6F143858404 for ; Mon, 14 Nov 2022 23:16:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D6F143858404 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=1668467769; 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: in-reply-to:in-reply-to:references:references; bh=gR4zgpjuD5LOeusJnalH08iitxQjJc9bnJ9NxsAtDNA=; b=Nj+SYCLnQf+WxLamTWE8w6eO8TwsGZj1xZWc+meF8gXhP/rSwWSX7Hf1cFR6hNfDZxhhpf K5Jb2xIV+/UWrzaPaLKWEwS9TiJFMzOk6XFvuR3Ui/KbEGK4SVjlI4vfK83luFuDDuzPvN C5WSeuhfz+GU2bb7pxNKj5orJMzARfM= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-119-wXh3WJixP6e3qeDStledZg-1; Mon, 14 Nov 2022 18:16:08 -0500 X-MC-Unique: wXh3WJixP6e3qeDStledZg-1 Received: by mail-qv1-f71.google.com with SMTP id mh13-20020a056214564d00b004c60dd95880so8279967qvb.6 for ; Mon, 14 Nov 2022 15:16:08 -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: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=gR4zgpjuD5LOeusJnalH08iitxQjJc9bnJ9NxsAtDNA=; b=zmHCWXWoCdSIXnb0RrSNKqpTmY6yyb8OLaEjhOSIzptpGgfW+8ts2Zr/GNUZAFrUUb 4op4788bcQMeQ5f+swf4QBxsCvlQHzr9/P99RMdrMnbVntHyYpFqvdS3RVW2STwPSPys UysV3bM0wGydJTxoZaMGYTDsSQwLOzKcVeuz3Q4QzQWyO567KP7we1IElMo0OF48tRMS BusnB+degl5kTY2hdxPfTk5L5UollxEIvp0ZHkOY7CTpCOI05IgFvOOvtyBaeGDy1jzE abPkL4v06G680ictU9QONhDh3Uc+Uf9Z351RW24mRysmmXXJovpVWyTdrzYEK8dPBCOg Ijcw== X-Gm-Message-State: ANoB5plfrEp6KfgfjPBtJRnbCKOUs1jWwESHXxk2bP0EsZuBemrBNeg/ FjvFjg/xp2hOPMiT4QuQ4S0jKkwuz+rOl8lWzusHXf7DjGd25/wnu5+Ihfb2Sf0tqh4mxlgOzE5 /uNOcMMlQJ1XBua+ENQ== X-Received: by 2002:ae9:e203:0:b0:6ce:1a26:ff75 with SMTP id c3-20020ae9e203000000b006ce1a26ff75mr13115848qkc.698.1668467767388; Mon, 14 Nov 2022 15:16:07 -0800 (PST) X-Google-Smtp-Source: AA0mqf6YDzPtOmqDUrIMHwemDG8Fxxqva6L4f+YUw6Yn6Yrl0BbPoKIxz9vHGwDQ70Wj7HAarha6Fw== X-Received: by 2002:ae9:e203:0:b0:6ce:1a26:ff75 with SMTP id c3-20020ae9e203000000b006ce1a26ff75mr13115829qkc.698.1668467767056; Mon, 14 Nov 2022 15:16:07 -0800 (PST) Received: from [192.168.1.101] (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 l11-20020a37f90b000000b006e8f8ca8287sm7287639qkj.120.2022.11.14.15.16.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Nov 2022 15:16:06 -0800 (PST) Message-ID: <9ce5ee29-8e05-f27e-206f-89d4783608f1@redhat.com> Date: Mon, 14 Nov 2022 18:16:05 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: [PATCH] c++: Disable -Wdangling-reference when initing T& To: Marek Polacek , GCC Patches References: <20221111202226.103649-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: <20221111202226.103649-1-polacek@redhat.com> 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 11/11/22 10:22, Marek Polacek wrote: > 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? OK. > 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 > > 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" } > +} > > base-commit: 0a7b437ca71e2721e9bcf070762fc54ef7991aeb