method_deduce.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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/method_deduce.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/generic/method_deduce.carbon
  10. class A {}
  11. class B {}
  12. class Class(T:! type) {
  13. fn Get(U:! type) -> (T, U);
  14. fn GetNoDeduce(x: T, U:! type) -> (T, U);
  15. }
  16. fn CallGenericMethod(c: Class(A)) -> (A, B) {
  17. return c.Get(B);
  18. }
  19. fn CallGenericMethodWithNonDeducedParam(c: Class(A)) -> (A, B) {
  20. return c.GetNoDeduce({}, B);
  21. }
  22. // CHECK:STDOUT: --- method_deduce.carbon
  23. // CHECK:STDOUT:
  24. // CHECK:STDOUT: constants {
  25. // CHECK:STDOUT: %A: type = class_type @A [template]
  26. // CHECK:STDOUT: %.1: type = struct_type {} [template]
  27. // CHECK:STDOUT: %B: type = class_type @B [template]
  28. // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic]
  29. // CHECK:STDOUT: %Class.type: type = generic_class_type @Class [template]
  30. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  31. // CHECK:STDOUT: %Class.1: %Class.type = struct_value () [template]
  32. // CHECK:STDOUT: %Class.2: type = class_type @Class, @Class(%T) [symbolic]
  33. // CHECK:STDOUT: %U: type = bind_symbolic_name U 1 [symbolic]
  34. // CHECK:STDOUT: %.3: type = tuple_type (type, type) [template]
  35. // CHECK:STDOUT: %.4: type = tuple_type (%T, %U) [symbolic]
  36. // CHECK:STDOUT: %Get.type.1: type = fn_type @Get, @Class(%T) [symbolic]
  37. // CHECK:STDOUT: %Get.1: %Get.type.1 = struct_value () [symbolic]
  38. // CHECK:STDOUT: %GetNoDeduce.type.1: type = fn_type @GetNoDeduce, @Class(%T) [symbolic]
  39. // CHECK:STDOUT: %GetNoDeduce.1: %GetNoDeduce.type.1 = struct_value () [symbolic]
  40. // CHECK:STDOUT: %Class.3: type = class_type @Class, @Class(%A) [template]
  41. // CHECK:STDOUT: %.5: type = tuple_type (%A, %B) [template]
  42. // CHECK:STDOUT: %CallGenericMethod.type: type = fn_type @CallGenericMethod [template]
  43. // CHECK:STDOUT: %CallGenericMethod: %CallGenericMethod.type = struct_value () [template]
  44. // CHECK:STDOUT: %.6: type = ptr_type %.1 [template]
  45. // CHECK:STDOUT: %.7: type = tuple_type (%.6, %.6) [template]
  46. // CHECK:STDOUT: %.8: type = ptr_type %.7 [template]
  47. // CHECK:STDOUT: %Get.type.2: type = fn_type @Get, @Class(%A) [template]
  48. // CHECK:STDOUT: %Get.2: %Get.type.2 = struct_value () [template]
  49. // CHECK:STDOUT: %GetNoDeduce.type.2: type = fn_type @GetNoDeduce, @Class(%A) [template]
  50. // CHECK:STDOUT: %GetNoDeduce.2: %GetNoDeduce.type.2 = struct_value () [template]
  51. // CHECK:STDOUT: %CallGenericMethodWithNonDeducedParam.type: type = fn_type @CallGenericMethodWithNonDeducedParam [template]
  52. // CHECK:STDOUT: %CallGenericMethodWithNonDeducedParam: %CallGenericMethodWithNonDeducedParam.type = struct_value () [template]
  53. // CHECK:STDOUT: %struct: %A = struct_value () [template]
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: imports {
  57. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [template] {
  58. // CHECK:STDOUT: import Core//prelude
  59. // CHECK:STDOUT: import Core//prelude/operators
  60. // CHECK:STDOUT: import Core//prelude/types
  61. // CHECK:STDOUT: import Core//prelude/operators/arithmetic
  62. // CHECK:STDOUT: import Core//prelude/operators/as
  63. // CHECK:STDOUT: import Core//prelude/operators/bitwise
  64. // CHECK:STDOUT: import Core//prelude/operators/comparison
  65. // CHECK:STDOUT: import Core//prelude/types/bool
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: file {
  70. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  71. // CHECK:STDOUT: .Core = imports.%Core
  72. // CHECK:STDOUT: .A = %A.decl
  73. // CHECK:STDOUT: .B = %B.decl
  74. // CHECK:STDOUT: .Class = %Class.decl
  75. // CHECK:STDOUT: .CallGenericMethod = %CallGenericMethod.decl
  76. // CHECK:STDOUT: .CallGenericMethodWithNonDeducedParam = %CallGenericMethodWithNonDeducedParam.decl
  77. // CHECK:STDOUT: }
  78. // CHECK:STDOUT: %Core.import = import Core
  79. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {}
  80. // CHECK:STDOUT: %B.decl: type = class_decl @B [template = constants.%B] {}
  81. // CHECK:STDOUT: %Class.decl: %Class.type = class_decl @Class [template = constants.%Class.1] {
  82. // CHECK:STDOUT: %T.loc14_13.1: type = param T
  83. // CHECK:STDOUT: %T.loc14_13.2: type = bind_symbolic_name T 0, %T.loc14_13.1 [symbolic = @Class.%T (constants.%T)]
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT: %CallGenericMethod.decl: %CallGenericMethod.type = fn_decl @CallGenericMethod [template = constants.%CallGenericMethod] {
  86. // CHECK:STDOUT: %Class.ref.loc19: %Class.type = name_ref Class, %Class.decl [template = constants.%Class.1]
  87. // CHECK:STDOUT: %A.ref.loc19_31: type = name_ref A, %A.decl [template = constants.%A]
  88. // CHECK:STDOUT: %.loc19_30: init type = call %Class.ref.loc19(%A.ref.loc19_31) [template = constants.%Class.3]
  89. // CHECK:STDOUT: %.loc19_32.1: type = value_of_initializer %.loc19_30 [template = constants.%Class.3]
  90. // CHECK:STDOUT: %.loc19_32.2: type = converted %.loc19_30, %.loc19_32.1 [template = constants.%Class.3]
  91. // CHECK:STDOUT: %c.loc19_22.1: %Class.3 = param c
  92. // CHECK:STDOUT: @CallGenericMethod.%c: %Class.3 = bind_name c, %c.loc19_22.1
  93. // CHECK:STDOUT: %A.ref.loc19_39: type = name_ref A, %A.decl [template = constants.%A]
  94. // CHECK:STDOUT: %B.ref.loc19: type = name_ref B, %B.decl [template = constants.%B]
  95. // CHECK:STDOUT: %.loc19_43.1: %.3 = tuple_literal (%A.ref.loc19_39, %B.ref.loc19)
  96. // CHECK:STDOUT: %.loc19_43.2: type = converted %.loc19_43.1, constants.%.5 [template = constants.%.5]
  97. // CHECK:STDOUT: @CallGenericMethod.%return: ref %.5 = var <return slot>
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: %CallGenericMethodWithNonDeducedParam.decl: %CallGenericMethodWithNonDeducedParam.type = fn_decl @CallGenericMethodWithNonDeducedParam [template = constants.%CallGenericMethodWithNonDeducedParam] {
  100. // CHECK:STDOUT: %Class.ref.loc23: %Class.type = name_ref Class, %Class.decl [template = constants.%Class.1]
  101. // CHECK:STDOUT: %A.ref.loc23_50: type = name_ref A, %A.decl [template = constants.%A]
  102. // CHECK:STDOUT: %.loc23_49: init type = call %Class.ref.loc23(%A.ref.loc23_50) [template = constants.%Class.3]
  103. // CHECK:STDOUT: %.loc23_51.1: type = value_of_initializer %.loc23_49 [template = constants.%Class.3]
  104. // CHECK:STDOUT: %.loc23_51.2: type = converted %.loc23_49, %.loc23_51.1 [template = constants.%Class.3]
  105. // CHECK:STDOUT: %c.loc23_41.1: %Class.3 = param c
  106. // CHECK:STDOUT: @CallGenericMethodWithNonDeducedParam.%c: %Class.3 = bind_name c, %c.loc23_41.1
  107. // CHECK:STDOUT: %A.ref.loc23_58: type = name_ref A, %A.decl [template = constants.%A]
  108. // CHECK:STDOUT: %B.ref.loc23: type = name_ref B, %B.decl [template = constants.%B]
  109. // CHECK:STDOUT: %.loc23_62.1: %.3 = tuple_literal (%A.ref.loc23_58, %B.ref.loc23)
  110. // CHECK:STDOUT: %.loc23_62.2: type = converted %.loc23_62.1, constants.%.5 [template = constants.%.5]
  111. // CHECK:STDOUT: @CallGenericMethodWithNonDeducedParam.%return: ref %.5 = var <return slot>
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT: }
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: class @A {
  116. // CHECK:STDOUT: !members:
  117. // CHECK:STDOUT: .Self = constants.%A
  118. // CHECK:STDOUT: }
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: class @B {
  121. // CHECK:STDOUT: !members:
  122. // CHECK:STDOUT: .Self = constants.%B
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: generic class @Class(file.%T.loc14_13.2: type) {
  126. // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic = %T (constants.%T)]
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: !definition:
  129. // CHECK:STDOUT: %Get.type: type = fn_type @Get, @Class(%T) [symbolic = %Get.type (constants.%Get.type.1)]
  130. // CHECK:STDOUT: %Get: @Class.%Get.type (%Get.type.1) = struct_value () [symbolic = %Get (constants.%Get.1)]
  131. // CHECK:STDOUT: %GetNoDeduce.type: type = fn_type @GetNoDeduce, @Class(%T) [symbolic = %GetNoDeduce.type (constants.%GetNoDeduce.type.1)]
  132. // CHECK:STDOUT: %GetNoDeduce: @Class.%GetNoDeduce.type (%GetNoDeduce.type.1) = struct_value () [symbolic = %GetNoDeduce (constants.%GetNoDeduce.1)]
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: class {
  135. // CHECK:STDOUT: %Get.decl: @Class.%Get.type (%Get.type.1) = fn_decl @Get [symbolic = %Get (constants.%Get.1)] {
  136. // CHECK:STDOUT: %U.loc15_10.1: type = param U
  137. // CHECK:STDOUT: %U.loc15_10.2: type = bind_symbolic_name U 1, %U.loc15_10.1 [symbolic = @Get.%U.1 (constants.%U)]
  138. // CHECK:STDOUT: %T.ref.loc15: type = name_ref T, file.%T.loc14_13.2 [symbolic = @Get.%T (constants.%T)]
  139. // CHECK:STDOUT: %U.ref.loc15: type = name_ref U, %U.loc15_10.2 [symbolic = @Get.%U.1 (constants.%U)]
  140. // CHECK:STDOUT: %.loc15_28.1: %.3 = tuple_literal (%T.ref.loc15, %U.ref.loc15)
  141. // CHECK:STDOUT: %.loc15_28.2: type = converted %.loc15_28.1, constants.%.4 [symbolic = @Get.%.1 (constants.%.4)]
  142. // CHECK:STDOUT: %return.var.loc15: ref @Get.%.1 (%.4) = var <return slot>
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT: %GetNoDeduce.decl: @Class.%GetNoDeduce.type (%GetNoDeduce.type.1) = fn_decl @GetNoDeduce [symbolic = %GetNoDeduce (constants.%GetNoDeduce.1)] {
  145. // CHECK:STDOUT: %T.ref.loc16_21: type = name_ref T, file.%T.loc14_13.2 [symbolic = @GetNoDeduce.%T (constants.%T)]
  146. // CHECK:STDOUT: %x.loc16_18.1: @GetNoDeduce.%T (%T) = param x
  147. // CHECK:STDOUT: %x.loc16_18.2: @GetNoDeduce.%T (%T) = bind_name x, %x.loc16_18.1
  148. // CHECK:STDOUT: %U.loc16_24.1: type = param U
  149. // CHECK:STDOUT: %U.loc16_24.2: type = bind_symbolic_name U 1, %U.loc16_24.1 [symbolic = @GetNoDeduce.%U.1 (constants.%U)]
  150. // CHECK:STDOUT: %T.ref.loc16_38: type = name_ref T, file.%T.loc14_13.2 [symbolic = @GetNoDeduce.%T (constants.%T)]
  151. // CHECK:STDOUT: %U.ref.loc16: type = name_ref U, %U.loc16_24.2 [symbolic = @GetNoDeduce.%U.1 (constants.%U)]
  152. // CHECK:STDOUT: %.loc16_42.1: %.3 = tuple_literal (%T.ref.loc16_38, %U.ref.loc16)
  153. // CHECK:STDOUT: %.loc16_42.2: type = converted %.loc16_42.1, constants.%.4 [symbolic = @GetNoDeduce.%.1 (constants.%.4)]
  154. // CHECK:STDOUT: %return.var.loc16: ref @GetNoDeduce.%.1 (%.4) = var <return slot>
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: !members:
  158. // CHECK:STDOUT: .Self = constants.%Class.2
  159. // CHECK:STDOUT: .Get = %Get.decl
  160. // CHECK:STDOUT: .GetNoDeduce = %GetNoDeduce.decl
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: generic fn @Get(file.%T.loc14_13.2: type, @Class.%U.loc15_10.2: type) {
  165. // CHECK:STDOUT: %U.1: type = bind_symbolic_name U 1 [symbolic = %U.1 (constants.%U)]
  166. // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic = %T (constants.%T)]
  167. // CHECK:STDOUT: %.1: type = tuple_type (@Get.%T (%T), @Get.%U.1 (%U)) [symbolic = %.1 (constants.%.4)]
  168. // CHECK:STDOUT:
  169. // CHECK:STDOUT: fn(@Class.%U.loc15_10.2: type) -> @Get.%.1 (%.4);
  170. // CHECK:STDOUT: }
  171. // CHECK:STDOUT:
  172. // CHECK:STDOUT: generic fn @GetNoDeduce(file.%T.loc14_13.2: type, @Class.%U.loc16_24.2: type) {
  173. // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic = %T (constants.%T)]
  174. // CHECK:STDOUT: %U.1: type = bind_symbolic_name U 1 [symbolic = %U.1 (constants.%U)]
  175. // CHECK:STDOUT: %.1: type = tuple_type (@GetNoDeduce.%T (%T), @GetNoDeduce.%U.1 (%U)) [symbolic = %.1 (constants.%.4)]
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: fn(@Class.%x.loc16_18.2: @GetNoDeduce.%T (%T), @Class.%U.loc16_24.2: type) -> @GetNoDeduce.%.1 (%.4);
  178. // CHECK:STDOUT: }
  179. // CHECK:STDOUT:
  180. // CHECK:STDOUT: fn @CallGenericMethod(%c: %Class.3) -> %return: %.5 {
  181. // CHECK:STDOUT: !entry:
  182. // CHECK:STDOUT: %c.ref: %Class.3 = name_ref c, %c
  183. // CHECK:STDOUT: %.loc20: %Get.type.2 = specific_constant @Class.%Get.decl, @Class(constants.%A) [template = constants.%Get.2]
  184. // CHECK:STDOUT: %Get.ref: %Get.type.2 = name_ref Get, %.loc20 [template = constants.%Get.2]
  185. // CHECK:STDOUT: %B.ref: type = name_ref B, file.%B.decl [template = constants.%B]
  186. // CHECK:STDOUT: %.loc19: ref %.5 = splice_block %return {}
  187. // CHECK:STDOUT: %Get.call: init %.5 = call %Get.ref(%B.ref) to %.loc19
  188. // CHECK:STDOUT: return %Get.call to %return
  189. // CHECK:STDOUT: }
  190. // CHECK:STDOUT:
  191. // CHECK:STDOUT: fn @CallGenericMethodWithNonDeducedParam(%c: %Class.3) -> %return: %.5 {
  192. // CHECK:STDOUT: !entry:
  193. // CHECK:STDOUT: %c.ref: %Class.3 = name_ref c, %c
  194. // CHECK:STDOUT: %.loc24_11: %GetNoDeduce.type.2 = specific_constant @Class.%GetNoDeduce.decl, @Class(constants.%A) [template = constants.%GetNoDeduce.2]
  195. // CHECK:STDOUT: %GetNoDeduce.ref: %GetNoDeduce.type.2 = name_ref GetNoDeduce, %.loc24_11 [template = constants.%GetNoDeduce.2]
  196. // CHECK:STDOUT: %.loc24_25.1: %.1 = struct_literal ()
  197. // CHECK:STDOUT: %B.ref: type = name_ref B, file.%B.decl [template = constants.%B]
  198. // CHECK:STDOUT: %.loc23: ref %.5 = splice_block %return {}
  199. // CHECK:STDOUT: %.loc24_25.2: ref %A = temporary_storage
  200. // CHECK:STDOUT: %.loc24_25.3: init %A = class_init (), %.loc24_25.2 [template = constants.%struct]
  201. // CHECK:STDOUT: %.loc24_25.4: ref %A = temporary %.loc24_25.2, %.loc24_25.3
  202. // CHECK:STDOUT: %.loc24_23.1: ref %A = converted %.loc24_25.1, %.loc24_25.4
  203. // CHECK:STDOUT: %.loc24_23.2: %A = bind_value %.loc24_23.1
  204. // CHECK:STDOUT: %GetNoDeduce.call: init %.5 = call %GetNoDeduce.ref(%.loc24_23.2, %B.ref) to %.loc23
  205. // CHECK:STDOUT: return %GetNoDeduce.call to %return
  206. // CHECK:STDOUT: }
  207. // CHECK:STDOUT:
  208. // CHECK:STDOUT: specific @Class(constants.%T) {
  209. // CHECK:STDOUT: %T => constants.%T
  210. // CHECK:STDOUT: }
  211. // CHECK:STDOUT:
  212. // CHECK:STDOUT: specific @Get(constants.%T, constants.%U) {
  213. // CHECK:STDOUT: %U.1 => constants.%U
  214. // CHECK:STDOUT: %T => constants.%T
  215. // CHECK:STDOUT: %.1 => constants.%.4
  216. // CHECK:STDOUT: }
  217. // CHECK:STDOUT:
  218. // CHECK:STDOUT: specific @GetNoDeduce(constants.%T, constants.%U) {
  219. // CHECK:STDOUT: %T => constants.%T
  220. // CHECK:STDOUT: %U.1 => constants.%U
  221. // CHECK:STDOUT: %.1 => constants.%.4
  222. // CHECK:STDOUT: }
  223. // CHECK:STDOUT:
  224. // CHECK:STDOUT: specific @Class(@Class.%T) {
  225. // CHECK:STDOUT: %T => constants.%T
  226. // CHECK:STDOUT: }
  227. // CHECK:STDOUT:
  228. // CHECK:STDOUT: specific @Class(constants.%A) {
  229. // CHECK:STDOUT: %T => constants.%A
  230. // CHECK:STDOUT:
  231. // CHECK:STDOUT: !definition:
  232. // CHECK:STDOUT: %Get.type => constants.%Get.type.2
  233. // CHECK:STDOUT: %Get => constants.%Get.2
  234. // CHECK:STDOUT: %GetNoDeduce.type => constants.%GetNoDeduce.type.2
  235. // CHECK:STDOUT: %GetNoDeduce => constants.%GetNoDeduce.2
  236. // CHECK:STDOUT: }
  237. // CHECK:STDOUT:
  238. // CHECK:STDOUT: specific @Get(constants.%A, constants.%B) {
  239. // CHECK:STDOUT: %U.1 => constants.%B
  240. // CHECK:STDOUT: %T => constants.%A
  241. // CHECK:STDOUT: %.1 => constants.%.5
  242. // CHECK:STDOUT: }
  243. // CHECK:STDOUT:
  244. // CHECK:STDOUT: specific @GetNoDeduce(constants.%A, constants.%B) {
  245. // CHECK:STDOUT: %T => constants.%A
  246. // CHECK:STDOUT: %U.1 => constants.%B
  247. // CHECK:STDOUT: %.1 => constants.%.5
  248. // CHECK:STDOUT: }
  249. // CHECK:STDOUT: