diff mbox series

[pushed] c++: -Wdeprecated-copy and using operator= [PR92145]

Message ID 20210427194230.2640967-1-jason@redhat.com
State New
Headers show
Series [pushed] c++: -Wdeprecated-copy and using operator= [PR92145] | expand

Commit Message

Jason Merrill April 27, 2021, 7:42 p.m. UTC
For the purpose of [depr.impldec] "if the class has a user-declared copy
assignment operator", an operator= brought in from a base class with 'using'
may be a copy-assignment operator, but it isn't a copy-assignment operator
for the derived class.

Tested x86_64-pc-linux-gnu, applying to trunk.

gcc/cp/ChangeLog:

	PR c++/92145
	* class.c (classtype_has_depr_implicit_copy): Check DECL_CONTEXT
	of operator=.

gcc/testsuite/ChangeLog:

	PR c++/92145
	* g++.dg/cpp0x/depr-copy3.C: New test.
---
 gcc/cp/class.c                          |  3 ++-
 gcc/testsuite/g++.dg/cpp0x/depr-copy3.C | 35 +++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/depr-copy3.C


base-commit: dfdc02bf29670c1c7f5f2820b6db11c66c258716
diff mbox series

Patch

diff --git a/gcc/cp/class.c b/gcc/cp/class.c
index 90b343803a0..2cf527e4a84 100644
--- a/gcc/cp/class.c
+++ b/gcc/cp/class.c
@@ -5670,7 +5670,8 @@  classtype_has_depr_implicit_copy (tree t)
 	 iter; ++iter)
       {
 	tree fn = *iter;
-	if (user_provided_p (fn) && copy_fn_p (fn))
+	if (DECL_CONTEXT (fn) == t
+	    && user_provided_p (fn) && copy_fn_p (fn))
 	  return fn;
       }
 
diff --git a/gcc/testsuite/g++.dg/cpp0x/depr-copy3.C b/gcc/testsuite/g++.dg/cpp0x/depr-copy3.C
new file mode 100644
index 00000000000..c303c9d5d40
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/depr-copy3.C
@@ -0,0 +1,35 @@ 
+// PR c++/92145
+// { dg-do compile { target c++11 } }
+// { dg-additional-options "-Wdeprecated-copy" }
+
+struct base
+{
+  base() { }
+  base(const base&) { }
+  base(base&&) { }
+  base& operator=(const base&) { return *this; }
+  base& operator=(base&&) { return *this; }
+};
+
+struct foo : base
+{
+  //using base::base;
+  using base::operator=;
+};
+
+struct bar
+{
+  bar& operator=(foo v)
+  {
+    value = v;
+    return *this;
+  }
+
+  foo value;
+};
+
+int main()
+{
+  foo a;
+  foo{a};
+}