
poor (and wrong) approximation of the actual rules governing when to build a copy and when it can be elided. The correct implementation is actually simpler than the approximation. When we only enumerate constructors as part of initialization (e.g., for direct initialization or when we're copying from a class type or one of its derived classes), we don't create a copy. When we enumerate all conversion functions, we do create a copy. Before, we created some extra copies and missed some others. The new test copy-initialization.cpp shows a case where we missed creating a (required, non-elidable) copy as part of a user-defined conversion, which resulted in a miscompile. This commit also fixes PR6757, where the missing copy made us reject well-formed code in the ternary operator. This commit also cleans up our handling of copy elision in the case where we create an extra copy of a temporary object, which became necessary now that we produce the right copies. The code that seeks to find the temporary object being copied has moved into Expr::getTemporaryObject(); it used to have two different not-quite-the-same implementations, one in Sema and one in CodeGen. Note that we still do not attempt to perform the named return value optimization, so we miss copy elisions for return values and throw expressions. llvm-svn: 100196
44 lines
1.0 KiB
C++
44 lines
1.0 KiB
C++
// RUN: %clang_cc1 -fsyntax-only -verify %s
|
|
class X {
|
|
public:
|
|
explicit X(const X&);
|
|
X(int*); // expected-note 2{{candidate constructor}}
|
|
explicit X(float*);
|
|
};
|
|
|
|
class Y : public X { };
|
|
|
|
void f(Y y, int *ip, float *fp) {
|
|
X x1 = y; // expected-error{{no matching constructor for initialization of 'X'}}
|
|
X x2 = 0;
|
|
X x3 = ip;
|
|
X x4 = fp; // expected-error{{no viable conversion}}
|
|
}
|
|
|
|
struct foo {
|
|
void bar();
|
|
};
|
|
|
|
// PR3600
|
|
void test(const foo *P) { P->bar(); } // expected-error{{cannot initialize object parameter of type 'foo' with an expression of type 'foo const'}}
|
|
|
|
namespace PR6757 {
|
|
struct Foo {
|
|
Foo();
|
|
Foo(Foo&);
|
|
};
|
|
|
|
struct Bar {
|
|
operator const Foo&() const;
|
|
};
|
|
|
|
void f(Foo); // expected-note{{candidate function not viable: no known conversion from 'PR6757::Bar' to 'PR6757::Foo' for 1st argument}}
|
|
|
|
// FIXME: This isn't really the right reason for the failure. We
|
|
// should fail after overload resolution.
|
|
void g(Foo foo) {
|
|
f(Bar()); // expected-error{{no matching function for call to 'f'}}
|
|
f(foo);
|
|
}
|
|
}
|