import_self.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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: %Add.type: type = facet_type <@Add> [template]
  28. // CHECK:STDOUT: %Self: %Add.type = bind_symbolic_name Self, 0 [symbolic]
  29. // CHECK:STDOUT: %Self.as_type: type = facet_access_type %Self [symbolic]
  30. // CHECK:STDOUT: %Op.type: type = fn_type @Op [template]
  31. // CHECK:STDOUT: %Op: %Op.type = struct_value () [template]
  32. // CHECK:STDOUT: %Add.assoc_type: type = assoc_entity_type %Add.type [template]
  33. // CHECK:STDOUT: %assoc0: %Add.assoc_type = 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.%Add.type] {} {}
  41. // CHECK:STDOUT: }
  42. // CHECK:STDOUT:
  43. // CHECK:STDOUT: interface @Add {
  44. // CHECK:STDOUT: %Self: %Add.type = 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.as_type.loc5_15.1 (%Self.as_type) = binding_pattern self
  47. // CHECK:STDOUT: %self.param_patt: @Op.%Self.as_type.loc5_15.1 (%Self.as_type) = value_param_pattern %self.patt, runtime_param0
  48. // CHECK:STDOUT: %other.patt: @Op.%Self.as_type.loc5_15.1 (%Self.as_type) = binding_pattern other
  49. // CHECK:STDOUT: %other.param_patt: @Op.%Self.as_type.loc5_15.1 (%Self.as_type) = value_param_pattern %other.patt, runtime_param1
  50. // CHECK:STDOUT: %return.patt: @Op.%Self.as_type.loc5_15.1 (%Self.as_type) = return_slot_pattern
  51. // CHECK:STDOUT: %return.param_patt: @Op.%Self.as_type.loc5_15.1 (%Self.as_type) = out_param_pattern %return.patt, runtime_param2
  52. // CHECK:STDOUT: } {
  53. // CHECK:STDOUT: %Self.ref.loc5_37: %Add.type = name_ref Self, @Add.%Self [symbolic = %Self (constants.%Self)]
  54. // CHECK:STDOUT: %Self.as_type.loc5_37: type = facet_access_type %Self.ref.loc5_37 [symbolic = %Self.as_type.loc5_15.1 (constants.%Self.as_type)]
  55. // CHECK:STDOUT: %.loc5_37: type = converted %Self.ref.loc5_37, %Self.as_type.loc5_37 [symbolic = %Self.as_type.loc5_15.1 (constants.%Self.as_type)]
  56. // CHECK:STDOUT: %self.param: @Op.%Self.as_type.loc5_15.1 (%Self.as_type) = value_param runtime_param0
  57. // CHECK:STDOUT: %.loc5_15.1: type = splice_block %.loc5_15.2 [symbolic = %Self.as_type.loc5_15.1 (constants.%Self.as_type)] {
  58. // CHECK:STDOUT: %Self.ref.loc5_15: %Add.type = name_ref Self, @Add.%Self [symbolic = %Self (constants.%Self)]
  59. // CHECK:STDOUT: %Self.as_type.loc5_15.2: type = facet_access_type %Self.ref.loc5_15 [symbolic = %Self.as_type.loc5_15.1 (constants.%Self.as_type)]
  60. // CHECK:STDOUT: %.loc5_15.2: type = converted %Self.ref.loc5_15, %Self.as_type.loc5_15.2 [symbolic = %Self.as_type.loc5_15.1 (constants.%Self.as_type)]
  61. // CHECK:STDOUT: }
  62. // CHECK:STDOUT: %self: @Op.%Self.as_type.loc5_15.1 (%Self.as_type) = bind_name self, %self.param
  63. // CHECK:STDOUT: %other.param: @Op.%Self.as_type.loc5_15.1 (%Self.as_type) = value_param runtime_param1
  64. // CHECK:STDOUT: %.loc5_28.1: type = splice_block %.loc5_28.2 [symbolic = %Self.as_type.loc5_15.1 (constants.%Self.as_type)] {
  65. // CHECK:STDOUT: %Self.ref.loc5_28: %Add.type = name_ref Self, @Add.%Self [symbolic = %Self (constants.%Self)]
  66. // CHECK:STDOUT: %Self.as_type.loc5_28: type = facet_access_type %Self.ref.loc5_28 [symbolic = %Self.as_type.loc5_15.1 (constants.%Self.as_type)]
  67. // CHECK:STDOUT: %.loc5_28.2: type = converted %Self.ref.loc5_28, %Self.as_type.loc5_28 [symbolic = %Self.as_type.loc5_15.1 (constants.%Self.as_type)]
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT: %other: @Op.%Self.as_type.loc5_15.1 (%Self.as_type) = bind_name other, %other.param
  70. // CHECK:STDOUT: %return.param: ref @Op.%Self.as_type.loc5_15.1 (%Self.as_type) = out_param runtime_param2
  71. // CHECK:STDOUT: %return: ref @Op.%Self.as_type.loc5_15.1 (%Self.as_type) = return_slot %return.param
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: %assoc0: %Add.assoc_type = assoc_entity element0, %Op.decl [template = constants.%assoc0]
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: !members:
  76. // CHECK:STDOUT: .Self = %Self
  77. // CHECK:STDOUT: .Op = %assoc0
  78. // CHECK:STDOUT: witness = (%Op.decl)
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: generic fn @Op(@Add.%Self: %Add.type) {
  82. // CHECK:STDOUT: %Self: %Add.type = bind_symbolic_name Self, 0 [symbolic = %Self (constants.%Self)]
  83. // CHECK:STDOUT: %Self.as_type.loc5_15.1: type = facet_access_type %Self [symbolic = %Self.as_type.loc5_15.1 (constants.%Self.as_type)]
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: fn[%self.param_patt: @Op.%Self.as_type.loc5_15.1 (%Self.as_type)](%other.param_patt: @Op.%Self.as_type.loc5_15.1 (%Self.as_type)) -> @Op.%Self.as_type.loc5_15.1 (%Self.as_type);
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: specific @Op(constants.%Self) {
  89. // CHECK:STDOUT: %Self => constants.%Self
  90. // CHECK:STDOUT: %Self.as_type.loc5_15.1 => constants.%Self.as_type
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: --- b.carbon
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: constants {
  96. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [template]
  97. // CHECK:STDOUT: %Add.type: type = facet_type <@Add> [template]
  98. // CHECK:STDOUT: %Self: %Add.type = bind_symbolic_name Self, 0 [symbolic]
  99. // CHECK:STDOUT: %Op.type.31b: type = fn_type @Op.1 [template]
  100. // CHECK:STDOUT: %Op.d59: %Op.type.31b = struct_value () [template]
  101. // CHECK:STDOUT: %Self.as_type: type = facet_access_type %Self [symbolic]
  102. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness (@impl.%Op.decl) [template]
  103. // CHECK:STDOUT: %Op.type.adf: type = fn_type @Op.2 [template]
  104. // CHECK:STDOUT: %Op.489: %Op.type.adf = struct_value () [template]
  105. // CHECK:STDOUT: %Add.facet: %Add.type = facet_value %empty_tuple.type, %impl_witness [template]
  106. // CHECK:STDOUT: %empty_tuple: %empty_tuple.type = tuple_value () [template]
  107. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  108. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  109. // CHECK:STDOUT: %Add.assoc_type: type = assoc_entity_type %Add.type [template]
  110. // CHECK:STDOUT: %assoc0: %Add.assoc_type = assoc_entity element0, imports.%Main.import_ref.5a3 [template]
  111. // CHECK:STDOUT: %.a26: type = fn_type_with_self_type %Op.type.31b, %Add.facet [template]
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT:
  114. // CHECK:STDOUT: imports {
  115. // CHECK:STDOUT: %Main.Add: type = import_ref Main//a, Add, loaded [template = constants.%Add.type]
  116. // CHECK:STDOUT: %Main.import_ref.07c = import_ref Main//a, inst15 [no loc], unloaded
  117. // CHECK:STDOUT: %Main.import_ref.db7: %Add.assoc_type = import_ref Main//a, loc5_41, loaded [template = constants.%assoc0]
  118. // CHECK:STDOUT: %Main.Op: %Op.type.31b = import_ref Main//a, Op, loaded [template = constants.%Op.d59]
  119. // CHECK:STDOUT: %Main.import_ref.e5e: %Add.type = import_ref Main//a, inst15 [no loc], loaded [symbolic = constants.%Self]
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: file {
  123. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  124. // CHECK:STDOUT: .Add = imports.%Main.Add
  125. // CHECK:STDOUT: .F = %F.decl
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT: %default.import = import <none>
  128. // CHECK:STDOUT: impl_decl @impl [template] {} {
  129. // CHECK:STDOUT: %.loc6_7.1: %empty_tuple.type = tuple_literal ()
  130. // CHECK:STDOUT: %.loc6_7.2: type = converted %.loc6_7.1, constants.%empty_tuple.type [template = constants.%empty_tuple.type]
  131. // CHECK:STDOUT: %Add.ref: type = name_ref Add, imports.%Main.Add [template = constants.%Add.type]
  132. // CHECK:STDOUT: }
  133. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness (@impl.%Op.decl) [template = constants.%impl_witness]
  134. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {
  135. // CHECK:STDOUT: %x.patt: %empty_tuple.type = binding_pattern x
  136. // CHECK:STDOUT: %x.param_patt: %empty_tuple.type = value_param_pattern %x.patt, runtime_param0
  137. // CHECK:STDOUT: %y.patt: %empty_tuple.type = binding_pattern y
  138. // CHECK:STDOUT: %y.param_patt: %empty_tuple.type = value_param_pattern %y.patt, runtime_param1
  139. // CHECK:STDOUT: %return.patt: %empty_tuple.type = return_slot_pattern
  140. // CHECK:STDOUT: %return.param_patt: %empty_tuple.type = out_param_pattern %return.patt, runtime_param2
  141. // CHECK:STDOUT: } {
  142. // CHECK:STDOUT: %.loc10_24.1: %empty_tuple.type = tuple_literal ()
  143. // CHECK:STDOUT: %.loc10_24.2: type = converted %.loc10_24.1, constants.%empty_tuple.type [template = constants.%empty_tuple.type]
  144. // CHECK:STDOUT: %x.param: %empty_tuple.type = value_param runtime_param0
  145. // CHECK:STDOUT: %.loc10_10.1: type = splice_block %.loc10_10.3 [template = constants.%empty_tuple.type] {
  146. // CHECK:STDOUT: %.loc10_10.2: %empty_tuple.type = tuple_literal ()
  147. // CHECK:STDOUT: %.loc10_10.3: type = converted %.loc10_10.2, constants.%empty_tuple.type [template = constants.%empty_tuple.type]
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT: %x: %empty_tuple.type = bind_name x, %x.param
  150. // CHECK:STDOUT: %y.param: %empty_tuple.type = value_param runtime_param1
  151. // CHECK:STDOUT: %.loc10_17.1: type = splice_block %.loc10_17.3 [template = constants.%empty_tuple.type] {
  152. // CHECK:STDOUT: %.loc10_17.2: %empty_tuple.type = tuple_literal ()
  153. // CHECK:STDOUT: %.loc10_17.3: type = converted %.loc10_17.2, constants.%empty_tuple.type [template = constants.%empty_tuple.type]
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT: %y: %empty_tuple.type = bind_name y, %y.param
  156. // CHECK:STDOUT: %return.param: ref %empty_tuple.type = out_param runtime_param2
  157. // CHECK:STDOUT: %return: ref %empty_tuple.type = return_slot %return.param
  158. // CHECK:STDOUT: }
  159. // CHECK:STDOUT: }
  160. // CHECK:STDOUT:
  161. // CHECK:STDOUT: interface @Add [from "a.carbon"] {
  162. // CHECK:STDOUT: !members:
  163. // CHECK:STDOUT: .Self = imports.%Main.import_ref.07c
  164. // CHECK:STDOUT: .Op = imports.%Main.import_ref.db7
  165. // CHECK:STDOUT: witness = (imports.%Main.Op)
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: impl @impl: %.loc6_7.2 as %Add.ref {
  169. // CHECK:STDOUT: %Op.decl: %Op.type.adf = fn_decl @Op.2 [template = constants.%Op.489] {
  170. // CHECK:STDOUT: %self.patt: %empty_tuple.type = binding_pattern self
  171. // CHECK:STDOUT: %self.param_patt: %empty_tuple.type = value_param_pattern %self.patt, runtime_param0
  172. // CHECK:STDOUT: %other.patt: %empty_tuple.type = binding_pattern other
  173. // CHECK:STDOUT: %other.param_patt: %empty_tuple.type = value_param_pattern %other.patt, runtime_param1
  174. // CHECK:STDOUT: %return.patt: %empty_tuple.type = return_slot_pattern
  175. // CHECK:STDOUT: %return.param_patt: %empty_tuple.type = out_param_pattern %return.patt, runtime_param2
  176. // CHECK:STDOUT: } {
  177. // CHECK:STDOUT: %Self.ref.loc7_37: type = name_ref Self, @impl.%.loc6_7.2 [template = constants.%empty_tuple.type]
  178. // CHECK:STDOUT: %self.param: %empty_tuple.type = value_param runtime_param0
  179. // CHECK:STDOUT: %Self.ref.loc7_15: type = name_ref Self, @impl.%.loc6_7.2 [template = constants.%empty_tuple.type]
  180. // CHECK:STDOUT: %self: %empty_tuple.type = bind_name self, %self.param
  181. // CHECK:STDOUT: %other.param: %empty_tuple.type = value_param runtime_param1
  182. // CHECK:STDOUT: %Self.ref.loc7_28: type = name_ref Self, @impl.%.loc6_7.2 [template = constants.%empty_tuple.type]
  183. // CHECK:STDOUT: %other: %empty_tuple.type = bind_name other, %other.param
  184. // CHECK:STDOUT: %return.param: ref %empty_tuple.type = out_param runtime_param2
  185. // CHECK:STDOUT: %return: ref %empty_tuple.type = return_slot %return.param
  186. // CHECK:STDOUT: }
  187. // CHECK:STDOUT:
  188. // CHECK:STDOUT: !members:
  189. // CHECK:STDOUT: .Op = %Op.decl
  190. // CHECK:STDOUT: witness = file.%impl_witness
  191. // CHECK:STDOUT: }
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: generic fn @Op.1(imports.%Main.import_ref.e5e: %Add.type) [from "a.carbon"] {
  194. // CHECK:STDOUT: %Self: %Add.type = bind_symbolic_name Self, 0 [symbolic = %Self (constants.%Self)]
  195. // CHECK:STDOUT: %Self.as_type: type = facet_access_type %Self [symbolic = %Self.as_type (constants.%Self.as_type)]
  196. // CHECK:STDOUT:
  197. // CHECK:STDOUT: fn[%self.param_patt: @Op.1.%Self.as_type (%Self.as_type)](%other.param_patt: @Op.1.%Self.as_type (%Self.as_type)) -> @Op.1.%Self.as_type (%Self.as_type);
  198. // CHECK:STDOUT: }
  199. // CHECK:STDOUT:
  200. // CHECK:STDOUT: fn @Op.2[%self.param_patt: %empty_tuple.type](%other.param_patt: %empty_tuple.type) -> %empty_tuple.type {
  201. // CHECK:STDOUT: !entry:
  202. // CHECK:STDOUT: %.loc7_52: %empty_tuple.type = tuple_literal ()
  203. // CHECK:STDOUT: %empty_tuple: %empty_tuple.type = tuple_value () [template = constants.%empty_tuple]
  204. // CHECK:STDOUT: %.loc7_53: %empty_tuple.type = converted %.loc7_52, %empty_tuple [template = constants.%empty_tuple]
  205. // CHECK:STDOUT: return %.loc7_53
  206. // CHECK:STDOUT: }
  207. // CHECK:STDOUT:
  208. // CHECK:STDOUT: fn @F(%x.param_patt: %empty_tuple.type, %y.param_patt: %empty_tuple.type) -> %empty_tuple.type {
  209. // CHECK:STDOUT: !entry:
  210. // CHECK:STDOUT: %x.ref: %empty_tuple.type = name_ref x, %x
  211. // CHECK:STDOUT: %Add.ref: type = name_ref Add, imports.%Main.Add [template = constants.%Add.type]
  212. // CHECK:STDOUT: %Op.ref: %Add.assoc_type = name_ref Op, imports.%Main.import_ref.db7 [template = constants.%assoc0]
  213. // CHECK:STDOUT: %impl.elem0: %.a26 = impl_witness_access constants.%impl_witness, element0 [template = constants.%Op.489]
  214. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %x.ref, %impl.elem0
  215. // CHECK:STDOUT: %y.ref: %empty_tuple.type = name_ref y, %y
  216. // CHECK:STDOUT: %Op.call: init %empty_tuple.type = call %bound_method(%x.ref, %y.ref)
  217. // CHECK:STDOUT: %.loc11_22.1: ref %empty_tuple.type = temporary_storage
  218. // CHECK:STDOUT: %.loc11_22.2: ref %empty_tuple.type = temporary %.loc11_22.1, %Op.call
  219. // CHECK:STDOUT: %tuple: %empty_tuple.type = tuple_value () [template = constants.%empty_tuple]
  220. // CHECK:STDOUT: %.loc11_23: %empty_tuple.type = converted %Op.call, %tuple [template = constants.%empty_tuple]
  221. // CHECK:STDOUT: return %.loc11_23
  222. // CHECK:STDOUT: }
  223. // CHECK:STDOUT:
  224. // CHECK:STDOUT: specific @Op.1(constants.%Self) {
  225. // CHECK:STDOUT: %Self => constants.%Self
  226. // CHECK:STDOUT: %Self.as_type => constants.%Self.as_type
  227. // CHECK:STDOUT: }
  228. // CHECK:STDOUT:
  229. // CHECK:STDOUT: specific @Op.1(constants.%Add.facet) {
  230. // CHECK:STDOUT: %Self => constants.%Add.facet
  231. // CHECK:STDOUT: %Self.as_type => constants.%empty_tuple.type
  232. // CHECK:STDOUT: }
  233. // CHECK:STDOUT: