import.carbon 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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/interface/no_prelude/import.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interface/no_prelude/import.carbon
  10. // --- a.carbon
  11. library "[[@TEST_NAME]]";
  12. interface Empty {
  13. }
  14. interface Basic {
  15. let T:! type;
  16. fn F();
  17. }
  18. // TODO: Implement merging of interface on import.
  19. // interface ForwardDeclared;
  20. interface ForwardDeclared {
  21. let T:! type;
  22. fn F();
  23. }
  24. var f_ref: {.f: ForwardDeclared};
  25. // --- b.carbon
  26. library "[[@TEST_NAME]]";
  27. import library "a";
  28. fn UseEmpty(e: Empty) {}
  29. fn UseBasic(e: Basic) {}
  30. fn UseForwardDeclared(f: ForwardDeclared) {}
  31. alias UseBasicT = Basic.T;
  32. alias UseBasicF = Basic.F;
  33. alias UseForwardDeclaredT = ForwardDeclared.T;
  34. alias UseForwardDeclaredF = ForwardDeclared.F;
  35. var f: ForwardDeclared* = &f_ref.f;
  36. // CHECK:STDOUT: --- a.carbon
  37. // CHECK:STDOUT:
  38. // CHECK:STDOUT: constants {
  39. // CHECK:STDOUT: %Empty.type: type = facet_type <@Empty> [template]
  40. // CHECK:STDOUT: %Self.e1a: %Empty.type = bind_symbolic_name Self, 0 [symbolic]
  41. // CHECK:STDOUT: %Basic.type: type = facet_type <@Basic> [template]
  42. // CHECK:STDOUT: %Self.09c: %Basic.type = bind_symbolic_name Self, 0 [symbolic]
  43. // CHECK:STDOUT: %assoc_type.6af: type = assoc_entity_type %Basic.type, type [template]
  44. // CHECK:STDOUT: %assoc0.d6f: %assoc_type.6af = assoc_entity element0, @Basic.%T [template]
  45. // CHECK:STDOUT: %F.type.def: type = fn_type @F.1 [template]
  46. // CHECK:STDOUT: %F.415: %F.type.def = struct_value () [template]
  47. // CHECK:STDOUT: %F.assoc_type.4b0: type = assoc_entity_type %Basic.type, %F.type.def [template]
  48. // CHECK:STDOUT: %assoc1.ffc: %F.assoc_type.4b0 = assoc_entity element1, @Basic.%F.decl [template]
  49. // CHECK:STDOUT: %ForwardDeclared.type: type = facet_type <@ForwardDeclared> [template]
  50. // CHECK:STDOUT: %Self.975: %ForwardDeclared.type = bind_symbolic_name Self, 0 [symbolic]
  51. // CHECK:STDOUT: %assoc_type.3e8: type = assoc_entity_type %ForwardDeclared.type, type [template]
  52. // CHECK:STDOUT: %assoc0.bef: %assoc_type.3e8 = assoc_entity element0, @ForwardDeclared.%T [template]
  53. // CHECK:STDOUT: %F.type.a8f: type = fn_type @F.2 [template]
  54. // CHECK:STDOUT: %F.ef7: %F.type.a8f = struct_value () [template]
  55. // CHECK:STDOUT: %F.assoc_type.cf5: type = assoc_entity_type %ForwardDeclared.type, %F.type.a8f [template]
  56. // CHECK:STDOUT: %assoc1.763: %F.assoc_type.cf5 = assoc_entity element1, @ForwardDeclared.%F.decl [template]
  57. // CHECK:STDOUT: %struct_type.f.2f5: type = struct_type {.f: %ForwardDeclared.type} [template]
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT:
  60. // CHECK:STDOUT: file {
  61. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  62. // CHECK:STDOUT: .Empty = %Empty.decl
  63. // CHECK:STDOUT: .Basic = %Basic.decl
  64. // CHECK:STDOUT: .ForwardDeclared = %ForwardDeclared.decl
  65. // CHECK:STDOUT: .f_ref = %f_ref
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT: %Empty.decl: type = interface_decl @Empty [template = constants.%Empty.type] {} {}
  68. // CHECK:STDOUT: %Basic.decl: type = interface_decl @Basic [template = constants.%Basic.type] {} {}
  69. // CHECK:STDOUT: %ForwardDeclared.decl: type = interface_decl @ForwardDeclared [template = constants.%ForwardDeclared.type] {} {}
  70. // CHECK:STDOUT: %f_ref.var: ref %struct_type.f.2f5 = var f_ref
  71. // CHECK:STDOUT: %f_ref: ref %struct_type.f.2f5 = bind_name f_ref, %f_ref.var
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: interface @Empty {
  75. // CHECK:STDOUT: %Self: %Empty.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self.e1a]
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: !members:
  78. // CHECK:STDOUT: .Self = %Self
  79. // CHECK:STDOUT: witness = ()
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: interface @Basic {
  83. // CHECK:STDOUT: %Self: %Basic.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self.09c]
  84. // CHECK:STDOUT: %T: type = assoc_const_decl T [template]
  85. // CHECK:STDOUT: %assoc0: %assoc_type.6af = assoc_entity element0, %T [template = constants.%assoc0.d6f]
  86. // CHECK:STDOUT: %F.decl: %F.type.def = fn_decl @F.1 [template = constants.%F.415] {} {}
  87. // CHECK:STDOUT: %assoc1: %F.assoc_type.4b0 = assoc_entity element1, %F.decl [template = constants.%assoc1.ffc]
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: !members:
  90. // CHECK:STDOUT: .Self = %Self
  91. // CHECK:STDOUT: .T = %assoc0
  92. // CHECK:STDOUT: .F = %assoc1
  93. // CHECK:STDOUT: witness = (%T, %F.decl)
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: interface @ForwardDeclared {
  97. // CHECK:STDOUT: %Self: %ForwardDeclared.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self.975]
  98. // CHECK:STDOUT: %T: type = assoc_const_decl T [template]
  99. // CHECK:STDOUT: %assoc0: %assoc_type.3e8 = assoc_entity element0, %T [template = constants.%assoc0.bef]
  100. // CHECK:STDOUT: %F.decl: %F.type.a8f = fn_decl @F.2 [template = constants.%F.ef7] {} {}
  101. // CHECK:STDOUT: %assoc1: %F.assoc_type.cf5 = assoc_entity element1, %F.decl [template = constants.%assoc1.763]
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: !members:
  104. // CHECK:STDOUT: .Self = %Self
  105. // CHECK:STDOUT: .T = %assoc0
  106. // CHECK:STDOUT: .F = %assoc1
  107. // CHECK:STDOUT: witness = (%T, %F.decl)
  108. // CHECK:STDOUT: }
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: generic fn @F.1(@Basic.%Self: %Basic.type) {
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: fn();
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: generic fn @F.2(@ForwardDeclared.%Self: %ForwardDeclared.type) {
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: fn();
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: specific @F.1(constants.%Self.09c) {}
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: specific @F.2(constants.%Self.975) {}
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: --- b.carbon
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: constants {
  127. // CHECK:STDOUT: %Empty.type: type = facet_type <@Empty> [template]
  128. // CHECK:STDOUT: %UseEmpty.type: type = fn_type @UseEmpty [template]
  129. // CHECK:STDOUT: %UseEmpty: %UseEmpty.type = struct_value () [template]
  130. // CHECK:STDOUT: %Basic.type: type = facet_type <@Basic> [template]
  131. // CHECK:STDOUT: %Self.09c: %Basic.type = bind_symbolic_name Self, 0 [symbolic]
  132. // CHECK:STDOUT: %UseBasic.type: type = fn_type @UseBasic [template]
  133. // CHECK:STDOUT: %UseBasic: %UseBasic.type = struct_value () [template]
  134. // CHECK:STDOUT: %ForwardDeclared.type: type = facet_type <@ForwardDeclared> [template]
  135. // CHECK:STDOUT: %Self.975: %ForwardDeclared.type = bind_symbolic_name Self, 0 [symbolic]
  136. // CHECK:STDOUT: %UseForwardDeclared.type: type = fn_type @UseForwardDeclared [template]
  137. // CHECK:STDOUT: %UseForwardDeclared: %UseForwardDeclared.type = struct_value () [template]
  138. // CHECK:STDOUT: %assoc_type.6af: type = assoc_entity_type %Basic.type, type [template]
  139. // CHECK:STDOUT: %assoc0.1d6: %assoc_type.6af = assoc_entity element0, imports.%import_ref.ef4597.1 [template]
  140. // CHECK:STDOUT: %F.type.def: type = fn_type @F.1 [template]
  141. // CHECK:STDOUT: %F.assoc_type.4b0: type = assoc_entity_type %Basic.type, %F.type.def [template]
  142. // CHECK:STDOUT: %assoc1.51a: %F.assoc_type.4b0 = assoc_entity element1, imports.%import_ref.9c3 [template]
  143. // CHECK:STDOUT: %assoc_type.3e8: type = assoc_entity_type %ForwardDeclared.type, type [template]
  144. // CHECK:STDOUT: %assoc0.910: %assoc_type.3e8 = assoc_entity element0, imports.%import_ref.ef4597.2 [template]
  145. // CHECK:STDOUT: %F.type.a8f: type = fn_type @F.2 [template]
  146. // CHECK:STDOUT: %F.assoc_type.cf5: type = assoc_entity_type %ForwardDeclared.type, %F.type.a8f [template]
  147. // CHECK:STDOUT: %assoc1.975: %F.assoc_type.cf5 = assoc_entity element1, imports.%import_ref.9d1 [template]
  148. // CHECK:STDOUT: %ptr: type = ptr_type %ForwardDeclared.type [template]
  149. // CHECK:STDOUT: %struct_type.f.2f5: type = struct_type {.f: %ForwardDeclared.type} [template]
  150. // CHECK:STDOUT: }
  151. // CHECK:STDOUT:
  152. // CHECK:STDOUT: imports {
  153. // CHECK:STDOUT: %import_ref.db6: type = import_ref Main//a, Empty, loaded [template = constants.%Empty.type]
  154. // CHECK:STDOUT: %import_ref.73d: type = import_ref Main//a, Basic, loaded [template = constants.%Basic.type]
  155. // CHECK:STDOUT: %import_ref.1f1: type = import_ref Main//a, ForwardDeclared, loaded [template = constants.%ForwardDeclared.type]
  156. // CHECK:STDOUT: %import_ref.2ac: ref %struct_type.f.2f5 = import_ref Main//a, f_ref, loaded
  157. // CHECK:STDOUT: %import_ref.77a = import_ref Main//a, inst15 [no loc], unloaded
  158. // CHECK:STDOUT: %import_ref.5e6 = import_ref Main//a, inst19 [no loc], unloaded
  159. // CHECK:STDOUT: %import_ref.42e: %assoc_type.6af = import_ref Main//a, loc8_15, loaded [template = constants.%assoc0.1d6]
  160. // CHECK:STDOUT: %import_ref.52d: %F.assoc_type.4b0 = import_ref Main//a, loc9_9, loaded [template = constants.%assoc1.51a]
  161. // CHECK:STDOUT: %import_ref.08d600.1 = import_ref Main//a, T, unloaded
  162. // CHECK:STDOUT: %import_ref.b85 = import_ref Main//a, F, unloaded
  163. // CHECK:STDOUT: %import_ref.566 = import_ref Main//a, inst34 [no loc], unloaded
  164. // CHECK:STDOUT: %import_ref.95f: %assoc_type.3e8 = import_ref Main//a, loc16_15, loaded [template = constants.%assoc0.910]
  165. // CHECK:STDOUT: %import_ref.1d4: %F.assoc_type.cf5 = import_ref Main//a, loc17_9, loaded [template = constants.%assoc1.975]
  166. // CHECK:STDOUT: %import_ref.08d600.2 = import_ref Main//a, T, unloaded
  167. // CHECK:STDOUT: %import_ref.c2e = import_ref Main//a, F, unloaded
  168. // CHECK:STDOUT: }
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: file {
  171. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  172. // CHECK:STDOUT: .Empty = imports.%import_ref.db6
  173. // CHECK:STDOUT: .Basic = imports.%import_ref.73d
  174. // CHECK:STDOUT: .ForwardDeclared = imports.%import_ref.1f1
  175. // CHECK:STDOUT: .f_ref = imports.%import_ref.2ac
  176. // CHECK:STDOUT: .UseEmpty = %UseEmpty.decl
  177. // CHECK:STDOUT: .UseBasic = %UseBasic.decl
  178. // CHECK:STDOUT: .UseForwardDeclared = %UseForwardDeclared.decl
  179. // CHECK:STDOUT: .UseBasicT = %UseBasicT
  180. // CHECK:STDOUT: .UseBasicF = %UseBasicF
  181. // CHECK:STDOUT: .UseForwardDeclaredT = %UseForwardDeclaredT
  182. // CHECK:STDOUT: .UseForwardDeclaredF = %UseForwardDeclaredF
  183. // CHECK:STDOUT: .f = %f
  184. // CHECK:STDOUT: }
  185. // CHECK:STDOUT: %default.import = import <invalid>
  186. // CHECK:STDOUT: %UseEmpty.decl: %UseEmpty.type = fn_decl @UseEmpty [template = constants.%UseEmpty] {
  187. // CHECK:STDOUT: %e.patt: %Empty.type = binding_pattern e
  188. // CHECK:STDOUT: %e.param_patt: %Empty.type = value_param_pattern %e.patt, runtime_param0
  189. // CHECK:STDOUT: } {
  190. // CHECK:STDOUT: %e.param: %Empty.type = value_param runtime_param0
  191. // CHECK:STDOUT: %Empty.ref: type = name_ref Empty, imports.%import_ref.db6 [template = constants.%Empty.type]
  192. // CHECK:STDOUT: %e: %Empty.type = bind_name e, %e.param
  193. // CHECK:STDOUT: }
  194. // CHECK:STDOUT: %UseBasic.decl: %UseBasic.type = fn_decl @UseBasic [template = constants.%UseBasic] {
  195. // CHECK:STDOUT: %e.patt: %Basic.type = binding_pattern e
  196. // CHECK:STDOUT: %e.param_patt: %Basic.type = value_param_pattern %e.patt, runtime_param0
  197. // CHECK:STDOUT: } {
  198. // CHECK:STDOUT: %e.param: %Basic.type = value_param runtime_param0
  199. // CHECK:STDOUT: %Basic.ref: type = name_ref Basic, imports.%import_ref.73d [template = constants.%Basic.type]
  200. // CHECK:STDOUT: %e: %Basic.type = bind_name e, %e.param
  201. // CHECK:STDOUT: }
  202. // CHECK:STDOUT: %UseForwardDeclared.decl: %UseForwardDeclared.type = fn_decl @UseForwardDeclared [template = constants.%UseForwardDeclared] {
  203. // CHECK:STDOUT: %f.patt: %ForwardDeclared.type = binding_pattern f
  204. // CHECK:STDOUT: %f.param_patt: %ForwardDeclared.type = value_param_pattern %f.patt, runtime_param0
  205. // CHECK:STDOUT: } {
  206. // CHECK:STDOUT: %f.param: %ForwardDeclared.type = value_param runtime_param0
  207. // CHECK:STDOUT: %ForwardDeclared.ref: type = name_ref ForwardDeclared, imports.%import_ref.1f1 [template = constants.%ForwardDeclared.type]
  208. // CHECK:STDOUT: %f: %ForwardDeclared.type = bind_name f, %f.param
  209. // CHECK:STDOUT: }
  210. // CHECK:STDOUT: %Basic.ref.loc10: type = name_ref Basic, imports.%import_ref.73d [template = constants.%Basic.type]
  211. // CHECK:STDOUT: %T.ref.loc10: %assoc_type.6af = name_ref T, imports.%import_ref.42e [template = constants.%assoc0.1d6]
  212. // CHECK:STDOUT: %UseBasicT: %assoc_type.6af = bind_alias UseBasicT, imports.%import_ref.42e [template = constants.%assoc0.1d6]
  213. // CHECK:STDOUT: %Basic.ref.loc11: type = name_ref Basic, imports.%import_ref.73d [template = constants.%Basic.type]
  214. // CHECK:STDOUT: %F.ref.loc11: %F.assoc_type.4b0 = name_ref F, imports.%import_ref.52d [template = constants.%assoc1.51a]
  215. // CHECK:STDOUT: %UseBasicF: %F.assoc_type.4b0 = bind_alias UseBasicF, imports.%import_ref.52d [template = constants.%assoc1.51a]
  216. // CHECK:STDOUT: %ForwardDeclared.ref.loc13: type = name_ref ForwardDeclared, imports.%import_ref.1f1 [template = constants.%ForwardDeclared.type]
  217. // CHECK:STDOUT: %T.ref.loc13: %assoc_type.3e8 = name_ref T, imports.%import_ref.95f [template = constants.%assoc0.910]
  218. // CHECK:STDOUT: %UseForwardDeclaredT: %assoc_type.3e8 = bind_alias UseForwardDeclaredT, imports.%import_ref.95f [template = constants.%assoc0.910]
  219. // CHECK:STDOUT: %ForwardDeclared.ref.loc14: type = name_ref ForwardDeclared, imports.%import_ref.1f1 [template = constants.%ForwardDeclared.type]
  220. // CHECK:STDOUT: %F.ref.loc14: %F.assoc_type.cf5 = name_ref F, imports.%import_ref.1d4 [template = constants.%assoc1.975]
  221. // CHECK:STDOUT: %UseForwardDeclaredF: %F.assoc_type.cf5 = bind_alias UseForwardDeclaredF, imports.%import_ref.1d4 [template = constants.%assoc1.975]
  222. // CHECK:STDOUT: %f.var: ref %ptr = var f
  223. // CHECK:STDOUT: %f: ref %ptr = bind_name f, %f.var
  224. // CHECK:STDOUT: }
  225. // CHECK:STDOUT:
  226. // CHECK:STDOUT: interface @Empty [from "a.carbon"] {
  227. // CHECK:STDOUT: !members:
  228. // CHECK:STDOUT: .Self = imports.%import_ref.77a
  229. // CHECK:STDOUT: witness = ()
  230. // CHECK:STDOUT: }
  231. // CHECK:STDOUT:
  232. // CHECK:STDOUT: interface @Basic [from "a.carbon"] {
  233. // CHECK:STDOUT: !members:
  234. // CHECK:STDOUT: .Self = imports.%import_ref.5e6
  235. // CHECK:STDOUT: .T = imports.%import_ref.42e
  236. // CHECK:STDOUT: .F = imports.%import_ref.52d
  237. // CHECK:STDOUT: witness = (imports.%import_ref.08d600.1, imports.%import_ref.b85)
  238. // CHECK:STDOUT: }
  239. // CHECK:STDOUT:
  240. // CHECK:STDOUT: interface @ForwardDeclared [from "a.carbon"] {
  241. // CHECK:STDOUT: !members:
  242. // CHECK:STDOUT: .Self = imports.%import_ref.566
  243. // CHECK:STDOUT: .T = imports.%import_ref.95f
  244. // CHECK:STDOUT: .F = imports.%import_ref.1d4
  245. // CHECK:STDOUT: witness = (imports.%import_ref.08d600.2, imports.%import_ref.c2e)
  246. // CHECK:STDOUT: }
  247. // CHECK:STDOUT:
  248. // CHECK:STDOUT: fn @UseEmpty(%e.param_patt: %Empty.type) {
  249. // CHECK:STDOUT: !entry:
  250. // CHECK:STDOUT: return
  251. // CHECK:STDOUT: }
  252. // CHECK:STDOUT:
  253. // CHECK:STDOUT: fn @UseBasic(%e.param_patt: %Basic.type) {
  254. // CHECK:STDOUT: !entry:
  255. // CHECK:STDOUT: return
  256. // CHECK:STDOUT: }
  257. // CHECK:STDOUT:
  258. // CHECK:STDOUT: fn @UseForwardDeclared(%f.param_patt: %ForwardDeclared.type) {
  259. // CHECK:STDOUT: !entry:
  260. // CHECK:STDOUT: return
  261. // CHECK:STDOUT: }
  262. // CHECK:STDOUT:
  263. // CHECK:STDOUT: generic fn @F.1(constants.%Self.09c: %Basic.type) [from "a.carbon"] {
  264. // CHECK:STDOUT:
  265. // CHECK:STDOUT: fn();
  266. // CHECK:STDOUT: }
  267. // CHECK:STDOUT:
  268. // CHECK:STDOUT: generic fn @F.2(constants.%Self.975: %ForwardDeclared.type) [from "a.carbon"] {
  269. // CHECK:STDOUT:
  270. // CHECK:STDOUT: fn();
  271. // CHECK:STDOUT: }
  272. // CHECK:STDOUT:
  273. // CHECK:STDOUT: fn @__global_init() {
  274. // CHECK:STDOUT: !entry:
  275. // CHECK:STDOUT: %f_ref.ref: ref %struct_type.f.2f5 = name_ref f_ref, imports.%import_ref.2ac
  276. // CHECK:STDOUT: %.loc16: ref %ForwardDeclared.type = struct_access %f_ref.ref, element0
  277. // CHECK:STDOUT: %addr: %ptr = addr_of %.loc16
  278. // CHECK:STDOUT: assign file.%f.var, %addr
  279. // CHECK:STDOUT: return
  280. // CHECK:STDOUT: }
  281. // CHECK:STDOUT:
  282. // CHECK:STDOUT: specific @F.1(constants.%Self.09c) {}
  283. // CHECK:STDOUT:
  284. // CHECK:STDOUT: specific @F.2(constants.%Self.975) {}
  285. // CHECK:STDOUT: