init.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/class/generic/init.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/generic/init.carbon
  10. // --- init.carbon
  11. class Class(T:! type) {
  12. var k: T;
  13. }
  14. fn InitFromStructGeneric(T:! type, x: T) -> T {
  15. var v: Class(T) = {.k = x};
  16. return v.k;
  17. }
  18. // --- fail_todo_init.carbon
  19. library "fail_todo_init";
  20. class Class(T:! type) {
  21. var k: T;
  22. }
  23. // TODO: The following should work.
  24. fn InitFromStructSpecific() -> i32 {
  25. // CHECK:STDERR: fail_todo_init.carbon:[[@LINE+3]]:23: ERROR: Cannot implicitly convert from `i32` to `T`.
  26. // CHECK:STDERR: var v: Class(i32) = {.k = 0};
  27. // CHECK:STDERR: ^~~~~~~~
  28. var v: Class(i32) = {.k = 0};
  29. return v.k;
  30. }
  31. // CHECK:STDOUT: --- init.carbon
  32. // CHECK:STDOUT:
  33. // CHECK:STDOUT: constants {
  34. // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic]
  35. // CHECK:STDOUT: %Class.type: type = generic_class_type @Class [template]
  36. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  37. // CHECK:STDOUT: %Class.1: %Class.type = struct_value () [template]
  38. // CHECK:STDOUT: %Class.2: type = class_type @Class, @Class(%T) [symbolic]
  39. // CHECK:STDOUT: %.2: type = unbound_element_type %Class.2, %T [symbolic]
  40. // CHECK:STDOUT: %.3: type = struct_type {.k: %T} [symbolic]
  41. // CHECK:STDOUT: %InitFromStructGeneric.type: type = fn_type @InitFromStructGeneric [template]
  42. // CHECK:STDOUT: %InitFromStructGeneric: %InitFromStructGeneric.type = struct_value () [template]
  43. // CHECK:STDOUT: %.4: type = ptr_type %.3 [symbolic]
  44. // CHECK:STDOUT: }
  45. // CHECK:STDOUT:
  46. // CHECK:STDOUT: imports {
  47. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  48. // CHECK:STDOUT: import Core//prelude
  49. // CHECK:STDOUT: import Core//prelude/operators
  50. // CHECK:STDOUT: import Core//prelude/types
  51. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  52. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  53. // CHECK:STDOUT: import Core//prelude/operators/comparison
  54. // CHECK:STDOUT: import Core//prelude/types/bool
  55. // CHECK:STDOUT: }
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: file {
  59. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  60. // CHECK:STDOUT: .Core = imports.%Core
  61. // CHECK:STDOUT: .Class = %Class.decl
  62. // CHECK:STDOUT: .InitFromStructGeneric = %InitFromStructGeneric.decl
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %Core.import = import Core
  65. // CHECK:STDOUT: %Class.decl: %Class.type = class_decl @Class [template = constants.%Class.1] {
  66. // CHECK:STDOUT: %T.loc2_13.1: type = param T
  67. // CHECK:STDOUT: %T.loc2_13.2: type = bind_symbolic_name T 0, %T.loc2_13.1 [symbolic = @Class.%T (constants.%T)]
  68. // CHECK:STDOUT: }
  69. // CHECK:STDOUT: %InitFromStructGeneric.decl: %InitFromStructGeneric.type = fn_decl @InitFromStructGeneric [template = constants.%InitFromStructGeneric] {
  70. // CHECK:STDOUT: %T.loc6_26.1: type = param T
  71. // CHECK:STDOUT: @InitFromStructGeneric.%T.loc6: type = bind_symbolic_name T 0, %T.loc6_26.1 [symbolic = @InitFromStructGeneric.%T.1 (constants.%T)]
  72. // CHECK:STDOUT: %T.ref.loc6_39: type = name_ref T, @InitFromStructGeneric.%T.loc6 [symbolic = @InitFromStructGeneric.%T.1 (constants.%T)]
  73. // CHECK:STDOUT: %x.loc6_36.1: @InitFromStructGeneric.%T.1 (%T) = param x
  74. // CHECK:STDOUT: @InitFromStructGeneric.%x: @InitFromStructGeneric.%T.1 (%T) = bind_name x, %x.loc6_36.1
  75. // CHECK:STDOUT: %T.ref.loc6_45: type = name_ref T, @InitFromStructGeneric.%T.loc6 [symbolic = @InitFromStructGeneric.%T.1 (constants.%T)]
  76. // CHECK:STDOUT: @InitFromStructGeneric.%return: ref @InitFromStructGeneric.%T.1 (%T) = var <return slot>
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: generic class @Class(file.%T.loc2_13.2: type) {
  81. // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic = %T (constants.%T)]
  82. // CHECK:STDOUT:
  83. // CHECK:STDOUT: !definition:
  84. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T) [symbolic = %Class (constants.%Class.2)]
  85. // CHECK:STDOUT: %.1: type = unbound_element_type @Class.%Class (%Class.2), @Class.%T (%T) [symbolic = %.1 (constants.%.2)]
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: class {
  88. // CHECK:STDOUT: %T.ref: type = name_ref T, file.%T.loc2_13.2 [symbolic = %T (constants.%T)]
  89. // CHECK:STDOUT: %.loc3: @Class.%.1 (%.2) = field_decl k, element0 [template]
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: !members:
  92. // CHECK:STDOUT: .Self = constants.%Class.2
  93. // CHECK:STDOUT: .k = %.loc3
  94. // CHECK:STDOUT: }
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: generic fn @InitFromStructGeneric(%T.loc6: type) {
  98. // CHECK:STDOUT: %T.1: type = bind_symbolic_name T 0 [symbolic = %T.1 (constants.%T)]
  99. // CHECK:STDOUT:
  100. // CHECK:STDOUT: !definition:
  101. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T.1) [symbolic = %Class (constants.%Class.2)]
  102. // CHECK:STDOUT: %.1: type = struct_type {.k: @InitFromStructGeneric.%T.1 (%T)} [symbolic = %.1 (constants.%.3)]
  103. // CHECK:STDOUT: %.2: type = unbound_element_type @InitFromStructGeneric.%Class (%Class.2), @InitFromStructGeneric.%T.1 (%T) [symbolic = %.2 (constants.%.2)]
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: fn(%T.loc6: type, %x: @InitFromStructGeneric.%T.1 (%T)) -> @InitFromStructGeneric.%T.1 (%T) {
  106. // CHECK:STDOUT: !entry:
  107. // CHECK:STDOUT: %Class.ref: %Class.type = name_ref Class, file.%Class.decl [template = constants.%Class.1]
  108. // CHECK:STDOUT: %T.ref: type = name_ref T, %T.loc6 [symbolic = %T.1 (constants.%T)]
  109. // CHECK:STDOUT: %.loc7_15: init type = call %Class.ref(%T.ref) [symbolic = %Class (constants.%Class.2)]
  110. // CHECK:STDOUT: %.loc7_17.1: type = value_of_initializer %.loc7_15 [symbolic = %Class (constants.%Class.2)]
  111. // CHECK:STDOUT: %.loc7_17.2: type = converted %.loc7_15, %.loc7_17.1 [symbolic = %Class (constants.%Class.2)]
  112. // CHECK:STDOUT: %v.var: ref @InitFromStructGeneric.%Class (%Class.2) = var v
  113. // CHECK:STDOUT: %v: ref @InitFromStructGeneric.%Class (%Class.2) = bind_name v, %v.var
  114. // CHECK:STDOUT: %x.ref: @InitFromStructGeneric.%T.1 (%T) = name_ref x, %x
  115. // CHECK:STDOUT: %.loc7_28.1: @InitFromStructGeneric.%.1 (%.3) = struct_literal (%x.ref)
  116. // CHECK:STDOUT: %.loc7_28.2: ref @InitFromStructGeneric.%T.1 (%T) = class_element_access %v.var, element0
  117. // CHECK:STDOUT: %.loc7_28.3: init @InitFromStructGeneric.%T.1 (%T) = initialize_from %x.ref to %.loc7_28.2
  118. // CHECK:STDOUT: %.loc7_28.4: init @InitFromStructGeneric.%Class (%Class.2) = class_init (%.loc7_28.3), %v.var
  119. // CHECK:STDOUT: %.loc7_29: init @InitFromStructGeneric.%Class (%Class.2) = converted %.loc7_28.1, %.loc7_28.4
  120. // CHECK:STDOUT: assign %v.var, %.loc7_29
  121. // CHECK:STDOUT: %v.ref: ref @InitFromStructGeneric.%Class (%Class.2) = name_ref v, %v
  122. // CHECK:STDOUT: %k.ref: @InitFromStructGeneric.%.2 (%.2) = name_ref k, @Class.%.loc3 [template = @Class.%.loc3]
  123. // CHECK:STDOUT: %.loc8_11.1: ref @InitFromStructGeneric.%T.1 (%T) = class_element_access %v.ref, element0
  124. // CHECK:STDOUT: %.loc8_11.2: @InitFromStructGeneric.%T.1 (%T) = bind_value %.loc8_11.1
  125. // CHECK:STDOUT: return %.loc8_11.2
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: specific @Class(constants.%T) {
  130. // CHECK:STDOUT: %T => constants.%T
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: !definition:
  133. // CHECK:STDOUT: %Class => constants.%Class.2
  134. // CHECK:STDOUT: %.1 => constants.%.2
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: specific @Class(@Class.%T) {
  138. // CHECK:STDOUT: %T => constants.%T
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: specific @InitFromStructGeneric(constants.%T) {
  142. // CHECK:STDOUT: %T.1 => constants.%T
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT:
  145. // CHECK:STDOUT: specific @Class(@InitFromStructGeneric.%T.1) {
  146. // CHECK:STDOUT: %T => constants.%T
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: --- fail_todo_init.carbon
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: constants {
  152. // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic]
  153. // CHECK:STDOUT: %Class.type: type = generic_class_type @Class [template]
  154. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  155. // CHECK:STDOUT: %Class.1: %Class.type = struct_value () [template]
  156. // CHECK:STDOUT: %Class.2: type = class_type @Class, @Class(%T) [symbolic]
  157. // CHECK:STDOUT: %.2: type = unbound_element_type %Class.2, %T [symbolic]
  158. // CHECK:STDOUT: %.3: type = struct_type {.k: %T} [symbolic]
  159. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  160. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  161. // CHECK:STDOUT: %InitFromStructSpecific.type: type = fn_type @InitFromStructSpecific [template]
  162. // CHECK:STDOUT: %InitFromStructSpecific: %InitFromStructSpecific.type = struct_value () [template]
  163. // CHECK:STDOUT: %Class.3: type = class_type @Class, @Class(i32) [template]
  164. // CHECK:STDOUT: %.4: type = unbound_element_type %Class.3, i32 [template]
  165. // CHECK:STDOUT: %.5: type = ptr_type %.3 [symbolic]
  166. // CHECK:STDOUT: %.6: i32 = int_literal 0 [template]
  167. // CHECK:STDOUT: %.7: type = struct_type {.k: i32} [template]
  168. // CHECK:STDOUT: }
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: imports {
  171. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  172. // CHECK:STDOUT: .Int32 = %import_ref
  173. // CHECK:STDOUT: import Core//prelude
  174. // CHECK:STDOUT: import Core//prelude/operators
  175. // CHECK:STDOUT: import Core//prelude/types
  176. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  177. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  178. // CHECK:STDOUT: import Core//prelude/operators/comparison
  179. // CHECK:STDOUT: import Core//prelude/types/bool
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT: %import_ref: %Int32.type = import_ref Core//prelude/types, inst+4, loaded [template = constants.%Int32]
  182. // CHECK:STDOUT: }
  183. // CHECK:STDOUT:
  184. // CHECK:STDOUT: file {
  185. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  186. // CHECK:STDOUT: .Core = imports.%Core
  187. // CHECK:STDOUT: .Class = %Class.decl
  188. // CHECK:STDOUT: .InitFromStructSpecific = %InitFromStructSpecific.decl
  189. // CHECK:STDOUT: }
  190. // CHECK:STDOUT: %Core.import = import Core
  191. // CHECK:STDOUT: %Class.decl: %Class.type = class_decl @Class [template = constants.%Class.1] {
  192. // CHECK:STDOUT: %T.loc4_13.1: type = param T
  193. // CHECK:STDOUT: %T.loc4_13.2: type = bind_symbolic_name T 0, %T.loc4_13.1 [symbolic = @Class.%T (constants.%T)]
  194. // CHECK:STDOUT: }
  195. // CHECK:STDOUT: %InitFromStructSpecific.decl: %InitFromStructSpecific.type = fn_decl @InitFromStructSpecific [template = constants.%InitFromStructSpecific] {
  196. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  197. // CHECK:STDOUT: %.loc9_32.1: type = value_of_initializer %int.make_type_32 [template = i32]
  198. // CHECK:STDOUT: %.loc9_32.2: type = converted %int.make_type_32, %.loc9_32.1 [template = i32]
  199. // CHECK:STDOUT: @InitFromStructSpecific.%return: ref i32 = var <return slot>
  200. // CHECK:STDOUT: }
  201. // CHECK:STDOUT: }
  202. // CHECK:STDOUT:
  203. // CHECK:STDOUT: generic class @Class(file.%T.loc4_13.2: type) {
  204. // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic = %T (constants.%T)]
  205. // CHECK:STDOUT:
  206. // CHECK:STDOUT: !definition:
  207. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T) [symbolic = %Class (constants.%Class.2)]
  208. // CHECK:STDOUT: %.1: type = unbound_element_type @Class.%Class (%Class.2), @Class.%T (%T) [symbolic = %.1 (constants.%.2)]
  209. // CHECK:STDOUT:
  210. // CHECK:STDOUT: class {
  211. // CHECK:STDOUT: %T.ref: type = name_ref T, file.%T.loc4_13.2 [symbolic = %T (constants.%T)]
  212. // CHECK:STDOUT: %.loc5: @Class.%.1 (%.2) = field_decl k, element0 [template]
  213. // CHECK:STDOUT:
  214. // CHECK:STDOUT: !members:
  215. // CHECK:STDOUT: .Self = constants.%Class.2
  216. // CHECK:STDOUT: .k = %.loc5
  217. // CHECK:STDOUT: }
  218. // CHECK:STDOUT: }
  219. // CHECK:STDOUT:
  220. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  221. // CHECK:STDOUT:
  222. // CHECK:STDOUT: fn @InitFromStructSpecific() -> i32 {
  223. // CHECK:STDOUT: !entry:
  224. // CHECK:STDOUT: %Class.ref: %Class.type = name_ref Class, file.%Class.decl [template = constants.%Class.1]
  225. // CHECK:STDOUT: %int.make_type_32: init type = call constants.%Int32() [template = i32]
  226. // CHECK:STDOUT: %.loc13_15.1: type = value_of_initializer %int.make_type_32 [template = i32]
  227. // CHECK:STDOUT: %.loc13_15.2: type = converted %int.make_type_32, %.loc13_15.1 [template = i32]
  228. // CHECK:STDOUT: %.loc13_15.3: init type = call %Class.ref(%.loc13_15.2) [template = constants.%Class.3]
  229. // CHECK:STDOUT: %.loc13_19.1: type = value_of_initializer %.loc13_15.3 [template = constants.%Class.3]
  230. // CHECK:STDOUT: %.loc13_19.2: type = converted %.loc13_15.3, %.loc13_19.1 [template = constants.%Class.3]
  231. // CHECK:STDOUT: %v.var: ref %Class.3 = var v
  232. // CHECK:STDOUT: %v: ref %Class.3 = bind_name v, %v.var
  233. // CHECK:STDOUT: %.loc13_29: i32 = int_literal 0 [template = constants.%.6]
  234. // CHECK:STDOUT: %.loc13_30: %.7 = struct_literal (%.loc13_29)
  235. // CHECK:STDOUT: assign %v.var, <error>
  236. // CHECK:STDOUT: %v.ref: ref %Class.3 = name_ref v, %v
  237. // CHECK:STDOUT: %k.ref: %.4 = name_ref k, @Class.%.loc5 [template = @Class.%.loc5]
  238. // CHECK:STDOUT: %.loc14_11.1: ref i32 = class_element_access %v.ref, element0
  239. // CHECK:STDOUT: %.loc14_11.2: i32 = bind_value %.loc14_11.1
  240. // CHECK:STDOUT: return %.loc14_11.2
  241. // CHECK:STDOUT: }
  242. // CHECK:STDOUT:
  243. // CHECK:STDOUT: specific @Class(constants.%T) {
  244. // CHECK:STDOUT: %T => constants.%T
  245. // CHECK:STDOUT: }
  246. // CHECK:STDOUT:
  247. // CHECK:STDOUT: specific @Class(@Class.%T) {
  248. // CHECK:STDOUT: %T => constants.%T
  249. // CHECK:STDOUT: }
  250. // CHECK:STDOUT:
  251. // CHECK:STDOUT: specific @Class(i32) {
  252. // CHECK:STDOUT: %T => i32
  253. // CHECK:STDOUT:
  254. // CHECK:STDOUT: !definition:
  255. // CHECK:STDOUT: %Class => constants.%Class.3
  256. // CHECK:STDOUT: %.1 => constants.%.4
  257. // CHECK:STDOUT: }
  258. // CHECK:STDOUT: