[clang] [Clang] Diagnose defaulted assignment operator with incompatible object parameter (PR #70176)
Shafik Yaghmour via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 27 10:07:44 PDT 2023
================
@@ -585,3 +585,34 @@ class Server : public Thing {
S name_;
};
}
+
+namespace GH69233 {
+struct Base {};
+struct S : Base {
+ int j;
+ S& operator=(this Base& self, const S&) = default;
+ // expected-warning at -1 {{explicitly defaulted copy assignment operator is implicitly deleted}}
+ // expected-note at -2 {{function is implicitly deleted because its declared type does not match the type of an implicit copy assignment operator}}
+ // expected-note at -3 {{explicitly defaulted function was implicitly deleted here}}
+};
+
+struct S2 {
+ S2& operator=(this int&& self, const S2&);
+ S2& operator=(this int&& self, S2&&);
+ operator int();
+};
+
+S2& S2::operator=(this int&& self, const S2&) = default;
+// expected-error at -1 {{the type of the explicit object parameter of an explicitly-defaulted copy assignment operator should match the type of the class 'S2'}}
+
+S2& S2::operator=(this int&& self, S2&&) = default;
+// expected-error at -1 {{the type of the explicit object parameter of an explicitly-defaulted move assignment operator should match the type of the class 'S2'}}
+
+void test() {
+ S s;
+ s = s; // expected-error {{object of type 'S' cannot be assigned because its copy assignment operator is implicitly deleted}}
----------------
shafik wrote:
Can we get an example where move assignment fails as well.
https://github.com/llvm/llvm-project/pull/70176
More information about the cfe-commits
mailing list