merging_with_indirections.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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: %.71f: Core.Form = init_form %A [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 [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: Core.Form = init_form %A.ref [concrete = constants.%.71f]
  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() -> out %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 to %return.param = class_init () [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: %DefaultOrUnformed.type: type = facet_type <@DefaultOrUnformed> [concrete]
  161. // CHECK:STDOUT: %T: type = symbolic_binding T, 0 [symbolic]
  162. // CHECK:STDOUT: %T.as.DefaultOrUnformed.impl.Op.type.6c6: type = fn_type @T.as.DefaultOrUnformed.impl.Op, @T.as.DefaultOrUnformed.impl(%T) [symbolic]
  163. // CHECK:STDOUT: %T.as.DefaultOrUnformed.impl.Op.ae1: %T.as.DefaultOrUnformed.impl.Op.type.6c6 = struct_value () [symbolic]
  164. // CHECK:STDOUT: %DefaultOrUnformed.impl_witness.c9e: <witness> = impl_witness imports.%DefaultOrUnformed.impl_witness_table, @T.as.DefaultOrUnformed.impl(%A) [concrete]
  165. // CHECK:STDOUT: %T.as.DefaultOrUnformed.impl.Op.type.ffd: type = fn_type @T.as.DefaultOrUnformed.impl.Op, @T.as.DefaultOrUnformed.impl(%A) [concrete]
  166. // CHECK:STDOUT: %T.as.DefaultOrUnformed.impl.Op.993: %T.as.DefaultOrUnformed.impl.Op.type.ffd = struct_value () [concrete]
  167. // CHECK:STDOUT: %DefaultOrUnformed.facet: %DefaultOrUnformed.type = facet_value %A, (%DefaultOrUnformed.impl_witness.c9e) [concrete]
  168. // CHECK:STDOUT: %T.as.DefaultOrUnformed.impl.Op.specific_fn: <specific function> = specific_function %T.as.DefaultOrUnformed.impl.Op.993, @T.as.DefaultOrUnformed.impl.Op(%A) [concrete]
  169. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  170. // CHECK:STDOUT: %Destroy.Op.type: type = fn_type @Destroy.Op [concrete]
  171. // CHECK:STDOUT: %Destroy.Op: %Destroy.Op.type = struct_value () [concrete]
  172. // CHECK:STDOUT: }
  173. // CHECK:STDOUT:
  174. // CHECK:STDOUT: imports {
  175. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  176. // CHECK:STDOUT: .DefaultOrUnformed = %Core.DefaultOrUnformed
  177. // CHECK:STDOUT: .Destroy = %Core.Destroy
  178. // CHECK:STDOUT: import Core//prelude
  179. // CHECK:STDOUT: import Core//prelude/...
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT: %Other: <namespace> = namespace file.%Other.import, [concrete] {
  182. // CHECK:STDOUT: .F = %Other.F
  183. // CHECK:STDOUT: .NS1 = %NS1.f3d
  184. // CHECK:STDOUT: import Other//b
  185. // CHECK:STDOUT: import Other//a
  186. // CHECK:STDOUT: }
  187. // CHECK:STDOUT: %Other.F: %F.type = import_ref Other//b, F, loaded [concrete = constants.%F]
  188. // CHECK:STDOUT: %Other.import_ref.8db: <witness> = import_ref Other//b, inst{{[0-9A-F]+}} [indirect], loaded [concrete = constants.%complete_type]
  189. // CHECK:STDOUT: %Other.import_ref.8b6 = import_ref Other//b, inst{{[0-9A-F]+}} [indirect], unloaded
  190. // CHECK:STDOUT: %Other.NS1: <namespace> = import_ref Other//b, NS1, loaded
  191. // CHECK:STDOUT: %NS1.f3d: <namespace> = namespace %Other.NS1, [concrete] {
  192. // CHECK:STDOUT: .A = %Other.A
  193. // CHECK:STDOUT: import Other//b
  194. // CHECK:STDOUT: import Other//a
  195. // CHECK:STDOUT: }
  196. // CHECK:STDOUT: %Other.A: type = import_ref Other//a, A, loaded [concrete = constants.%A]
  197. // CHECK:STDOUT: %Core.DefaultOrUnformed: type = import_ref Core//prelude/parts/default, DefaultOrUnformed, loaded [concrete = constants.%DefaultOrUnformed.type]
  198. // CHECK:STDOUT: %Core.import_ref.01d: @T.as.DefaultOrUnformed.impl.%T.as.DefaultOrUnformed.impl.Op.type (%T.as.DefaultOrUnformed.impl.Op.type.6c6) = import_ref Core//prelude/parts/default, loc{{\d+_\d+}}, loaded [symbolic = @T.as.DefaultOrUnformed.impl.%T.as.DefaultOrUnformed.impl.Op (constants.%T.as.DefaultOrUnformed.impl.Op.ae1)]
  199. // CHECK:STDOUT: %DefaultOrUnformed.impl_witness_table = impl_witness_table (%Core.import_ref.01d), @T.as.DefaultOrUnformed.impl [concrete]
  200. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  201. // CHECK:STDOUT: }
  202. // CHECK:STDOUT:
  203. // CHECK:STDOUT: file {
  204. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  205. // CHECK:STDOUT: .Core = imports.%Core
  206. // CHECK:STDOUT: .Other = imports.%Other
  207. // CHECK:STDOUT: .Run = %Run.decl
  208. // CHECK:STDOUT: }
  209. // CHECK:STDOUT: %Core.import = import Core
  210. // CHECK:STDOUT: %Other.import = import Other
  211. // CHECK:STDOUT: %Run.decl: %Run.type = fn_decl @Run [concrete = constants.%Run] {} {}
  212. // CHECK:STDOUT: }
  213. // CHECK:STDOUT:
  214. // CHECK:STDOUT: class @A [from "b.carbon"] {
  215. // CHECK:STDOUT: complete_type_witness = imports.%Other.import_ref.8db
  216. // CHECK:STDOUT:
  217. // CHECK:STDOUT: !members:
  218. // CHECK:STDOUT: .Self = imports.%Other.import_ref.8b6
  219. // CHECK:STDOUT: }
  220. // CHECK:STDOUT:
  221. // CHECK:STDOUT: fn @Run() {
  222. // CHECK:STDOUT: !entry:
  223. // CHECK:STDOUT: %Other.ref.loc7: <namespace> = name_ref Other, imports.%Other [concrete = imports.%Other]
  224. // CHECK:STDOUT: %F.ref.loc7: %F.type = name_ref F, imports.%Other.F [concrete = constants.%F]
  225. // CHECK:STDOUT: %.loc7_11.1: ref %A = temporary_storage
  226. // CHECK:STDOUT: %F.call.loc7: init %A to %.loc7_11.1 = call %F.ref.loc7()
  227. // CHECK:STDOUT: %.loc7_11.2: ref %A = temporary %.loc7_11.1, %F.call.loc7
  228. // CHECK:STDOUT: name_binding_decl {
  229. // CHECK:STDOUT: %a.patt: %pattern_type.272 = ref_binding_pattern a [concrete]
  230. // CHECK:STDOUT: %a.var_patt: %pattern_type.272 = var_pattern %a.patt [concrete]
  231. // CHECK:STDOUT: }
  232. // CHECK:STDOUT: %a.var: ref %A = var %a.var_patt
  233. // CHECK:STDOUT: %DefaultOrUnformed.facet: %DefaultOrUnformed.type = facet_value constants.%A, (constants.%DefaultOrUnformed.impl_witness.c9e) [concrete = constants.%DefaultOrUnformed.facet]
  234. // CHECK:STDOUT: %.loc10_21.1: %DefaultOrUnformed.type = converted constants.%A, %DefaultOrUnformed.facet [concrete = constants.%DefaultOrUnformed.facet]
  235. // CHECK:STDOUT: %as_type: type = facet_access_type %.loc10_21.1 [concrete = constants.%A]
  236. // CHECK:STDOUT: %.loc10_21.2: type = converted %.loc10_21.1, %as_type [concrete = constants.%A]
  237. // CHECK:STDOUT: %T.as.DefaultOrUnformed.impl.Op.specific_fn: <specific function> = specific_function constants.%T.as.DefaultOrUnformed.impl.Op.993, @T.as.DefaultOrUnformed.impl.Op(constants.%A) [concrete = constants.%T.as.DefaultOrUnformed.impl.Op.specific_fn]
  238. // CHECK:STDOUT: %.loc10_3: ref %A = splice_block %a.var {}
  239. // CHECK:STDOUT: %T.as.DefaultOrUnformed.impl.Op.call: init %A to %.loc10_3 = call %T.as.DefaultOrUnformed.impl.Op.specific_fn()
  240. // CHECK:STDOUT: assign %a.var, %T.as.DefaultOrUnformed.impl.Op.call
  241. // CHECK:STDOUT: %.loc10_19: type = splice_block %A.ref [concrete = constants.%A] {
  242. // CHECK:STDOUT: %Other.ref.loc10: <namespace> = name_ref Other, imports.%Other [concrete = imports.%Other]
  243. // CHECK:STDOUT: %NS1.ref: <namespace> = name_ref NS1, imports.%NS1.f3d [concrete = imports.%NS1.f3d]
  244. // CHECK:STDOUT: %A.ref: type = name_ref A, imports.%Other.A [concrete = constants.%A]
  245. // CHECK:STDOUT: }
  246. // CHECK:STDOUT: %a: ref %A = ref_binding a, %a.var
  247. // CHECK:STDOUT: %a.ref: ref %A = name_ref a, %a
  248. // CHECK:STDOUT: %Other.ref.loc13: <namespace> = name_ref Other, imports.%Other [concrete = imports.%Other]
  249. // CHECK:STDOUT: %F.ref.loc13: %F.type = name_ref F, imports.%Other.F [concrete = constants.%F]
  250. // CHECK:STDOUT: %.loc13: ref %A = splice_block %a.ref {}
  251. // CHECK:STDOUT: %F.call.loc13: init %A to %.loc13 = call %F.ref.loc13()
  252. // CHECK:STDOUT: assign %a.ref, %F.call.loc13
  253. // CHECK:STDOUT: %Destroy.Op.bound.loc10: <bound method> = bound_method %a.var, constants.%Destroy.Op
  254. // CHECK:STDOUT: %Destroy.Op.call.loc10: init %empty_tuple.type = call %Destroy.Op.bound.loc10(%a.var)
  255. // CHECK:STDOUT: %Destroy.Op.bound.loc7: <bound method> = bound_method %.loc7_11.2, constants.%Destroy.Op
  256. // CHECK:STDOUT: %Destroy.Op.call.loc7: init %empty_tuple.type = call %Destroy.Op.bound.loc7(%.loc7_11.2)
  257. // CHECK:STDOUT: return
  258. // CHECK:STDOUT: }
  259. // CHECK:STDOUT:
  260. // CHECK:STDOUT: fn @F [from "b.carbon"];
  261. // CHECK:STDOUT:
  262. // CHECK:STDOUT: fn @Destroy.Op(%self.param: ref %A) = "no_op";
  263. // CHECK:STDOUT: