init_adapt.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. // --- fail_todo_init_adapt.carbon
  7. library "fail_todo_init_adapt" api;
  8. class C {
  9. var a: i32;
  10. var b: i32;
  11. }
  12. class AdaptC {
  13. adapt C;
  14. }
  15. let a: C = {.a = 1, .b = 2};
  16. // TODO: Allow these as explicit conversions.
  17. // CHECK:STDERR: fail_todo_init_adapt.carbon:[[@LINE+4]]:17: ERROR: Cannot convert from `C` to `AdaptC` with `as`.
  18. // CHECK:STDERR: let b: AdaptC = a as AdaptC;
  19. // CHECK:STDERR: ^~~~~~~~~~~
  20. // CHECK:STDERR:
  21. let b: AdaptC = a as AdaptC;
  22. // CHECK:STDERR: fail_todo_init_adapt.carbon:[[@LINE+4]]:12: ERROR: Cannot convert from `AdaptC` to `C` with `as`.
  23. // CHECK:STDERR: let c: C = b as C;
  24. // CHECK:STDERR: ^~~~~~
  25. // CHECK:STDERR:
  26. let c: C = b as C;
  27. fn MakeC() -> C;
  28. fn MakeAdaptC() -> AdaptC;
  29. // CHECK:STDERR: fail_todo_init_adapt.carbon:[[@LINE+4]]:17: ERROR: Cannot convert from `C` to `AdaptC` with `as`.
  30. // CHECK:STDERR: var d: AdaptC = MakeC() as AdaptC;
  31. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~
  32. // CHECK:STDERR:
  33. var d: AdaptC = MakeC() as AdaptC;
  34. // CHECK:STDERR: fail_todo_init_adapt.carbon:[[@LINE+4]]:12: ERROR: Cannot convert from `AdaptC` to `C` with `as`.
  35. // CHECK:STDERR: var e: C = MakeAdaptC() as C;
  36. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~
  37. // CHECK:STDERR:
  38. var e: C = MakeAdaptC() as C;
  39. // --- fail_not_implicit.carbon
  40. library "fail_not_implicit" api;
  41. class C {
  42. var a: i32;
  43. var b: i32;
  44. }
  45. class AdaptC {
  46. adapt C;
  47. }
  48. let a: C = {.a = 1, .b = 2};
  49. // Cannot implicitly convert between a type and an adapter for the type.
  50. // CHECK:STDERR: fail_not_implicit.carbon:[[@LINE+4]]:1: ERROR: Cannot implicitly convert from `C` to `AdaptC`.
  51. // CHECK:STDERR: let b: AdaptC = a;
  52. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
  53. // CHECK:STDERR:
  54. let b: AdaptC = a;
  55. // CHECK:STDERR: fail_not_implicit.carbon:[[@LINE+4]]:1: ERROR: Cannot implicitly convert from `AdaptC` to `C`.
  56. // CHECK:STDERR: let c: C = b;
  57. // CHECK:STDERR: ^~~~~~~~~~~~~
  58. // CHECK:STDERR:
  59. let c: C = b;
  60. fn MakeC() -> C;
  61. fn MakeAdaptC() -> AdaptC;
  62. // CHECK:STDERR: fail_not_implicit.carbon:[[@LINE+4]]:1: ERROR: Cannot implicitly convert from `C` to `AdaptC`.
  63. // CHECK:STDERR: var d: AdaptC = MakeC();
  64. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  65. // CHECK:STDERR:
  66. var d: AdaptC = MakeC();
  67. // CHECK:STDERR: fail_not_implicit.carbon:[[@LINE+3]]:1: ERROR: Cannot implicitly convert from `AdaptC` to `C`.
  68. // CHECK:STDERR: var e: C = MakeAdaptC();
  69. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~
  70. var e: C = MakeAdaptC();
  71. // CHECK:STDOUT: --- fail_todo_init_adapt.carbon
  72. // CHECK:STDOUT:
  73. // CHECK:STDOUT: constants {
  74. // CHECK:STDOUT: %C: type = class_type @C [template]
  75. // CHECK:STDOUT: %.1: type = unbound_element_type C, i32 [template]
  76. // CHECK:STDOUT: %.2: type = struct_type {.a: i32, .b: i32} [template]
  77. // CHECK:STDOUT: %AdaptC: type = class_type @AdaptC [template]
  78. // CHECK:STDOUT: %.3: type = ptr_type {.a: i32, .b: i32} [template]
  79. // CHECK:STDOUT: %.4: i32 = int_literal 1 [template]
  80. // CHECK:STDOUT: %.5: i32 = int_literal 2 [template]
  81. // CHECK:STDOUT: %.6: C = struct_value (%.4, %.5) [template]
  82. // CHECK:STDOUT: %.7: type = ptr_type C [template]
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: file {
  86. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  87. // CHECK:STDOUT: .Core = %Core
  88. // CHECK:STDOUT: .C = %C.decl
  89. // CHECK:STDOUT: .AdaptC = %AdaptC.decl
  90. // CHECK:STDOUT: .MakeC = %MakeC
  91. // CHECK:STDOUT: .MakeAdaptC = %MakeAdaptC
  92. // CHECK:STDOUT: .d = %d
  93. // CHECK:STDOUT: .e = %e
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  96. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {}
  97. // CHECK:STDOUT: %AdaptC.decl: type = class_decl @AdaptC [template = constants.%AdaptC] {}
  98. // CHECK:STDOUT: %C.ref.loc13: type = name_ref C, %C.decl [template = constants.%C]
  99. // CHECK:STDOUT: %.loc13_18: i32 = int_literal 1 [template = constants.%.4]
  100. // CHECK:STDOUT: %.loc13_26: i32 = int_literal 2 [template = constants.%.5]
  101. // CHECK:STDOUT: %.loc13_27.1: {.a: i32, .b: i32} = struct_literal (%.loc13_18, %.loc13_26)
  102. // CHECK:STDOUT: %.loc13_27.2: ref C = temporary_storage
  103. // CHECK:STDOUT: %.loc13_27.3: ref i32 = class_element_access %.loc13_27.2, element0
  104. // CHECK:STDOUT: %.loc13_27.4: init i32 = initialize_from %.loc13_18 to %.loc13_27.3 [template = constants.%.4]
  105. // CHECK:STDOUT: %.loc13_27.5: ref i32 = class_element_access %.loc13_27.2, element1
  106. // CHECK:STDOUT: %.loc13_27.6: init i32 = initialize_from %.loc13_26 to %.loc13_27.5 [template = constants.%.5]
  107. // CHECK:STDOUT: %.loc13_27.7: init C = class_init (%.loc13_27.4, %.loc13_27.6), %.loc13_27.2 [template = constants.%.6]
  108. // CHECK:STDOUT: %.loc13_27.8: ref C = temporary %.loc13_27.2, %.loc13_27.7
  109. // CHECK:STDOUT: %.loc13_27.9: ref C = converted %.loc13_27.1, %.loc13_27.8
  110. // CHECK:STDOUT: %.loc13_27.10: C = bind_value %.loc13_27.9
  111. // CHECK:STDOUT: %a: C = bind_name a, %.loc13_27.10
  112. // CHECK:STDOUT: %AdaptC.ref.loc21_8: type = name_ref AdaptC, %AdaptC.decl [template = constants.%AdaptC]
  113. // CHECK:STDOUT: %a.ref: C = name_ref a, %a
  114. // CHECK:STDOUT: %AdaptC.ref.loc21_22: type = name_ref AdaptC, %AdaptC.decl [template = constants.%AdaptC]
  115. // CHECK:STDOUT: %b: AdaptC = bind_name b, <error>
  116. // CHECK:STDOUT: %C.ref.loc27_8: type = name_ref C, %C.decl [template = constants.%C]
  117. // CHECK:STDOUT: %b.ref: AdaptC = name_ref b, %b
  118. // CHECK:STDOUT: %C.ref.loc27_17: type = name_ref C, %C.decl [template = constants.%C]
  119. // CHECK:STDOUT: %c: C = bind_name c, <error>
  120. // CHECK:STDOUT: %MakeC: <function> = fn_decl @MakeC [template] {
  121. // CHECK:STDOUT: %C.ref.loc29: type = name_ref C, %C.decl [template = constants.%C]
  122. // CHECK:STDOUT: @MakeC.%return: ref C = var <return slot>
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT: %MakeAdaptC: <function> = fn_decl @MakeAdaptC [template] {
  125. // CHECK:STDOUT: %AdaptC.ref.loc31: type = name_ref AdaptC, %AdaptC.decl [template = constants.%AdaptC]
  126. // CHECK:STDOUT: @MakeAdaptC.%return: ref AdaptC = var <return slot>
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT: %AdaptC.ref.loc37: type = name_ref AdaptC, %AdaptC.decl [template = constants.%AdaptC]
  129. // CHECK:STDOUT: %d.var: ref AdaptC = var d
  130. // CHECK:STDOUT: %d: ref AdaptC = bind_name d, %d.var
  131. // CHECK:STDOUT: %C.ref.loc43: type = name_ref C, %C.decl [template = constants.%C]
  132. // CHECK:STDOUT: %e.var: ref C = var e
  133. // CHECK:STDOUT: %e: ref C = bind_name e, %e.var
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: class @C {
  137. // CHECK:STDOUT: %.loc5: <unbound element of class C> = field_decl a, element0 [template]
  138. // CHECK:STDOUT: %.loc6: <unbound element of class C> = field_decl b, element1 [template]
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: !members:
  141. // CHECK:STDOUT: .Self = constants.%C
  142. // CHECK:STDOUT: .a = %.loc5
  143. // CHECK:STDOUT: .b = %.loc6
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: class @AdaptC {
  147. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  148. // CHECK:STDOUT: adapt_decl C
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: !members:
  151. // CHECK:STDOUT: .Self = constants.%AdaptC
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: fn @MakeC() -> %return: C;
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: fn @MakeAdaptC() -> %return: AdaptC;
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: fn @__global_init() {
  159. // CHECK:STDOUT: !entry:
  160. // CHECK:STDOUT: %MakeC.ref: <function> = name_ref MakeC, file.%MakeC [template = file.%MakeC]
  161. // CHECK:STDOUT: %.loc37_22.1: ref C = temporary_storage
  162. // CHECK:STDOUT: %.loc37_22.2: init C = call %MakeC.ref() to %.loc37_22.1
  163. // CHECK:STDOUT: %AdaptC.ref: type = name_ref AdaptC, file.%AdaptC.decl [template = constants.%AdaptC]
  164. // CHECK:STDOUT: assign file.%d.var, <error>
  165. // CHECK:STDOUT: %MakeAdaptC.ref: <function> = name_ref MakeAdaptC, file.%MakeAdaptC [template = file.%MakeAdaptC]
  166. // CHECK:STDOUT: %.loc43_22.1: ref AdaptC = temporary_storage
  167. // CHECK:STDOUT: %.loc43_22.2: init AdaptC = call %MakeAdaptC.ref() to %.loc43_22.1
  168. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  169. // CHECK:STDOUT: assign file.%e.var, <error>
  170. // CHECK:STDOUT: return
  171. // CHECK:STDOUT: }
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: --- fail_not_implicit.carbon
  174. // CHECK:STDOUT:
  175. // CHECK:STDOUT: constants {
  176. // CHECK:STDOUT: %C: type = class_type @C [template]
  177. // CHECK:STDOUT: %.1: type = unbound_element_type C, i32 [template]
  178. // CHECK:STDOUT: %.2: type = struct_type {.a: i32, .b: i32} [template]
  179. // CHECK:STDOUT: %AdaptC: type = class_type @AdaptC [template]
  180. // CHECK:STDOUT: %.3: type = ptr_type {.a: i32, .b: i32} [template]
  181. // CHECK:STDOUT: %.4: i32 = int_literal 1 [template]
  182. // CHECK:STDOUT: %.5: i32 = int_literal 2 [template]
  183. // CHECK:STDOUT: %.6: C = struct_value (%.4, %.5) [template]
  184. // CHECK:STDOUT: %.7: type = ptr_type C [template]
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: file {
  188. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  189. // CHECK:STDOUT: .Core = %Core
  190. // CHECK:STDOUT: .C = %C.decl
  191. // CHECK:STDOUT: .AdaptC = %AdaptC.decl
  192. // CHECK:STDOUT: .MakeC = %MakeC
  193. // CHECK:STDOUT: .MakeAdaptC = %MakeAdaptC
  194. // CHECK:STDOUT: .d = %d
  195. // CHECK:STDOUT: .e = %e
  196. // CHECK:STDOUT: }
  197. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  198. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {}
  199. // CHECK:STDOUT: %AdaptC.decl: type = class_decl @AdaptC [template = constants.%AdaptC] {}
  200. // CHECK:STDOUT: %C.ref.loc13: type = name_ref C, %C.decl [template = constants.%C]
  201. // CHECK:STDOUT: %.loc13_18: i32 = int_literal 1 [template = constants.%.4]
  202. // CHECK:STDOUT: %.loc13_26: i32 = int_literal 2 [template = constants.%.5]
  203. // CHECK:STDOUT: %.loc13_27.1: {.a: i32, .b: i32} = struct_literal (%.loc13_18, %.loc13_26)
  204. // CHECK:STDOUT: %.loc13_27.2: ref C = temporary_storage
  205. // CHECK:STDOUT: %.loc13_27.3: ref i32 = class_element_access %.loc13_27.2, element0
  206. // CHECK:STDOUT: %.loc13_27.4: init i32 = initialize_from %.loc13_18 to %.loc13_27.3 [template = constants.%.4]
  207. // CHECK:STDOUT: %.loc13_27.5: ref i32 = class_element_access %.loc13_27.2, element1
  208. // CHECK:STDOUT: %.loc13_27.6: init i32 = initialize_from %.loc13_26 to %.loc13_27.5 [template = constants.%.5]
  209. // CHECK:STDOUT: %.loc13_27.7: init C = class_init (%.loc13_27.4, %.loc13_27.6), %.loc13_27.2 [template = constants.%.6]
  210. // CHECK:STDOUT: %.loc13_27.8: ref C = temporary %.loc13_27.2, %.loc13_27.7
  211. // CHECK:STDOUT: %.loc13_27.9: ref C = converted %.loc13_27.1, %.loc13_27.8
  212. // CHECK:STDOUT: %.loc13_27.10: C = bind_value %.loc13_27.9
  213. // CHECK:STDOUT: %a: C = bind_name a, %.loc13_27.10
  214. // CHECK:STDOUT: %AdaptC.ref.loc21: type = name_ref AdaptC, %AdaptC.decl [template = constants.%AdaptC]
  215. // CHECK:STDOUT: %a.ref: C = name_ref a, %a
  216. // CHECK:STDOUT: %b: AdaptC = bind_name b, <error>
  217. // CHECK:STDOUT: %C.ref.loc27: type = name_ref C, %C.decl [template = constants.%C]
  218. // CHECK:STDOUT: %b.ref: AdaptC = name_ref b, %b
  219. // CHECK:STDOUT: %c: C = bind_name c, <error>
  220. // CHECK:STDOUT: %MakeC: <function> = fn_decl @MakeC [template] {
  221. // CHECK:STDOUT: %C.ref.loc29: type = name_ref C, %C.decl [template = constants.%C]
  222. // CHECK:STDOUT: @MakeC.%return: ref C = var <return slot>
  223. // CHECK:STDOUT: }
  224. // CHECK:STDOUT: %MakeAdaptC: <function> = fn_decl @MakeAdaptC [template] {
  225. // CHECK:STDOUT: %AdaptC.ref.loc31: type = name_ref AdaptC, %AdaptC.decl [template = constants.%AdaptC]
  226. // CHECK:STDOUT: @MakeAdaptC.%return: ref AdaptC = var <return slot>
  227. // CHECK:STDOUT: }
  228. // CHECK:STDOUT: %AdaptC.ref.loc37: type = name_ref AdaptC, %AdaptC.decl [template = constants.%AdaptC]
  229. // CHECK:STDOUT: %d.var: ref AdaptC = var d
  230. // CHECK:STDOUT: %d: ref AdaptC = bind_name d, %d.var
  231. // CHECK:STDOUT: %C.ref.loc42: type = name_ref C, %C.decl [template = constants.%C]
  232. // CHECK:STDOUT: %e.var: ref C = var e
  233. // CHECK:STDOUT: %e: ref C = bind_name e, %e.var
  234. // CHECK:STDOUT: }
  235. // CHECK:STDOUT:
  236. // CHECK:STDOUT: class @C {
  237. // CHECK:STDOUT: %.loc5: <unbound element of class C> = field_decl a, element0 [template]
  238. // CHECK:STDOUT: %.loc6: <unbound element of class C> = field_decl b, element1 [template]
  239. // CHECK:STDOUT:
  240. // CHECK:STDOUT: !members:
  241. // CHECK:STDOUT: .Self = constants.%C
  242. // CHECK:STDOUT: .a = %.loc5
  243. // CHECK:STDOUT: .b = %.loc6
  244. // CHECK:STDOUT: }
  245. // CHECK:STDOUT:
  246. // CHECK:STDOUT: class @AdaptC {
  247. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  248. // CHECK:STDOUT: adapt_decl C
  249. // CHECK:STDOUT:
  250. // CHECK:STDOUT: !members:
  251. // CHECK:STDOUT: .Self = constants.%AdaptC
  252. // CHECK:STDOUT: }
  253. // CHECK:STDOUT:
  254. // CHECK:STDOUT: fn @MakeC() -> %return: C;
  255. // CHECK:STDOUT:
  256. // CHECK:STDOUT: fn @MakeAdaptC() -> %return: AdaptC;
  257. // CHECK:STDOUT:
  258. // CHECK:STDOUT: fn @__global_init() {
  259. // CHECK:STDOUT: !entry:
  260. // CHECK:STDOUT: %MakeC.ref: <function> = name_ref MakeC, file.%MakeC [template = file.%MakeC]
  261. // CHECK:STDOUT: %.loc37_22.1: ref C = temporary_storage
  262. // CHECK:STDOUT: %.loc37_22.2: init C = call %MakeC.ref() to %.loc37_22.1
  263. // CHECK:STDOUT: assign file.%d.var, <error>
  264. // CHECK:STDOUT: %MakeAdaptC.ref: <function> = name_ref MakeAdaptC, file.%MakeAdaptC [template = file.%MakeAdaptC]
  265. // CHECK:STDOUT: %.loc42_22.1: ref AdaptC = temporary_storage
  266. // CHECK:STDOUT: %.loc42_22.2: init AdaptC = call %MakeAdaptC.ref() to %.loc42_22.1
  267. // CHECK:STDOUT: assign file.%e.var, <error>
  268. // CHECK:STDOUT: return
  269. // CHECK:STDOUT: }
  270. // CHECK:STDOUT: