merging_with_indirections.carbon 10 KB

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