]> granicus.if.org Git - clang/commitdiff
Avoid using the built-in type checker for assignment in C++ when classes are involved...
authorSebastian Redl <sebastian.redl@getdesigned.at>
Thu, 21 May 2009 11:50:50 +0000 (11:50 +0000)
committerSebastian Redl <sebastian.redl@getdesigned.at>
Thu, 21 May 2009 11:50:50 +0000 (11:50 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72212 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaOverload.cpp
test/SemaCXX/copy-assignment.cpp [new file with mode: 0644]
test/SemaCXX/namespace.cpp
test/SemaCXX/overloaded-builtin-operators.cpp
test/SemaTemplate/qualified-names-diag.cpp

index ef175315e6e9e980667f2db675df24df1bc20652..98ee13af856a88f594137b576968b9dbda5bc5d5 100644 (file)
@@ -4020,6 +4020,15 @@ Sema::CreateOverloadedBinOp(SourceLocation OpLoc,
     }
 
     case OR_No_Viable_Function:
+      // For class as left operand for assignment or compound assigment operator
+      // do not fall through to handling in built-in, but report that no overloaded
+      // assignment operator found
+      if (LHS->getType()->isRecordType() && Opc >= BinaryOperator::Assign && Opc <= BinaryOperator::OrAssign) {
+        Diag(OpLoc,  diag::err_ovl_no_viable_oper)
+             << BinaryOperator::getOpcodeStr(Opc)
+             << LHS->getSourceRange() << RHS->getSourceRange();
+        return ExprError();
+      }
       // No viable function; fall through to handling this as a
       // built-in operator, which will produce an error message for us.
       break;
diff --git a/test/SemaCXX/copy-assignment.cpp b/test/SemaCXX/copy-assignment.cpp
new file mode 100644 (file)
index 0000000..6e5012f
--- /dev/null
@@ -0,0 +1,99 @@
+// RUN: clang-cc -fsyntax-only -verify %s 
+struct A {
+};
+
+struct ConvertibleToA {
+  operator A();
+};
+
+struct ConvertibleToConstA {
+  operator const A();
+};
+
+struct B {
+  B& operator=(B&);
+};
+
+struct ConvertibleToB {
+  operator B();
+};
+
+struct ConvertibleToBref {
+  operator B&();
+};
+
+struct ConvertibleToConstB {
+  operator const B();
+};
+
+struct ConvertibleToConstBref {
+  operator const B&();
+};
+
+struct C {
+  int operator=(int); // expected-note{{candidate function}}
+  long operator=(long); // expected-note{{candidate function}}
+  int operator+=(int); // expected-note{{candidate function}}
+  int operator+=(long); // expected-note{{candidate function}}
+};
+
+struct D {
+  D& operator+=(const D &);
+};
+
+struct ConvertibleToInt {
+  operator int();
+};
+
+void test() {
+  A a, na;
+  const A constA;
+  ConvertibleToA convertibleToA;
+  ConvertibleToConstA convertibleToConstA;
+
+  B b, nb;
+  const B constB;
+  ConvertibleToB convertibleToB;
+  ConvertibleToBref convertibleToBref;
+  ConvertibleToConstB convertibleToConstB;
+  ConvertibleToConstBref convertibleToConstBref;
+
+  C c, nc;
+  const C constC;
+
+  D d, nd;
+  const D constD;
+
+  ConvertibleToInt convertibleToInt;
+
+  na = a;
+  na = constA;
+  na = convertibleToA;
+  na = convertibleToConstA;
+  na += a; // expected-error{{no viable overloaded '+='}}
+
+  nb = b;
+  nb = constB;  // expected-error{{no viable overloaded '='}}
+  nb = convertibleToB; // expected-error{{no viable overloaded '='}}
+  nb = convertibleToBref;
+  nb = convertibleToConstB; // expected-error{{no viable overloaded '='}}
+  nb = convertibleToConstBref; // expected-error{{no viable overloaded '='}}
+
+  nc = c;
+  nc = constC;
+  nc = 1;
+  nc = 1L;
+  nc = 1.0; // expected-error{{use of overloaded operator '=' is ambiguous}}
+  nc += 1;
+  nc += 1L;
+  nc += 1.0; // expected-error{{use of overloaded operator '+=' is ambiguous}}
+
+  nd = d;
+  nd += d;
+  nd += constD;
+
+  int i;
+  i = convertibleToInt;
+  i = a; // expected-error{{incompatible type assigning 'struct A', expected 'int'}}
+}
+
index 592ca30790f16e5df57e6f522941503531e07dcf..696ea818f6570eea24080fbd0e74eb27a39d66e1 100644 (file)
@@ -37,7 +37,7 @@ namespace S1 {
     
     namespace S3 {
       void f() {
-        x = 0; // expected-error {{incompatible type assigning 'int', expected 'class B'}}
+        x = 0; // expected-error {{no viable overloaded '='}}
       }
     }
 
index 5989dc272639fe9c8c9035ac3e0838cdb16b6bb9..2a6c24a6778ac0c76f5afe0d3d045df55935ec94 100644 (file)
@@ -118,5 +118,5 @@ void test_with_ptrs(VolatileIntPtr vip, ConstIntPtr cip, ShortRef sr,
 // C++ [over.match.open]p4
 
 void test_assign_restrictions(ShortRef& sr) {
-  sr = (short)0; // expected-error{{incompatible type assigning 'short', expected 'struct ShortRef'}}
+  sr = (short)0; // expected-error{{no viable overloaded '='}}
 }
index 4bc8bdfc9a84e81afdca14bde246fe1328cb2cf9..c875332905fb321fbd9abba14e15947ed7b0f7bf 100644 (file)
@@ -12,5 +12,5 @@ void test() {
 
   std::vector<INT> v1;
   vector<Real> v2;
-  v1 = v2; // expected-error{{incompatible type assigning 'vector<Real>', expected 'std::vector<INT>'}}
+  v1 = v2; // expected-error{{no viable overloaded '='}}
 }