merging_with_indirections.carbon 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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: %.1: type = struct_type {} [template]
  36. // CHECK:STDOUT: %.2: <witness> = complete_type_witness %.1 [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/operators
  43. // CHECK:STDOUT: import Core//prelude/types
  44. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  45. // CHECK:STDOUT: import Core//prelude/operators/as
  46. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  47. // CHECK:STDOUT: import Core//prelude/operators/comparison
  48. // CHECK:STDOUT: import Core//prelude/types/bool
  49. // CHECK:STDOUT: }
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT:
  52. // CHECK:STDOUT: file {
  53. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  54. // CHECK:STDOUT: .Core = imports.%Core
  55. // CHECK:STDOUT: .NS1 = %NS1
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT: %Core.import = import Core
  58. // CHECK:STDOUT: %NS1: <namespace> = namespace [template] {
  59. // CHECK:STDOUT: .A = %A.decl
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {} {}
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: class @A {
  65. // CHECK:STDOUT: %.loc5: <witness> = complete_type_witness %.1 [template = constants.%.2]
  66. // CHECK:STDOUT:
  67. // CHECK:STDOUT: !members:
  68. // CHECK:STDOUT: .Self = constants.%A
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: --- b.carbon
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: constants {
  74. // CHECK:STDOUT: %B: type = class_type @B [template]
  75. // CHECK:STDOUT: %.1: type = struct_type {} [template]
  76. // CHECK:STDOUT: %.2: <witness> = complete_type_witness %.1 [template]
  77. // CHECK:STDOUT: %A: type = class_type @A [template]
  78. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  79. // CHECK:STDOUT: %.3: type = tuple_type () [template]
  80. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  81. // CHECK:STDOUT: %.4: type = ptr_type %.1 [template]
  82. // CHECK:STDOUT: %struct: %A = struct_value () [template]
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: imports {
  86. // CHECK:STDOUT: %import_ref.1: <namespace> = import_ref Other//a, inst+3, loaded
  87. // CHECK:STDOUT: %NS1: <namespace> = namespace %import_ref.1, [template] {
  88. // CHECK:STDOUT: .A = %import_ref.2
  89. // CHECK:STDOUT: .B = file.%B.decl
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: %import_ref.2: type = import_ref Other//a, inst+4, loaded [template = constants.%A]
  92. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  93. // CHECK:STDOUT: import Core//prelude
  94. // CHECK:STDOUT: import Core//prelude/operators
  95. // CHECK:STDOUT: import Core//prelude/types
  96. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  97. // CHECK:STDOUT: import Core//prelude/operators/as
  98. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  99. // CHECK:STDOUT: import Core//prelude/operators/comparison
  100. // CHECK:STDOUT: import Core//prelude/types/bool
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT: %import_ref.3 = import_ref Other//a, inst+5, unloaded
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: file {
  106. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  107. // CHECK:STDOUT: .NS1 = imports.%NS1
  108. // CHECK:STDOUT: .Core = imports.%Core
  109. // CHECK:STDOUT: .F = %F.decl
  110. // CHECK:STDOUT: }
  111. // CHECK:STDOUT: %Core.import = import Core
  112. // CHECK:STDOUT: %default.import = import <invalid>
  113. // CHECK:STDOUT: %NS1: <namespace> = namespace [template] {
  114. // CHECK:STDOUT: .A = imports.%import_ref.2
  115. // CHECK:STDOUT: .B = %B.decl
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT: %B.decl: type = class_decl @B [template = constants.%B] {} {}
  118. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {
  119. // CHECK:STDOUT: %NS1.ref: <namespace> = name_ref NS1, imports.%NS1 [template = imports.%NS1]
  120. // CHECK:STDOUT: %A.ref: type = name_ref A, imports.%import_ref.2 [template = constants.%A]
  121. // CHECK:STDOUT: %return: ref %A = var <return slot>
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: class @B {
  126. // CHECK:STDOUT: %.loc6: <witness> = complete_type_witness %.1 [template = constants.%.2]
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: !members:
  129. // CHECK:STDOUT: .Self = constants.%B
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: class @A {
  133. // CHECK:STDOUT: !members:
  134. // CHECK:STDOUT: .Self = imports.%import_ref.3
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: fn @F() -> %return: %A {
  138. // CHECK:STDOUT: !entry:
  139. // CHECK:STDOUT: %.loc8_27.1: %.1 = struct_literal ()
  140. // CHECK:STDOUT: %.loc8_27.2: init %A = class_init (), %return [template = constants.%struct]
  141. // CHECK:STDOUT: %.loc8_28: init %A = converted %.loc8_27.1, %.loc8_27.2 [template = constants.%struct]
  142. // CHECK:STDOUT: return %.loc8_28 to %return
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT:
  145. // CHECK:STDOUT: --- main.carbon
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: constants {
  148. // CHECK:STDOUT: %Run.type: type = fn_type @Run [template]
  149. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  150. // CHECK:STDOUT: %Run: %Run.type = struct_value () [template]
  151. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  152. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  153. // CHECK:STDOUT: %A: type = class_type @A [template]
  154. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  155. // CHECK:STDOUT: %.3: <witness> = complete_type_witness %.2 [template]
  156. // CHECK:STDOUT: %.4: type = ptr_type %.2 [template]
  157. // CHECK:STDOUT: }
  158. // CHECK:STDOUT:
  159. // CHECK:STDOUT: imports {
  160. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  161. // CHECK:STDOUT: import Core//prelude
  162. // CHECK:STDOUT: import Core//prelude/operators
  163. // CHECK:STDOUT: import Core//prelude/types
  164. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  165. // CHECK:STDOUT: import Core//prelude/operators/as
  166. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  167. // CHECK:STDOUT: import Core//prelude/operators/comparison
  168. // CHECK:STDOUT: import Core//prelude/types/bool
  169. // CHECK:STDOUT: }
  170. // CHECK:STDOUT: %Other: <namespace> = namespace file.%Other.import, [template] {
  171. // CHECK:STDOUT: .F = %import_ref.1
  172. // CHECK:STDOUT: .NS1 = %NS1
  173. // CHECK:STDOUT: import Other//b
  174. // CHECK:STDOUT: import Other//a
  175. // CHECK:STDOUT: }
  176. // CHECK:STDOUT: %import_ref.1: %F.type = import_ref Other//b, inst+19, loaded [template = constants.%F]
  177. // CHECK:STDOUT: %import_ref.2 = import_ref Other//b, inst+16, unloaded
  178. // CHECK:STDOUT: %import_ref.3: <namespace> = import_ref Other//b, inst+4, loaded
  179. // CHECK:STDOUT: %NS1: <namespace> = namespace %import_ref.3, [template] {
  180. // CHECK:STDOUT: .A = %import_ref.4
  181. // CHECK:STDOUT: import Other//b
  182. // CHECK:STDOUT: import Other//a
  183. // CHECK:STDOUT: }
  184. // CHECK:STDOUT: %import_ref.4: type = import_ref Other//a, inst+4, loaded [template = constants.%A]
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: file {
  188. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  189. // CHECK:STDOUT: .Core = imports.%Core
  190. // CHECK:STDOUT: .Other = imports.%Other
  191. // CHECK:STDOUT: .Run = %Run.decl
  192. // CHECK:STDOUT: }
  193. // CHECK:STDOUT: %Core.import = import Core
  194. // CHECK:STDOUT: %Other.import = import Other
  195. // CHECK:STDOUT: %Run.decl: %Run.type = fn_decl @Run [template = constants.%Run] {} {}
  196. // CHECK:STDOUT: }
  197. // CHECK:STDOUT:
  198. // CHECK:STDOUT: class @A {
  199. // CHECK:STDOUT: !members:
  200. // CHECK:STDOUT: .Self = imports.%import_ref.2
  201. // CHECK:STDOUT: }
  202. // CHECK:STDOUT:
  203. // CHECK:STDOUT: fn @Run() {
  204. // CHECK:STDOUT: !entry:
  205. // CHECK:STDOUT: %Other.ref.loc7: <namespace> = name_ref Other, imports.%Other [template = imports.%Other]
  206. // CHECK:STDOUT: %F.ref.loc7: %F.type = name_ref F, imports.%import_ref.1 [template = constants.%F]
  207. // CHECK:STDOUT: %.loc7_10.1: ref %A = temporary_storage
  208. // CHECK:STDOUT: %F.call.loc7: init %A = call %F.ref.loc7() to %.loc7_10.1
  209. // CHECK:STDOUT: %.loc7_10.2: ref %A = temporary %.loc7_10.1, %F.call.loc7
  210. // CHECK:STDOUT: %Other.ref.loc10: <namespace> = name_ref Other, imports.%Other [template = imports.%Other]
  211. // CHECK:STDOUT: %NS1.ref: <namespace> = name_ref NS1, imports.%NS1 [template = imports.%NS1]
  212. // CHECK:STDOUT: %A.ref: type = name_ref A, imports.%import_ref.4 [template = constants.%A]
  213. // CHECK:STDOUT: %a.var: ref %A = var a
  214. // CHECK:STDOUT: %a: ref %A = bind_name a, %a.var
  215. // CHECK:STDOUT: %a.ref: ref %A = name_ref a, %a
  216. // CHECK:STDOUT: %Other.ref.loc13: <namespace> = name_ref Other, imports.%Other [template = imports.%Other]
  217. // CHECK:STDOUT: %F.ref.loc13: %F.type = name_ref F, imports.%import_ref.1 [template = constants.%F]
  218. // CHECK:STDOUT: %.loc13: ref %A = splice_block %a.ref {}
  219. // CHECK:STDOUT: %F.call.loc13: init %A = call %F.ref.loc13() to %.loc13
  220. // CHECK:STDOUT: assign %a.ref, %F.call.loc13
  221. // CHECK:STDOUT: return
  222. // CHECK:STDOUT: }
  223. // CHECK:STDOUT:
  224. // CHECK:STDOUT: fn @F() -> %A;
  225. // CHECK:STDOUT: