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 370383857379 for ; Mon, 7 Nov 2022 14:07:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 370383857379 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=1667830052; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=7FCjoXZwvsKKvceoLc179KMRJKYyA5Wxsz1E4JDQZfo=; b=PH9YsO2+twGVyS07ijjhXpVgGMU4iTchDmmBnXTLkIVRccOB033VmrqZDMTQWQPjFXP5Sd SaBIC2UpOkQ9JKE/6QizEXDIGVqk1N45SeZwP+1sm5ym/Yxy2JBCvhidk8YQfOOIzh9D2J NS+/6tP6pnNnZpvdfLEvjdR2HlNgX1U= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-423-rdXIJo9QOcC_61tPIcS0GQ-1; Mon, 07 Nov 2022 09:07:28 -0500 X-MC-Unique: rdXIJo9QOcC_61tPIcS0GQ-1 Received: by mail-qk1-f200.google.com with SMTP id bs7-20020a05620a470700b006fac7447b1cso3244239qkb.17 for ; Mon, 07 Nov 2022 06:07:27 -0800 (PST) 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:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7FCjoXZwvsKKvceoLc179KMRJKYyA5Wxsz1E4JDQZfo=; b=aei9p0yBfTrfsXPqp1aIvRTGk61JXWCrbkRHhBWfWxOYEYCK/HldLgZS7T1hqXQdv5 hNkBJV3CdPKchR4DGbOoCWLVKYFsW5aN2QjwKDAMwzA+9oaaeoZUb6ZrbUh6QjpDmbYw wE31DGC+t7gQVCof8u3Vu3cAZTozwbMwb4w7GVCGCwMV2F5eBgCtkQ/kqRCmLT6/hQRk cdHchpw9GihSsPEoPMd0NCBDz/ZC0xDVJ1l+ehrWTyuD/W8ygfIR/oEK2l7ZY+MMUhw+ l/9k809Znfzi/rTniIUlvx4ny7qWjxX0OdO/mNFxFpHb7njCKXBZ28ppGippzwtR+lNH XQMg== X-Gm-Message-State: ANoB5pkOB7uGXkUGLWoHMemHM2qlYsiMRxWNvlZz4nbv3dpZSjO3Z6U4 MlfiI1hnBtRbHpG3eJHYhYs/DSr4SiikuKSeKNa/370vXpLih8WQUYPUmlXZm39UL1NfFTL1KoA pCMV4lTejki6Kc8CYh3AtK6G5zRwYOBJsef2ql5gR2Zc5NAcNpBFiru38SZxvATBsmigE X-Received: by 2002:a05:622a:18c:b0:3a5:826f:d9b9 with SMTP id s12-20020a05622a018c00b003a5826fd9b9mr6780687qtw.516.1667830046398; Mon, 07 Nov 2022 06:07:26 -0800 (PST) X-Google-Smtp-Source: AA0mqf6bwVFDffhy5eaaiznlsYwUAKhCA0oLZU2EYi9En4tNG6tGvNMAt2Dh7tJ8p7JCRVh76dQyJg== X-Received: by 2002:a05:622a:18c:b0:3a5:826f:d9b9 with SMTP id s12-20020a05622a018c00b003a5826fd9b9mr6780657qtw.516.1667830046041; Mon, 07 Nov 2022 06:07:26 -0800 (PST) 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 v18-20020ac85792000000b003988b3d5280sm6265588qta.70.2022.11.07.06.07.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Nov 2022 06:07:25 -0800 (PST) Date: Mon, 7 Nov 2022 09:07:23 -0500 From: Marek Polacek To: GCC Patches , Jason Merrill Subject: Re: [PATCH] c++: Tweaks for -Wredundant-move [PR107363] Message-ID: References: <20221028204233.409310-1-polacek@redhat.com> MIME-Version: 1.0 In-Reply-To: <20221028204233.409310-1-polacek@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=-12.2 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_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: Ping. On Fri, Oct 28, 2022 at 04:42:33PM -0400, Marek Polacek via Gcc-patches wrote: > Two things here: > > 1) when we're pointing out that std::move on a constant object is > redundant, don't say "in return statement" when we aren't in a > return statement; > 2) suppress the warning when the std::move call was dependent, because > removing the std::move may not be correct for a different > instantiation of the original template. > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > PR c++/107363 > > gcc/cp/ChangeLog: > > * semantics.cc (finish_call_expr): Suppress OPT_Wpessimizing_move. > * typeck.cc (maybe_warn_pessimizing_move): Check warn_redundant_move > and warning_suppressed_p. Adjust a message depending on return_p. > (check_return_expr): Don't suppress OPT_Wpessimizing_move here. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp0x/Wredundant-move13.C: New test. > --- > gcc/cp/semantics.cc | 4 ++ > gcc/cp/typeck.cc | 16 ++--- > .../g++.dg/cpp0x/Wredundant-move13.C | 61 +++++++++++++++++++ > 3 files changed, 73 insertions(+), 8 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp0x/Wredundant-move13.C > > diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc > index 36aa9c4499f..caaa40fde19 100644 > --- a/gcc/cp/semantics.cc > +++ b/gcc/cp/semantics.cc > @@ -2738,6 +2738,10 @@ finish_call_expr (tree fn, vec **args, bool disallow_virtual, > result = build_min_nt_call_vec (orig_fn, *args); > SET_EXPR_LOCATION (result, cp_expr_loc_or_input_loc (fn)); > KOENIG_LOOKUP_P (result) = koenig_p; > + /* Disable the std::move warnings since this call was dependent > + (c++/89780, c++/107363). This also suppresses the > + -Wredundant-move warning. */ > + suppress_warning (result, OPT_Wpessimizing_move); > if (is_overloaded_fn (fn)) > fn = get_fns (fn); > > diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc > index 2e0fd8fbf17..5f5fb2a212b 100644 > --- a/gcc/cp/typeck.cc > +++ b/gcc/cp/typeck.cc > @@ -10885,7 +10885,9 @@ maybe_warn_pessimizing_move (tree expr, tree type, bool return_p) > and where the std::move does nothing if T does not have a T(const T&&) > constructor, because the argument is const. It will not use T(T&&) > because that would mean losing the const. */ > - else if (TYPE_REF_P (TREE_TYPE (arg)) > + else if (warn_redundant_move > + && !warning_suppressed_p (expr, OPT_Wredundant_move) > + && TYPE_REF_P (TREE_TYPE (arg)) > && CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg)))) > { > tree rtype = TREE_TYPE (TREE_TYPE (arg)); > @@ -10901,8 +10903,11 @@ maybe_warn_pessimizing_move (tree expr, tree type, bool return_p) > return; > } > auto_diagnostic_group d; > - if (warning_at (loc, OPT_Wredundant_move, > - "redundant move in return statement")) > + if (return_p > + ? warning_at (loc, OPT_Wredundant_move, > + "redundant move in return statement") > + : warning_at (loc, OPT_Wredundant_move, > + "redundant move in initialization")) > inform (loc, "remove % call"); > } > } > @@ -11126,11 +11131,6 @@ check_return_expr (tree retval, bool *no_warning) > /* We don't know if this is an lvalue or rvalue use, but > either way we can mark it as read. */ > mark_exp_read (retval); > - /* Disable our std::move warnings when we're returning > - a dependent expression (c++/89780). */ > - if (retval && TREE_CODE (retval) == CALL_EXPR) > - /* This also suppresses -Wredundant-move. */ > - suppress_warning (retval, OPT_Wpessimizing_move); > return retval; > } > > diff --git a/gcc/testsuite/g++.dg/cpp0x/Wredundant-move13.C b/gcc/testsuite/g++.dg/cpp0x/Wredundant-move13.C > new file mode 100644 > index 00000000000..80e7d80cd02 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp0x/Wredundant-move13.C > @@ -0,0 +1,61 @@ > +// PR c++/107363 > +// { dg-do compile { target c++11 } } > +// { dg-options "-Wredundant-move" } > + > +// Define std::move. > +namespace std { > + template > + struct remove_reference > + { typedef _Tp type; }; > + > + template > + struct remove_reference<_Tp&> > + { typedef _Tp type; }; > + > + template > + struct remove_reference<_Tp&&> > + { typedef _Tp type; }; > + > + template > + constexpr typename std::remove_reference<_Tp>::type&& > + move(_Tp&& __t) noexcept > + { return static_cast::type&&>(__t); } > +} > + > +template > +struct Optional { > + U &value(); > + T release_value() { > + T t = std::move (value ()); > + return t; > + } > +}; > + > +struct Foo {}; > +void test(Optional o) { o.release_value(); } > + > +struct F { > + F(const F&); > + F(F&&) = delete; > +}; > + > +struct Z { > + Z(const Z&) = delete; > + Z(Z&&) = delete; > + Z(const Z&&); > +}; > + > +const F& constfref(); > +const Z& constzref(); > + > +void > +g () > +{ > + // Will call F::F(const F&) w/ and w/o std::move. So it's redundant. > + F f = std::move (constfref()); // { dg-warning "redundant move in initialization" } > + (void) f; > + // Will call Z::Z(const Z&&) w/ std::move, and Z::Z(const Z&) w/o. > + // So it's not redundant. > + Z z = std::move (constzref()); > + (void) z; > +} > > base-commit: e583c86f49b9ef6991b25308a0ad60de9697f24a > -- > 2.38.1 > Marek