import_self.carbon 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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/import_self.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/import_self.carbon
  10. // --- a.carbon
  11. library "[[@TEST_NAME]]";
  12. interface Add {
  13. fn Op[self: Self](other: Self) -> Self;
  14. }
  15. // --- b.carbon
  16. library "[[@TEST_NAME]]";
  17. import library "a";
  18. impl () as Add {
  19. fn Op[self: Self](other: Self) -> Self { return (); }
  20. }
  21. fn F(x: (), y: ()) -> () {
  22. return x.(Add.Op)(y);
  23. }
  24. // CHECK:STDOUT: --- a.carbon
  25. // CHECK:STDOUT:
  26. // CHECK:STDOUT: constants {
  27. // CHECK:STDOUT: %.1: type = interface_type @Add [template]
  28. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic]
  29. // CHECK:STDOUT: %Op.type: type = fn_type @Op [template]
  30. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  31. // CHECK:STDOUT: %Op: %Op.type = struct_value () [template]
  32. // CHECK:STDOUT: %.3: type = assoc_entity_type %.1, %Op.type [template]
  33. // CHECK:STDOUT: %.4: %.3 = assoc_entity element0, @Add.%Op.decl [template]
  34. // CHECK:STDOUT: }
  35. // CHECK:STDOUT:
  36. // CHECK:STDOUT: file {
  37. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  38. // CHECK:STDOUT: .Add = %Add.decl
  39. // CHECK:STDOUT: }
  40. // CHECK:STDOUT: %Add.decl: type = interface_decl @Add [template = constants.%.1] {} {}
  41. // CHECK:STDOUT: }
  42. // CHECK:STDOUT:
  43. // CHECK:STDOUT: interface @Add {
  44. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic = constants.%Self]
  45. // CHECK:STDOUT: %Op.decl: %Op.type = fn_decl @Op [template = constants.%Op] {
  46. // CHECK:STDOUT: %self.patt: @Op.%Self (%Self) = binding_pattern self
  47. // CHECK:STDOUT: %other.patt: @Op.%Self (%Self) = binding_pattern other
  48. // CHECK:STDOUT: } {
  49. // CHECK:STDOUT: %Self.ref.loc5_15: %.1 = name_ref Self, @Add.%Self [symbolic = %Self (constants.%Self)]
  50. // CHECK:STDOUT: %.loc5_15.1: type = facet_type_access %Self.ref.loc5_15 [symbolic = %Self (constants.%Self)]
  51. // CHECK:STDOUT: %.loc5_15.2: type = converted %Self.ref.loc5_15, %.loc5_15.1 [symbolic = %Self (constants.%Self)]
  52. // CHECK:STDOUT: %self.param: @Op.%Self (%Self) = param self, runtime_param0
  53. // CHECK:STDOUT: %self: @Op.%Self (%Self) = bind_name self, %self.param
  54. // CHECK:STDOUT: %Self.ref.loc5_28: %.1 = name_ref Self, @Add.%Self [symbolic = %Self (constants.%Self)]
  55. // CHECK:STDOUT: %.loc5_28.1: type = facet_type_access %Self.ref.loc5_28 [symbolic = %Self (constants.%Self)]
  56. // CHECK:STDOUT: %.loc5_28.2: type = converted %Self.ref.loc5_28, %.loc5_28.1 [symbolic = %Self (constants.%Self)]
  57. // CHECK:STDOUT: %other.param: @Op.%Self (%Self) = param other, runtime_param1
  58. // CHECK:STDOUT: %other: @Op.%Self (%Self) = bind_name other, %other.param
  59. // CHECK:STDOUT: %Self.ref.loc5_37: %.1 = name_ref Self, @Add.%Self [symbolic = %Self (constants.%Self)]
  60. // CHECK:STDOUT: %.loc5_37.1: type = facet_type_access %Self.ref.loc5_37 [symbolic = %Self (constants.%Self)]
  61. // CHECK:STDOUT: %.loc5_37.2: type = converted %Self.ref.loc5_37, %.loc5_37.1 [symbolic = %Self (constants.%Self)]
  62. // CHECK:STDOUT: %return: ref @Op.%Self (%Self) = var <return slot>
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %.loc5: %.3 = assoc_entity element0, %Op.decl [template = constants.%.4]
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: !members:
  67. // CHECK:STDOUT: .Self = %Self
  68. // CHECK:STDOUT: .Op = %.loc5
  69. // CHECK:STDOUT: witness = (%Op.decl)
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: generic fn @Op(@Add.%Self: %.1) {
  73. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic = %Self (constants.%Self)]
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: fn[%self: @Op.%Self (%Self)](%other: @Op.%Self (%Self)) -> @Op.%Self (%Self);
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: specific @Op(constants.%Self) {
  79. // CHECK:STDOUT: %Self => constants.%Self
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: --- b.carbon
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: constants {
  85. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  86. // CHECK:STDOUT: %.2: type = interface_type @Add [template]
  87. // CHECK:STDOUT: %Self: %.2 = bind_symbolic_name Self 0 [symbolic]
  88. // CHECK:STDOUT: %Op.type.1: type = fn_type @Op.1 [template]
  89. // CHECK:STDOUT: %Op.1: %Op.type.1 = struct_value () [template]
  90. // CHECK:STDOUT: %Op.type.2: type = fn_type @Op.2 [template]
  91. // CHECK:STDOUT: %Op.2: %Op.type.2 = struct_value () [template]
  92. // CHECK:STDOUT: %.3: <witness> = interface_witness (%Op.1) [template]
  93. // CHECK:STDOUT: %tuple: %.1 = tuple_value () [template]
  94. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  95. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  96. // CHECK:STDOUT: %.4: type = assoc_entity_type %.2, %Op.type.2 [template]
  97. // CHECK:STDOUT: %.5: %.4 = assoc_entity element0, imports.%import_ref.5 [template]
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: imports {
  101. // CHECK:STDOUT: %import_ref.1: type = import_ref Main//a, inst+1, loaded [template = constants.%.2]
  102. // CHECK:STDOUT: %import_ref.2 = import_ref Main//a, inst+3, unloaded
  103. // CHECK:STDOUT: %import_ref.3: %.4 = import_ref Main//a, inst+27, loaded [template = constants.%.5]
  104. // CHECK:STDOUT: %import_ref.4: %Op.type.2 = import_ref Main//a, inst+21, loaded [template = constants.%Op.2]
  105. // CHECK:STDOUT: %import_ref.5 = import_ref Main//a, inst+21, unloaded
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: file {
  109. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  110. // CHECK:STDOUT: .Add = imports.%import_ref.1
  111. // CHECK:STDOUT: .F = %F.decl
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT: %default.import = import <invalid>
  114. // CHECK:STDOUT: impl_decl @impl {} {
  115. // CHECK:STDOUT: %.loc6_7.1: %.1 = tuple_literal ()
  116. // CHECK:STDOUT: %.loc6_7.2: type = converted %.loc6_7.1, constants.%.1 [template = constants.%.1]
  117. // CHECK:STDOUT: %Add.ref: type = name_ref Add, imports.%import_ref.1 [template = constants.%.2]
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  120. // CHECK:STDOUT: %x.patt: %.1 = binding_pattern x
  121. // CHECK:STDOUT: %y.patt: %.1 = binding_pattern y
  122. // CHECK:STDOUT: } {
  123. // CHECK:STDOUT: %.loc10_10.1: %.1 = tuple_literal ()
  124. // CHECK:STDOUT: %.loc10_10.2: type = converted %.loc10_10.1, constants.%.1 [template = constants.%.1]
  125. // CHECK:STDOUT: %x.param: %.1 = param x, runtime_param0
  126. // CHECK:STDOUT: %x: %.1 = bind_name x, %x.param
  127. // CHECK:STDOUT: %.loc10_17.1: %.1 = tuple_literal ()
  128. // CHECK:STDOUT: %.loc10_17.2: type = converted %.loc10_17.1, constants.%.1 [template = constants.%.1]
  129. // CHECK:STDOUT: %y.param: %.1 = param y, runtime_param1
  130. // CHECK:STDOUT: %y: %.1 = bind_name y, %y.param
  131. // CHECK:STDOUT: %.loc10_24.1: %.1 = tuple_literal ()
  132. // CHECK:STDOUT: %.loc10_24.2: type = converted %.loc10_24.1, constants.%.1 [template = constants.%.1]
  133. // CHECK:STDOUT: %return: ref %.1 = var <return slot>
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: interface @Add {
  138. // CHECK:STDOUT: !members:
  139. // CHECK:STDOUT: .Self = imports.%import_ref.2
  140. // CHECK:STDOUT: .Op = imports.%import_ref.3
  141. // CHECK:STDOUT: witness = (imports.%import_ref.4)
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: impl @impl: %.1 as %.2 {
  145. // CHECK:STDOUT: %Op.decl: %Op.type.1 = fn_decl @Op.1 [template = constants.%Op.1] {
  146. // CHECK:STDOUT: %self.patt: %.1 = binding_pattern self
  147. // CHECK:STDOUT: %other.patt: %.1 = binding_pattern other
  148. // CHECK:STDOUT: } {
  149. // CHECK:STDOUT: %Self.ref.loc7_15: type = name_ref Self, constants.%.1 [template = constants.%.1]
  150. // CHECK:STDOUT: %self.param: %.1 = param self, runtime_param0
  151. // CHECK:STDOUT: %self: %.1 = bind_name self, %self.param
  152. // CHECK:STDOUT: %Self.ref.loc7_28: type = name_ref Self, constants.%.1 [template = constants.%.1]
  153. // CHECK:STDOUT: %other.param: %.1 = param other, runtime_param1
  154. // CHECK:STDOUT: %other: %.1 = bind_name other, %other.param
  155. // CHECK:STDOUT: %Self.ref.loc7_37: type = name_ref Self, constants.%.1 [template = constants.%.1]
  156. // CHECK:STDOUT: %return: ref %.1 = var <return slot>
  157. // CHECK:STDOUT: }
  158. // CHECK:STDOUT: %.loc6_16: <witness> = interface_witness (%Op.decl) [template = constants.%.3]
  159. // CHECK:STDOUT:
  160. // CHECK:STDOUT: !members:
  161. // CHECK:STDOUT: .Op = %Op.decl
  162. // CHECK:STDOUT: witness = %.loc6_16
  163. // CHECK:STDOUT: }
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: fn @Op.1[%self: %.1](%other: %.1) -> %.1 {
  166. // CHECK:STDOUT: !entry:
  167. // CHECK:STDOUT: %.loc7_52: %.1 = tuple_literal ()
  168. // CHECK:STDOUT: %tuple: %.1 = tuple_value () [template = constants.%tuple]
  169. // CHECK:STDOUT: %.loc7_53: %.1 = converted %.loc7_52, %tuple [template = constants.%tuple]
  170. // CHECK:STDOUT: return %.loc7_53
  171. // CHECK:STDOUT: }
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: generic fn @Op.2(constants.%Self: %.2) {
  174. // CHECK:STDOUT: %Self: %.2 = bind_symbolic_name Self 0 [symbolic = %Self (constants.%Self)]
  175. // CHECK:STDOUT:
  176. // CHECK:STDOUT: fn[%self: @Op.2.%Self (%Self)](%other: @Op.2.%Self (%Self)) -> @Op.2.%Self (%Self);
  177. // CHECK:STDOUT: }
  178. // CHECK:STDOUT:
  179. // CHECK:STDOUT: fn @F(%x: %.1, %y: %.1) -> %.1 {
  180. // CHECK:STDOUT: !entry:
  181. // CHECK:STDOUT: %x.ref: %.1 = name_ref x, %x
  182. // CHECK:STDOUT: %Add.ref: type = name_ref Add, imports.%import_ref.1 [template = constants.%.2]
  183. // CHECK:STDOUT: %Op.ref: %.4 = name_ref Op, imports.%import_ref.3 [template = constants.%.5]
  184. // CHECK:STDOUT: %.loc11_11.1: %Op.type.2 = interface_witness_access @impl.%.loc6_16, element0 [template = constants.%Op.1]
  185. // CHECK:STDOUT: %.loc11_11.2: <bound method> = bound_method %x.ref, %.loc11_11.1
  186. // CHECK:STDOUT: %y.ref: %.1 = name_ref y, %y
  187. // CHECK:STDOUT: %Op.call: init %.1 = call %.loc11_11.2(%x.ref, %y.ref)
  188. // CHECK:STDOUT: %.loc11_20.1: ref %.1 = temporary_storage
  189. // CHECK:STDOUT: %.loc11_20.2: ref %.1 = temporary %.loc11_20.1, %Op.call
  190. // CHECK:STDOUT: %tuple: %.1 = tuple_value () [template = constants.%tuple]
  191. // CHECK:STDOUT: %.loc11_23: %.1 = converted %Op.call, %tuple [template = constants.%tuple]
  192. // CHECK:STDOUT: return %.loc11_23
  193. // CHECK:STDOUT: }
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: specific @Op.2(constants.%Self) {
  196. // CHECK:STDOUT: %Self => constants.%Self
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: specific @Op.2(constants.%.1) {
  200. // CHECK:STDOUT: %Self => constants.%.1
  201. // CHECK:STDOUT: }
  202. // CHECK:STDOUT: