transitive.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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/lookup/transitive.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/lookup/transitive.carbon
  10. // --- i.carbon
  11. library "[[@TEST_NAME]]";
  12. interface I { fn F(); }
  13. // --- c.carbon
  14. library "[[@TEST_NAME]]";
  15. import library "i";
  16. class C {}
  17. impl C as I { fn F() {} }
  18. // --- get.carbon
  19. library "[[@TEST_NAME]]";
  20. import library "c";
  21. fn Get() -> C;
  22. // --- call.carbon
  23. library "[[@TEST_NAME]]";
  24. import library "get";
  25. import library "i";
  26. fn Call() {
  27. // We find C in "get", but should look in "c" instead.
  28. Get().(I.F)();
  29. }
  30. // CHECK:STDOUT: --- i.carbon
  31. // CHECK:STDOUT:
  32. // CHECK:STDOUT: constants {
  33. // CHECK:STDOUT: %I.type: type = facet_type <@I> [template]
  34. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic]
  35. // CHECK:STDOUT: %F.type: type = fn_type @F [template]
  36. // CHECK:STDOUT: %F: %F.type = struct_value () [template]
  37. // CHECK:STDOUT: %I.assoc_type: type = assoc_entity_type %I.type [template]
  38. // CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, @I.%F.decl [template]
  39. // CHECK:STDOUT: }
  40. // CHECK:STDOUT:
  41. // CHECK:STDOUT: imports {
  42. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  43. // CHECK:STDOUT: import Core//prelude
  44. // CHECK:STDOUT: import Core//prelude/...
  45. // CHECK:STDOUT: }
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: file {
  49. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  50. // CHECK:STDOUT: .Core = imports.%Core
  51. // CHECK:STDOUT: .I = %I.decl
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT: %Core.import = import Core
  54. // CHECK:STDOUT: %I.decl: type = interface_decl @I [template = constants.%I.type] {} {}
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT:
  57. // CHECK:STDOUT: interface @I {
  58. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self]
  59. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [template = constants.%F] {} {}
  60. // CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, %F.decl [template = constants.%assoc0]
  61. // CHECK:STDOUT:
  62. // CHECK:STDOUT: !members:
  63. // CHECK:STDOUT: .Self = %Self
  64. // CHECK:STDOUT: .F = %assoc0
  65. // CHECK:STDOUT: witness = (%F.decl)
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: generic fn @F(@I.%Self: %I.type) {
  69. // CHECK:STDOUT: fn();
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: specific @F(constants.%Self) {}
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: --- c.carbon
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: constants {
  77. // CHECK:STDOUT: %C: type = class_type @C [template]
  78. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  79. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  80. // CHECK:STDOUT: %I.type: type = facet_type <@I> [template]
  81. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic]
  82. // CHECK:STDOUT: %F.type.cf0: type = fn_type @F.1 [template]
  83. // CHECK:STDOUT: %F.bc6: %F.type.cf0 = struct_value () [template]
  84. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness (@impl.%F.decl) [template]
  85. // CHECK:STDOUT: %F.type.5d6: type = fn_type @F.2 [template]
  86. // CHECK:STDOUT: %F.a2e: %F.type.5d6 = struct_value () [template]
  87. // CHECK:STDOUT: %I.facet: %I.type = facet_value %C, %impl_witness [template]
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: imports {
  91. // CHECK:STDOUT: %Main.I: type = import_ref Main//i, I, loaded [template = constants.%I.type]
  92. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  93. // CHECK:STDOUT: import Core//prelude
  94. // CHECK:STDOUT: import Core//prelude/...
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT: %Main.import_ref.e5d = import_ref Main//i, inst17 [no loc], unloaded
  97. // CHECK:STDOUT: %Main.import_ref.1e0 = import_ref Main//i, loc4_21, unloaded
  98. // CHECK:STDOUT: %Main.F: %F.type.cf0 = import_ref Main//i, F, loaded [template = constants.%F.bc6]
  99. // CHECK:STDOUT: %Main.import_ref.5dd: %I.type = import_ref Main//i, inst17 [no loc], loaded [symbolic = constants.%Self]
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: file {
  103. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  104. // CHECK:STDOUT: .I = imports.%Main.I
  105. // CHECK:STDOUT: .Core = imports.%Core
  106. // CHECK:STDOUT: .C = %C.decl
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT: %Core.import = import Core
  109. // CHECK:STDOUT: %default.import = import <none>
  110. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {} {}
  111. // CHECK:STDOUT: impl_decl @impl [template] {} {
  112. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  113. // CHECK:STDOUT: %I.ref: type = name_ref I, imports.%Main.I [template = constants.%I.type]
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness (@impl.%F.decl) [template = constants.%impl_witness]
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: interface @I [from "i.carbon"] {
  119. // CHECK:STDOUT: !members:
  120. // CHECK:STDOUT: .Self = imports.%Main.import_ref.e5d
  121. // CHECK:STDOUT: .F = imports.%Main.import_ref.1e0
  122. // CHECK:STDOUT: witness = (imports.%Main.F)
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: impl @impl: %C.ref as %I.ref {
  126. // CHECK:STDOUT: %F.decl: %F.type.5d6 = fn_decl @F.2 [template = constants.%F.a2e] {} {}
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: !members:
  129. // CHECK:STDOUT: .F = %F.decl
  130. // CHECK:STDOUT: witness = file.%impl_witness
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: class @C {
  134. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  135. // CHECK:STDOUT: complete_type_witness = %complete_type
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: !members:
  138. // CHECK:STDOUT: .Self = constants.%C
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: generic fn @F.1(imports.%Main.import_ref.5dd: %I.type) [from "i.carbon"] {
  142. // CHECK:STDOUT: fn();
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT:
  145. // CHECK:STDOUT: fn @F.2() {
  146. // CHECK:STDOUT: !entry:
  147. // CHECK:STDOUT: return
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: specific @F.1(constants.%Self) {}
  151. // CHECK:STDOUT:
  152. // CHECK:STDOUT: specific @F.1(constants.%I.facet) {}
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: --- get.carbon
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: constants {
  157. // CHECK:STDOUT: %C: type = class_type @C [template]
  158. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  159. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  160. // CHECK:STDOUT: %Get.type: type = fn_type @Get [template]
  161. // CHECK:STDOUT: %Get: %Get.type = struct_value () [template]
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: imports {
  165. // CHECK:STDOUT: %Main.C: type = import_ref Main//c, C, loaded [template = constants.%C]
  166. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  167. // CHECK:STDOUT: import Core//prelude
  168. // CHECK:STDOUT: import Core//prelude/...
  169. // CHECK:STDOUT: }
  170. // CHECK:STDOUT: %Main.import_ref.8f2: <witness> = import_ref Main//c, loc6_10, loaded [template = constants.%complete_type]
  171. // CHECK:STDOUT: %Main.import_ref.2c4 = import_ref Main//c, inst18 [no loc], unloaded
  172. // CHECK:STDOUT: }
  173. // CHECK:STDOUT:
  174. // CHECK:STDOUT: file {
  175. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  176. // CHECK:STDOUT: .C = imports.%Main.C
  177. // CHECK:STDOUT: .Core = imports.%Core
  178. // CHECK:STDOUT: .Get = %Get.decl
  179. // CHECK:STDOUT: }
  180. // CHECK:STDOUT: %Core.import = import Core
  181. // CHECK:STDOUT: %default.import = import <none>
  182. // CHECK:STDOUT: %Get.decl: %Get.type = fn_decl @Get [template = constants.%Get] {
  183. // CHECK:STDOUT: %return.patt: %C = return_slot_pattern
  184. // CHECK:STDOUT: %return.param_patt: %C = out_param_pattern %return.patt, runtime_param0
  185. // CHECK:STDOUT: } {
  186. // CHECK:STDOUT: %C.ref: type = name_ref C, imports.%Main.C [template = constants.%C]
  187. // CHECK:STDOUT: %return.param: ref %C = out_param runtime_param0
  188. // CHECK:STDOUT: %return: ref %C = return_slot %return.param
  189. // CHECK:STDOUT: }
  190. // CHECK:STDOUT: }
  191. // CHECK:STDOUT:
  192. // CHECK:STDOUT: class @C [from "c.carbon"] {
  193. // CHECK:STDOUT: complete_type_witness = imports.%Main.import_ref.8f2
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: !members:
  196. // CHECK:STDOUT: .Self = imports.%Main.import_ref.2c4
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: fn @Get() -> %C;
  200. // CHECK:STDOUT:
  201. // CHECK:STDOUT: --- call.carbon
  202. // CHECK:STDOUT:
  203. // CHECK:STDOUT: constants {
  204. // CHECK:STDOUT: %Call.type: type = fn_type @Call [template]
  205. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [template]
  206. // CHECK:STDOUT: %Call: %Call.type = struct_value () [template]
  207. // CHECK:STDOUT: %Get.type: type = fn_type @Get [template]
  208. // CHECK:STDOUT: %Get: %Get.type = struct_value () [template]
  209. // CHECK:STDOUT: %C: type = class_type @C [template]
  210. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  211. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  212. // CHECK:STDOUT: %I.type: type = facet_type <@I> [template]
  213. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic]
  214. // CHECK:STDOUT: %I.assoc_type: type = assoc_entity_type %I.type [template]
  215. // CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, imports.%Main.import_ref.e03 [template]
  216. // CHECK:STDOUT: %impl_witness: <witness> = impl_witness (imports.%Main.import_ref.742) [template]
  217. // CHECK:STDOUT: %F.type.cf0: type = fn_type @F.1 [template]
  218. // CHECK:STDOUT: %I.facet: %I.type = facet_value %C, %impl_witness [template]
  219. // CHECK:STDOUT: %.076: type = fn_type_with_self_type %F.type.cf0, %I.facet [template]
  220. // CHECK:STDOUT: %F.type.5d6: type = fn_type @F.2 [template]
  221. // CHECK:STDOUT: %F.a2e: %F.type.5d6 = struct_value () [template]
  222. // CHECK:STDOUT: }
  223. // CHECK:STDOUT:
  224. // CHECK:STDOUT: imports {
  225. // CHECK:STDOUT: %Main.Get: %Get.type = import_ref Main//get, Get, loaded [template = constants.%Get]
  226. // CHECK:STDOUT: %Main.I: type = import_ref Main//i, I, loaded [template = constants.%I.type]
  227. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  228. // CHECK:STDOUT: import Core//prelude
  229. // CHECK:STDOUT: import Core//prelude/...
  230. // CHECK:STDOUT: }
  231. // CHECK:STDOUT: %Main.import_ref.8db: <witness> = import_ref Main//get, inst21 [indirect], loaded [template = constants.%complete_type]
  232. // CHECK:STDOUT: %Main.import_ref.6a9 = import_ref Main//get, inst22 [indirect], unloaded
  233. // CHECK:STDOUT: %Main.import_ref.e5d = import_ref Main//i, inst17 [no loc], unloaded
  234. // CHECK:STDOUT: %Main.import_ref.bcb: %I.assoc_type = import_ref Main//i, loc4_21, loaded [template = constants.%assoc0]
  235. // CHECK:STDOUT: %Main.F = import_ref Main//i, F, unloaded
  236. // CHECK:STDOUT: %Main.import_ref.e53: <witness> = import_ref Main//c, loc7_13, loaded [template = constants.%impl_witness]
  237. // CHECK:STDOUT: %Main.import_ref.29a: type = import_ref Main//c, loc7_6, loaded [template = constants.%C]
  238. // CHECK:STDOUT: %Main.import_ref.f50: type = import_ref Main//c, loc7_11, loaded [template = constants.%I.type]
  239. // CHECK:STDOUT: %Main.import_ref.5dd: %I.type = import_ref Main//i, inst17 [no loc], loaded [symbolic = constants.%Self]
  240. // CHECK:STDOUT: }
  241. // CHECK:STDOUT:
  242. // CHECK:STDOUT: file {
  243. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  244. // CHECK:STDOUT: .Get = imports.%Main.Get
  245. // CHECK:STDOUT: .I = imports.%Main.I
  246. // CHECK:STDOUT: .Core = imports.%Core
  247. // CHECK:STDOUT: .Call = %Call.decl
  248. // CHECK:STDOUT: }
  249. // CHECK:STDOUT: %Core.import = import Core
  250. // CHECK:STDOUT: %default.import = import <none>
  251. // CHECK:STDOUT: %Call.decl: %Call.type = fn_decl @Call [template = constants.%Call] {} {}
  252. // CHECK:STDOUT: }
  253. // CHECK:STDOUT:
  254. // CHECK:STDOUT: interface @I [from "i.carbon"] {
  255. // CHECK:STDOUT: !members:
  256. // CHECK:STDOUT: .Self = imports.%Main.import_ref.e5d
  257. // CHECK:STDOUT: .F = imports.%Main.import_ref.bcb
  258. // CHECK:STDOUT: witness = (imports.%Main.F)
  259. // CHECK:STDOUT: }
  260. // CHECK:STDOUT:
  261. // CHECK:STDOUT: impl @impl: imports.%Main.import_ref.29a as imports.%Main.import_ref.f50 [from "c.carbon"] {
  262. // CHECK:STDOUT: !members:
  263. // CHECK:STDOUT: witness = imports.%Main.import_ref.e53
  264. // CHECK:STDOUT: }
  265. // CHECK:STDOUT:
  266. // CHECK:STDOUT: class @C [from "get.carbon"] {
  267. // CHECK:STDOUT: complete_type_witness = imports.%Main.import_ref.8db
  268. // CHECK:STDOUT:
  269. // CHECK:STDOUT: !members:
  270. // CHECK:STDOUT: .Self = imports.%Main.import_ref.6a9
  271. // CHECK:STDOUT: }
  272. // CHECK:STDOUT:
  273. // CHECK:STDOUT: fn @Call() {
  274. // CHECK:STDOUT: !entry:
  275. // CHECK:STDOUT: %Get.ref: %Get.type = name_ref Get, imports.%Main.Get [template = constants.%Get]
  276. // CHECK:STDOUT: %.loc9: ref %C = temporary_storage
  277. // CHECK:STDOUT: %Get.call: init %C = call %Get.ref() to %.loc9
  278. // CHECK:STDOUT: %I.ref: type = name_ref I, imports.%Main.I [template = constants.%I.type]
  279. // CHECK:STDOUT: %F.ref: %I.assoc_type = name_ref F, imports.%Main.import_ref.bcb [template = constants.%assoc0]
  280. // CHECK:STDOUT: %impl.elem0: %.076 = impl_witness_access constants.%impl_witness, element0 [template = constants.%F.a2e]
  281. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %impl.elem0()
  282. // CHECK:STDOUT: return
  283. // CHECK:STDOUT: }
  284. // CHECK:STDOUT:
  285. // CHECK:STDOUT: fn @Get() -> %C [from "get.carbon"];
  286. // CHECK:STDOUT:
  287. // CHECK:STDOUT: generic fn @F.1(imports.%Main.import_ref.5dd: %I.type) [from "i.carbon"] {
  288. // CHECK:STDOUT: fn();
  289. // CHECK:STDOUT: }
  290. // CHECK:STDOUT:
  291. // CHECK:STDOUT: fn @F.2() [from "c.carbon"];
  292. // CHECK:STDOUT:
  293. // CHECK:STDOUT: specific @F.1(constants.%Self) {}
  294. // CHECK:STDOUT: