157d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-show-option -verify %s
2523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
3523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor// C++03 requires that we check for a copy constructor when binding a
4523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor// reference to a temporary, since we are allowed to make a copy, Even
5523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor// though we don't actually make that copy, make sure that we diagnose
657d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin// cases where that copy constructor is somehow unavailable.  As an
757d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin// extension, this is only a warning.
8523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
9523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregorstruct X1 {
10523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  X1();
11523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  explicit X1(const X1&);
12523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor};
13523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
14523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregorstruct X2 {
15523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  X2();
16523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
17523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregorprivate:
18523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  X2(const X2&); // expected-note{{declared private here}}
19523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor};
20523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
21523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregorstruct X3 {
22523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  X3();
23523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
24523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregorprivate:
250d3317e0a2e0adb57ce8d075ebdcb41a3f939805Kaelyn Uhrain  X3(X3&); // expected-note{{candidate constructor not viable: expects an l-value for 1st argument}}
26523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor};
27523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
282559a7045a74679c80376305397a5953d038e1d0Douglas Gregor// Check for instantiation of default arguments
29523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregortemplate<typename T>
30523d46af407f32fc53861e6f068e8076d4fe84a8Douglas GregorT get_value_badly() {
31523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  double *dp = 0;
3257d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin  // The extension doesn't extend far enough to turn this error into a warning.
332fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieu  T *tp = dp; // expected-error{{cannot initialize a variable of type 'int *' with an lvalue of type 'double *'}}
34523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  return T();
35523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor}
36523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
37523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregortemplate<typename T>
38523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregorstruct X4 {
39523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  X4();
402559a7045a74679c80376305397a5953d038e1d0Douglas Gregor  X4(const X4&, T = get_value_badly<T>()); // expected-note{{in instantiation of}}
412559a7045a74679c80376305397a5953d038e1d0Douglas Gregor};
422559a7045a74679c80376305397a5953d038e1d0Douglas Gregor
432559a7045a74679c80376305397a5953d038e1d0Douglas Gregor// Check for "dangerous" default arguments that could cause recursion.
442559a7045a74679c80376305397a5953d038e1d0Douglas Gregorstruct X5 {
452559a7045a74679c80376305397a5953d038e1d0Douglas Gregor  X5();
4657d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin  X5(const X5&, const X5& = X5()); // expected-warning{{no viable constructor copying parameter of type 'X5'}}
47523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor};
48523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
49523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregorvoid g1(const X1&);
50523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregorvoid g2(const X2&);
51523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregorvoid g3(const X3&);
52523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregorvoid g4(const X4<int>&);
532559a7045a74679c80376305397a5953d038e1d0Douglas Gregorvoid g5(const X5&);
54523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
55523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregorvoid test() {
568ff338bfd8abd9ac5d0c1d89c1b05e2c02727174Douglas Gregor  g1(X1());
572fe9b7fb07dff15dd15dd8755a9a9e6de0fe46fcRichard Trieu  g2(X2()); // expected-warning{{C++98 requires an accessible copy constructor for class 'X2' when binding a reference to a temporary; was private}}
5857d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin  g3(X3()); // expected-warning{{no viable constructor copying parameter of type 'X3'}}
59523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  g4(X4<int>());
6057d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin  g5(X5());  // Generates a warning in the default argument.
61523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor}
622559a7045a74679c80376305397a5953d038e1d0Douglas Gregor
6357d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin// Check that unavailable copy constructors still cause SFINAE failures.
6457d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskintemplate<int> struct int_c { };
6557d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin
6657d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskintemplate<typename T> T f(const T&);
6757d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin
6857d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin// Would be ambiguous with the next g(), except the instantiation failure in
6957d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin// sizeof() prevents that.
7057d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskintemplate<typename T>
7157d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskinint &g(int_c<sizeof(f(T()))> * = 0);
7257d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin
7357d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskintemplate<typename T> float &g();
7457d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin
7557d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskinvoid h() {
7657d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin  float &fp2 = g<X3>();  // Not ambiguous.
7757d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin}
78