call.carbon 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/call.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/generic/call.carbon
  10. // --- call.carbon
  11. library "call";
  12. class Class(T:! type, N:! i32) {}
  13. var a: Class(i32*, 5);
  14. // Requires an implicit conversion to type `type`.
  15. var b: Class((), 0);
  16. // --- fail_too_few.carbon
  17. library "too_few";
  18. class Class(T:! type, N:! i32) {}
  19. // CHECK:STDERR: fail_too_few.carbon:[[@LINE+7]]:8: ERROR: 1 argument(s) passed to function expecting 2 argument(s).
  20. // CHECK:STDERR: var a: Class(i32*);
  21. // CHECK:STDERR: ^~~~~~
  22. // CHECK:STDERR: fail_too_few.carbon:[[@LINE-5]]:1: Calling function declared here.
  23. // CHECK:STDERR: class Class(T:! type, N:! i32) {}
  24. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  25. // CHECK:STDERR:
  26. var a: Class(i32*);
  27. // --- fail_too_many.carbon
  28. library "too_many";
  29. class Class(T:! type, N:! i32) {}
  30. // CHECK:STDERR: fail_too_many.carbon:[[@LINE+7]]:8: ERROR: 3 argument(s) passed to function expecting 2 argument(s).
  31. // CHECK:STDERR: var a: Class(i32*, 1, 2);
  32. // CHECK:STDERR: ^~~~~~
  33. // CHECK:STDERR: fail_too_many.carbon:[[@LINE-5]]:1: Calling function declared here.
  34. // CHECK:STDERR: class Class(T:! type, N:! i32) {}
  35. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  36. // CHECK:STDERR:
  37. var a: Class(i32*, 1, 2);
  38. // --- fail_no_conversion.carbon
  39. library "no_conversion";
  40. class Class(T:! type, N:! i32) {}
  41. // CHECK:STDERR: fail_no_conversion.carbon:[[@LINE+6]]:8: ERROR: Cannot implicitly convert from `i32` to `type`.
  42. // CHECK:STDERR: var a: Class(5, i32*);
  43. // CHECK:STDERR: ^~~~~~
  44. // CHECK:STDERR: fail_no_conversion.carbon:[[@LINE-5]]:1: Initializing parameter 1 of function declared here.
  45. // CHECK:STDERR: class Class(T:! type, N:! i32) {}
  46. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  47. var a: Class(5, i32*);
  48. // CHECK:STDOUT: --- call.carbon
  49. // CHECK:STDOUT:
  50. // CHECK:STDOUT: constants {
  51. // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic]
  52. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  53. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  54. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  55. // CHECK:STDOUT: %N: i32 = bind_symbolic_name N 1 [symbolic]
  56. // CHECK:STDOUT: %Class.type: type = generic_class_type @Class [template]
  57. // CHECK:STDOUT: %Class.1: %Class.type = struct_value () [template]
  58. // CHECK:STDOUT: %Class.2: type = class_type @Class, (%T, %N) [symbolic]
  59. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  60. // CHECK:STDOUT: %.3: type = ptr_type i32 [template]
  61. // CHECK:STDOUT: %.4: i32 = int_literal 5 [template]
  62. // CHECK:STDOUT: %Class.3: type = class_type @Class, (%.3, %.4) [template]
  63. // CHECK:STDOUT: %.5: type = ptr_type %.2 [template]
  64. // CHECK:STDOUT: %.6: i32 = int_literal 0 [template]
  65. // CHECK:STDOUT: %Class.4: type = class_type @Class, (%.1, %.6) [template]
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: file {
  69. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  70. // CHECK:STDOUT: .Core = %Core
  71. // CHECK:STDOUT: .Class = %Class.decl
  72. // CHECK:STDOUT: .a = %a
  73. // CHECK:STDOUT: .b = %b
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  76. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  77. // CHECK:STDOUT: %Class.decl: %Class.type = class_decl @Class [template = constants.%Class.1] {
  78. // CHECK:STDOUT: %T.loc4_13.1: type = param T
  79. // CHECK:STDOUT: %T.loc4_13.2: type = bind_symbolic_name T 0, %T.loc4_13.1 [symbolic = constants.%T]
  80. // CHECK:STDOUT: %int.make_type_32.loc4: init type = call constants.%Int32() [template = i32]
  81. // CHECK:STDOUT: %.loc4_27.1: type = value_of_initializer %int.make_type_32.loc4 [template = i32]
  82. // CHECK:STDOUT: %.loc4_27.2: type = converted %int.make_type_32.loc4, %.loc4_27.1 [template = i32]
  83. // CHECK:STDOUT: %N.loc4_23.1: i32 = param N
  84. // CHECK:STDOUT: %N.loc4_23.2: i32 = bind_symbolic_name N 1, %N.loc4_23.1 [symbolic = constants.%N]
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT: %Class.ref.loc6: %Class.type = name_ref Class, %Class.decl [template = constants.%Class.1]
  87. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  88. // CHECK:STDOUT: %int.make_type_32.loc6: init type = call constants.%Int32() [template = i32]
  89. // CHECK:STDOUT: %.loc6_17.1: type = value_of_initializer %int.make_type_32.loc6 [template = i32]
  90. // CHECK:STDOUT: %.loc6_17.2: type = converted %int.make_type_32.loc6, %.loc6_17.1 [template = i32]
  91. // CHECK:STDOUT: %.loc6_17.3: type = ptr_type i32 [template = constants.%.3]
  92. // CHECK:STDOUT: %.loc6_20: i32 = int_literal 5 [template = constants.%.4]
  93. // CHECK:STDOUT: %.loc6_13: init type = call %Class.ref.loc6(%.loc6_17.3, %.loc6_20) [template = constants.%Class.3]
  94. // CHECK:STDOUT: %.loc6_21.1: type = value_of_initializer %.loc6_13 [template = constants.%Class.3]
  95. // CHECK:STDOUT: %.loc6_21.2: type = converted %.loc6_13, %.loc6_21.1 [template = constants.%Class.3]
  96. // CHECK:STDOUT: %a.var: ref %Class.3 = var a
  97. // CHECK:STDOUT: %a: ref %Class.3 = bind_name a, %a.var
  98. // CHECK:STDOUT: %Class.ref.loc9: %Class.type = name_ref Class, %Class.decl [template = constants.%Class.1]
  99. // CHECK:STDOUT: %.loc9_15: %.1 = tuple_literal ()
  100. // CHECK:STDOUT: %.loc9_18: i32 = int_literal 0 [template = constants.%.6]
  101. // CHECK:STDOUT: %.loc9_13.1: type = converted %.loc9_15, constants.%.1 [template = constants.%.1]
  102. // CHECK:STDOUT: %.loc9_13.2: init type = call %Class.ref.loc9(%.loc9_13.1, %.loc9_18) [template = constants.%Class.4]
  103. // CHECK:STDOUT: %.loc9_19.1: type = value_of_initializer %.loc9_13.2 [template = constants.%Class.4]
  104. // CHECK:STDOUT: %.loc9_19.2: type = converted %.loc9_13.2, %.loc9_19.1 [template = constants.%Class.4]
  105. // CHECK:STDOUT: %b.var: ref %Class.4 = var b
  106. // CHECK:STDOUT: %b: ref %Class.4 = bind_name b, %b.var
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT:
  109. // CHECK:STDOUT: class @Class
  110. // CHECK:STDOUT: generic [file.%T.loc4_13.2: type, file.%N.loc4_23.2: i32] {
  111. // CHECK:STDOUT: !members:
  112. // CHECK:STDOUT: .Self = constants.%Class.2
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: --- fail_too_few.carbon
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: constants {
  120. // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic]
  121. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  122. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  123. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  124. // CHECK:STDOUT: %N: i32 = bind_symbolic_name N 1 [symbolic]
  125. // CHECK:STDOUT: %Class.type: type = generic_class_type @Class [template]
  126. // CHECK:STDOUT: %Class.1: %Class.type = struct_value () [template]
  127. // CHECK:STDOUT: %Class.2: type = class_type @Class, (%T, %N) [symbolic]
  128. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  129. // CHECK:STDOUT: %.3: type = ptr_type i32 [template]
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: file {
  133. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  134. // CHECK:STDOUT: .Core = %Core
  135. // CHECK:STDOUT: .Class = %Class.decl
  136. // CHECK:STDOUT: .a = %a
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  139. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  140. // CHECK:STDOUT: %Class.decl: %Class.type = class_decl @Class [template = constants.%Class.1] {
  141. // CHECK:STDOUT: %T.loc4_13.1: type = param T
  142. // CHECK:STDOUT: %T.loc4_13.2: type = bind_symbolic_name T 0, %T.loc4_13.1 [symbolic = constants.%T]
  143. // CHECK:STDOUT: %int.make_type_32.loc4: init type = call constants.%Int32() [template = i32]
  144. // CHECK:STDOUT: %.loc4_27.1: type = value_of_initializer %int.make_type_32.loc4 [template = i32]
  145. // CHECK:STDOUT: %.loc4_27.2: type = converted %int.make_type_32.loc4, %.loc4_27.1 [template = i32]
  146. // CHECK:STDOUT: %N.loc4_23.1: i32 = param N
  147. // CHECK:STDOUT: %N.loc4_23.2: i32 = bind_symbolic_name N 1, %N.loc4_23.1 [symbolic = constants.%N]
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT: %Class.ref: %Class.type = name_ref Class, %Class.decl [template = constants.%Class.1]
  150. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  151. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  152. // CHECK:STDOUT: %.loc13_17.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  153. // CHECK:STDOUT: %.loc13_17.2: type = converted %int.make_type_32.loc13, %.loc13_17.1 [template = i32]
  154. // CHECK:STDOUT: %.loc13_17.3: type = ptr_type i32 [template = constants.%.3]
  155. // CHECK:STDOUT: %.loc13_13: init type = call %Class.ref(<invalid>) [template = <error>]
  156. // CHECK:STDOUT: %.loc13_18.1: type = value_of_initializer %.loc13_13 [template = <error>]
  157. // CHECK:STDOUT: %.loc13_18.2: type = converted %.loc13_13, %.loc13_18.1 [template = <error>]
  158. // CHECK:STDOUT: %a.var: ref <error> = var a
  159. // CHECK:STDOUT: %a: ref <error> = bind_name a, %a.var
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: class @Class
  163. // CHECK:STDOUT: generic [file.%T.loc4_13.2: type, file.%N.loc4_23.2: i32] {
  164. // CHECK:STDOUT: !members:
  165. // CHECK:STDOUT: .Self = constants.%Class.2
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: --- fail_too_many.carbon
  171. // CHECK:STDOUT:
  172. // CHECK:STDOUT: constants {
  173. // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic]
  174. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  175. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  176. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  177. // CHECK:STDOUT: %N: i32 = bind_symbolic_name N 1 [symbolic]
  178. // CHECK:STDOUT: %Class.type: type = generic_class_type @Class [template]
  179. // CHECK:STDOUT: %Class.1: %Class.type = struct_value () [template]
  180. // CHECK:STDOUT: %Class.2: type = class_type @Class, (%T, %N) [symbolic]
  181. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  182. // CHECK:STDOUT: %.3: type = ptr_type i32 [template]
  183. // CHECK:STDOUT: %.4: i32 = int_literal 1 [template]
  184. // CHECK:STDOUT: %.5: i32 = int_literal 2 [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: .Class = %Class.decl
  191. // CHECK:STDOUT: .a = %a
  192. // CHECK:STDOUT: }
  193. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  194. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  195. // CHECK:STDOUT: %Class.decl: %Class.type = class_decl @Class [template = constants.%Class.1] {
  196. // CHECK:STDOUT: %T.loc4_13.1: type = param T
  197. // CHECK:STDOUT: %T.loc4_13.2: type = bind_symbolic_name T 0, %T.loc4_13.1 [symbolic = constants.%T]
  198. // CHECK:STDOUT: %int.make_type_32.loc4: init type = call constants.%Int32() [template = i32]
  199. // CHECK:STDOUT: %.loc4_27.1: type = value_of_initializer %int.make_type_32.loc4 [template = i32]
  200. // CHECK:STDOUT: %.loc4_27.2: type = converted %int.make_type_32.loc4, %.loc4_27.1 [template = i32]
  201. // CHECK:STDOUT: %N.loc4_23.1: i32 = param N
  202. // CHECK:STDOUT: %N.loc4_23.2: i32 = bind_symbolic_name N 1, %N.loc4_23.1 [symbolic = constants.%N]
  203. // CHECK:STDOUT: }
  204. // CHECK:STDOUT: %Class.ref: %Class.type = name_ref Class, %Class.decl [template = constants.%Class.1]
  205. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  206. // CHECK:STDOUT: %int.make_type_32.loc13: init type = call constants.%Int32() [template = i32]
  207. // CHECK:STDOUT: %.loc13_17.1: type = value_of_initializer %int.make_type_32.loc13 [template = i32]
  208. // CHECK:STDOUT: %.loc13_17.2: type = converted %int.make_type_32.loc13, %.loc13_17.1 [template = i32]
  209. // CHECK:STDOUT: %.loc13_17.3: type = ptr_type i32 [template = constants.%.3]
  210. // CHECK:STDOUT: %.loc13_20: i32 = int_literal 1 [template = constants.%.4]
  211. // CHECK:STDOUT: %.loc13_23: i32 = int_literal 2 [template = constants.%.5]
  212. // CHECK:STDOUT: %.loc13_13: init type = call %Class.ref(<invalid>) [template = <error>]
  213. // CHECK:STDOUT: %.loc13_24.1: type = value_of_initializer %.loc13_13 [template = <error>]
  214. // CHECK:STDOUT: %.loc13_24.2: type = converted %.loc13_13, %.loc13_24.1 [template = <error>]
  215. // CHECK:STDOUT: %a.var: ref <error> = var a
  216. // CHECK:STDOUT: %a: ref <error> = bind_name a, %a.var
  217. // CHECK:STDOUT: }
  218. // CHECK:STDOUT:
  219. // CHECK:STDOUT: class @Class
  220. // CHECK:STDOUT: generic [file.%T.loc4_13.2: type, file.%N.loc4_23.2: i32] {
  221. // CHECK:STDOUT: !members:
  222. // CHECK:STDOUT: .Self = constants.%Class.2
  223. // CHECK:STDOUT: }
  224. // CHECK:STDOUT:
  225. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  226. // CHECK:STDOUT:
  227. // CHECK:STDOUT: --- fail_no_conversion.carbon
  228. // CHECK:STDOUT:
  229. // CHECK:STDOUT: constants {
  230. // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic]
  231. // CHECK:STDOUT: %Int32.type: type = fn_type @Int32 [template]
  232. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  233. // CHECK:STDOUT: %Int32: %Int32.type = struct_value () [template]
  234. // CHECK:STDOUT: %N: i32 = bind_symbolic_name N 1 [symbolic]
  235. // CHECK:STDOUT: %Class.type: type = generic_class_type @Class [template]
  236. // CHECK:STDOUT: %Class.1: %Class.type = struct_value () [template]
  237. // CHECK:STDOUT: %Class.2: type = class_type @Class, (%T, %N) [symbolic]
  238. // CHECK:STDOUT: %.2: type = struct_type {} [template]
  239. // CHECK:STDOUT: %.3: i32 = int_literal 5 [template]
  240. // CHECK:STDOUT: %.4: type = ptr_type i32 [template]
  241. // CHECK:STDOUT: }
  242. // CHECK:STDOUT:
  243. // CHECK:STDOUT: file {
  244. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  245. // CHECK:STDOUT: .Core = %Core
  246. // CHECK:STDOUT: .Class = %Class.decl
  247. // CHECK:STDOUT: .a = %a
  248. // CHECK:STDOUT: }
  249. // CHECK:STDOUT: %Core: <namespace> = namespace [template] {}
  250. // CHECK:STDOUT: %import_ref.1: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  251. // CHECK:STDOUT: %Class.decl: %Class.type = class_decl @Class [template = constants.%Class.1] {
  252. // CHECK:STDOUT: %T.loc4_13.1: type = param T
  253. // CHECK:STDOUT: %T.loc4_13.2: type = bind_symbolic_name T 0, %T.loc4_13.1 [symbolic = constants.%T]
  254. // CHECK:STDOUT: %int.make_type_32.loc4: init type = call constants.%Int32() [template = i32]
  255. // CHECK:STDOUT: %.loc4_27.1: type = value_of_initializer %int.make_type_32.loc4 [template = i32]
  256. // CHECK:STDOUT: %.loc4_27.2: type = converted %int.make_type_32.loc4, %.loc4_27.1 [template = i32]
  257. // CHECK:STDOUT: %N.loc4_23.1: i32 = param N
  258. // CHECK:STDOUT: %N.loc4_23.2: i32 = bind_symbolic_name N 1, %N.loc4_23.1 [symbolic = constants.%N]
  259. // CHECK:STDOUT: }
  260. // CHECK:STDOUT: %Class.ref: %Class.type = name_ref Class, %Class.decl [template = constants.%Class.1]
  261. // CHECK:STDOUT: %.loc12_14: i32 = int_literal 5 [template = constants.%.3]
  262. // CHECK:STDOUT: %import_ref.2: %Int32.type = import_ref ir3, inst+3, loaded [template = constants.%Int32]
  263. // CHECK:STDOUT: %int.make_type_32.loc12: init type = call constants.%Int32() [template = i32]
  264. // CHECK:STDOUT: %.loc12_20.1: type = value_of_initializer %int.make_type_32.loc12 [template = i32]
  265. // CHECK:STDOUT: %.loc12_20.2: type = converted %int.make_type_32.loc12, %.loc12_20.1 [template = i32]
  266. // CHECK:STDOUT: %.loc12_20.3: type = ptr_type i32 [template = constants.%.4]
  267. // CHECK:STDOUT: %.loc12_13: init type = call %Class.ref(<invalid>) [template = <error>]
  268. // CHECK:STDOUT: %.loc12_21.1: type = value_of_initializer %.loc12_13 [template = <error>]
  269. // CHECK:STDOUT: %.loc12_21.2: type = converted %.loc12_13, %.loc12_21.1 [template = <error>]
  270. // CHECK:STDOUT: %a.var: ref <error> = var a
  271. // CHECK:STDOUT: %a: ref <error> = bind_name a, %a.var
  272. // CHECK:STDOUT: }
  273. // CHECK:STDOUT:
  274. // CHECK:STDOUT: class @Class
  275. // CHECK:STDOUT: generic [file.%T.loc4_13.2: type, file.%N.loc4_23.2: i32] {
  276. // CHECK:STDOUT: !members:
  277. // CHECK:STDOUT: .Self = constants.%Class.2
  278. // CHECK:STDOUT: }
  279. // CHECK:STDOUT:
  280. // CHECK:STDOUT: fn @Int32() -> type = "int.make_type_32";
  281. // CHECK:STDOUT: