method_deduce.carbon 15 KB

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