export_name.carbon 12 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/alias/no_prelude/export_name.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/alias/no_prelude/export_name.carbon
  10. // ============================================================================
  11. // Setup files
  12. // ============================================================================
  13. // --- base.carbon
  14. library "[[@TEST_NAME]]";
  15. class C {}
  16. alias D = C;
  17. // --- export.carbon
  18. library "[[@TEST_NAME]]";
  19. import library "base";
  20. export D;
  21. // --- export_orig.carbon
  22. library "[[@TEST_NAME]]";
  23. import library "base";
  24. export C;
  25. // ============================================================================
  26. // Test files
  27. // ============================================================================
  28. // --- use_export.carbon
  29. library "[[@TEST_NAME]]";
  30. import library "export";
  31. var d: D = {};
  32. // --- fail_orig_name_not_in_export.carbon
  33. library "[[@TEST_NAME]]";
  34. import library "export";
  35. // CHECK:STDERR: fail_orig_name_not_in_export.carbon:[[@LINE+4]]:8: error: name `C` not found [NameNotFound]
  36. // CHECK:STDERR: var c: C = {};
  37. // CHECK:STDERR: ^
  38. // CHECK:STDERR:
  39. var c: C = {};
  40. // --- indirect_compat.carbon
  41. library "[[@TEST_NAME]]";
  42. import library "export";
  43. import library "export_orig";
  44. var c: C = {};
  45. var d: D* = &c;
  46. // CHECK:STDOUT: --- base.carbon
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: constants {
  49. // CHECK:STDOUT: %C: type = class_type @C [template]
  50. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  51. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT:
  54. // CHECK:STDOUT: file {
  55. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  56. // CHECK:STDOUT: .C = %C.decl
  57. // CHECK:STDOUT: .D = %D
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {} {}
  60. // CHECK:STDOUT: %C.ref: type = name_ref C, %C.decl [template = constants.%C]
  61. // CHECK:STDOUT: %D: type = bind_alias D, %C.decl [template = constants.%C]
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: class @C {
  65. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template = constants.%complete_type]
  66. // CHECK:STDOUT: complete_type_witness = %complete_type
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: !members:
  69. // CHECK:STDOUT: .Self = constants.%C
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: --- export.carbon
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: constants {
  75. // CHECK:STDOUT: %C: type = class_type @C [template]
  76. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  77. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: imports {
  81. // CHECK:STDOUT: %Main.C = import_ref Main//base, C, unloaded
  82. // CHECK:STDOUT: %Main.D: type = import_ref Main//base, D, loaded [template = constants.%C]
  83. // CHECK:STDOUT: %Main.import_ref.8f2: <witness> = import_ref Main//base, loc4_10, loaded [template = constants.%complete_type]
  84. // CHECK:STDOUT: %Main.import_ref.2c4 = import_ref Main//base, inst14 [no loc], unloaded
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: file {
  88. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  89. // CHECK:STDOUT: .C = imports.%Main.C
  90. // CHECK:STDOUT: .D = %D
  91. // CHECK:STDOUT: }
  92. // CHECK:STDOUT: %default.import = import <none>
  93. // CHECK:STDOUT: %D: type = export D, imports.%Main.D [template = constants.%C]
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: class @C [from "base.carbon"] {
  97. // CHECK:STDOUT: complete_type_witness = imports.%Main.import_ref.8f2
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: !members:
  100. // CHECK:STDOUT: .Self = imports.%Main.import_ref.2c4
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: --- export_orig.carbon
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: constants {
  106. // CHECK:STDOUT: %C: type = class_type @C [template]
  107. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  108. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: imports {
  112. // CHECK:STDOUT: %Main.C: type = import_ref Main//base, C, loaded [template = constants.%C]
  113. // CHECK:STDOUT: %Main.D = import_ref Main//base, D, unloaded
  114. // CHECK:STDOUT: %Main.import_ref.8f2: <witness> = import_ref Main//base, loc4_10, loaded [template = constants.%complete_type]
  115. // CHECK:STDOUT: %Main.import_ref.2c4 = import_ref Main//base, inst14 [no loc], unloaded
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: file {
  119. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  120. // CHECK:STDOUT: .C = %C
  121. // CHECK:STDOUT: .D = imports.%Main.D
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT: %default.import = import <none>
  124. // CHECK:STDOUT: %C: type = export C, imports.%Main.C [template = constants.%C]
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: class @C [from "base.carbon"] {
  128. // CHECK:STDOUT: complete_type_witness = imports.%Main.import_ref.8f2
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: !members:
  131. // CHECK:STDOUT: .Self = imports.%Main.import_ref.2c4
  132. // CHECK:STDOUT: }
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: --- use_export.carbon
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: constants {
  137. // CHECK:STDOUT: %C: type = class_type @C [template]
  138. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  139. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  140. // CHECK:STDOUT: %C.val: %C = struct_value () [template]
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: imports {
  144. // CHECK:STDOUT: %Main.D: type = import_ref Main//export, D, loaded [template = constants.%C]
  145. // CHECK:STDOUT: %Main.import_ref.8db: <witness> = import_ref Main//export, inst20 [indirect], loaded [template = constants.%complete_type]
  146. // CHECK:STDOUT: %Main.import_ref.6a9 = import_ref Main//export, inst21 [indirect], unloaded
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: file {
  150. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  151. // CHECK:STDOUT: .D = imports.%Main.D
  152. // CHECK:STDOUT: .d = %d
  153. // CHECK:STDOUT: }
  154. // CHECK:STDOUT: %default.import = import <none>
  155. // CHECK:STDOUT: name_binding_decl {
  156. // CHECK:STDOUT: %d.patt: %C = binding_pattern d
  157. // CHECK:STDOUT: %.loc6: %C = var_pattern %d.patt
  158. // CHECK:STDOUT: }
  159. // CHECK:STDOUT: %d.var: ref %C = var d
  160. // CHECK:STDOUT: %D.ref: type = name_ref D, imports.%Main.D [template = constants.%C]
  161. // CHECK:STDOUT: %d: ref %C = bind_name d, %d.var
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: class @C [from "export.carbon"] {
  165. // CHECK:STDOUT: complete_type_witness = imports.%Main.import_ref.8db
  166. // CHECK:STDOUT:
  167. // CHECK:STDOUT: !members:
  168. // CHECK:STDOUT: .Self = imports.%Main.import_ref.6a9
  169. // CHECK:STDOUT: }
  170. // CHECK:STDOUT:
  171. // CHECK:STDOUT: fn @__global_init() {
  172. // CHECK:STDOUT: !entry:
  173. // CHECK:STDOUT: %.loc6_13.1: %empty_struct_type = struct_literal ()
  174. // CHECK:STDOUT: %.loc6_13.2: init %C = class_init (), file.%d.var [template = constants.%C.val]
  175. // CHECK:STDOUT: %.loc6_1: init %C = converted %.loc6_13.1, %.loc6_13.2 [template = constants.%C.val]
  176. // CHECK:STDOUT: assign file.%d.var, %.loc6_1
  177. // CHECK:STDOUT: return
  178. // CHECK:STDOUT: }
  179. // CHECK:STDOUT:
  180. // CHECK:STDOUT: --- fail_orig_name_not_in_export.carbon
  181. // CHECK:STDOUT:
  182. // CHECK:STDOUT: constants {
  183. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  184. // CHECK:STDOUT: }
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: imports {
  187. // CHECK:STDOUT: %Main.D = import_ref Main//export, D, unloaded
  188. // CHECK:STDOUT: }
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: file {
  191. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  192. // CHECK:STDOUT: .D = imports.%Main.D
  193. // CHECK:STDOUT: .c = %c
  194. // CHECK:STDOUT: }
  195. // CHECK:STDOUT: %default.import = import <none>
  196. // CHECK:STDOUT: name_binding_decl {
  197. // CHECK:STDOUT: %c.patt: <error> = binding_pattern c
  198. // CHECK:STDOUT: %.loc10: <error> = var_pattern %c.patt
  199. // CHECK:STDOUT: }
  200. // CHECK:STDOUT: %c.var: ref <error> = var c
  201. // CHECK:STDOUT: %C.ref: <error> = name_ref C, <error> [template = <error>]
  202. // CHECK:STDOUT: %c: <error> = bind_name c, <error>
  203. // CHECK:STDOUT: }
  204. // CHECK:STDOUT:
  205. // CHECK:STDOUT: fn @__global_init() {
  206. // CHECK:STDOUT: !entry:
  207. // CHECK:STDOUT: %.loc10: %empty_struct_type = struct_literal ()
  208. // CHECK:STDOUT: assign file.%c.var, <error>
  209. // CHECK:STDOUT: return
  210. // CHECK:STDOUT: }
  211. // CHECK:STDOUT:
  212. // CHECK:STDOUT: --- indirect_compat.carbon
  213. // CHECK:STDOUT:
  214. // CHECK:STDOUT: constants {
  215. // CHECK:STDOUT: %C: type = class_type @C [template]
  216. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [template]
  217. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [template]
  218. // CHECK:STDOUT: %C.val: %C = struct_value () [template]
  219. // CHECK:STDOUT: %ptr.019: type = ptr_type %C [template]
  220. // CHECK:STDOUT: }
  221. // CHECK:STDOUT:
  222. // CHECK:STDOUT: imports {
  223. // CHECK:STDOUT: %Main.D: type = import_ref Main//export, D, loaded [template = constants.%C]
  224. // CHECK:STDOUT: %Main.C: type = import_ref Main//export_orig, C, loaded [template = constants.%C]
  225. // CHECK:STDOUT: %Main.import_ref.8db: <witness> = import_ref Main//export_orig, inst20 [indirect], loaded [template = constants.%complete_type]
  226. // CHECK:STDOUT: %Main.import_ref.6a9 = import_ref Main//export_orig, inst21 [indirect], unloaded
  227. // CHECK:STDOUT: }
  228. // CHECK:STDOUT:
  229. // CHECK:STDOUT: file {
  230. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  231. // CHECK:STDOUT: .D = imports.%Main.D
  232. // CHECK:STDOUT: .C = imports.%Main.C
  233. // CHECK:STDOUT: .c = %c
  234. // CHECK:STDOUT: .d = %d
  235. // CHECK:STDOUT: }
  236. // CHECK:STDOUT: %default.import = import <none>
  237. // CHECK:STDOUT: name_binding_decl {
  238. // CHECK:STDOUT: %c.patt: %C = binding_pattern c
  239. // CHECK:STDOUT: %.loc7: %C = var_pattern %c.patt
  240. // CHECK:STDOUT: }
  241. // CHECK:STDOUT: %c.var: ref %C = var c
  242. // CHECK:STDOUT: %C.ref: type = name_ref C, imports.%Main.C [template = constants.%C]
  243. // CHECK:STDOUT: %c: ref %C = bind_name c, %c.var
  244. // CHECK:STDOUT: name_binding_decl {
  245. // CHECK:STDOUT: %d.patt: %ptr.019 = binding_pattern d
  246. // CHECK:STDOUT: %.loc8_1: %ptr.019 = var_pattern %d.patt
  247. // CHECK:STDOUT: }
  248. // CHECK:STDOUT: %d.var: ref %ptr.019 = var d
  249. // CHECK:STDOUT: %.loc8_9: type = splice_block %ptr [template = constants.%ptr.019] {
  250. // CHECK:STDOUT: %D.ref: type = name_ref D, imports.%Main.D [template = constants.%C]
  251. // CHECK:STDOUT: %ptr: type = ptr_type %C [template = constants.%ptr.019]
  252. // CHECK:STDOUT: }
  253. // CHECK:STDOUT: %d: ref %ptr.019 = bind_name d, %d.var
  254. // CHECK:STDOUT: }
  255. // CHECK:STDOUT:
  256. // CHECK:STDOUT: class @C [from "export_orig.carbon"] {
  257. // CHECK:STDOUT: complete_type_witness = imports.%Main.import_ref.8db
  258. // CHECK:STDOUT:
  259. // CHECK:STDOUT: !members:
  260. // CHECK:STDOUT: .Self = imports.%Main.import_ref.6a9
  261. // CHECK:STDOUT: }
  262. // CHECK:STDOUT:
  263. // CHECK:STDOUT: fn @__global_init() {
  264. // CHECK:STDOUT: !entry:
  265. // CHECK:STDOUT: %.loc7_13.1: %empty_struct_type = struct_literal ()
  266. // CHECK:STDOUT: %.loc7_13.2: init %C = class_init (), file.%c.var [template = constants.%C.val]
  267. // CHECK:STDOUT: %.loc7_1: init %C = converted %.loc7_13.1, %.loc7_13.2 [template = constants.%C.val]
  268. // CHECK:STDOUT: assign file.%c.var, %.loc7_1
  269. // CHECK:STDOUT: %c.ref: ref %C = name_ref c, file.%c
  270. // CHECK:STDOUT: %addr: %ptr.019 = addr_of %c.ref
  271. // CHECK:STDOUT: assign file.%d.var, %addr
  272. // CHECK:STDOUT: return
  273. // CHECK:STDOUT: }
  274. // CHECK:STDOUT: