method_deduce.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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.patt: type = symbolic_binding_pattern T 0
  84. // CHECK:STDOUT: } {
  85. // CHECK:STDOUT: %T.param: type = param T, runtime_param<invalid>
  86. // CHECK:STDOUT: %T.loc14: type = bind_symbolic_name T 0, %T.param [symbolic = %T.1 (constants.%T)]
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT: %CallGenericMethod.decl: %CallGenericMethod.type = fn_decl @CallGenericMethod [template = constants.%CallGenericMethod] {
  89. // CHECK:STDOUT: %c.patt: %Class.3 = binding_pattern c
  90. // CHECK:STDOUT: } {
  91. // CHECK:STDOUT: %Class.ref: %Class.type = name_ref Class, file.%Class.decl [template = constants.%Class.1]
  92. // CHECK:STDOUT: %A.ref.loc19_31: type = name_ref A, file.%A.decl [template = constants.%A]
  93. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(constants.%A) [template = constants.%Class.3]
  94. // CHECK:STDOUT: %c.param: %Class.3 = param c, runtime_param0
  95. // CHECK:STDOUT: %c: %Class.3 = bind_name c, %c.param
  96. // CHECK:STDOUT: %A.ref.loc19_39: type = name_ref A, file.%A.decl [template = constants.%A]
  97. // CHECK:STDOUT: %B.ref.loc19: type = name_ref B, file.%B.decl [template = constants.%B]
  98. // CHECK:STDOUT: %.loc19_43.1: %.4 = tuple_literal (%A.ref.loc19_39, %B.ref.loc19)
  99. // CHECK:STDOUT: %.loc19_43.2: type = converted %.loc19_43.1, constants.%.6 [template = constants.%.6]
  100. // CHECK:STDOUT: %return: ref %.6 = var <return slot>
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT: %CallGenericMethodWithNonDeducedParam.decl: %CallGenericMethodWithNonDeducedParam.type = fn_decl @CallGenericMethodWithNonDeducedParam [template = constants.%CallGenericMethodWithNonDeducedParam] {
  103. // CHECK:STDOUT: %c.patt: %Class.3 = binding_pattern c
  104. // CHECK:STDOUT: } {
  105. // CHECK:STDOUT: %Class.ref: %Class.type = name_ref Class, file.%Class.decl [template = constants.%Class.1]
  106. // CHECK:STDOUT: %A.ref.loc23_50: type = name_ref A, file.%A.decl [template = constants.%A]
  107. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(constants.%A) [template = constants.%Class.3]
  108. // CHECK:STDOUT: %c.param: %Class.3 = param c, runtime_param0
  109. // CHECK:STDOUT: %c: %Class.3 = bind_name c, %c.param
  110. // CHECK:STDOUT: %A.ref.loc23_58: type = name_ref A, file.%A.decl [template = constants.%A]
  111. // CHECK:STDOUT: %B.ref.loc23: type = name_ref B, file.%B.decl [template = constants.%B]
  112. // CHECK:STDOUT: %.loc23_62.1: %.4 = tuple_literal (%A.ref.loc23_58, %B.ref.loc23)
  113. // CHECK:STDOUT: %.loc23_62.2: type = converted %.loc23_62.1, constants.%.6 [template = constants.%.6]
  114. // CHECK:STDOUT: %return: ref %.6 = var <return slot>
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: class @A {
  119. // CHECK:STDOUT: %.loc11: <witness> = complete_type_witness %.1 [template = constants.%.2]
  120. // CHECK:STDOUT:
  121. // CHECK:STDOUT: !members:
  122. // CHECK:STDOUT: .Self = constants.%A
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT:
  125. // CHECK:STDOUT: class @B {
  126. // CHECK:STDOUT: %.loc12: <witness> = complete_type_witness %.1 [template = constants.%.2]
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: !members:
  129. // CHECK:STDOUT: .Self = constants.%B
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: generic class @Class(%T.loc14: type) {
  133. // CHECK:STDOUT: %T.1: type = bind_symbolic_name T 0 [symbolic = %T.1 (constants.%T)]
  134. // CHECK:STDOUT:
  135. // CHECK:STDOUT: !definition:
  136. // CHECK:STDOUT: %Get.type: type = fn_type @Get, @Class(%T.1) [symbolic = %Get.type (constants.%Get.type.1)]
  137. // CHECK:STDOUT: %Get: @Class.%Get.type (%Get.type.1) = struct_value () [symbolic = %Get (constants.%Get.1)]
  138. // CHECK:STDOUT: %GetNoDeduce.type: type = fn_type @GetNoDeduce, @Class(%T.1) [symbolic = %GetNoDeduce.type (constants.%GetNoDeduce.type.1)]
  139. // CHECK:STDOUT: %GetNoDeduce: @Class.%GetNoDeduce.type (%GetNoDeduce.type.1) = struct_value () [symbolic = %GetNoDeduce (constants.%GetNoDeduce.1)]
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: class {
  142. // CHECK:STDOUT: %Get.decl: @Class.%Get.type (%Get.type.1) = fn_decl @Get [symbolic = @Class.%Get (constants.%Get.1)] {
  143. // CHECK:STDOUT: %U.patt: type = symbolic_binding_pattern U 1
  144. // CHECK:STDOUT: } {
  145. // CHECK:STDOUT: %U.param: type = param U, runtime_param<invalid>
  146. // CHECK:STDOUT: %U.loc15: type = bind_symbolic_name U 1, %U.param [symbolic = %U.1 (constants.%U)]
  147. // CHECK:STDOUT: %T.ref: type = name_ref T, @Class.%T.loc14 [symbolic = %T (constants.%T)]
  148. // CHECK:STDOUT: %U.ref: type = name_ref U, %U.loc15 [symbolic = %U.1 (constants.%U)]
  149. // CHECK:STDOUT: %.loc15_28.1: %.4 = tuple_literal (%T.ref, %U.ref)
  150. // CHECK:STDOUT: %.loc15_28.2: type = converted %.loc15_28.1, constants.%.5 [symbolic = %.1 (constants.%.5)]
  151. // CHECK:STDOUT: %return: ref @Get.%.1 (%.5) = var <return slot>
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT: %GetNoDeduce.decl: @Class.%GetNoDeduce.type (%GetNoDeduce.type.1) = fn_decl @GetNoDeduce [symbolic = @Class.%GetNoDeduce (constants.%GetNoDeduce.1)] {
  154. // CHECK:STDOUT: %x.patt: @GetNoDeduce.%T (%T) = binding_pattern x
  155. // CHECK:STDOUT: %U.patt: type = symbolic_binding_pattern U 1
  156. // CHECK:STDOUT: } {
  157. // CHECK:STDOUT: %T.ref.loc16_21: type = name_ref T, @Class.%T.loc14 [symbolic = %T (constants.%T)]
  158. // CHECK:STDOUT: %x.param: @GetNoDeduce.%T (%T) = param x, runtime_param0
  159. // CHECK:STDOUT: %x: @GetNoDeduce.%T (%T) = bind_name x, %x.param
  160. // CHECK:STDOUT: %U.param: type = param U, runtime_param<invalid>
  161. // CHECK:STDOUT: %U.loc16: type = bind_symbolic_name U 1, %U.param [symbolic = %U.1 (constants.%U)]
  162. // CHECK:STDOUT: %T.ref.loc16_38: type = name_ref T, @Class.%T.loc14 [symbolic = %T (constants.%T)]
  163. // CHECK:STDOUT: %U.ref: type = name_ref U, %U.loc16 [symbolic = %U.1 (constants.%U)]
  164. // CHECK:STDOUT: %.loc16_42.1: %.4 = tuple_literal (%T.ref.loc16_38, %U.ref)
  165. // CHECK:STDOUT: %.loc16_42.2: type = converted %.loc16_42.1, constants.%.5 [symbolic = %.1 (constants.%.5)]
  166. // CHECK:STDOUT: %return: ref @GetNoDeduce.%.1 (%.5) = var <return slot>
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT: %.loc17: <witness> = complete_type_witness %.1 [template = constants.%.2]
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: !members:
  171. // CHECK:STDOUT: .Self = constants.%Class.2
  172. // CHECK:STDOUT: .Get = %Get.decl
  173. // CHECK:STDOUT: .GetNoDeduce = %GetNoDeduce.decl
  174. // CHECK:STDOUT: }
  175. // CHECK:STDOUT: }
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: generic fn @Get(@Class.%T.loc14: type, %U.loc15: type) {
  178. // CHECK:STDOUT: %U.1: type = bind_symbolic_name U 1 [symbolic = %U.1 (constants.%U)]
  179. // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic = %T (constants.%T)]
  180. // CHECK:STDOUT: %.1: type = tuple_type (@Get.%T (%T), @Get.%U.1 (%U)) [symbolic = %.1 (constants.%.5)]
  181. // CHECK:STDOUT:
  182. // CHECK:STDOUT: fn(%U.loc15: type) -> @Get.%.1 (%.5);
  183. // CHECK:STDOUT: }
  184. // CHECK:STDOUT:
  185. // CHECK:STDOUT: generic fn @GetNoDeduce(@Class.%T.loc14: type, %U.loc16: type) {
  186. // CHECK:STDOUT: %T: type = bind_symbolic_name T 0 [symbolic = %T (constants.%T)]
  187. // CHECK:STDOUT: %U.1: type = bind_symbolic_name U 1 [symbolic = %U.1 (constants.%U)]
  188. // CHECK:STDOUT: %.1: type = tuple_type (@GetNoDeduce.%T (%T), @GetNoDeduce.%U.1 (%U)) [symbolic = %.1 (constants.%.5)]
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: fn(%x: @GetNoDeduce.%T (%T), %U.loc16: type) -> @GetNoDeduce.%.1 (%.5);
  191. // CHECK:STDOUT: }
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: fn @CallGenericMethod(%c: %Class.3) -> %return: %.6 {
  194. // CHECK:STDOUT: !entry:
  195. // CHECK:STDOUT: %c.ref: %Class.3 = name_ref c, %c
  196. // CHECK:STDOUT: %.loc20: %Get.type.2 = specific_constant @Class.%Get.decl, @Class(constants.%A) [template = constants.%Get.2]
  197. // CHECK:STDOUT: %Get.ref: %Get.type.2 = name_ref Get, %.loc20 [template = constants.%Get.2]
  198. // CHECK:STDOUT: %B.ref.loc20: type = name_ref B, file.%B.decl [template = constants.%B]
  199. // CHECK:STDOUT: %.loc19_35: ref %.6 = splice_block %return {}
  200. // CHECK:STDOUT: %Get.call: init %.6 = call %Get.ref() to %.loc19_35
  201. // CHECK:STDOUT: return %Get.call to %return
  202. // CHECK:STDOUT: }
  203. // CHECK:STDOUT:
  204. // CHECK:STDOUT: fn @CallGenericMethodWithNonDeducedParam(%c: %Class.3) -> %return: %.6 {
  205. // CHECK:STDOUT: !entry:
  206. // CHECK:STDOUT: %c.ref: %Class.3 = name_ref c, %c
  207. // CHECK:STDOUT: %.loc24_11: %GetNoDeduce.type.2 = specific_constant @Class.%GetNoDeduce.decl, @Class(constants.%A) [template = constants.%GetNoDeduce.2]
  208. // CHECK:STDOUT: %GetNoDeduce.ref: %GetNoDeduce.type.2 = name_ref GetNoDeduce, %.loc24_11 [template = constants.%GetNoDeduce.2]
  209. // CHECK:STDOUT: %.loc24_25.1: %.1 = struct_literal ()
  210. // CHECK:STDOUT: %B.ref.loc24: type = name_ref B, file.%B.decl [template = constants.%B]
  211. // CHECK:STDOUT: %.loc23_54: ref %.6 = splice_block %return {}
  212. // CHECK:STDOUT: %.loc24_25.2: ref %A = temporary_storage
  213. // CHECK:STDOUT: %.loc24_25.3: init %A = class_init (), %.loc24_25.2 [template = constants.%struct]
  214. // CHECK:STDOUT: %.loc24_25.4: ref %A = temporary %.loc24_25.2, %.loc24_25.3
  215. // CHECK:STDOUT: %.loc24_23.1: ref %A = converted %.loc24_25.1, %.loc24_25.4
  216. // CHECK:STDOUT: %.loc24_23.2: %A = bind_value %.loc24_23.1
  217. // CHECK:STDOUT: %GetNoDeduce.call: init %.6 = call %GetNoDeduce.ref(%.loc24_23.2) to %.loc23_54
  218. // CHECK:STDOUT: return %GetNoDeduce.call to %return
  219. // CHECK:STDOUT: }
  220. // CHECK:STDOUT:
  221. // CHECK:STDOUT: specific @Class(constants.%T) {
  222. // CHECK:STDOUT: %T.1 => constants.%T
  223. // CHECK:STDOUT: }
  224. // CHECK:STDOUT:
  225. // CHECK:STDOUT: specific @Get(constants.%T, constants.%U) {
  226. // CHECK:STDOUT: %U.1 => constants.%U
  227. // CHECK:STDOUT: %T => constants.%T
  228. // CHECK:STDOUT: %.1 => constants.%.5
  229. // CHECK:STDOUT: }
  230. // CHECK:STDOUT:
  231. // CHECK:STDOUT: specific @GetNoDeduce(constants.%T, constants.%U) {
  232. // CHECK:STDOUT: %T => constants.%T
  233. // CHECK:STDOUT: %U.1 => constants.%U
  234. // CHECK:STDOUT: %.1 => constants.%.5
  235. // CHECK:STDOUT: }
  236. // CHECK:STDOUT:
  237. // CHECK:STDOUT: specific @Class(@Class.%T.1) {
  238. // CHECK:STDOUT: %T.1 => constants.%T
  239. // CHECK:STDOUT: }
  240. // CHECK:STDOUT:
  241. // CHECK:STDOUT: specific @Class(constants.%A) {
  242. // CHECK:STDOUT: %T.1 => constants.%A
  243. // CHECK:STDOUT:
  244. // CHECK:STDOUT: !definition:
  245. // CHECK:STDOUT: %Get.type => constants.%Get.type.2
  246. // CHECK:STDOUT: %Get => constants.%Get.2
  247. // CHECK:STDOUT: %GetNoDeduce.type => constants.%GetNoDeduce.type.2
  248. // CHECK:STDOUT: %GetNoDeduce => constants.%GetNoDeduce.2
  249. // CHECK:STDOUT: }
  250. // CHECK:STDOUT:
  251. // CHECK:STDOUT: specific @Get(constants.%A, constants.%B) {
  252. // CHECK:STDOUT: %U.1 => constants.%B
  253. // CHECK:STDOUT: %T => constants.%A
  254. // CHECK:STDOUT: %.1 => constants.%.6
  255. // CHECK:STDOUT: }
  256. // CHECK:STDOUT:
  257. // CHECK:STDOUT: specific @GetNoDeduce(constants.%A, constants.%B) {
  258. // CHECK:STDOUT: %T => constants.%A
  259. // CHECK:STDOUT: %U.1 => constants.%B
  260. // CHECK:STDOUT: %.1 => constants.%.6
  261. // CHECK:STDOUT: }
  262. // CHECK:STDOUT: