implicit-member-target.cu 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // RUN: %clang_cc1 -std=gnu++11 -fsyntax-only -verify %s -Wno-defaulted-function-deleted
  2. #include "Inputs/cuda.h"
  3. //------------------------------------------------------------------------------
  4. // Test 1: infer default ctor to be host.
  5. struct A1_with_host_ctor {
  6. A1_with_host_ctor() {}
  7. };
  8. // The implicit default constructor is inferred to be host because it only needs
  9. // to invoke a single host constructor (A1_with_host_ctor's). So we'll encounter
  10. // an error when calling it from a __device__ function, but not from a __host__
  11. // function.
  12. struct B1_with_implicit_default_ctor : A1_with_host_ctor {
  13. };
  14. // expected-note@-3 {{call to __host__ function from __device__}}
  15. // expected-note@-4 {{candidate constructor (the implicit copy constructor) not viable}}
  16. // expected-note@-5 {{candidate constructor (the implicit move constructor) not viable}}
  17. void hostfoo() {
  18. B1_with_implicit_default_ctor b;
  19. }
  20. __device__ void devicefoo() {
  21. B1_with_implicit_default_ctor b; // expected-error {{no matching constructor}}
  22. }
  23. //------------------------------------------------------------------------------
  24. // Test 2: infer default ctor to be device.
  25. struct A2_with_device_ctor {
  26. __device__ A2_with_device_ctor() {}
  27. };
  28. struct B2_with_implicit_default_ctor : A2_with_device_ctor {
  29. };
  30. // expected-note@-3 {{call to __device__ function from __host__}}
  31. // expected-note@-4 {{candidate constructor (the implicit copy constructor) not viable}}
  32. // expected-note@-5 {{candidate constructor (the implicit move constructor) not viable}}
  33. void hostfoo2() {
  34. B2_with_implicit_default_ctor b; // expected-error {{no matching constructor}}
  35. }
  36. __device__ void devicefoo2() {
  37. B2_with_implicit_default_ctor b;
  38. }
  39. //------------------------------------------------------------------------------
  40. // Test 3: infer copy ctor
  41. struct A3_with_device_ctors {
  42. __host__ A3_with_device_ctors() {}
  43. __device__ A3_with_device_ctors(const A3_with_device_ctors&) {}
  44. };
  45. struct B3_with_implicit_ctors : A3_with_device_ctors {
  46. };
  47. // expected-note@-2 2{{call to __device__ function from __host__ function}}
  48. // expected-note@-3 {{default constructor}}
  49. void hostfoo3() {
  50. B3_with_implicit_ctors b; // this is OK because the inferred default ctor
  51. // here is __host__
  52. B3_with_implicit_ctors b2 = b; // expected-error {{no matching constructor}}
  53. }
  54. //------------------------------------------------------------------------------
  55. // Test 4: infer default ctor from a field, not a base
  56. struct A4_with_host_ctor {
  57. A4_with_host_ctor() {}
  58. };
  59. struct B4_with_implicit_default_ctor {
  60. A4_with_host_ctor field;
  61. };
  62. // expected-note@-4 {{call to __host__ function from __device__}}
  63. // expected-note@-5 {{candidate constructor (the implicit copy constructor) not viable}}
  64. // expected-note@-6 {{candidate constructor (the implicit move constructor) not viable}}
  65. void hostfoo4() {
  66. B4_with_implicit_default_ctor b;
  67. }
  68. __device__ void devicefoo4() {
  69. B4_with_implicit_default_ctor b; // expected-error {{no matching constructor}}
  70. }
  71. //------------------------------------------------------------------------------
  72. // Test 5: copy ctor with non-const param
  73. struct A5_copy_ctor_constness {
  74. __host__ A5_copy_ctor_constness() {}
  75. __host__ A5_copy_ctor_constness(A5_copy_ctor_constness&) {}
  76. };
  77. struct B5_copy_ctor_constness : A5_copy_ctor_constness {
  78. };
  79. // expected-note@-3 {{candidate constructor (the implicit copy constructor) not viable: call to __host__ function from __device__ function}}
  80. // expected-note@-4 {{candidate constructor (the implicit default constructor) not viable}}
  81. void hostfoo5(B5_copy_ctor_constness& b_arg) {
  82. B5_copy_ctor_constness b = b_arg;
  83. }
  84. __device__ void devicefoo5(B5_copy_ctor_constness& b_arg) {
  85. B5_copy_ctor_constness b = b_arg; // expected-error {{no matching constructor}}
  86. }
  87. //------------------------------------------------------------------------------
  88. // Test 6: explicitly defaulted ctor: since they are spelled out, they have
  89. // a host/device designation explicitly so no inference needs to be done.
  90. struct A6_with_device_ctor {
  91. __device__ A6_with_device_ctor() {}
  92. };
  93. struct B6_with_defaulted_ctor : A6_with_device_ctor {
  94. __host__ B6_with_defaulted_ctor() = default;
  95. };
  96. // expected-note@-3 {{candidate constructor not viable: call to __host__ function from __device__ function}}
  97. // expected-note@-5 {{candidate constructor (the implicit copy constructor) not viable}}
  98. // expected-note@-6 {{candidate constructor (the implicit move constructor) not viable}}
  99. __device__ void devicefoo6() {
  100. B6_with_defaulted_ctor b; // expected-error {{no matching constructor}}
  101. }
  102. //------------------------------------------------------------------------------
  103. // Test 7: copy assignment operator
  104. struct A7_with_copy_assign {
  105. A7_with_copy_assign() {}
  106. __device__ A7_with_copy_assign& operator=(const A7_with_copy_assign&) {}
  107. };
  108. struct B7_with_copy_assign : A7_with_copy_assign {
  109. };
  110. // expected-note@-3 {{candidate function (the implicit copy assignment operator) not viable: call to __device__ function from __host__ function}}
  111. // expected-note@-4 {{candidate function (the implicit move assignment operator) not viable: call to __device__ function from __host__ function}}
  112. void hostfoo7() {
  113. B7_with_copy_assign b1, b2;
  114. b1 = b2; // expected-error {{no viable overloaded '='}}
  115. }
  116. //------------------------------------------------------------------------------
  117. // Test 8: move assignment operator
  118. // definitions for std::move
  119. namespace std {
  120. inline namespace foo {
  121. template <class T> struct remove_reference { typedef T type; };
  122. template <class T> struct remove_reference<T&> { typedef T type; };
  123. template <class T> struct remove_reference<T&&> { typedef T type; };
  124. template <class T> typename remove_reference<T>::type&& move(T&& t);
  125. }
  126. }
  127. struct A8_with_move_assign {
  128. A8_with_move_assign() {}
  129. __device__ A8_with_move_assign& operator=(A8_with_move_assign&&) {}
  130. __device__ A8_with_move_assign& operator=(const A8_with_move_assign&) {}
  131. };
  132. struct B8_with_move_assign : A8_with_move_assign {
  133. };
  134. // expected-note@-3 {{candidate function (the implicit copy assignment operator) not viable: call to __device__ function from __host__ function}}
  135. // expected-note@-4 {{candidate function (the implicit move assignment operator) not viable: call to __device__ function from __host__ function}}
  136. void hostfoo8() {
  137. B8_with_move_assign b1, b2;
  138. b1 = std::move(b2); // expected-error {{no viable overloaded '='}}
  139. }