generic_method.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/convert.carbon
  6. // TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
  7. // EXTRA-ARGS: --dump-sem-ir-ranges=if-present
  8. //
  9. // AUTOUPDATE
  10. // TIP: To test this file alone, run:
  11. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/class/generic_method.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/class/generic_method.carbon
  14. class Class(T:! type) {
  15. var a: T;
  16. fn F[self: Self](n: T);
  17. }
  18. fn Class(T:! type).F[self: Self](n: T) {}
  19. // CHECK:STDOUT: --- generic_method.carbon
  20. // CHECK:STDOUT:
  21. // CHECK:STDOUT: constants {
  22. // CHECK:STDOUT: %type: type = facet_type <type> [concrete]
  23. // CHECK:STDOUT: %.Self: %type = bind_symbolic_name .Self [symbolic_self]
  24. // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic]
  25. // CHECK:STDOUT: %pattern_type.98f: type = pattern_type type [concrete]
  26. // CHECK:STDOUT: %Class.type: type = generic_class_type @Class [concrete]
  27. // CHECK:STDOUT: %Class.generic: %Class.type = struct_value () [concrete]
  28. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T) [symbolic]
  29. // CHECK:STDOUT: %require_complete.4ae: <witness> = require_complete_type %T [symbolic]
  30. // CHECK:STDOUT: %Class.elem: type = unbound_element_type %Class, %T [symbolic]
  31. // CHECK:STDOUT: %pattern_type.3c1: type = pattern_type %Class [symbolic]
  32. // CHECK:STDOUT: %pattern_type.7dc: type = pattern_type %T [symbolic]
  33. // CHECK:STDOUT: %Class.F.type: type = fn_type @Class.F, @Class(%T) [symbolic]
  34. // CHECK:STDOUT: %Class.F: %Class.F.type = struct_value () [symbolic]
  35. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  36. // CHECK:STDOUT: %pattern_type.f6d: type = pattern_type auto [concrete]
  37. // CHECK:STDOUT: %Destroy.impl_witness: <witness> = impl_witness @Class.%Destroy.impl_witness_table, @Class.as.Destroy.impl(%T) [symbolic]
  38. // CHECK:STDOUT: %ptr.955: type = ptr_type %Class [symbolic]
  39. // CHECK:STDOUT: %pattern_type.9e0: type = pattern_type %ptr.955 [symbolic]
  40. // CHECK:STDOUT: %Class.as.Destroy.impl.Op.type: type = fn_type @Class.as.Destroy.impl.Op, @Class.as.Destroy.impl(%T) [symbolic]
  41. // CHECK:STDOUT: %Class.as.Destroy.impl.Op: %Class.as.Destroy.impl.Op.type = struct_value () [symbolic]
  42. // CHECK:STDOUT: %struct_type.a: type = struct_type {.a: %T} [symbolic]
  43. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.a [symbolic]
  44. // CHECK:STDOUT: %require_complete.4f8: <witness> = require_complete_type %Class [symbolic]
  45. // CHECK:STDOUT: }
  46. // CHECK:STDOUT:
  47. // CHECK:STDOUT: imports {
  48. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  49. // CHECK:STDOUT: .Destroy = %Core.Destroy
  50. // CHECK:STDOUT: import Core//prelude
  51. // CHECK:STDOUT: import Core//prelude/...
  52. // CHECK:STDOUT: }
  53. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  54. // CHECK:STDOUT: }
  55. // CHECK:STDOUT:
  56. // CHECK:STDOUT: file {
  57. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  58. // CHECK:STDOUT: .Core = imports.%Core
  59. // CHECK:STDOUT: .Class = %Class.decl
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: %Core.import = import Core
  62. // CHECK:STDOUT: %Class.decl: %Class.type = class_decl @Class [concrete = constants.%Class.generic] {
  63. // CHECK:STDOUT: %T.patt: %pattern_type.98f = symbolic_binding_pattern T, 0 [concrete]
  64. // CHECK:STDOUT: } {
  65. // CHECK:STDOUT: %.Self: %type = bind_symbolic_name .Self [symbolic_self = constants.%.Self]
  66. // CHECK:STDOUT: %T.loc15_13.2: type = bind_symbolic_name T, 0 [symbolic = %T.loc15_13.1 (constants.%T)]
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT: %Class.F.decl: %Class.F.type = fn_decl @Class.F [symbolic = constants.%Class.F] {
  69. // CHECK:STDOUT: %self.patt: @Class.F.%pattern_type.loc17_8 (%pattern_type.3c1) = binding_pattern self [concrete]
  70. // CHECK:STDOUT: %self.param_patt: @Class.F.%pattern_type.loc17_8 (%pattern_type.3c1) = value_param_pattern %self.patt, call_param0 [concrete]
  71. // CHECK:STDOUT: %n.patt: @Class.F.%pattern_type.loc17_20 (%pattern_type.7dc) = binding_pattern n [concrete]
  72. // CHECK:STDOUT: %n.param_patt: @Class.F.%pattern_type.loc17_20 (%pattern_type.7dc) = value_param_pattern %n.patt, call_param1 [concrete]
  73. // CHECK:STDOUT: } {
  74. // CHECK:STDOUT: %.Self: %type = bind_symbolic_name .Self [symbolic_self = constants.%.Self]
  75. // CHECK:STDOUT: %T.loc20: type = bind_symbolic_name T, 0 [symbolic = @Class.%T.loc15_13.1 (constants.%T)]
  76. // CHECK:STDOUT: %self.param.loc20: @Class.F.%Class (%Class) = value_param call_param0
  77. // CHECK:STDOUT: %.loc20_28.1: type = splice_block %Self.ref.loc20 [symbolic = %Class (constants.%Class)] {
  78. // CHECK:STDOUT: %.loc20_28.2: type = specific_constant constants.%Class, @Class(constants.%T) [symbolic = %Class (constants.%Class)]
  79. // CHECK:STDOUT: %Self.ref.loc20: type = name_ref Self, %.loc20_28.2 [symbolic = %Class (constants.%Class)]
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT: %self.loc20: @Class.F.%Class (%Class) = bind_name self, %self.param.loc20
  82. // CHECK:STDOUT: %n.param.loc20: @Class.F.%T.loc17 (%T) = value_param call_param1
  83. // CHECK:STDOUT: %T.ref.loc20: type = name_ref T, %T.loc20 [symbolic = %T.loc17 (constants.%T)]
  84. // CHECK:STDOUT: %n.loc20: @Class.F.%T.loc17 (%T) = bind_name n, %n.param.loc20
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: generic impl @Class.as.Destroy.impl(@Class.%T.loc15_13.2: type) {
  89. // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic = %T (constants.%T)]
  90. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T) [symbolic = %Class (constants.%Class)]
  91. // CHECK:STDOUT: %Destroy.impl_witness: <witness> = impl_witness @Class.%Destroy.impl_witness_table, @Class.as.Destroy.impl(%T) [symbolic = %Destroy.impl_witness (constants.%Destroy.impl_witness)]
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: !definition:
  94. // CHECK:STDOUT: %Class.as.Destroy.impl.Op.type: type = fn_type @Class.as.Destroy.impl.Op, @Class.as.Destroy.impl(%T) [symbolic = %Class.as.Destroy.impl.Op.type (constants.%Class.as.Destroy.impl.Op.type)]
  95. // CHECK:STDOUT: %Class.as.Destroy.impl.Op: @Class.as.Destroy.impl.%Class.as.Destroy.impl.Op.type (%Class.as.Destroy.impl.Op.type) = struct_value () [symbolic = %Class.as.Destroy.impl.Op (constants.%Class.as.Destroy.impl.Op)]
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: impl: @Class.%Self.ref as constants.%Destroy.type {
  98. // CHECK:STDOUT: %Class.as.Destroy.impl.Op.decl: @Class.as.Destroy.impl.%Class.as.Destroy.impl.Op.type (%Class.as.Destroy.impl.Op.type) = fn_decl @Class.as.Destroy.impl.Op [symbolic = @Class.as.Destroy.impl.%Class.as.Destroy.impl.Op (constants.%Class.as.Destroy.impl.Op)] {
  99. // CHECK:STDOUT: %self.patt: @Class.as.Destroy.impl.Op.%pattern_type (%pattern_type.9e0) = binding_pattern self [concrete]
  100. // CHECK:STDOUT: %self.param_patt: @Class.as.Destroy.impl.Op.%pattern_type (%pattern_type.9e0) = value_param_pattern %self.patt, call_param0 [concrete]
  101. // CHECK:STDOUT: %.loc15_23.1: %pattern_type.f6d = addr_pattern %self.param_patt [concrete]
  102. // CHECK:STDOUT: } {
  103. // CHECK:STDOUT: %self.param: @Class.as.Destroy.impl.Op.%ptr (%ptr.955) = value_param call_param0
  104. // CHECK:STDOUT: %.loc15_23.2: type = splice_block %Self.ref [symbolic = %Class (constants.%Class)] {
  105. // CHECK:STDOUT: %.loc15_23.3: type = specific_constant constants.%Class, @Class(constants.%T) [symbolic = %Class (constants.%Class)]
  106. // CHECK:STDOUT: %Self.ref: type = name_ref Self, %.loc15_23.3 [symbolic = %Class (constants.%Class)]
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT: %self: @Class.as.Destroy.impl.Op.%ptr (%ptr.955) = bind_name self, %self.param
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: !members:
  112. // CHECK:STDOUT: .Op = %Class.as.Destroy.impl.Op.decl
  113. // CHECK:STDOUT: witness = @Class.%Destroy.impl_witness
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: generic class @Class(%T.loc15_13.2: type) {
  118. // CHECK:STDOUT: %T.loc15_13.1: type = bind_symbolic_name T, 0 [symbolic = %T.loc15_13.1 (constants.%T)]
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: !definition:
  121. // CHECK:STDOUT: %require_complete: <witness> = require_complete_type %T.loc15_13.1 [symbolic = %require_complete (constants.%require_complete.4ae)]
  122. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T.loc15_13.1) [symbolic = %Class (constants.%Class)]
  123. // CHECK:STDOUT: %Class.elem: type = unbound_element_type %Class, %T.loc15_13.1 [symbolic = %Class.elem (constants.%Class.elem)]
  124. // CHECK:STDOUT: %Class.F.type: type = fn_type @Class.F, @Class(%T.loc15_13.1) [symbolic = %Class.F.type (constants.%Class.F.type)]
  125. // CHECK:STDOUT: %Class.F: @Class.%Class.F.type (%Class.F.type) = struct_value () [symbolic = %Class.F (constants.%Class.F)]
  126. // CHECK:STDOUT: %struct_type.a: type = struct_type {.a: @Class.%T.loc15_13.1 (%T)} [symbolic = %struct_type.a (constants.%struct_type.a)]
  127. // CHECK:STDOUT: %complete_type.loc18_1.2: <witness> = complete_type_witness %struct_type.a [symbolic = %complete_type.loc18_1.2 (constants.%complete_type)]
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: class {
  130. // CHECK:STDOUT: %T.ref: type = name_ref T, %T.loc15_13.2 [symbolic = %T.loc15_13.1 (constants.%T)]
  131. // CHECK:STDOUT: %.loc16: @Class.%Class.elem (%Class.elem) = field_decl a, element0 [concrete]
  132. // CHECK:STDOUT: %Class.F.decl: @Class.%Class.F.type (%Class.F.type) = fn_decl @Class.F [symbolic = @Class.%Class.F (constants.%Class.F)] {
  133. // CHECK:STDOUT: %self.patt: @Class.F.%pattern_type.loc17_8 (%pattern_type.3c1) = binding_pattern self [concrete]
  134. // CHECK:STDOUT: %self.param_patt: @Class.F.%pattern_type.loc17_8 (%pattern_type.3c1) = value_param_pattern %self.patt, call_param0 [concrete]
  135. // CHECK:STDOUT: %n.patt: @Class.F.%pattern_type.loc17_20 (%pattern_type.7dc) = binding_pattern n [concrete]
  136. // CHECK:STDOUT: %n.param_patt: @Class.F.%pattern_type.loc17_20 (%pattern_type.7dc) = value_param_pattern %n.patt, call_param1 [concrete]
  137. // CHECK:STDOUT: } {
  138. // CHECK:STDOUT: %self.param.loc17: @Class.F.%Class (%Class) = value_param call_param0
  139. // CHECK:STDOUT: %.loc17_14.1: type = splice_block %Self.ref.loc17 [symbolic = %Class (constants.%Class)] {
  140. // CHECK:STDOUT: %.loc17_14.2: type = specific_constant constants.%Class, @Class(constants.%T) [symbolic = %Class (constants.%Class)]
  141. // CHECK:STDOUT: %Self.ref.loc17: type = name_ref Self, %.loc17_14.2 [symbolic = %Class (constants.%Class)]
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT: %self.loc17: @Class.F.%Class (%Class) = bind_name self, %self.param.loc17
  144. // CHECK:STDOUT: %n.param.loc17: @Class.F.%T.loc17 (%T) = value_param call_param1
  145. // CHECK:STDOUT: %T.ref.loc17: type = name_ref T, @Class.%T.loc15_13.2 [symbolic = %T.loc17 (constants.%T)]
  146. // CHECK:STDOUT: %n.loc17: @Class.F.%T.loc17 (%T) = bind_name n, %n.param.loc17
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%Class [symbolic = @Class.as.Destroy.impl.%Class (constants.%Class)]
  149. // CHECK:STDOUT: impl_decl @Class.as.Destroy.impl [concrete] {} {}
  150. // CHECK:STDOUT: %Destroy.impl_witness_table = impl_witness_table (@Class.as.Destroy.impl.%Class.as.Destroy.impl.Op.decl), @Class.as.Destroy.impl [concrete]
  151. // CHECK:STDOUT: %Destroy.impl_witness: <witness> = impl_witness %Destroy.impl_witness_table, @Class.as.Destroy.impl(constants.%T) [symbolic = @Class.as.Destroy.impl.%Destroy.impl_witness (constants.%Destroy.impl_witness)]
  152. // CHECK:STDOUT: %complete_type.loc18_1.1: <witness> = complete_type_witness constants.%struct_type.a [symbolic = %complete_type.loc18_1.2 (constants.%complete_type)]
  153. // CHECK:STDOUT: complete_type_witness = %complete_type.loc18_1.1
  154. // CHECK:STDOUT:
  155. // CHECK:STDOUT: !members:
  156. // CHECK:STDOUT: .Self = constants.%Class
  157. // CHECK:STDOUT: .T = <poisoned>
  158. // CHECK:STDOUT: .a = %.loc16
  159. // CHECK:STDOUT: .F = %Class.F.decl
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: generic fn @Class.F(@Class.%T.loc15_13.2: type) {
  164. // CHECK:STDOUT: %T.loc17: type = bind_symbolic_name T, 0 [symbolic = %T.loc17 (constants.%T)]
  165. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T.loc17) [symbolic = %Class (constants.%Class)]
  166. // CHECK:STDOUT: %pattern_type.loc17_8: type = pattern_type %Class [symbolic = %pattern_type.loc17_8 (constants.%pattern_type.3c1)]
  167. // CHECK:STDOUT: %pattern_type.loc17_20: type = pattern_type %T.loc17 [symbolic = %pattern_type.loc17_20 (constants.%pattern_type.7dc)]
  168. // CHECK:STDOUT:
  169. // CHECK:STDOUT: !definition:
  170. // CHECK:STDOUT: %require_complete.loc20_26: <witness> = require_complete_type %Class [symbolic = %require_complete.loc20_26 (constants.%require_complete.4f8)]
  171. // CHECK:STDOUT: %require_complete.loc20_35: <witness> = require_complete_type %T.loc17 [symbolic = %require_complete.loc20_35 (constants.%require_complete.4ae)]
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: fn(%self.param.loc20: @Class.F.%Class (%Class), %n.param.loc20: @Class.F.%T.loc17 (%T)) {
  174. // CHECK:STDOUT: !entry:
  175. // CHECK:STDOUT: return
  176. // CHECK:STDOUT: }
  177. // CHECK:STDOUT: }
  178. // CHECK:STDOUT:
  179. // CHECK:STDOUT: generic fn @Class.as.Destroy.impl.Op(@Class.%T.loc15_13.2: type) {
  180. // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic = %T (constants.%T)]
  181. // CHECK:STDOUT: %Class: type = class_type @Class, @Class(%T) [symbolic = %Class (constants.%Class)]
  182. // CHECK:STDOUT: %ptr: type = ptr_type %Class [symbolic = %ptr (constants.%ptr.955)]
  183. // CHECK:STDOUT: %pattern_type: type = pattern_type %ptr [symbolic = %pattern_type (constants.%pattern_type.9e0)]
  184. // CHECK:STDOUT:
  185. // CHECK:STDOUT: !definition:
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: fn(%self.param: @Class.as.Destroy.impl.Op.%ptr (%ptr.955)) = "no_op";
  188. // CHECK:STDOUT: }
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: specific @Class(constants.%T) {
  191. // CHECK:STDOUT: %T.loc15_13.1 => constants.%T
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: !definition:
  194. // CHECK:STDOUT: %require_complete => constants.%require_complete.4ae
  195. // CHECK:STDOUT: %Class => constants.%Class
  196. // CHECK:STDOUT: %Class.elem => constants.%Class.elem
  197. // CHECK:STDOUT: %Class.F.type => constants.%Class.F.type
  198. // CHECK:STDOUT: %Class.F => constants.%Class.F
  199. // CHECK:STDOUT: %struct_type.a => constants.%struct_type.a
  200. // CHECK:STDOUT: %complete_type.loc18_1.2 => constants.%complete_type
  201. // CHECK:STDOUT: }
  202. // CHECK:STDOUT:
  203. // CHECK:STDOUT: specific @Class.F(constants.%T) {
  204. // CHECK:STDOUT: %T.loc17 => constants.%T
  205. // CHECK:STDOUT: %Class => constants.%Class
  206. // CHECK:STDOUT: %pattern_type.loc17_8 => constants.%pattern_type.3c1
  207. // CHECK:STDOUT: %pattern_type.loc17_20 => constants.%pattern_type.7dc
  208. // CHECK:STDOUT: }
  209. // CHECK:STDOUT:
  210. // CHECK:STDOUT: specific @Class.as.Destroy.impl(constants.%T) {
  211. // CHECK:STDOUT: %T => constants.%T
  212. // CHECK:STDOUT: %Class => constants.%Class
  213. // CHECK:STDOUT: %Destroy.impl_witness => constants.%Destroy.impl_witness
  214. // CHECK:STDOUT: }
  215. // CHECK:STDOUT:
  216. // CHECK:STDOUT: specific @Class.as.Destroy.impl.Op(constants.%T) {
  217. // CHECK:STDOUT: %T => constants.%T
  218. // CHECK:STDOUT: %Class => constants.%Class
  219. // CHECK:STDOUT: %ptr => constants.%ptr.955
  220. // CHECK:STDOUT: %pattern_type => constants.%pattern_type.9e0
  221. // CHECK:STDOUT: }
  222. // CHECK:STDOUT: