fail_extend_impl_scope.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/impl/no_prelude/fail_extend_impl_scope.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/no_prelude/fail_extend_impl_scope.carbon
  10. // --- fail_extend_impl_file_scope.carbon
  11. library "[[@TEST_NAME]]";
  12. interface I {}
  13. // CHECK:STDERR: fail_extend_impl_file_scope.carbon:[[@LINE+4]]:1: error: `extend impl` can only be used in a class [ExtendImplOutsideClass]
  14. // CHECK:STDERR: extend impl () as I {}
  15. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~
  16. // CHECK:STDERR:
  17. extend impl () as I {}
  18. // --- fail_extend_impl_function_scope.carbon
  19. library "[[@TEST_NAME]]";
  20. interface J {}
  21. fn F() {
  22. // CHECK:STDERR: fail_extend_impl_function_scope.carbon:[[@LINE+4]]:3: error: `extend impl` can only be used in a class [ExtendImplOutsideClass]
  23. // CHECK:STDERR: extend impl {} as J {}
  24. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~
  25. // CHECK:STDERR:
  26. extend impl {} as J {}
  27. }
  28. // --- fail_extend_impl_self_interface.carbon
  29. library "[[@TEST_NAME]]";
  30. interface Z {
  31. fn Zero();
  32. // CHECK:STDERR: fail_extend_impl_self_interface.carbon:[[@LINE+4]]:15: error: `impl as` can only be used in a class [ImplAsOutsideClass]
  33. // CHECK:STDERR: extend impl as Z {
  34. // CHECK:STDERR: ^~
  35. // CHECK:STDERR:
  36. extend impl as Z {
  37. fn Zero() {}
  38. }
  39. }
  40. class Point {
  41. extend impl as Z {
  42. fn Zero() {}
  43. }
  44. }
  45. fn F() {
  46. // Even if the `extend impl` is diagnosed above, we must not add the impl of
  47. // the interface to itself in a way that allows it to be used during impl
  48. // lookup, or we end up with infinite impl lookup recursion here.
  49. ({} as Point).Zero();
  50. }
  51. // CHECK:STDOUT: --- fail_extend_impl_file_scope.carbon
  52. // CHECK:STDOUT:
  53. // CHECK:STDOUT: constants {
  54. // CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete]
  55. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic]
  56. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  57. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness () [concrete]
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT:
  60. // CHECK:STDOUT: file {
  61. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  62. // CHECK:STDOUT: .I = %I.decl
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %I.decl: type = interface_decl @I [concrete = constants.%I.type] {} {}
  65. // CHECK:STDOUT: impl_decl @impl [concrete] {} {
  66. // CHECK:STDOUT: %.loc9_14.1: %empty_tuple.type = tuple_literal ()
  67. // CHECK:STDOUT: %.loc9_14.2: type = converted %.loc9_14.1, constants.%empty_tuple.type [concrete = constants.%empty_tuple.type]
  68. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness () [concrete = constants.%impl_witness]
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: interface @I {
  74. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self]
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: !members:
  77. // CHECK:STDOUT: .Self = %Self
  78. // CHECK:STDOUT: witness = ()
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: impl @impl: %.loc9_14.2 as %I.ref {
  82. // CHECK:STDOUT: !members:
  83. // CHECK:STDOUT: witness = <error>
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT:
  86. // CHECK:STDOUT: --- fail_extend_impl_function_scope.carbon
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: constants {
  89. // CHECK:STDOUT: %J.type: type = facet_type <@J> [concrete]
  90. // CHECK:STDOUT: %Self: %J.type = bind_symbolic_name Self, 0 [symbolic]
  91. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  92. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  93. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  94. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness () [concrete]
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: file {
  98. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  99. // CHECK:STDOUT: .J = %J.decl
  100. // CHECK:STDOUT: .F = %F.decl
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT: %J.decl: type = interface_decl @J [concrete = constants.%J.type] {} {}
  103. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {} {}
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: interface @J {
  107. // CHECK:STDOUT: %Self: %J.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self]
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: !members:
  110. // CHECK:STDOUT: .Self = %Self
  111. // CHECK:STDOUT: witness = ()
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: impl @impl: %.loc10_16.2 as %J.ref {
  115. // CHECK:STDOUT: !members:
  116. // CHECK:STDOUT: witness = <error>
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: fn @F() {
  120. // CHECK:STDOUT: !entry:
  121. // CHECK:STDOUT: impl_decl @impl [concrete] {} {
  122. // CHECK:STDOUT: %.loc10_16.1: %empty_struct_type = struct_literal ()
  123. // CHECK:STDOUT: %.loc10_16.2: type = converted %.loc10_16.1, constants.%empty_struct_type [concrete = constants.%empty_struct_type]
  124. // CHECK:STDOUT: %J.ref: type = name_ref J, file.%J.decl [concrete = constants.%J.type]
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness () [concrete = constants.%impl_witness]
  127. // CHECK:STDOUT: return
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: --- fail_extend_impl_self_interface.carbon
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: constants {
  133. // CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete]
  134. // CHECK:STDOUT: %Self: %Z.type = bind_symbolic_name Self, 0 [symbolic]
  135. // CHECK:STDOUT: %Zero.type.822: type = fn_type @Zero.1 [concrete]
  136. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  137. // CHECK:STDOUT: %Zero.d14: %Zero.type.822 = struct_value () [concrete]
  138. // CHECK:STDOUT: %Z.assoc_type: type = assoc_entity_type @Z [concrete]
  139. // CHECK:STDOUT: %assoc0: %Z.assoc_type = assoc_entity element0, @Z.%Zero.decl [concrete]
  140. // CHECK:STDOUT: %Zero.type.db4: type = fn_type @Zero.2, @impl.6b5(%Self) [symbolic]
  141. // CHECK:STDOUT: %Zero.8fb: %Zero.type.db4 = struct_value () [symbolic]
  142. // CHECK:STDOUT: %Point: type = class_type @Point [concrete]
  143. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness (@impl.c94.%Zero.decl) [concrete]
  144. // CHECK:STDOUT: %Zero.type.e33: type = fn_type @Zero.3 [concrete]
  145. // CHECK:STDOUT: %Zero.dec: %Zero.type.e33 = struct_value () [concrete]
  146. // CHECK:STDOUT: %Z.facet: %Z.type = facet_value %Point, (%impl_witness) [concrete]
  147. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  148. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  149. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  150. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  151. // CHECK:STDOUT: %Point.val: %Point = struct_value () [concrete]
  152. // CHECK:STDOUT: %.728: type = fn_type_with_self_type %Zero.type.822, %Z.facet [concrete]
  153. // CHECK:STDOUT: }
  154. // CHECK:STDOUT:
  155. // CHECK:STDOUT: file {
  156. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  157. // CHECK:STDOUT: .Z = %Z.decl
  158. // CHECK:STDOUT: .Point = %Point.decl
  159. // CHECK:STDOUT: .F = %F.decl
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT: %Z.decl: type = interface_decl @Z [concrete = constants.%Z.type] {} {}
  162. // CHECK:STDOUT: %Point.decl: type = class_decl @Point [concrete = constants.%Point] {} {}
  163. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {} {}
  164. // CHECK:STDOUT: }
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: interface @Z {
  167. // CHECK:STDOUT: %Self: %Z.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self]
  168. // CHECK:STDOUT: %Zero.decl: %Zero.type.822 = fn_decl @Zero.1 [concrete = constants.%Zero.d14] {} {}
  169. // CHECK:STDOUT: %assoc0: %Z.assoc_type = assoc_entity element0, %Zero.decl [concrete = constants.%assoc0]
  170. // CHECK:STDOUT: impl_decl @impl.6b5 [concrete] {} {
  171. // CHECK:STDOUT: %Self.ref: type = name_ref Self, <error> [concrete = <error>]
  172. // CHECK:STDOUT: %Z.ref: type = name_ref Z, file.%Z.decl [concrete = constants.%Z.type]
  173. // CHECK:STDOUT: }
  174. // CHECK:STDOUT:
  175. // CHECK:STDOUT: !members:
  176. // CHECK:STDOUT: .Self = %Self
  177. // CHECK:STDOUT: .Zero = %assoc0
  178. // CHECK:STDOUT: .Z = <poisoned>
  179. // CHECK:STDOUT: witness = (%Zero.decl)
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT:
  182. // CHECK:STDOUT: generic impl @impl.6b5(@Z.%Self: %Z.type) {
  183. // CHECK:STDOUT: !definition:
  184. // CHECK:STDOUT: %Self: %Z.type = bind_symbolic_name Self, 0 [symbolic = %Self (constants.%Self)]
  185. // CHECK:STDOUT: %Zero.type: type = fn_type @Zero.2, @impl.6b5(%Self) [symbolic = %Zero.type (constants.%Zero.type.db4)]
  186. // CHECK:STDOUT: %Zero: @impl.6b5.%Zero.type (%Zero.type.db4) = struct_value () [symbolic = %Zero (constants.%Zero.8fb)]
  187. // CHECK:STDOUT:
  188. // CHECK:STDOUT: impl: %Self.ref as %Z.ref {
  189. // CHECK:STDOUT: %Zero.decl: @impl.6b5.%Zero.type (%Zero.type.db4) = fn_decl @Zero.2 [symbolic = @impl.6b5.%Zero (constants.%Zero.8fb)] {} {}
  190. // CHECK:STDOUT:
  191. // CHECK:STDOUT: !members:
  192. // CHECK:STDOUT: .Zero = %Zero.decl
  193. // CHECK:STDOUT: witness = <error>
  194. // CHECK:STDOUT: }
  195. // CHECK:STDOUT: }
  196. // CHECK:STDOUT:
  197. // CHECK:STDOUT: impl @impl.c94: %Self.ref as %Z.ref {
  198. // CHECK:STDOUT: %Zero.decl: %Zero.type.e33 = fn_decl @Zero.3 [concrete = constants.%Zero.dec] {} {}
  199. // CHECK:STDOUT:
  200. // CHECK:STDOUT: !members:
  201. // CHECK:STDOUT: .Zero = %Zero.decl
  202. // CHECK:STDOUT: witness = @Point.%impl_witness
  203. // CHECK:STDOUT: }
  204. // CHECK:STDOUT:
  205. // CHECK:STDOUT: class @Point {
  206. // CHECK:STDOUT: impl_decl @impl.c94 [concrete] {} {
  207. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Point [concrete = constants.%Point]
  208. // CHECK:STDOUT: %Z.ref: type = name_ref Z, file.%Z.decl [concrete = constants.%Z.type]
  209. // CHECK:STDOUT: }
  210. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness (@impl.c94.%Zero.decl) [concrete = constants.%impl_witness]
  211. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete = constants.%complete_type]
  212. // CHECK:STDOUT: complete_type_witness = %complete_type
  213. // CHECK:STDOUT:
  214. // CHECK:STDOUT: !members:
  215. // CHECK:STDOUT: .Self = constants.%Point
  216. // CHECK:STDOUT: .Z = <poisoned>
  217. // CHECK:STDOUT: .Zero = <poisoned>
  218. // CHECK:STDOUT: extend @impl.c94.%Z.ref
  219. // CHECK:STDOUT: }
  220. // CHECK:STDOUT:
  221. // CHECK:STDOUT: generic fn @Zero.1(@Z.%Self: %Z.type) {
  222. // CHECK:STDOUT: fn();
  223. // CHECK:STDOUT: }
  224. // CHECK:STDOUT:
  225. // CHECK:STDOUT: generic fn @Zero.2(@Z.%Self: %Z.type) {
  226. // CHECK:STDOUT: !definition:
  227. // CHECK:STDOUT:
  228. // CHECK:STDOUT: fn() {
  229. // CHECK:STDOUT: !entry:
  230. // CHECK:STDOUT: return
  231. // CHECK:STDOUT: }
  232. // CHECK:STDOUT: }
  233. // CHECK:STDOUT:
  234. // CHECK:STDOUT: fn @Zero.3() {
  235. // CHECK:STDOUT: !entry:
  236. // CHECK:STDOUT: return
  237. // CHECK:STDOUT: }
  238. // CHECK:STDOUT:
  239. // CHECK:STDOUT: fn @F() {
  240. // CHECK:STDOUT: !entry:
  241. // CHECK:STDOUT: %.loc25_5.1: %empty_struct_type = struct_literal ()
  242. // CHECK:STDOUT: %Point.ref: type = name_ref Point, file.%Point.decl [concrete = constants.%Point]
  243. // CHECK:STDOUT: %.loc25_5.2: ref %Point = temporary_storage
  244. // CHECK:STDOUT: %.loc25_5.3: init %Point = class_init (), %.loc25_5.2 [concrete = constants.%Point.val]
  245. // CHECK:STDOUT: %.loc25_5.4: ref %Point = temporary %.loc25_5.2, %.loc25_5.3
  246. // CHECK:STDOUT: %.loc25_7: ref %Point = converted %.loc25_5.1, %.loc25_5.4
  247. // CHECK:STDOUT: %Zero.ref: %Z.assoc_type = name_ref Zero, @Z.%assoc0 [concrete = constants.%assoc0]
  248. // CHECK:STDOUT: %impl.elem0: %.728 = impl_witness_access constants.%impl_witness, element0 [concrete = constants.%Zero.dec]
  249. // CHECK:STDOUT: %Zero.call: init %empty_tuple.type = call %impl.elem0()
  250. // CHECK:STDOUT: return
  251. // CHECK:STDOUT: }
  252. // CHECK:STDOUT:
  253. // CHECK:STDOUT: specific @Zero.1(constants.%Self) {}
  254. // CHECK:STDOUT:
  255. // CHECK:STDOUT: specific @impl.6b5(constants.%Self) {
  256. // CHECK:STDOUT: !definition:
  257. // CHECK:STDOUT: %Self => constants.%Self
  258. // CHECK:STDOUT: %Zero.type => constants.%Zero.type.db4
  259. // CHECK:STDOUT: %Zero => constants.%Zero.8fb
  260. // CHECK:STDOUT: }
  261. // CHECK:STDOUT:
  262. // CHECK:STDOUT: specific @Zero.2(constants.%Self) {}
  263. // CHECK:STDOUT:
  264. // CHECK:STDOUT: specific @impl.6b5(%Self) {}
  265. // CHECK:STDOUT:
  266. // CHECK:STDOUT: specific @Zero.1(constants.%Z.facet) {}
  267. // CHECK:STDOUT: