public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r11-6964] c++: Fix -Weffc++ in templates [PR98841]
Date: Thu, 28 Jan 2021 23:40:27 +0000 (GMT)	[thread overview]
Message-ID: <20210128234027.69D6739C0BF1@sourceware.org> (raw)

https://gcc.gnu.org/g:850a8ec54c4310d779004299bf9a0dec52324e9e

commit r11-6964-g850a8ec54c4310d779004299bf9a0dec52324e9e
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Jan 29 00:39:00 2021 +0100

    c++: Fix -Weffc++ in templates [PR98841]
    
    We emit a bogus warning on the following testcase, suggesting that the
    operator should return *this even when it does that already.
    The problem is that normally cp_build_indirect_ref_1 ensures that *this
    is folded as current_class_ref, but in templates (if return type is
    non-dependent, otherwise check_return_expr doesn't check it) it didn't
    go through cp_build_indirect_ref_1, but just built another INDIRECT_REF.
    Which means it then doesn't compare pointer-equal to current_class_ref.
    
    The following patch fixes it by doing in build_x_indirect_ref for
    *this what cp_build_indirect_ref_1 would do.
    
    2021-01-28  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/98841
            * typeck.c (build_x_indirect_ref): For *this, return current_class_ref.
    
            * g++.dg/warn/effc5.C: New test.

Diff:
---
 gcc/cp/typeck.c                   | 10 +++++++++-
 gcc/testsuite/g++.dg/warn/effc5.C | 17 +++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 315f706de73..9322e087345 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -3326,7 +3326,15 @@ build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
     {
       /* Retain the type if we know the operand is a pointer.  */
       if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
-	return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
+	{
+	  if (expr == current_class_ptr
+	      || (TREE_CODE (expr) == NOP_EXPR
+		  && TREE_OPERAND (expr, 0) == current_class_ptr
+		  && (same_type_ignoring_top_level_qualifiers_p
+			(TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
+	    return current_class_ref;
+	  return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
+	}
       if (type_dependent_expression_p (expr))
 	return build_min_nt_loc (loc, INDIRECT_REF, expr);
       expr = build_non_dependent_expr (expr);
diff --git a/gcc/testsuite/g++.dg/warn/effc5.C b/gcc/testsuite/g++.dg/warn/effc5.C
new file mode 100644
index 00000000000..43fdd095216
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/effc5.C
@@ -0,0 +1,17 @@
+// PR c++/98841
+// { dg-do compile }
+// { dg-options "-Weffc++" }
+
+struct S {
+  template <typename T>
+  S& operator=(const T&) { return *this; }	// { dg-bogus "should return a reference to" }
+  S& operator=(const S&) { return *this; }
+};
+
+void
+foo ()
+{
+  S s, t;
+  s = 1;
+  s = t;
+}


                 reply	other threads:[~2021-01-28 23:40 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210128234027.69D6739C0BF1@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).