fail_extend_impl_scope.carbon 14 KB

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