missing_prelude.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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/packages/no_prelude/missing_prelude.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/packages/no_prelude/missing_prelude.carbon
  10. // --- fail_missing_prelude.carbon
  11. library "[[@TEST_NAME]]";
  12. // CHECK:STDERR: fail_missing_prelude.carbon:[[@LINE+4]]:8: error: package `Core` implicitly referenced here, but not found [CoreNotFound]
  13. // CHECK:STDERR: var n: i32;
  14. // CHECK:STDERR: ^~~
  15. // CHECK:STDERR:
  16. var n: i32;
  17. // --- prelude_empty.carbon
  18. package Core library "[[@TEST_NAME]]";
  19. // --- fail_missing_prelude_member.carbon
  20. library "[[@TEST_NAME]]";
  21. import Core library "prelude_empty";
  22. // CHECK:STDERR: fail_missing_prelude_member.carbon:[[@LINE+4]]:8: error: name `Core.Int32` implicitly referenced here, but not found [CoreNameNotFound]
  23. // CHECK:STDERR: var n: i32;
  24. // CHECK:STDERR: ^~~
  25. // CHECK:STDERR:
  26. var n: i32;
  27. // --- prelude_fake_int.carbon
  28. package Core library "[[@TEST_NAME]]";
  29. fn Int32() -> {} { return {}; }
  30. // --- use_fake_int.carbon
  31. library "[[@TEST_NAME]]";
  32. import Core library "prelude_fake_int";
  33. var n: {} = i32;
  34. // --- prelude_use_in_prelude.carbon
  35. package Core library "[[@TEST_NAME]]";
  36. // Core is not an imported package here.
  37. // Note that we are intentionally using a different type here from other cases
  38. // to test that this function is called, not the one from prelude_fake_int.
  39. fn Int32() -> () { return (); }
  40. var n: () = i32;
  41. // --- prelude_as_namespace.carbon
  42. library "[[@TEST_NAME]]";
  43. // TODO: Decide whether we want to accept this.
  44. namespace Core;
  45. fn Core.Int32() -> {} { return {}; }
  46. var n: {} = i32;
  47. // --- fail_prelude_as_class.carbon
  48. library "[[@TEST_NAME]]";
  49. // TODO: Decide whether we want to accept this.
  50. class Core {
  51. fn Int32() -> {} { return {}; }
  52. }
  53. // CHECK:STDERR: fail_prelude_as_class.carbon:[[@LINE+3]]:13: error: package `Core` implicitly referenced here, but not found [CoreNotFound]
  54. // CHECK:STDERR: var n: {} = i32;
  55. // CHECK:STDERR: ^~~
  56. var n: {} = i32;
  57. // CHECK:STDOUT: --- fail_missing_prelude.carbon
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: file {
  60. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  61. // CHECK:STDOUT: .n = %n
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT: %n.var: ref <error> = var n
  64. // CHECK:STDOUT: %n: ref <error> = bind_name n, %n.var
  65. // CHECK:STDOUT: }
  66. // CHECK:STDOUT:
  67. // CHECK:STDOUT: --- prelude_empty.carbon
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: file {
  70. // CHECK:STDOUT: package: <namespace> = namespace [template] {}
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: --- fail_missing_prelude_member.carbon
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: imports {
  76. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  77. // CHECK:STDOUT: import Core//prelude_empty
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: file {
  82. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  83. // CHECK:STDOUT: .Core = imports.%Core
  84. // CHECK:STDOUT: .n = %n
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT: %Core.import = import Core
  87. // CHECK:STDOUT: %n.var: ref <error> = var n
  88. // CHECK:STDOUT: %n: ref <error> = bind_name n, %n.var
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: --- prelude_fake_int.carbon
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: constants {
  94. // CHECK:STDOUT: %.1: type = struct_type {} [template]
  95. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  96. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  97. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  98. // CHECK:STDOUT: %struct: %.1 = struct_value () [template]
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: file {
  102. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  103. // CHECK:STDOUT: .Int32 = %Int32.decl
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT: %Int32.decl: %Int32.type = fn_decl @Int32 [template = constants.%Int32] {
  106. // CHECK:STDOUT: %return.patt: %.1 = return_slot_pattern
  107. // CHECK:STDOUT: %return.param_patt: %.1 = out_param_pattern %return.patt, runtime_param0
  108. // CHECK:STDOUT: } {
  109. // CHECK:STDOUT: %.loc4_16.1: %.1 = struct_literal ()
  110. // CHECK:STDOUT: %.loc4_16.2: type = converted %.loc4_16.1, constants.%.1 [template = constants.%.1]
  111. // CHECK:STDOUT: %return.param: ref %.1 = out_param runtime_param0
  112. // CHECK:STDOUT: %return: ref %.1 = return_slot %return.param
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: fn @Int32() -> %.1 {
  117. // CHECK:STDOUT: !entry:
  118. // CHECK:STDOUT: %.loc4_28: %.1 = struct_literal ()
  119. // CHECK:STDOUT: %struct: %.1 = struct_value () [template = constants.%struct]
  120. // CHECK:STDOUT: %.loc4_29: %.1 = converted %.loc4_28, %struct [template = constants.%struct]
  121. // CHECK:STDOUT: return %.loc4_29
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: --- use_fake_int.carbon
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: constants {
  127. // CHECK:STDOUT: %.1: type = struct_type {} [template]
  128. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  129. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  130. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: imports {
  134. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  135. // CHECK:STDOUT: .Int32 = %import_ref
  136. // CHECK:STDOUT: import Core//prelude_fake_int
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude_fake_int, inst+8, loaded [template = constants.%Int32]
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: file {
  142. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  143. // CHECK:STDOUT: .Core = imports.%Core
  144. // CHECK:STDOUT: .n = %n
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT: %Core.import = import Core
  147. // CHECK:STDOUT: %.loc6_9.1: %.1 = struct_literal ()
  148. // CHECK:STDOUT: %.loc6_9.2: type = converted %.loc6_9.1, constants.%.1 [template = constants.%.1]
  149. // CHECK:STDOUT: %n.var: ref %.1 = var n
  150. // CHECK:STDOUT: %n: ref %.1 = bind_name n, %n.var
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: fn @Int32() -> %.1;
  154. // CHECK:STDOUT:
  155. // CHECK:STDOUT: fn @__global_init() {
  156. // CHECK:STDOUT: !entry:
  157. // CHECK:STDOUT: %Int32.call: init %.1 = call constants.%Int32()
  158. // CHECK:STDOUT: assign file.%n.var, %Int32.call
  159. // CHECK:STDOUT: return
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: --- prelude_use_in_prelude.carbon
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: constants {
  165. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  166. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  167. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  168. // CHECK:STDOUT: %tuple: %.1 = tuple_value () [template]
  169. // CHECK:STDOUT: }
  170. // CHECK:STDOUT:
  171. // CHECK:STDOUT: file {
  172. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  173. // CHECK:STDOUT: .Int32 = %Int32.decl
  174. // CHECK:STDOUT: .n = %n
  175. // CHECK:STDOUT: }
  176. // CHECK:STDOUT: %Int32.decl: %Int32.type = fn_decl @Int32 [template = constants.%Int32] {
  177. // CHECK:STDOUT: %return.patt: %.1 = return_slot_pattern
  178. // CHECK:STDOUT: %return.param_patt: %.1 = out_param_pattern %return.patt, runtime_param0
  179. // CHECK:STDOUT: } {
  180. // CHECK:STDOUT: %.loc8_16.1: %.1 = tuple_literal ()
  181. // CHECK:STDOUT: %.loc8_16.2: type = converted %.loc8_16.1, constants.%.1 [template = constants.%.1]
  182. // CHECK:STDOUT: %return.param: ref %.1 = out_param runtime_param0
  183. // CHECK:STDOUT: %return: ref %.1 = return_slot %return.param
  184. // CHECK:STDOUT: }
  185. // CHECK:STDOUT: %.loc10_9.1: %.1 = tuple_literal ()
  186. // CHECK:STDOUT: %.loc10_9.2: type = converted %.loc10_9.1, constants.%.1 [template = constants.%.1]
  187. // CHECK:STDOUT: %n.var: ref %.1 = var n
  188. // CHECK:STDOUT: %n: ref %.1 = bind_name n, %n.var
  189. // CHECK:STDOUT: }
  190. // CHECK:STDOUT:
  191. // CHECK:STDOUT: fn @Int32() -> %.1 {
  192. // CHECK:STDOUT: !entry:
  193. // CHECK:STDOUT: %.loc8_28: %.1 = tuple_literal ()
  194. // CHECK:STDOUT: %tuple: %.1 = tuple_value () [template = constants.%tuple]
  195. // CHECK:STDOUT: %.loc8_29: %.1 = converted %.loc8_28, %tuple [template = constants.%tuple]
  196. // CHECK:STDOUT: return %.loc8_29
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: fn @__global_init() {
  200. // CHECK:STDOUT: !entry:
  201. // CHECK:STDOUT: %Int32.call: init %.1 = call constants.%Int32()
  202. // CHECK:STDOUT: assign file.%n.var, %Int32.call
  203. // CHECK:STDOUT: return
  204. // CHECK:STDOUT: }
  205. // CHECK:STDOUT:
  206. // CHECK:STDOUT: --- prelude_as_namespace.carbon
  207. // CHECK:STDOUT:
  208. // CHECK:STDOUT: constants {
  209. // CHECK:STDOUT: %.1: type = struct_type {} [template]
  210. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  211. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  212. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  213. // CHECK:STDOUT: %struct: %.1 = struct_value () [template]
  214. // CHECK:STDOUT: }
  215. // CHECK:STDOUT:
  216. // CHECK:STDOUT: file {
  217. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  218. // CHECK:STDOUT: .Core = %Core
  219. // CHECK:STDOUT: .n = %n
  220. // CHECK:STDOUT: }
  221. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {
  222. // CHECK:STDOUT: .Int32 = %Int32.decl
  223. // CHECK:STDOUT: }
  224. // CHECK:STDOUT: %Int32.decl: %Int32.type = fn_decl @Int32 [template = constants.%Int32] {
  225. // CHECK:STDOUT: %return.patt: %.1 = return_slot_pattern
  226. // CHECK:STDOUT: %return.param_patt: %.1 = out_param_pattern %return.patt, runtime_param0
  227. // CHECK:STDOUT: } {
  228. // CHECK:STDOUT: %.loc7_21.1: %.1 = struct_literal ()
  229. // CHECK:STDOUT: %.loc7_21.2: type = converted %.loc7_21.1, constants.%.1 [template = constants.%.1]
  230. // CHECK:STDOUT: %return.param: ref %.1 = out_param runtime_param0
  231. // CHECK:STDOUT: %return: ref %.1 = return_slot %return.param
  232. // CHECK:STDOUT: }
  233. // CHECK:STDOUT: %.loc9_9.1: %.1 = struct_literal ()
  234. // CHECK:STDOUT: %.loc9_9.2: type = converted %.loc9_9.1, constants.%.1 [template = constants.%.1]
  235. // CHECK:STDOUT: %n.var: ref %.1 = var n
  236. // CHECK:STDOUT: %n: ref %.1 = bind_name n, %n.var
  237. // CHECK:STDOUT: }
  238. // CHECK:STDOUT:
  239. // CHECK:STDOUT: fn @Int32() -> %.1 {
  240. // CHECK:STDOUT: !entry:
  241. // CHECK:STDOUT: %.loc7_33: %.1 = struct_literal ()
  242. // CHECK:STDOUT: %struct: %.1 = struct_value () [template = constants.%struct]
  243. // CHECK:STDOUT: %.loc7_34: %.1 = converted %.loc7_33, %struct [template = constants.%struct]
  244. // CHECK:STDOUT: return %.loc7_34
  245. // CHECK:STDOUT: }
  246. // CHECK:STDOUT:
  247. // CHECK:STDOUT: fn @__global_init() {
  248. // CHECK:STDOUT: !entry:
  249. // CHECK:STDOUT: %Int32.call: init %.1 = call constants.%Int32()
  250. // CHECK:STDOUT: assign file.%n.var, %Int32.call
  251. // CHECK:STDOUT: return
  252. // CHECK:STDOUT: }
  253. // CHECK:STDOUT:
  254. // CHECK:STDOUT: --- fail_prelude_as_class.carbon
  255. // CHECK:STDOUT:
  256. // CHECK:STDOUT: constants {
  257. // CHECK:STDOUT: %Core: type = class_type @Core [template]
  258. // CHECK:STDOUT: %.1: type = struct_type {} [template]
  259. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  260. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  261. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  262. // CHECK:STDOUT: %.3: <witness> = complete_type_witness %.1 [template]
  263. // CHECK:STDOUT: %struct: %.1 = struct_value () [template]
  264. // CHECK:STDOUT: }
  265. // CHECK:STDOUT:
  266. // CHECK:STDOUT: file {
  267. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  268. // CHECK:STDOUT: .Core = %Core.decl
  269. // CHECK:STDOUT: .n = %n
  270. // CHECK:STDOUT: }
  271. // CHECK:STDOUT: %Core.decl: type = class_decl @Core [template = constants.%Core] {} {}
  272. // CHECK:STDOUT: %.loc12_9.1: %.1 = struct_literal ()
  273. // CHECK:STDOUT: %.loc12_9.2: type = converted %.loc12_9.1, constants.%.1 [template = constants.%.1]
  274. // CHECK:STDOUT: %n.var: ref %.1 = var n
  275. // CHECK:STDOUT: %n: ref %.1 = bind_name n, %n.var
  276. // CHECK:STDOUT: }
  277. // CHECK:STDOUT:
  278. // CHECK:STDOUT: class @Core {
  279. // CHECK:STDOUT: %Int32.decl: %Int32.type = fn_decl @Int32 [template = constants.%Int32] {
  280. // CHECK:STDOUT: %return.patt: %.1 = return_slot_pattern
  281. // CHECK:STDOUT: %return.param_patt: %.1 = out_param_pattern %return.patt, runtime_param0
  282. // CHECK:STDOUT: } {
  283. // CHECK:STDOUT: %.loc6_18.1: %.1 = struct_literal ()
  284. // CHECK:STDOUT: %.loc6_18.2: type = converted %.loc6_18.1, constants.%.1 [template = constants.%.1]
  285. // CHECK:STDOUT: %return.param: ref %.1 = out_param runtime_param0
  286. // CHECK:STDOUT: %return: ref %.1 = return_slot %return.param
  287. // CHECK:STDOUT: }
  288. // CHECK:STDOUT: %.loc7: <witness> = complete_type_witness %.1 [template = constants.%.3]
  289. // CHECK:STDOUT:
  290. // CHECK:STDOUT: !members:
  291. // CHECK:STDOUT: .Self = constants.%Core
  292. // CHECK:STDOUT: .Int32 = %Int32.decl
  293. // CHECK:STDOUT: }
  294. // CHECK:STDOUT:
  295. // CHECK:STDOUT: fn @Int32() -> %.1 {
  296. // CHECK:STDOUT: !entry:
  297. // CHECK:STDOUT: %.loc6_30: %.1 = struct_literal ()
  298. // CHECK:STDOUT: %struct: %.1 = struct_value () [template = constants.%struct]
  299. // CHECK:STDOUT: %.loc6_31: %.1 = converted %.loc6_30, %struct [template = constants.%struct]
  300. // CHECK:STDOUT: return %.loc6_31
  301. // CHECK:STDOUT: }
  302. // CHECK:STDOUT:
  303. // CHECK:STDOUT: fn @__global_init() {
  304. // CHECK:STDOUT: !entry:
  305. // CHECK:STDOUT: assign file.%n.var, <error>
  306. // CHECK:STDOUT: return
  307. // CHECK:STDOUT: }
  308. // CHECK:STDOUT: