implicit_import.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/none.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/class/implicit_import.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/implicit_import.carbon
  14. // --- basic.carbon
  15. library "[[@TEST_NAME]]";
  16. class C;
  17. // --- basic.impl.carbon
  18. impl library "[[@TEST_NAME]]";
  19. class C {}
  20. // --- redecl_after_def.carbon
  21. library "[[@TEST_NAME]]";
  22. class C {}
  23. // --- fail_redecl_after_def.impl.carbon
  24. impl library "[[@TEST_NAME]]";
  25. // CHECK:STDERR: fail_redecl_after_def.impl.carbon:[[@LINE+8]]:1: error: redeclaration of `class C` is redundant [RedeclRedundant]
  26. // CHECK:STDERR: class C;
  27. // CHECK:STDERR: ^~~~~~~~
  28. // CHECK:STDERR: fail_redecl_after_def.impl.carbon:[[@LINE-5]]:1: in import [InImport]
  29. // CHECK:STDERR: redecl_after_def.carbon:4:1: note: previously declared here [RedeclPrevDecl]
  30. // CHECK:STDERR: class C {}
  31. // CHECK:STDERR: ^~~~~~~~~
  32. // CHECK:STDERR:
  33. class C;
  34. // --- redef_after_def.carbon
  35. library "[[@TEST_NAME]]";
  36. class C {}
  37. // --- fail_redef_after_def.impl.carbon
  38. impl library "[[@TEST_NAME]]";
  39. // CHECK:STDERR: fail_redef_after_def.impl.carbon:[[@LINE+8]]:1: error: redefinition of `class C` [RedeclRedef]
  40. // CHECK:STDERR: class C {}
  41. // CHECK:STDERR: ^~~~~~~~~
  42. // CHECK:STDERR: fail_redef_after_def.impl.carbon:[[@LINE-5]]:1: in import [InImport]
  43. // CHECK:STDERR: redef_after_def.carbon:4:1: note: previously defined here [RedeclPrevDef]
  44. // CHECK:STDERR: class C {}
  45. // CHECK:STDERR: ^~~~~~~~~
  46. // CHECK:STDERR:
  47. class C {}
  48. // --- def_alias.carbon
  49. library "[[@TEST_NAME]]";
  50. class C;
  51. alias B = C;
  52. // --- fail_def_alias.impl.carbon
  53. impl library "[[@TEST_NAME]]";
  54. // CHECK:STDERR: fail_def_alias.impl.carbon:[[@LINE+8]]:7: error: duplicate name `B` being declared in the same scope [NameDeclDuplicate]
  55. // CHECK:STDERR: class B {}
  56. // CHECK:STDERR: ^
  57. // CHECK:STDERR: fail_def_alias.impl.carbon:[[@LINE-5]]:1: in import [InImport]
  58. // CHECK:STDERR: def_alias.carbon:5:7: note: name is previously declared here [NameDeclPrevious]
  59. // CHECK:STDERR: alias B = C;
  60. // CHECK:STDERR: ^
  61. // CHECK:STDERR:
  62. class B {}
  63. // CHECK:STDOUT: --- basic.carbon
  64. // CHECK:STDOUT:
  65. // CHECK:STDOUT: constants {
  66. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: file {
  70. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  71. // CHECK:STDOUT: .C = %C.decl
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: class @C;
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: --- basic.impl.carbon
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: constants {
  81. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  82. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  83. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT:
  86. // CHECK:STDOUT: imports {
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: file {
  90. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  91. // CHECK:STDOUT: .C = %C.decl
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT: %default.import.loc2_21.1 = import <none>
  94. // CHECK:STDOUT: %default.import.loc2_21.2 = import <none>
  95. // CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: class @C {
  99. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete = constants.%empty_struct_type]
  100. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete = constants.%complete_type]
  101. // CHECK:STDOUT: complete_type_witness = %complete_type
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: !members:
  104. // CHECK:STDOUT: .Self = constants.%C
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: --- redecl_after_def.carbon
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: constants {
  110. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  111. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  112. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: file {
  116. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  117. // CHECK:STDOUT: .C = %C.decl
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: class @C {
  123. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete = constants.%empty_struct_type]
  124. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete = constants.%complete_type]
  125. // CHECK:STDOUT: complete_type_witness = %complete_type
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: !members:
  128. // CHECK:STDOUT: .Self = constants.%C
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: --- fail_redecl_after_def.impl.carbon
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: constants {
  134. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  135. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  136. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: imports {
  140. // CHECK:STDOUT: %Main.import_ref.8f2: <witness> = import_ref Main//redecl_after_def, loc4_10, loaded [concrete = constants.%complete_type]
  141. // CHECK:STDOUT: %Main.import_ref.2c4 = import_ref Main//redecl_after_def, inst16 [no loc], unloaded
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: file {
  145. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  146. // CHECK:STDOUT: .C = %C.decl
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT: %default.import.loc2_32.1 = import <none>
  149. // CHECK:STDOUT: %default.import.loc2_32.2 = import <none>
  150. // CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: class @C {
  154. // CHECK:STDOUT: complete_type_witness = imports.%Main.import_ref.8f2
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: !members:
  157. // CHECK:STDOUT: .Self = imports.%Main.import_ref.2c4
  158. // CHECK:STDOUT: }
  159. // CHECK:STDOUT:
  160. // CHECK:STDOUT: --- redef_after_def.carbon
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: constants {
  163. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  164. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  165. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: file {
  169. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  170. // CHECK:STDOUT: .C = %C.decl
  171. // CHECK:STDOUT: }
  172. // CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
  173. // CHECK:STDOUT: }
  174. // CHECK:STDOUT:
  175. // CHECK:STDOUT: class @C {
  176. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete = constants.%empty_struct_type]
  177. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete = constants.%complete_type]
  178. // CHECK:STDOUT: complete_type_witness = %complete_type
  179. // CHECK:STDOUT:
  180. // CHECK:STDOUT: !members:
  181. // CHECK:STDOUT: .Self = constants.%C
  182. // CHECK:STDOUT: }
  183. // CHECK:STDOUT:
  184. // CHECK:STDOUT: --- fail_redef_after_def.impl.carbon
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: constants {
  187. // CHECK:STDOUT: %C.f794a0.1: type = class_type @C.1 [concrete]
  188. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  189. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  190. // CHECK:STDOUT: %C.f794a0.2: type = class_type @C.loc12 [concrete]
  191. // CHECK:STDOUT: }
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: imports {
  194. // CHECK:STDOUT: %Main.C: type = import_ref Main//redef_after_def, C, loaded [concrete = constants.%C.f794a0.1]
  195. // CHECK:STDOUT: %Main.import_ref.8f2: <witness> = import_ref Main//redef_after_def, loc4_10, loaded [concrete = constants.%complete_type]
  196. // CHECK:STDOUT: %Main.import_ref.2c4 = import_ref Main//redef_after_def, inst16 [no loc], unloaded
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: file {
  200. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  201. // CHECK:STDOUT: .C = imports.%Main.C
  202. // CHECK:STDOUT: }
  203. // CHECK:STDOUT: %default.import.loc2_31.1 = import <none>
  204. // CHECK:STDOUT: %default.import.loc2_31.2 = import <none>
  205. // CHECK:STDOUT: %C.decl: type = class_decl @C.loc12 [concrete = constants.%C.f794a0.2] {} {}
  206. // CHECK:STDOUT: }
  207. // CHECK:STDOUT:
  208. // CHECK:STDOUT: class @C.1 [from "redef_after_def.carbon"] {
  209. // CHECK:STDOUT: complete_type_witness = imports.%Main.import_ref.8f2
  210. // CHECK:STDOUT:
  211. // CHECK:STDOUT: !members:
  212. // CHECK:STDOUT: .Self = imports.%Main.import_ref.2c4
  213. // CHECK:STDOUT: }
  214. // CHECK:STDOUT:
  215. // CHECK:STDOUT: class @C.loc12 {
  216. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete = constants.%empty_struct_type]
  217. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete = constants.%complete_type]
  218. // CHECK:STDOUT: complete_type_witness = %complete_type
  219. // CHECK:STDOUT:
  220. // CHECK:STDOUT: !members:
  221. // CHECK:STDOUT: .Self = constants.%C.f794a0.2
  222. // CHECK:STDOUT: }
  223. // CHECK:STDOUT:
  224. // CHECK:STDOUT: --- def_alias.carbon
  225. // CHECK:STDOUT:
  226. // CHECK:STDOUT: constants {
  227. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  228. // CHECK:STDOUT: }
  229. // CHECK:STDOUT:
  230. // CHECK:STDOUT: file {
  231. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  232. // CHECK:STDOUT: .C = %C.decl
  233. // CHECK:STDOUT: .B = %B
  234. // CHECK:STDOUT: }
  235. // CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
  236. // CHECK:STDOUT: %C.ref: type = name_ref C, %C.decl [concrete = constants.%C]
  237. // CHECK:STDOUT: %B: type = bind_alias B, %C.decl [concrete = constants.%C]
  238. // CHECK:STDOUT: }
  239. // CHECK:STDOUT:
  240. // CHECK:STDOUT: class @C;
  241. // CHECK:STDOUT:
  242. // CHECK:STDOUT: --- fail_def_alias.impl.carbon
  243. // CHECK:STDOUT:
  244. // CHECK:STDOUT: constants {
  245. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  246. // CHECK:STDOUT: %B: type = class_type @B [concrete]
  247. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  248. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  249. // CHECK:STDOUT: }
  250. // CHECK:STDOUT:
  251. // CHECK:STDOUT: imports {
  252. // CHECK:STDOUT: %Main.C = import_ref Main//def_alias, C, unloaded
  253. // CHECK:STDOUT: %Main.B: type = import_ref Main//def_alias, B, loaded [concrete = constants.%C]
  254. // CHECK:STDOUT: }
  255. // CHECK:STDOUT:
  256. // CHECK:STDOUT: file {
  257. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  258. // CHECK:STDOUT: .C = imports.%Main.C
  259. // CHECK:STDOUT: .B = imports.%Main.B
  260. // CHECK:STDOUT: }
  261. // CHECK:STDOUT: %default.import.loc2_25.1 = import <none>
  262. // CHECK:STDOUT: %default.import.loc2_25.2 = import <none>
  263. // CHECK:STDOUT: %B.decl: type = class_decl @B [concrete = constants.%B] {} {}
  264. // CHECK:STDOUT: }
  265. // CHECK:STDOUT:
  266. // CHECK:STDOUT: class @C [from "def_alias.carbon"];
  267. // CHECK:STDOUT:
  268. // CHECK:STDOUT: class @B {
  269. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete = constants.%empty_struct_type]
  270. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete = constants.%complete_type]
  271. // CHECK:STDOUT: complete_type_witness = %complete_type
  272. // CHECK:STDOUT:
  273. // CHECK:STDOUT: !members:
  274. // CHECK:STDOUT: .Self = constants.%B
  275. // CHECK:STDOUT: }
  276. // CHECK:STDOUT: