adapter_conversion.carbon 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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/as/adapter_conversion.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/as/adapter_conversion.carbon
  10. // --- adapt_class.carbon
  11. library "adapt_class";
  12. class A {
  13. var x: i32;
  14. var y: i32;
  15. fn Make() -> A {
  16. return {.x = 1, .y = 2};
  17. }
  18. }
  19. class B {
  20. adapt A;
  21. }
  22. var a_ref: A = {.x = 1, .y = 2};
  23. let a_val: A = a_ref;
  24. // An `as` conversion to an adapter type preserves the expression category.
  25. let b_val: B = a_val as B;
  26. let b_ptr: B* = &(a_ref as B);
  27. var b_factory: B = A.Make() as B;
  28. // --- adapt_i32.carbon
  29. library "adapt_i32";
  30. class A {
  31. adapt i32;
  32. }
  33. let a: A = (1 as i32) as A;
  34. let n: i32 = a as i32;
  35. // --- multi_level_adapt.carbon
  36. library "multi_level_adapt";
  37. class A { adapt {}; }
  38. class B { adapt A; }
  39. class C { adapt B; }
  40. class D { adapt C; }
  41. let d: D = {} as D;
  42. // --- fail_init_class.carbon
  43. library "fail_init_class";
  44. class A {
  45. var x: i32;
  46. var y: i32;
  47. }
  48. class B {
  49. adapt A;
  50. }
  51. let b_value: B = ({.x = 1, .y = 2} as A) as B;
  52. // TODO: Here, we treat `{.x = 1, .y = 2} as A` as a value expression, not an
  53. // initializing expression, so `(...) as B` is a value expression too, requiring
  54. // a copy to perform initialization. It's not clear whether that is the right
  55. // behavior.
  56. // CHECK:STDERR: fail_init_class.carbon:[[@LINE+4]]:17: ERROR: Cannot copy value of type `B`.
  57. // CHECK:STDERR: var b_init: B = ({.x = 1, .y = 2} as A) as B;
  58. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  59. // CHECK:STDERR:
  60. var b_init: B = ({.x = 1, .y = 2} as A) as B;
  61. // --- fail_adapt_init_from_struct.carbon
  62. library "fail_adapt_init_from_struct";
  63. class A {
  64. var x: i32;
  65. }
  66. class B {
  67. adapt A;
  68. }
  69. // We do not try to implicitly convert from the first operand of `as` to the
  70. // adapted type of the second operand.
  71. // CHECK:STDERR: fail_adapt_init_from_struct.carbon:[[@LINE+3]]:12: ERROR: Cannot convert from `{.x: i32}` to `B` with `as`.
  72. // CHECK:STDERR: var b: B = {.x = 1} as B;
  73. // CHECK:STDERR: ^~~~~~~~~~~~~
  74. var b: B = {.x = 1} as B;
  75. // CHECK:STDOUT: --- adapt_class.carbon
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: constants {
  78. // CHECK:STDOUT: %A: type = class_type @A [template]
  79. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  80. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  81. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  82. // CHECK:STDOUT: %.2: type = unbound_element_type %A, i32 [template]
  83. // CHECK:STDOUT: %Make.type: type = fn_type @Make [template]
  84. // CHECK:STDOUT: %Make: %Make.type = struct_value () [template]
  85. // CHECK:STDOUT: %.3: type = struct_type {.x: i32, .y: i32} [template]
  86. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  87. // CHECK:STDOUT: %.5: i32 = int_literal 1 [template]
  88. // CHECK:STDOUT: %.6: i32 = int_literal 2 [template]
  89. // CHECK:STDOUT: %struct: %A = struct_value (%.5, %.6) [template]
  90. // CHECK:STDOUT: %B: type = class_type @B [template]
  91. // CHECK:STDOUT: %.7: type = ptr_type %B [template]
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: file {
  95. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  96. // CHECK:STDOUT: .Core = %Core
  97. // CHECK:STDOUT: .A = %A.decl
  98. // CHECK:STDOUT: .B = %B.decl
  99. // CHECK:STDOUT: .a_ref = %a_ref
  100. // CHECK:STDOUT: .a_val = @__global_init.%a_val
  101. // CHECK:STDOUT: .b_val = @__global_init.%b_val
  102. // CHECK:STDOUT: .b_ptr = @__global_init.%b_ptr
  103. // CHECK:STDOUT: .b_factory = %b_factory
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  106. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {}
  107. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  108. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  109. // CHECK:STDOUT: %B.decl: type = class_decl @B [template = constants.%B] {}
  110. // CHECK:STDOUT: %A.ref.loc17: type = name_ref A, %A.decl [template = constants.%A]
  111. // CHECK:STDOUT: %a_ref.var: ref %A = var a_ref
  112. // CHECK:STDOUT: %a_ref: ref %A = bind_name a_ref, %a_ref.var
  113. // CHECK:STDOUT: %A.ref.loc18: type = name_ref A, %A.decl [template = constants.%A]
  114. // CHECK:STDOUT: %B.ref.loc21: type = name_ref B, %B.decl [template = constants.%B]
  115. // CHECK:STDOUT: %B.ref.loc22: type = name_ref B, %B.decl [template = constants.%B]
  116. // CHECK:STDOUT: %.loc22: type = ptr_type %B [template = constants.%.7]
  117. // CHECK:STDOUT: %B.ref.loc24: type = name_ref B, %B.decl [template = constants.%B]
  118. // CHECK:STDOUT: %b_factory.var: ref %B = var b_factory
  119. // CHECK:STDOUT: %b_factory: ref %B = bind_name b_factory, %b_factory.var
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: class @A {
  123. // CHECK:STDOUT: %int.make_type_32.loc5: init type = call constants.%Int32() [template = i32]
  124. // CHECK:STDOUT: %.loc5_10.1: type = value_of_initializer %int.make_type_32.loc5 [template = i32]
  125. // CHECK:STDOUT: %.loc5_10.2: type = converted %int.make_type_32.loc5, %.loc5_10.1 [template = i32]
  126. // CHECK:STDOUT: %.loc5_8: %.2 = field_decl x, element0 [template]
  127. // CHECK:STDOUT: %int.make_type_32.loc6: init type = call constants.%Int32() [template = i32]
  128. // CHECK:STDOUT: %.loc6_10.1: type = value_of_initializer %int.make_type_32.loc6 [template = i32]
  129. // CHECK:STDOUT: %.loc6_10.2: type = converted %int.make_type_32.loc6, %.loc6_10.1 [template = i32]
  130. // CHECK:STDOUT: %.loc6_8: %.2 = field_decl y, element1 [template]
  131. // CHECK:STDOUT: %Make.decl: %Make.type = fn_decl @Make [template = constants.%Make] {
  132. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  133. // CHECK:STDOUT: %return.var: ref %A = var <return slot>
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: !members:
  137. // CHECK:STDOUT: .Self = constants.%A
  138. // CHECK:STDOUT: .x = %.loc5_8
  139. // CHECK:STDOUT: .y = %.loc6_8
  140. // CHECK:STDOUT: .Make = %Make.decl
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: class @B {
  144. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  145. // CHECK:STDOUT: adapt_decl %A
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: !members:
  148. // CHECK:STDOUT: .Self = constants.%B
  149. // CHECK:STDOUT: }
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: fn @Make() -> @A.%return.var: %A {
  154. // CHECK:STDOUT: !entry:
  155. // CHECK:STDOUT: %.loc9_18: i32 = int_literal 1 [template = constants.%.5]
  156. // CHECK:STDOUT: %.loc9_26: i32 = int_literal 2 [template = constants.%.6]
  157. // CHECK:STDOUT: %.loc9_27.1: %.3 = struct_literal (%.loc9_18, %.loc9_26)
  158. // CHECK:STDOUT: %.loc9_27.2: ref i32 = class_element_access @A.%return.var, element0
  159. // CHECK:STDOUT: %.loc9_27.3: init i32 = initialize_from %.loc9_18 to %.loc9_27.2 [template = constants.%.5]
  160. // CHECK:STDOUT: %.loc9_27.4: ref i32 = class_element_access @A.%return.var, element1
  161. // CHECK:STDOUT: %.loc9_27.5: init i32 = initialize_from %.loc9_26 to %.loc9_27.4 [template = constants.%.6]
  162. // CHECK:STDOUT: %.loc9_27.6: init %A = class_init (%.loc9_27.3, %.loc9_27.5), @A.%return.var [template = constants.%struct]
  163. // CHECK:STDOUT: %.loc9_28: init %A = converted %.loc9_27.1, %.loc9_27.6 [template = constants.%struct]
  164. // CHECK:STDOUT: return %.loc9_28 to @A.%return.var
  165. // CHECK:STDOUT: }
  166. // CHECK:STDOUT:
  167. // CHECK:STDOUT: fn @__global_init() {
  168. // CHECK:STDOUT: !entry:
  169. // CHECK:STDOUT: %.loc17_22: i32 = int_literal 1 [template = constants.%.5]
  170. // CHECK:STDOUT: %.loc17_30: i32 = int_literal 2 [template = constants.%.6]
  171. // CHECK:STDOUT: %.loc17_31.1: %.3 = struct_literal (%.loc17_22, %.loc17_30)
  172. // CHECK:STDOUT: %.loc17_31.2: ref i32 = class_element_access file.%a_ref.var, element0
  173. // CHECK:STDOUT: %.loc17_31.3: init i32 = initialize_from %.loc17_22 to %.loc17_31.2 [template = constants.%.5]
  174. // CHECK:STDOUT: %.loc17_31.4: ref i32 = class_element_access file.%a_ref.var, element1
  175. // CHECK:STDOUT: %.loc17_31.5: init i32 = initialize_from %.loc17_30 to %.loc17_31.4 [template = constants.%.6]
  176. // CHECK:STDOUT: %.loc17_31.6: init %A = class_init (%.loc17_31.3, %.loc17_31.5), file.%a_ref.var [template = constants.%struct]
  177. // CHECK:STDOUT: %.loc17_32: init %A = converted %.loc17_31.1, %.loc17_31.6 [template = constants.%struct]
  178. // CHECK:STDOUT: assign file.%a_ref.var, %.loc17_32
  179. // CHECK:STDOUT: %a_ref.ref.loc18: ref %A = name_ref a_ref, file.%a_ref
  180. // CHECK:STDOUT: %.loc18: %A = bind_value %a_ref.ref.loc18
  181. // CHECK:STDOUT: %a_val: %A = bind_name a_val, %.loc18
  182. // CHECK:STDOUT: %a_val.ref: %A = name_ref a_val, %a_val
  183. // CHECK:STDOUT: %B.ref.loc21: type = name_ref B, file.%B.decl [template = constants.%B]
  184. // CHECK:STDOUT: %.loc21_22.1: %B = as_compatible %a_val.ref
  185. // CHECK:STDOUT: %.loc21_22.2: %B = converted %a_val.ref, %.loc21_22.1
  186. // CHECK:STDOUT: %b_val: %B = bind_name b_val, %.loc21_22.2
  187. // CHECK:STDOUT: %a_ref.ref.loc22: ref %A = name_ref a_ref, file.%a_ref
  188. // CHECK:STDOUT: %B.ref.loc22: type = name_ref B, file.%B.decl [template = constants.%B]
  189. // CHECK:STDOUT: %.loc22_25.1: ref %B = as_compatible %a_ref.ref.loc22
  190. // CHECK:STDOUT: %.loc22_25.2: ref %B = converted %a_ref.ref.loc22, %.loc22_25.1
  191. // CHECK:STDOUT: %.loc22_17: %.7 = addr_of %.loc22_25.2
  192. // CHECK:STDOUT: %b_ptr: %.7 = bind_name b_ptr, %.loc22_17
  193. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  194. // CHECK:STDOUT: %Make.ref: %Make.type = name_ref Make, @A.%Make.decl [template = constants.%Make]
  195. // CHECK:STDOUT: %.loc24_5: ref %B = splice_block file.%b_factory.var {}
  196. // CHECK:STDOUT: %Make.call: init %A = call %Make.ref() to %.loc24_5
  197. // CHECK:STDOUT: %B.ref.loc24: type = name_ref B, file.%B.decl [template = constants.%B]
  198. // CHECK:STDOUT: %.loc24_29.1: init %B = as_compatible %Make.call
  199. // CHECK:STDOUT: %.loc24_29.2: init %B = converted %Make.call, %.loc24_29.1
  200. // CHECK:STDOUT: assign file.%b_factory.var, %.loc24_29.2
  201. // CHECK:STDOUT: return
  202. // CHECK:STDOUT: }
  203. // CHECK:STDOUT:
  204. // CHECK:STDOUT: --- adapt_i32.carbon
  205. // CHECK:STDOUT:
  206. // CHECK:STDOUT: constants {
  207. // CHECK:STDOUT: %A: type = class_type @A [template]
  208. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  209. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  210. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  211. // CHECK:STDOUT: %.2: i32 = int_literal 1 [template]
  212. // CHECK:STDOUT: }
  213. // CHECK:STDOUT:
  214. // CHECK:STDOUT: file {
  215. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  216. // CHECK:STDOUT: .Core = %Core
  217. // CHECK:STDOUT: .A = %A.decl
  218. // CHECK:STDOUT: .a = @__global_init.%a
  219. // CHECK:STDOUT: .n = @__global_init.%n
  220. // CHECK:STDOUT: }
  221. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  222. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {}
  223. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  224. // CHECK:STDOUT: %A.ref: type = name_ref A, %A.decl [template = constants.%A]
  225. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  226. // CHECK:STDOUT: %import_ref.3: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  227. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  228. // CHECK:STDOUT: %.loc9_8.1: type = value_of_initializer %int.make_type_32 [template = i32]
  229. // CHECK:STDOUT: %.loc9_8.2: type = converted %int.make_type_32, %.loc9_8.1 [template = i32]
  230. // CHECK:STDOUT: %import_ref.4: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  231. // CHECK:STDOUT: }
  232. // CHECK:STDOUT:
  233. // CHECK:STDOUT: class @A {
  234. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  235. // CHECK:STDOUT: %.loc5_12.1: type = value_of_initializer %int.make_type_32 [template = i32]
  236. // CHECK:STDOUT: %.loc5_12.2: type = converted %int.make_type_32, %.loc5_12.1 [template = i32]
  237. // CHECK:STDOUT: adapt_decl i32
  238. // CHECK:STDOUT:
  239. // CHECK:STDOUT: !members:
  240. // CHECK:STDOUT: .Self = constants.%A
  241. // CHECK:STDOUT: }
  242. // CHECK:STDOUT:
  243. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  244. // CHECK:STDOUT:
  245. // CHECK:STDOUT: fn @__global_init() {
  246. // CHECK:STDOUT: !entry:
  247. // CHECK:STDOUT: %.loc8_13: i32 = int_literal 1 [template = constants.%.2]
  248. // CHECK:STDOUT: %int.make_type_32.loc8: init type = call constants.%Int32() [template = i32]
  249. // CHECK:STDOUT: %.loc8_18.1: type = value_of_initializer %int.make_type_32.loc8 [template = i32]
  250. // CHECK:STDOUT: %.loc8_18.2: type = converted %int.make_type_32.loc8, %.loc8_18.1 [template = i32]
  251. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  252. // CHECK:STDOUT: %.loc8_23.1: %A = as_compatible %.loc8_13 [template = constants.%.2]
  253. // CHECK:STDOUT: %.loc8_23.2: %A = converted %.loc8_13, %.loc8_23.1 [template = constants.%.2]
  254. // CHECK:STDOUT: %a: %A = bind_name a, %.loc8_23.2
  255. // CHECK:STDOUT: %a.ref: %A = name_ref a, %a
  256. // CHECK:STDOUT: %int.make_type_32.loc9: init type = call constants.%Int32() [template = i32]
  257. // CHECK:STDOUT: %.loc9_19.1: type = value_of_initializer %int.make_type_32.loc9 [template = i32]
  258. // CHECK:STDOUT: %.loc9_19.2: type = converted %int.make_type_32.loc9, %.loc9_19.1 [template = i32]
  259. // CHECK:STDOUT: %.loc9_16.1: i32 = as_compatible %a.ref
  260. // CHECK:STDOUT: %.loc9_16.2: i32 = converted %a.ref, %.loc9_16.1
  261. // CHECK:STDOUT: %n: i32 = bind_name n, %.loc9_16.2
  262. // CHECK:STDOUT: return
  263. // CHECK:STDOUT: }
  264. // CHECK:STDOUT:
  265. // CHECK:STDOUT: --- multi_level_adapt.carbon
  266. // CHECK:STDOUT:
  267. // CHECK:STDOUT: constants {
  268. // CHECK:STDOUT: %A: type = class_type @A [template]
  269. // CHECK:STDOUT: %.1: type = struct_type {} [template]
  270. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  271. // CHECK:STDOUT: %B: type = class_type @B [template]
  272. // CHECK:STDOUT: %C: type = class_type @C [template]
  273. // CHECK:STDOUT: %D: type = class_type @D [template]
  274. // CHECK:STDOUT: %struct: %.1 = struct_value () [template]
  275. // CHECK:STDOUT: }
  276. // CHECK:STDOUT:
  277. // CHECK:STDOUT: file {
  278. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  279. // CHECK:STDOUT: .Core = %Core
  280. // CHECK:STDOUT: .A = %A.decl
  281. // CHECK:STDOUT: .B = %B.decl
  282. // CHECK:STDOUT: .C = %C.decl
  283. // CHECK:STDOUT: .D = %D.decl
  284. // CHECK:STDOUT: .d = @__global_init.%d
  285. // CHECK:STDOUT: }
  286. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  287. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {}
  288. // CHECK:STDOUT: %B.decl: type = class_decl @B [template = constants.%B] {}
  289. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {}
  290. // CHECK:STDOUT: %D.decl: type = class_decl @D [template = constants.%D] {}
  291. // CHECK:STDOUT: %D.ref: type = name_ref D, %D.decl [template = constants.%D]
  292. // CHECK:STDOUT: }
  293. // CHECK:STDOUT:
  294. // CHECK:STDOUT: class @A {
  295. // CHECK:STDOUT: %.loc4_18: %.1 = struct_literal ()
  296. // CHECK:STDOUT: %.loc4_19: type = converted %.loc4_18, constants.%.1 [template = constants.%.1]
  297. // CHECK:STDOUT: adapt_decl %.1
  298. // CHECK:STDOUT:
  299. // CHECK:STDOUT: !members:
  300. // CHECK:STDOUT: .Self = constants.%A
  301. // CHECK:STDOUT: }
  302. // CHECK:STDOUT:
  303. // CHECK:STDOUT: class @B {
  304. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  305. // CHECK:STDOUT: adapt_decl %A
  306. // CHECK:STDOUT:
  307. // CHECK:STDOUT: !members:
  308. // CHECK:STDOUT: .Self = constants.%B
  309. // CHECK:STDOUT: }
  310. // CHECK:STDOUT:
  311. // CHECK:STDOUT: class @C {
  312. // CHECK:STDOUT: %B.ref: type = name_ref B, file.%B.decl [template = constants.%B]
  313. // CHECK:STDOUT: adapt_decl %B
  314. // CHECK:STDOUT:
  315. // CHECK:STDOUT: !members:
  316. // CHECK:STDOUT: .Self = constants.%C
  317. // CHECK:STDOUT: }
  318. // CHECK:STDOUT:
  319. // CHECK:STDOUT: class @D {
  320. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  321. // CHECK:STDOUT: adapt_decl %C
  322. // CHECK:STDOUT:
  323. // CHECK:STDOUT: !members:
  324. // CHECK:STDOUT: .Self = constants.%D
  325. // CHECK:STDOUT: }
  326. // CHECK:STDOUT:
  327. // CHECK:STDOUT: fn @__global_init() {
  328. // CHECK:STDOUT: !entry:
  329. // CHECK:STDOUT: %.loc9_13: %.1 = struct_literal ()
  330. // CHECK:STDOUT: %D.ref: type = name_ref D, file.%D.decl [template = constants.%D]
  331. // CHECK:STDOUT: %struct: %.1 = struct_value () [template = constants.%struct]
  332. // CHECK:STDOUT: %.loc9_15.1: %D = as_compatible %struct [template = constants.%struct]
  333. // CHECK:STDOUT: %.loc9_15.2: %D = converted %.loc9_13, %.loc9_15.1 [template = constants.%struct]
  334. // CHECK:STDOUT: %d: %D = bind_name d, %.loc9_15.2
  335. // CHECK:STDOUT: return
  336. // CHECK:STDOUT: }
  337. // CHECK:STDOUT:
  338. // CHECK:STDOUT: --- fail_init_class.carbon
  339. // CHECK:STDOUT:
  340. // CHECK:STDOUT: constants {
  341. // CHECK:STDOUT: %A: type = class_type @A [template]
  342. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  343. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  344. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  345. // CHECK:STDOUT: %.2: type = unbound_element_type %A, i32 [template]
  346. // CHECK:STDOUT: %.3: type = struct_type {.x: i32, .y: i32} [template]
  347. // CHECK:STDOUT: %B: type = class_type @B [template]
  348. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  349. // CHECK:STDOUT: %.5: i32 = int_literal 1 [template]
  350. // CHECK:STDOUT: %.6: i32 = int_literal 2 [template]
  351. // CHECK:STDOUT: %struct: %A = struct_value (%.5, %.6) [template]
  352. // CHECK:STDOUT: }
  353. // CHECK:STDOUT:
  354. // CHECK:STDOUT: file {
  355. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  356. // CHECK:STDOUT: .Core = %Core
  357. // CHECK:STDOUT: .A = %A.decl
  358. // CHECK:STDOUT: .B = %B.decl
  359. // CHECK:STDOUT: .b_value = @__global_init.%b_value
  360. // CHECK:STDOUT: .b_init = %b_init
  361. // CHECK:STDOUT: }
  362. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  363. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {}
  364. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  365. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  366. // CHECK:STDOUT: %B.decl: type = class_decl @B [template = constants.%B] {}
  367. // CHECK:STDOUT: %B.ref.loc13: type = name_ref B, %B.decl [template = constants.%B]
  368. // CHECK:STDOUT: %B.ref.loc24: type = name_ref B, %B.decl [template = constants.%B]
  369. // CHECK:STDOUT: %b_init.var: ref %B = var b_init
  370. // CHECK:STDOUT: %b_init: ref %B = bind_name b_init, %b_init.var
  371. // CHECK:STDOUT: }
  372. // CHECK:STDOUT:
  373. // CHECK:STDOUT: class @A {
  374. // CHECK:STDOUT: %int.make_type_32.loc5: init type = call constants.%Int32() [template = i32]
  375. // CHECK:STDOUT: %.loc5_10.1: type = value_of_initializer %int.make_type_32.loc5 [template = i32]
  376. // CHECK:STDOUT: %.loc5_10.2: type = converted %int.make_type_32.loc5, %.loc5_10.1 [template = i32]
  377. // CHECK:STDOUT: %.loc5_8: %.2 = field_decl x, element0 [template]
  378. // CHECK:STDOUT: %int.make_type_32.loc6: init type = call constants.%Int32() [template = i32]
  379. // CHECK:STDOUT: %.loc6_10.1: type = value_of_initializer %int.make_type_32.loc6 [template = i32]
  380. // CHECK:STDOUT: %.loc6_10.2: type = converted %int.make_type_32.loc6, %.loc6_10.1 [template = i32]
  381. // CHECK:STDOUT: %.loc6_8: %.2 = field_decl y, element1 [template]
  382. // CHECK:STDOUT:
  383. // CHECK:STDOUT: !members:
  384. // CHECK:STDOUT: .Self = constants.%A
  385. // CHECK:STDOUT: .x = %.loc5_8
  386. // CHECK:STDOUT: .y = %.loc6_8
  387. // CHECK:STDOUT: }
  388. // CHECK:STDOUT:
  389. // CHECK:STDOUT: class @B {
  390. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  391. // CHECK:STDOUT: adapt_decl %A
  392. // CHECK:STDOUT:
  393. // CHECK:STDOUT: !members:
  394. // CHECK:STDOUT: .Self = constants.%B
  395. // CHECK:STDOUT: }
  396. // CHECK:STDOUT:
  397. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  398. // CHECK:STDOUT:
  399. // CHECK:STDOUT: fn @__global_init() {
  400. // CHECK:STDOUT: !entry:
  401. // CHECK:STDOUT: %.loc13_25: i32 = int_literal 1 [template = constants.%.5]
  402. // CHECK:STDOUT: %.loc13_33: i32 = int_literal 2 [template = constants.%.6]
  403. // CHECK:STDOUT: %.loc13_34.1: %.3 = struct_literal (%.loc13_25, %.loc13_33)
  404. // CHECK:STDOUT: %A.ref.loc13: type = name_ref A, file.%A.decl [template = constants.%A]
  405. // CHECK:STDOUT: %.loc13_34.2: ref %A = temporary_storage
  406. // CHECK:STDOUT: %.loc13_34.3: ref i32 = class_element_access %.loc13_34.2, element0
  407. // CHECK:STDOUT: %.loc13_34.4: init i32 = initialize_from %.loc13_25 to %.loc13_34.3 [template = constants.%.5]
  408. // CHECK:STDOUT: %.loc13_34.5: ref i32 = class_element_access %.loc13_34.2, element1
  409. // CHECK:STDOUT: %.loc13_34.6: init i32 = initialize_from %.loc13_33 to %.loc13_34.5 [template = constants.%.6]
  410. // CHECK:STDOUT: %.loc13_34.7: init %A = class_init (%.loc13_34.4, %.loc13_34.6), %.loc13_34.2 [template = constants.%struct]
  411. // CHECK:STDOUT: %.loc13_34.8: ref %A = temporary %.loc13_34.2, %.loc13_34.7
  412. // CHECK:STDOUT: %.loc13_36: ref %A = converted %.loc13_34.1, %.loc13_34.8
  413. // CHECK:STDOUT: %B.ref.loc13: type = name_ref B, file.%B.decl [template = constants.%B]
  414. // CHECK:STDOUT: %.loc13_42.1: ref %B = as_compatible %.loc13_36
  415. // CHECK:STDOUT: %.loc13_42.2: ref %B = converted %.loc13_36, %.loc13_42.1
  416. // CHECK:STDOUT: %.loc13_42.3: %B = bind_value %.loc13_42.2
  417. // CHECK:STDOUT: %b_value: %B = bind_name b_value, %.loc13_42.3
  418. // CHECK:STDOUT: %.loc24_24: i32 = int_literal 1 [template = constants.%.5]
  419. // CHECK:STDOUT: %.loc24_32: i32 = int_literal 2 [template = constants.%.6]
  420. // CHECK:STDOUT: %.loc24_33.1: %.3 = struct_literal (%.loc24_24, %.loc24_32)
  421. // CHECK:STDOUT: %A.ref.loc24: type = name_ref A, file.%A.decl [template = constants.%A]
  422. // CHECK:STDOUT: %.loc24_33.2: ref %A = temporary_storage
  423. // CHECK:STDOUT: %.loc24_33.3: ref i32 = class_element_access %.loc24_33.2, element0
  424. // CHECK:STDOUT: %.loc24_33.4: init i32 = initialize_from %.loc24_24 to %.loc24_33.3 [template = constants.%.5]
  425. // CHECK:STDOUT: %.loc24_33.5: ref i32 = class_element_access %.loc24_33.2, element1
  426. // CHECK:STDOUT: %.loc24_33.6: init i32 = initialize_from %.loc24_32 to %.loc24_33.5 [template = constants.%.6]
  427. // CHECK:STDOUT: %.loc24_33.7: init %A = class_init (%.loc24_33.4, %.loc24_33.6), %.loc24_33.2 [template = constants.%struct]
  428. // CHECK:STDOUT: %.loc24_33.8: ref %A = temporary %.loc24_33.2, %.loc24_33.7
  429. // CHECK:STDOUT: %.loc24_35: ref %A = converted %.loc24_33.1, %.loc24_33.8
  430. // CHECK:STDOUT: %B.ref.loc24: type = name_ref B, file.%B.decl [template = constants.%B]
  431. // CHECK:STDOUT: %.loc24_41.1: ref %B = as_compatible %.loc24_35
  432. // CHECK:STDOUT: %.loc24_41.2: ref %B = converted %.loc24_35, %.loc24_41.1
  433. // CHECK:STDOUT: %.loc24_41.3: %B = bind_value %.loc24_41.2
  434. // CHECK:STDOUT: assign file.%b_init.var, <error>
  435. // CHECK:STDOUT: return
  436. // CHECK:STDOUT: }
  437. // CHECK:STDOUT:
  438. // CHECK:STDOUT: --- fail_adapt_init_from_struct.carbon
  439. // CHECK:STDOUT:
  440. // CHECK:STDOUT: constants {
  441. // CHECK:STDOUT: %A: type = class_type @A [template]
  442. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  443. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  444. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  445. // CHECK:STDOUT: %.2: type = unbound_element_type %A, i32 [template]
  446. // CHECK:STDOUT: %.3: type = struct_type {.x: i32} [template]
  447. // CHECK:STDOUT: %B: type = class_type @B [template]
  448. // CHECK:STDOUT: %.4: type = ptr_type %.3 [template]
  449. // CHECK:STDOUT: %.5: i32 = int_literal 1 [template]
  450. // CHECK:STDOUT: }
  451. // CHECK:STDOUT:
  452. // CHECK:STDOUT: file {
  453. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  454. // CHECK:STDOUT: .Core = %Core
  455. // CHECK:STDOUT: .A = %A.decl
  456. // CHECK:STDOUT: .B = %B.decl
  457. // CHECK:STDOUT: .b = %b
  458. // CHECK:STDOUT: }
  459. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  460. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {}
  461. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  462. // CHECK:STDOUT: %B.decl: type = class_decl @B [template = constants.%B] {}
  463. // CHECK:STDOUT: %B.ref: type = name_ref B, %B.decl [template = constants.%B]
  464. // CHECK:STDOUT: %b.var: ref %B = var b
  465. // CHECK:STDOUT: %b: ref %B = bind_name b, %b.var
  466. // CHECK:STDOUT: }
  467. // CHECK:STDOUT:
  468. // CHECK:STDOUT: class @A {
  469. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  470. // CHECK:STDOUT: %.loc5_10.1: type = value_of_initializer %int.make_type_32 [template = i32]
  471. // CHECK:STDOUT: %.loc5_10.2: type = converted %int.make_type_32, %.loc5_10.1 [template = i32]
  472. // CHECK:STDOUT: %.loc5_8: %.2 = field_decl x, element0 [template]
  473. // CHECK:STDOUT:
  474. // CHECK:STDOUT: !members:
  475. // CHECK:STDOUT: .Self = constants.%A
  476. // CHECK:STDOUT: .x = %.loc5_8
  477. // CHECK:STDOUT: }
  478. // CHECK:STDOUT:
  479. // CHECK:STDOUT: class @B {
  480. // CHECK:STDOUT: %A.ref: type = name_ref A, file.%A.decl [template = constants.%A]
  481. // CHECK:STDOUT: adapt_decl %A
  482. // CHECK:STDOUT:
  483. // CHECK:STDOUT: !members:
  484. // CHECK:STDOUT: .Self = constants.%B
  485. // CHECK:STDOUT: }
  486. // CHECK:STDOUT:
  487. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  488. // CHECK:STDOUT:
  489. // CHECK:STDOUT: fn @__global_init() {
  490. // CHECK:STDOUT: !entry:
  491. // CHECK:STDOUT: %.loc18_18: i32 = int_literal 1 [template = constants.%.5]
  492. // CHECK:STDOUT: %.loc18_19: %.3 = struct_literal (%.loc18_18)
  493. // CHECK:STDOUT: %B.ref: type = name_ref B, file.%B.decl [template = constants.%B]
  494. // CHECK:STDOUT: assign file.%b.var, <error>
  495. // CHECK:STDOUT: return
  496. // CHECK:STDOUT: }
  497. // CHECK:STDOUT: