destroy_decl.carbon 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/destroy.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/destroy_decl.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/destroy_decl.carbon
  12. // --- self.carbon
  13. library "[[@TEST_NAME]]";
  14. class C {
  15. fn destroy[self: Self]();
  16. }
  17. // --- declare_impl.carbon
  18. library "[[@TEST_NAME]]";
  19. class C {
  20. impl as Core.Destroy;
  21. }
  22. // --- fail_define_impl.carbon
  23. library "[[@TEST_NAME]]";
  24. // CHECK:STDERR: fail_define_impl.carbon:[[@LINE+3]]:1: error: redefinition of `impl Self as Core.Destroy` [ImplRedefinition]
  25. // CHECK:STDERR: class C {
  26. // CHECK:STDERR: ^~~~~~~~~
  27. class C {
  28. // CHECK:STDERR: fail_define_impl.carbon:[[@LINE+4]]:3: note: previous definition was here [ImplPreviousDefinition]
  29. // CHECK:STDERR: impl as Core.Destroy {
  30. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
  31. // CHECK:STDERR:
  32. impl as Core.Destroy {
  33. fn Op[addr self: Self*]() {}
  34. }
  35. }
  36. // --- addr_self.carbon
  37. library "[[@TEST_NAME]]";
  38. class C {
  39. fn destroy[addr self: Self*]();
  40. }
  41. // --- explicit_return.carbon
  42. library "[[@TEST_NAME]]";
  43. class C {
  44. fn destroy[self: Self]() -> ();
  45. }
  46. // --- fail_class_function.carbon
  47. library "[[@TEST_NAME]]";
  48. class C {
  49. // CHECK:STDERR: fail_class_function.carbon:[[@LINE+4]]:3: error: missing implicit `self` parameter [DestroyFunctionMissingSelf]
  50. // CHECK:STDERR: fn destroy();
  51. // CHECK:STDERR: ^~~~~~~~~~~~~
  52. // CHECK:STDERR:
  53. fn destroy();
  54. }
  55. // --- fail_extra_implicit_params_second.carbon
  56. library "[[@TEST_NAME]]";
  57. class C {
  58. // CHECK:STDERR: fail_extra_implicit_params_second.carbon:[[@LINE+4]]:26: error: unexpected implicit parameter [DestroyFunctionUnexpectedImplicitParam]
  59. // CHECK:STDERR: fn destroy[self: Self, T:! type]();
  60. // CHECK:STDERR: ^
  61. // CHECK:STDERR:
  62. fn destroy[self: Self, T:! type]();
  63. }
  64. // --- fail_extra_implicit_params_first.carbon
  65. library "[[@TEST_NAME]]";
  66. class C {
  67. // CHECK:STDERR: fail_extra_implicit_params_first.carbon:[[@LINE+4]]:14: error: unexpected implicit parameter [DestroyFunctionUnexpectedImplicitParam]
  68. // CHECK:STDERR: fn destroy[T:! type, self: Self]();
  69. // CHECK:STDERR: ^
  70. // CHECK:STDERR:
  71. fn destroy[T:! type, self: Self]();
  72. }
  73. // --- fail_positional_params.carbon
  74. library "[[@TEST_NAME]]";
  75. class C {
  76. // CHECK:STDERR: fail_positional_params.carbon:[[@LINE+4]]:3: error: missing empty explicit parameter list [DestroyFunctionPositionalParams]
  77. // CHECK:STDERR: fn destroy[self: Self];
  78. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
  79. // CHECK:STDERR:
  80. fn destroy[self: Self];
  81. }
  82. // --- fail_explicit_params.carbon
  83. library "[[@TEST_NAME]]";
  84. class C {
  85. // CHECK:STDERR: fail_explicit_params.carbon:[[@LINE+4]]:26: error: unexpected parameter [DestroyFunctionNonEmptyExplicitParams]
  86. // CHECK:STDERR: fn destroy[self: Self](x: ());
  87. // CHECK:STDERR: ^~~~~
  88. // CHECK:STDERR:
  89. fn destroy[self: Self](x: ());
  90. }
  91. // --- fail_return_type.carbon
  92. library "[[@TEST_NAME]]";
  93. class C {
  94. // CHECK:STDERR: fail_return_type.carbon:[[@LINE+4]]:28: error: incorrect return type; must be unspecified or `()` [DestroyFunctionIncorrectReturnType]
  95. // CHECK:STDERR: fn destroy[self: Self]() -> {};
  96. // CHECK:STDERR: ^~~~~
  97. // CHECK:STDERR:
  98. fn destroy[self: Self]() -> {};
  99. }
  100. // --- fail_out_of_line_missing_params.carbon
  101. library "[[@TEST_NAME]]";
  102. class C {
  103. fn destroy[self: Self]();
  104. }
  105. // CHECK:STDERR: fail_out_of_line_missing_params.carbon:[[@LINE+7]]:1: error: redeclaration differs because of missing implicit parameter list [RedeclParamListDiffers]
  106. // CHECK:STDERR: fn C.destroy {}
  107. // CHECK:STDERR: ^~~~~~~~~~~~~~
  108. // CHECK:STDERR: fail_out_of_line_missing_params.carbon:[[@LINE-6]]:3: note: previously declared with implicit parameter list [RedeclParamListPrevious]
  109. // CHECK:STDERR: fn destroy[self: Self]();
  110. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  111. // CHECK:STDERR:
  112. fn C.destroy {}
  113. // --- fail_destroy_in_file_scope.carbon
  114. library "[[@TEST_NAME]]";
  115. // CHECK:STDERR: fail_destroy_in_file_scope.carbon:[[@LINE+4]]:1: error: declaring `fn destroy` in non-class scope [DestroyFunctionOutsideClass]
  116. // CHECK:STDERR: fn destroy[self: ()]();
  117. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
  118. // CHECK:STDERR:
  119. fn destroy[self: ()]();
  120. // --- fail_destroy_in_namespace_scope.carbon
  121. library "[[@TEST_NAME]]";
  122. namespace NS;
  123. // CHECK:STDERR: fail_destroy_in_namespace_scope.carbon:[[@LINE+4]]:1: error: declaring `fn destroy` in non-class scope [DestroyFunctionOutsideClass]
  124. // CHECK:STDERR: fn NS.destroy();
  125. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  126. // CHECK:STDERR:
  127. fn NS.destroy();
  128. // --- fail_invalid_qualifier_with_params.carbon
  129. library "[[@TEST_NAME]]";
  130. class C {
  131. fn destroy[self: Self]();
  132. }
  133. // CHECK:STDERR: fail_invalid_qualifier_with_params.carbon:[[@LINE+7]]:6: error: name qualifiers are only allowed for entities that provide a scope [QualifiedNameInNonScope]
  134. // CHECK:STDERR: fn C.destroy[self: Self]().Foo() {}
  135. // CHECK:STDERR: ^~~~~~~
  136. // CHECK:STDERR: fail_invalid_qualifier_with_params.carbon:[[@LINE-6]]:3: note: referenced non-scope entity declared here [QualifiedNameNonScopeEntity]
  137. // CHECK:STDERR: fn destroy[self: Self]();
  138. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  139. // CHECK:STDERR:
  140. fn C.destroy[self: Self]().Foo() {}
  141. // --- fail_invalid_qualifier_no_params.carbon
  142. library "[[@TEST_NAME]]";
  143. class C {
  144. fn destroy[self: Self]();
  145. }
  146. // CHECK:STDERR: fail_invalid_qualifier_no_params.carbon:[[@LINE+7]]:6: error: name qualifiers are only allowed for entities that provide a scope [QualifiedNameInNonScope]
  147. // CHECK:STDERR: fn C.destroy.Foo() {}
  148. // CHECK:STDERR: ^~~~~~~
  149. // CHECK:STDERR: fail_invalid_qualifier_no_params.carbon:[[@LINE-6]]:3: note: referenced non-scope entity declared here [QualifiedNameNonScopeEntity]
  150. // CHECK:STDERR: fn destroy[self: Self]();
  151. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  152. // CHECK:STDERR:
  153. fn C.destroy.Foo() {}
  154. // --- fail_return_type_after_no_params.carbon
  155. library "[[@TEST_NAME]]";
  156. // Return type handling special-cases the "no params" case.
  157. class C {
  158. // CHECK:STDERR: fail_return_type_after_no_params.carbon:[[@LINE+4]]:3: error: missing implicit `self` parameter [DestroyFunctionMissingSelf]
  159. // CHECK:STDERR: fn destroy -> ();
  160. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~
  161. // CHECK:STDERR:
  162. fn destroy -> ();
  163. }