merging_with_indirections.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/convert.carbon
  6. // TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
  7. // EXTRA-ARGS: --dump-sem-ir-ranges=if-present
  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/namespace/merging_with_indirections.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/namespace/merging_with_indirections.carbon
  14. // --- a.carbon
  15. package Other library "[[@TEST_NAME]]";
  16. namespace NS1;
  17. class NS1.A {}
  18. // --- b.carbon
  19. package Other library "[[@TEST_NAME]]";
  20. import library "a";
  21. namespace NS1;
  22. class NS1.B {}
  23. fn F() -> NS1.A { return {}; }
  24. // --- main.carbon
  25. import Other library "b";
  26. import Other library "a";
  27. fn Run() {
  28. // Note `Other.NS.A` is part of the return type here.
  29. Other.F();
  30. // Use `Other.NS.A` directly.
  31. var a: Other.NS1.A;
  32. // Ensure the type is equivalent.
  33. a = Other.F();
  34. }
  35. // CHECK:STDOUT: --- a.carbon
  36. // CHECK:STDOUT:
  37. // CHECK:STDOUT: constants {
  38. // CHECK:STDOUT: %A: type = class_type @A [concrete]
  39. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  40. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  41. // CHECK:STDOUT: }
  42. // CHECK:STDOUT:
  43. // CHECK:STDOUT: imports {
  44. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  45. // CHECK:STDOUT: import Core//prelude
  46. // CHECK:STDOUT: import Core//prelude/...
  47. // CHECK:STDOUT: }
  48. // CHECK:STDOUT: }
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: file {
  51. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  52. // CHECK:STDOUT: .Core = imports.%Core
  53. // CHECK:STDOUT: .NS1 = %NS1
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT: %Core.import = import Core
  56. // CHECK:STDOUT: %NS1: <namespace> = namespace [concrete] {
  57. // CHECK:STDOUT: .A = %A.decl
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT: %A.decl: type = class_decl @A [concrete = constants.%A] {} {}
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT:
  62. // CHECK:STDOUT: class @A {
  63. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type]
  64. // CHECK:STDOUT: complete_type_witness = %complete_type
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: !members:
  67. // CHECK:STDOUT: .Self = constants.%A
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT:
  70. // CHECK:STDOUT: --- b.carbon
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: constants {
  73. // CHECK:STDOUT: %B: type = class_type @B [concrete]
  74. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  75. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  76. // CHECK:STDOUT: %A: type = class_type @A [concrete]
  77. // CHECK:STDOUT: %.b71: form = init_form %A, call_param0 [concrete]
  78. // CHECK:STDOUT: %pattern_type: type = pattern_type %A [concrete]
  79. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  80. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  81. // CHECK:STDOUT: %empty_struct: %empty_struct_type = struct_value () [concrete]
  82. // CHECK:STDOUT: %A.val: %A = struct_value () [concrete]
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: imports {
  86. // CHECK:STDOUT: %Other.NS1: <namespace> = import_ref Other//a, NS1, loaded
  87. // CHECK:STDOUT: %NS1: <namespace> = namespace %Other.NS1, [concrete] {
  88. // CHECK:STDOUT: .A = %Other.A
  89. // CHECK:STDOUT: .B = file.%B.decl
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: %Other.A: type = import_ref Other//a, A, loaded [concrete = constants.%A]
  92. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  93. // CHECK:STDOUT: import Core//prelude
  94. // CHECK:STDOUT: import Core//prelude/...
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT: %Other.import_ref.8f2: <witness> = import_ref Other//a, loc5_14, loaded [concrete = constants.%complete_type]
  97. // CHECK:STDOUT: %Other.import_ref.751 = import_ref Other//a, inst{{[0-9A-F]+}} [no loc], unloaded
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: file {
  101. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  102. // CHECK:STDOUT: .NS1 = imports.%NS1
  103. // CHECK:STDOUT: .Core = imports.%Core
  104. // CHECK:STDOUT: .F = %F.decl
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT: %Core.import = import Core
  107. // CHECK:STDOUT: %default.import = import <none>
  108. // CHECK:STDOUT: %NS1: <namespace> = namespace [concrete] {
  109. // CHECK:STDOUT: .A = imports.%Other.A
  110. // CHECK:STDOUT: .B = %B.decl
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT: %B.decl: type = class_decl @B [concrete = constants.%B] {} {}
  113. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
  114. // CHECK:STDOUT: %return.patt: %pattern_type = return_slot_pattern [concrete]
  115. // CHECK:STDOUT: %return.param_patt: %pattern_type = out_param_pattern %return.patt, call_param0 [concrete]
  116. // CHECK:STDOUT: } {
  117. // CHECK:STDOUT: %NS1.ref: <namespace> = name_ref NS1, imports.%NS1 [concrete = imports.%NS1]
  118. // CHECK:STDOUT: %A.ref: type = name_ref A, imports.%Other.A [concrete = constants.%A]
  119. // CHECK:STDOUT: %.loc8_14: form = init_form %A.ref, call_param0 [concrete = constants.%.b71]
  120. // CHECK:STDOUT: %return.param: ref %A = out_param call_param0
  121. // CHECK:STDOUT: %return: ref %A = return_slot %return.param
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: class @B {
  126. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type]
  127. // CHECK:STDOUT: complete_type_witness = %complete_type
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: !members:
  130. // CHECK:STDOUT: .Self = constants.%B
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: class @A [from "a.carbon"] {
  134. // CHECK:STDOUT: complete_type_witness = imports.%Other.import_ref.8f2
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: !members:
  137. // CHECK:STDOUT: .Self = imports.%Other.import_ref.751
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: fn @F() -> %return.param: %A {
  141. // CHECK:STDOUT: !entry:
  142. // CHECK:STDOUT: %.loc8_27.1: %empty_struct_type = struct_literal () [concrete = constants.%empty_struct]
  143. // CHECK:STDOUT: %.loc8_27.2: init %A = class_init (), %return.param [concrete = constants.%A.val]
  144. // CHECK:STDOUT: %.loc8_28: init %A = converted %.loc8_27.1, %.loc8_27.2 [concrete = constants.%A.val]
  145. // CHECK:STDOUT: return %.loc8_28 to %return.param
  146. // CHECK:STDOUT: }
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: --- main.carbon
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: constants {
  151. // CHECK:STDOUT: %Run.type: type = fn_type @Run [concrete]
  152. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  153. // CHECK:STDOUT: %Run: %Run.type = struct_value () [concrete]
  154. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  155. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  156. // CHECK:STDOUT: %A: type = class_type @A [concrete]
  157. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  158. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  159. // CHECK:STDOUT: %pattern_type.272: type = pattern_type %A [concrete]
  160. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  161. // CHECK:STDOUT: %DestroyOp.type: type = fn_type @DestroyOp [concrete]
  162. // CHECK:STDOUT: %DestroyOp: %DestroyOp.type = struct_value () [concrete]
  163. // CHECK:STDOUT: }
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: imports {
  166. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  167. // CHECK:STDOUT: .Destroy = %Core.Destroy
  168. // CHECK:STDOUT: import Core//prelude
  169. // CHECK:STDOUT: import Core//prelude/...
  170. // CHECK:STDOUT: }
  171. // CHECK:STDOUT: %Other: <namespace> = namespace file.%Other.import, [concrete] {
  172. // CHECK:STDOUT: .F = %Other.F
  173. // CHECK:STDOUT: .NS1 = %NS1.f3d
  174. // CHECK:STDOUT: import Other//b
  175. // CHECK:STDOUT: import Other//a
  176. // CHECK:STDOUT: }
  177. // CHECK:STDOUT: %Other.F: %F.type = import_ref Other//b, F, loaded [concrete = constants.%F]
  178. // CHECK:STDOUT: %Other.import_ref.8db: <witness> = import_ref Other//b, inst{{[0-9A-F]+}} [indirect], loaded [concrete = constants.%complete_type]
  179. // CHECK:STDOUT: %Other.import_ref.8b6 = import_ref Other//b, inst{{[0-9A-F]+}} [indirect], unloaded
  180. // CHECK:STDOUT: %Other.NS1: <namespace> = import_ref Other//b, NS1, loaded
  181. // CHECK:STDOUT: %NS1.f3d: <namespace> = namespace %Other.NS1, [concrete] {
  182. // CHECK:STDOUT: .A = %Other.A
  183. // CHECK:STDOUT: import Other//b
  184. // CHECK:STDOUT: import Other//a
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT: %Other.A: type = import_ref Other//a, A, loaded [concrete = constants.%A]
  187. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  188. // CHECK:STDOUT: }
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: file {
  191. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  192. // CHECK:STDOUT: .Core = imports.%Core
  193. // CHECK:STDOUT: .Other = imports.%Other
  194. // CHECK:STDOUT: .Run = %Run.decl
  195. // CHECK:STDOUT: }
  196. // CHECK:STDOUT: %Core.import = import Core
  197. // CHECK:STDOUT: %Other.import = import Other
  198. // CHECK:STDOUT: %Run.decl: %Run.type = fn_decl @Run [concrete = constants.%Run] {} {}
  199. // CHECK:STDOUT: }
  200. // CHECK:STDOUT:
  201. // CHECK:STDOUT: class @A [from "b.carbon"] {
  202. // CHECK:STDOUT: complete_type_witness = imports.%Other.import_ref.8db
  203. // CHECK:STDOUT:
  204. // CHECK:STDOUT: !members:
  205. // CHECK:STDOUT: .Self = imports.%Other.import_ref.8b6
  206. // CHECK:STDOUT: }
  207. // CHECK:STDOUT:
  208. // CHECK:STDOUT: fn @Run() {
  209. // CHECK:STDOUT: !entry:
  210. // CHECK:STDOUT: %Other.ref.loc7: <namespace> = name_ref Other, imports.%Other [concrete = imports.%Other]
  211. // CHECK:STDOUT: %F.ref.loc7: %F.type = name_ref F, imports.%Other.F [concrete = constants.%F]
  212. // CHECK:STDOUT: %.loc7_11.1: ref %A = temporary_storage
  213. // CHECK:STDOUT: %F.call.loc7: init %A = call %F.ref.loc7() to %.loc7_11.1
  214. // CHECK:STDOUT: %.loc7_11.2: ref %A = temporary %.loc7_11.1, %F.call.loc7
  215. // CHECK:STDOUT: name_binding_decl {
  216. // CHECK:STDOUT: %a.patt: %pattern_type.272 = ref_binding_pattern a [concrete]
  217. // CHECK:STDOUT: %a.var_patt: %pattern_type.272 = var_pattern %a.patt [concrete]
  218. // CHECK:STDOUT: }
  219. // CHECK:STDOUT: %a.var: ref %A = var %a.var_patt
  220. // CHECK:STDOUT: %.loc10: type = splice_block %A.ref [concrete = constants.%A] {
  221. // CHECK:STDOUT: %Other.ref.loc10: <namespace> = name_ref Other, imports.%Other [concrete = imports.%Other]
  222. // CHECK:STDOUT: %NS1.ref: <namespace> = name_ref NS1, imports.%NS1.f3d [concrete = imports.%NS1.f3d]
  223. // CHECK:STDOUT: %A.ref: type = name_ref A, imports.%Other.A [concrete = constants.%A]
  224. // CHECK:STDOUT: }
  225. // CHECK:STDOUT: %a: ref %A = ref_binding a, %a.var
  226. // CHECK:STDOUT: %a.ref: ref %A = name_ref a, %a
  227. // CHECK:STDOUT: %Other.ref.loc13: <namespace> = name_ref Other, imports.%Other [concrete = imports.%Other]
  228. // CHECK:STDOUT: %F.ref.loc13: %F.type = name_ref F, imports.%Other.F [concrete = constants.%F]
  229. // CHECK:STDOUT: %.loc13: ref %A = splice_block %a.ref {}
  230. // CHECK:STDOUT: %F.call.loc13: init %A = call %F.ref.loc13() to %.loc13
  231. // CHECK:STDOUT: assign %a.ref, %F.call.loc13
  232. // CHECK:STDOUT: %DestroyOp.bound.loc10: <bound method> = bound_method %a.var, constants.%DestroyOp
  233. // CHECK:STDOUT: %DestroyOp.call.loc10: init %empty_tuple.type = call %DestroyOp.bound.loc10(%a.var)
  234. // CHECK:STDOUT: %DestroyOp.bound.loc7: <bound method> = bound_method %.loc7_11.2, constants.%DestroyOp
  235. // CHECK:STDOUT: %DestroyOp.call.loc7: init %empty_tuple.type = call %DestroyOp.bound.loc7(%.loc7_11.2)
  236. // CHECK:STDOUT: return
  237. // CHECK:STDOUT: }
  238. // CHECK:STDOUT:
  239. // CHECK:STDOUT: fn @F [from "b.carbon"];
  240. // CHECK:STDOUT:
  241. // CHECK:STDOUT: fn @DestroyOp(%self.param: %A) = "no_op";
  242. // CHECK:STDOUT: