2021-06-14 19:23:18 +08:00
// RUN: %clang_cc1 -std=c++2b -fsyntax-only -verify=expected -triple %itanium_abi_triple -Wbind-to-temporary-copy %s
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=expected -triple %itanium_abi_triple -Wbind-to-temporary-copy %s
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify=expected,cxx98_11,cxx11 -triple %itanium_abi_triple -Wbind-to-temporary-copy %s
// RUN: %clang_cc1 -std=c++98 -fsyntax-only -verify=expected,cxx98_11,cxx98 -triple %itanium_abi_triple -Wbind-to-temporary-copy %s
2016-04-15 07:47:07 +08:00
2021-04-03 09:10:12 +08:00
class X {
2008-11-08 04:08:42 +08:00
public :
operator bool ( ) ;
operator int ( ) const ;
2008-11-18 04:34:05 +08:00
bool f ( ) {
return operator bool ( ) ;
}
float g ( ) {
2009-12-16 16:11:27 +08:00
return operator float ( ) ; // expected-error{{use of undeclared 'operator float'}}
2008-11-18 04:34:05 +08:00
}
2013-06-21 04:58:02 +08:00
static operator short ( ) ; // expected-error{{conversion function must be a non-static member function}}
2008-11-08 04:08:42 +08:00
} ;
operator int ( ) ; // expected-error{{conversion function must be a non-static member function}}
2008-11-18 06:58:34 +08:00
operator int ; // expected-error{{'operator int' cannot be the name of a variable or data member}}
2008-11-08 04:08:42 +08:00
typedef int func_type ( int ) ;
typedef int array_type [ 10 ] ;
class Y {
public :
void operator bool ( int , . . . ) const ; / / expected - error { { conversion function cannot have a return type } } \
2009-04-25 16:35:12 +08:00
// expected-error{{conversion function cannot have any parameters}}
2014-05-11 03:15:24 +08:00
2014-05-11 08:28:16 +08:00
operator bool ( int a = 4 , int b = 6 ) const ; // expected-error{{conversion function cannot have any parameters}}
2021-04-03 09:10:12 +08:00
2009-04-25 16:35:12 +08:00
operator float ( . . . ) const ; // expected-error{{conversion function cannot be variadic}}
2021-04-03 09:10:12 +08:00
2008-11-08 04:08:42 +08:00
operator func_type ( ) ; // expected-error{{conversion function cannot convert to a function type}}
operator array_type ( ) ; // expected-error{{conversion function cannot convert to an array type}}
} ;
typedef int INT ;
typedef INT * INT_PTR ;
2021-04-03 09:10:12 +08:00
class Z {
2008-11-24 07:12:31 +08:00
operator int ( ) ; // expected-note {{previous declaration is here}}
operator int * * ( ) ; // expected-note {{previous declaration is here}}
2021-04-03 09:10:12 +08:00
2008-11-08 04:08:42 +08:00
operator INT ( ) ; // expected-error{{conversion function cannot be redeclared}}
operator INT_PTR * ( ) ; // expected-error{{conversion function cannot be redeclared}}
} ;
class A { } ;
class B : public A {
public :
2010-03-10 19:27:22 +08:00
operator A & ( ) const ; // expected-warning{{conversion function converting 'B' to its base class 'A' will never be used}}
2010-09-05 08:04:01 +08:00
operator const void ( ) const ; // expected-warning{{conversion function converting 'B' to 'const void' will never be used}}
2010-03-10 19:27:22 +08:00
operator const B ( ) ; // expected-warning{{conversion function converting 'B' to itself will never be used}}
2008-11-08 04:08:42 +08:00
} ;
2009-04-17 01:51:27 +08:00
2020-04-19 10:23:21 +08:00
class BaseA { } ;
class DerivedA ;
class BaseB {
virtual operator BaseA & ( ) = 0 ;
virtual operator DerivedA & ( ) = 0 ;
} ;
class DerivedA : public BaseA , BaseB {
virtual operator BaseA & ( ) ; // OK. Overrides BaseB::operatorBaseA&()
virtual operator DerivedA & ( ) ; // OK. Overrides BaseB::operatorDerivedA&()
} ;
class DerivedB : public BaseA {
virtual operator DerivedB & ( ) ; // expected-warning{{conversion function converting 'DerivedB' to itself will never be used}}
virtual operator BaseA & ( ) ; // expected-warning{{conversion function converting 'DerivedB' to its base class 'BaseA' will never be used}}
} ;
2009-04-17 01:51:27 +08:00
// This used to crash Clang.
struct Flip ;
2010-04-25 04:54:38 +08:00
struct Flop {
2009-04-17 01:51:27 +08:00
Flop ( ) ;
2010-01-06 17:43:14 +08:00
Flop ( const Flip & ) ; // expected-note{{candidate constructor}}
2009-04-17 01:51:27 +08:00
} ;
struct Flip {
2009-12-19 11:01:41 +08:00
operator Flop ( ) const ; // expected-note{{candidate function}}
2009-04-17 01:51:27 +08:00
} ;
2010-03-10 19:27:22 +08:00
Flop flop = Flip ( ) ; // expected-error {{conversion from 'Flip' to 'Flop' is ambiguous}}
2009-09-14 05:33:06 +08:00
// This tests that we don't add the second conversion declaration to the list of user conversions
struct C {
operator const char * ( ) const ;
} ;
C : : operator const char * ( ) const { return 0 ; }
void f ( const C & c ) {
const char * v = c ;
}
2009-09-15 04:41:01 +08:00
// Test. Conversion in base class is visible in derived class.
2021-04-03 09:10:12 +08:00
class XB {
2009-09-15 04:41:01 +08:00
public :
2009-09-15 08:10:11 +08:00
operator int ( ) ; // expected-note {{candidate function}}
2009-09-15 04:41:01 +08:00
} ;
2021-04-03 09:10:12 +08:00
class Yb : public XB {
2009-09-15 04:41:01 +08:00
public :
2009-09-15 08:10:11 +08:00
operator char ( ) ; // expected-note {{candidate function}}
2009-09-15 04:41:01 +08:00
} ;
void f ( Yb & a ) {
2010-03-10 19:27:22 +08:00
if ( a ) { } // expected-error {{conversion from 'Yb' to 'bool' is ambiguous}}
2009-09-15 04:41:01 +08:00
int i = a ; // OK. calls XB::operator int();
char ch = a ; // OK. calls Yb::operator char();
}
[clang] C++98 implicit moves are back with a vengeance
After taking C++98 implicit moves out in D104500,
we put it back in, but now in a new form which preserves
compatibility with pure C++98 programs, while at the same time
giving almost all the goodies from P1825.
* We use the exact same rules as C++20 with regards to which
id-expressions are move eligible. The previous
incarnation would only benefit from the proper subset which is
copy ellidable. This means we can implicit move, in addition:
* Parameters.
* RValue references.
* Exception variables.
* Variables with higher-than-natural required alignment.
* Objects with different type from the function return type.
* We preserve the two-overload resolution, with one small tweak to the
first one: If we either pick a (possibly converting) constructor which
does not take an rvalue reference, or a user conversion operator which
is not ref-qualified, we abort into the second overload resolution.
This gives C++98 almost all the implicit move patterns which we had created test
cases for, while at the same time preserving the meaning of these
three patterns, which are found in pure C++98 programs:
* Classes with both const and non-const copy constructors, but no move
constructors, continue to have their non-const copy constructor
selected.
* We continue to reject as ambiguous the following pattern:
```
struct A { A(B &); };
struct B { operator A(); };
A foo(B x) { return x; }
```
* We continue to pick the copy constructor in the following pattern:
```
class AutoPtrRef { };
struct AutoPtr {
AutoPtr(AutoPtr &);
AutoPtr();
AutoPtr(AutoPtrRef);
operator AutoPtrRef();
};
AutoPtr test_auto_ptr() {
AutoPtr p;
return p;
}
```
Signed-off-by: Matheus Izvekov <mizvekov@gmail.com>
Reviewed By: Quuxplusone
Differential Revision: https://reviews.llvm.org/D105756
2021-07-10 08:34:17 +08:00
// Test conversion + copy construction. This is a pure C++98 test.
// However we may extend implicit moves into C++98, we must make sure the
// result here is not changed.
2009-11-14 02:44:21 +08:00
class AutoPtrRef { } ;
class AutoPtr {
2021-06-14 19:23:18 +08:00
AutoPtr ( AutoPtr & ) ; // cxx98-note {{declared private here}}
2021-04-03 09:10:12 +08:00
2009-11-14 02:44:21 +08:00
public :
AutoPtr ( ) ;
AutoPtr ( AutoPtrRef ) ;
2021-04-03 09:10:12 +08:00
2009-11-14 02:44:21 +08:00
operator AutoPtrRef ( ) ;
} ;
AutoPtr make_auto_ptr ( ) ;
AutoPtr test_auto_ptr ( bool Cond ) {
AutoPtr p1 ( make_auto_ptr ( ) ) ;
2021-04-03 09:10:12 +08:00
2009-11-14 02:44:21 +08:00
AutoPtr p ;
if ( Cond )
2021-06-14 19:23:18 +08:00
return p ; // cxx98-error {{calling a private constructor}}
2021-04-03 09:10:12 +08:00
2009-11-14 02:44:21 +08:00
return AutoPtr ( ) ;
}
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
struct A1 {
A1 ( const char * ) ;
~ A1 ( ) ;
2009-11-14 02:44:21 +08:00
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
private :
2021-06-14 19:23:18 +08:00
A1 ( const A1 & ) ; // cxx98_11-note 2 {{declared private here}}
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
} ;
A1 f ( ) {
2010-07-23 06:44:38 +08:00
// FIXME: redundant diagnostics!
2021-06-14 19:23:18 +08:00
return " Hello " ; // cxx98_11-error {{calling a private constructor}}
// cxx98-warning@-1 {{an accessible copy constructor}}
// cxx11-warning@-2 {{copying parameter of type 'A1' when binding a reference to a temporary would invoke an inaccessible constructor in C++98}}
Switch the initialization required by return statements over to the
new InitializationSequence. This fixes some bugs (e.g., PR5808),
changed some diagnostics, and caused more churn than expected. What's
new:
- InitializationSequence now has a "C conversion sequence" category
and step kind, which falls back to
- Changed the diagnostics for returns to always have the result type
of the function first and the type of the expression second.
CheckSingleAssignmentConstraints to peform checking in C.
- Improved ASTs for initialization of return values. The ASTs now
capture all of the temporaries we need to create, but
intentionally do not bind the tempoary that is actually returned,
so that it won't get destroyed twice.
- Make sure to perform an (elidable!) copy of the class object that
is returned from a class.
- Fix copy elision in CodeGen to properly see through the
subexpressions that occur with elidable copies.
- Give "new" its own entity kind; as with return values and thrown
objects, we don't bind the expression so we don't call a
destructor for it.
Note that, with this patch, I've broken returning move-only types in
C++0x. We'll fix it later, when we tackle NRVO.
llvm-svn: 91669
2009-12-18 13:02:21 +08:00
}
2010-04-13 07:19:01 +08:00
namespace source_locations {
template < typename T >
struct sneaky_int {
typedef int type ;
} ;
template < typename T , typename U >
struct A { } ;
template < typename T >
struct A < T , T > : A < T , int > { } ;
struct E {
template < typename T >
operator A < T , typename sneaky_int < T > : : type > & ( ) const ; // expected-note{{candidate function}}
} ;
void f ( ) {
A < float , float > & af = E ( ) ; // expected-error{{no viable conversion}}
A < float , int > & af2 = E ( ) ;
const A < float , int > & caf2 = E ( ) ;
}
2021-04-03 09:10:12 +08:00
// Check
2010-04-13 07:19:01 +08:00
template < typename T >
struct E2 {
operator T
* // expected-error{{pointer to a reference}}
( ) const ;
} ;
E2 < int & > e2i ; // expected-note{{in instantiation}}
}
2010-04-13 08:04:31 +08:00
namespace crazy_declarators {
struct A {
2014-12-19 10:07:47 +08:00
( & operator bool ( ) ) ( ) ; // expected-error {{use a typedef to declare a conversion to 'bool (&)()'}}
* operator int ( ) ; // expected-error {{put the complete type after 'operator'}}
// No suggestion of using a typedef here; that's not possible.
2016-04-15 07:47:07 +08:00
template < typename T > ( & operator T ( ) ) ( ) ;
# if __cplusplus <= 199711L
// expected-error-re@-2 {{cannot specify any part of a return type in the declaration of a conversion function{{$}}}}
# else
// expected-error-re@-4 {{cannot specify any part of a return type in the declaration of a conversion function; use an alias template to declare a conversion to 'T (&)()'{{$}}}}
# endif
2010-04-13 08:04:31 +08:00
} ;
}
2010-04-18 06:01:05 +08:00
namespace smart_ptr {
2021-04-03 09:10:12 +08:00
class Y {
2010-04-18 06:01:05 +08:00
class YRef { } ;
Y ( Y & ) ;
public :
Y ( ) ;
Y ( YRef ) ;
operator YRef ( ) ; // expected-note{{candidate function}}
} ;
struct X { // expected-note{{candidate constructor (the implicit copy constructor) not}}
2016-04-15 07:47:07 +08:00
# if __cplusplus >= 201103L
// expected-note@-2 {{candidate constructor (the implicit move constructor) not}}
# endif
2020-01-10 04:27:48 +08:00
explicit X ( Y ) ; // expected-note {{not a candidate}}
2010-04-18 06:01:05 +08:00
} ;
Y make_Y ( ) ;
X f ( ) {
X x = make_Y ( ) ; // expected-error{{no viable conversion from 'smart_ptr::Y' to 'smart_ptr::X'}}
X x2 ( make_Y ( ) ) ;
return X ( Y ( ) ) ;
}
}
2010-04-25 04:54:38 +08:00
struct Any {
Any ( . . . ) ;
} ;
struct Other {
2021-04-03 09:10:12 +08:00
Other ( const Other & ) ;
2010-04-25 04:54:38 +08:00
Other ( ) ;
} ;
void test_any ( ) {
2016-04-15 07:47:07 +08:00
Any any = Other ( ) ;
# if __cplusplus <= 199711L
// expected-error@-2 {{cannot pass object of non-POD type 'Other' through variadic constructor; call will abort at runtime}}
# else
// expected-error@-4 {{cannot pass object of non-trivial type 'Other' through variadic constructor; call will abort at runtime}}
# endif
2010-04-25 04:54:38 +08:00
}
2010-08-11 10:15:33 +08:00
namespace PR7055 {
// Make sure that we don't allow too many conversions in an
// auto_ptr-like template. In particular, we can't create multiple
// temporary objects when binding to a reference.
struct auto_ptr {
struct auto_ptr_ref { } ;
auto_ptr ( auto_ptr & ) ;
auto_ptr ( auto_ptr_ref ) ;
explicit auto_ptr ( int * ) ;
operator auto_ptr_ref ( ) ;
} ;
struct X {
X ( auto_ptr ) ;
} ;
X f ( ) {
X x ( auto_ptr ( new int ) ) ;
return X ( auto_ptr ( new int ) ) ;
}
auto_ptr foo ( ) ;
X e ( foo ( ) ) ;
struct Y {
Y ( X ) ;
} ;
2021-04-03 09:10:12 +08:00
2010-08-11 10:15:33 +08:00
Y f2 ( foo ( ) ) ;
}
2010-08-19 05:25:30 +08:00
namespace PR7934 {
typedef unsigned char uint8 ;
struct MutablePtr {
MutablePtr ( ) : ptr ( 0 ) { }
void * ptr ;
operator void * ( ) { return ptr ; }
private :
operator uint8 * ( ) { return reinterpret_cast < uint8 * > ( ptr ) ; }
operator const char * ( ) const { return reinterpret_cast < const char * > ( ptr ) ; }
} ;
void fake_memcpy ( const void * ) ;
void use ( ) {
MutablePtr ptr ;
fake_memcpy ( ptr ) ;
}
}
2010-08-19 23:57:50 +08:00
namespace rdar8018274 {
struct X { } ;
struct Y {
operator const struct X * ( ) const ;
} ;
struct Z : Y {
operator struct X * ( ) ;
} ;
void test ( ) {
Z x ;
( void ) ( x ! = __null ) ;
}
struct Base {
operator int ( ) ;
} ;
struct Derived1 : Base { } ;
struct Derived2 : Base { } ;
2021-04-03 09:10:12 +08:00
struct SuperDerived : Derived1 , Derived2 {
2010-08-19 23:57:50 +08:00
using Derived1 : : operator int ;
} ;
struct UeberDerived : SuperDerived {
operator long ( ) ;
} ;
void test2 ( UeberDerived ud ) {
2020-11-30 11:17:49 +08:00
int i = ud ; // expected-error{{ambiguous conversion from derived class 'rdar8018274::UeberDerived' to base class 'rdar8018274::Base'}}
2010-08-19 23:57:50 +08:00
}
2010-08-20 01:02:01 +08:00
struct Base2 {
operator int ( ) ;
} ;
struct Base3 {
operator int ( ) ;
} ;
2021-04-03 09:10:12 +08:00
struct Derived23 : Base2 , Base3 {
2010-08-20 01:02:01 +08:00
using Base2 : : operator int ;
} ;
struct ExtraDerived23 : Derived23 { } ;
void test3 ( ExtraDerived23 ed ) {
int i = ed ;
}
2010-08-19 23:57:50 +08:00
}
2010-09-12 15:22:28 +08:00
namespace PR8065 {
template < typename T > struct Iterator ;
template < typename T > struct Container ;
template < >
struct Iterator < int > {
typedef Container < int > container_type ;
} ;
template < typename T >
struct Container {
typedef typename Iterator < T > : : container_type X ;
operator X ( void ) { return X ( ) ; }
} ;
Container < int > test ;
}
2010-09-12 16:07:23 +08:00
namespace PR8034 {
struct C {
operator int ( ) ;
private :
template < typename T > operator T ( ) ;
} ;
int x = C ( ) . operator int ( ) ;
}
2011-03-06 17:03:20 +08:00
namespace PR9336 {
template < class T >
struct generic_list
{
template < class Container >
operator Container ( )
2021-04-03 09:10:12 +08:00
{
2011-03-06 17:03:20 +08:00
Container ar ;
T * i ;
ar [ 0 ] = * i ;
return ar ;
}
} ;
template < class T >
struct array
{
T & operator [ ] ( int ) ;
const T & operator [ ] ( int ) const ;
} ;
generic_list < generic_list < int > > l ;
array < array < int > > a = l ;
}
2011-07-14 06:53:21 +08:00
namespace PR8800 {
struct A ;
struct C {
operator A & ( ) ;
} ;
void f ( ) {
C c ;
A & a1 ( c ) ;
A & a2 = c ;
A & a3 = static_cast < A & > ( c ) ;
A & a4 = ( A & ) c ;
}
}
2012-05-06 08:04:32 +08:00
namespace PR12712 {
struct A { } ;
struct B {
operator A ( ) ;
operator A ( ) const ;
} ;
struct C : B { } ;
A f ( const C c ) { return c ; }
}
2014-01-08 08:56:48 +08:00
namespace PR18234 {
struct A {
2014-03-08 06:36:23 +08:00
operator enum E { e } ( ) ; // expected-error {{'PR18234::A::E' cannot be defined in a type specifier}}
operator struct S { int n ; } ( ) ; // expected-error {{'PR18234::A::S' cannot be defined in a type specifier}}
2016-10-27 16:37:14 +08:00
// expected-note@-1 {{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'struct A' to 'const PR18234::A::S &' for 1st argument}}
# if __cplusplus >= 201103L
// expected-note@-3 {{candidate constructor (the implicit move constructor) not viable: no known conversion from 'struct A' to 'PR18234::A::S &&' for 1st argument}}
# endif
2014-01-08 08:56:48 +08:00
} a ;
2016-10-27 16:37:14 +08:00
A : : S s = a ; // expected-error {{no viable conversion from 'struct A' to 'A::S'}}
2017-01-05 07:14:16 +08:00
A : : E e = a ;
2014-01-08 08:56:48 +08:00
bool k1 = e = = A : : e ; // expected-error {{no member named 'e'}}
bool k2 = e . n = = 0 ;
}
2018-04-13 00:41:55 +08:00
namespace PR30595 {
struct S {
const operator int ( ) ; // expected-error {{cannot specify any part of a return type in the declaration of a conversion function; put the complete type after 'operator'}}
const operator int ( ) const ; // expected-error {{cannot specify any part of a return type}}
volatile const operator int ( ) ; // expected-error {{cannot specify any part of a return type}}
operator const int ( ) const ;
} ;
}