overloaded.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/as/overloaded.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/as/overloaded.carbon
  10. class X {
  11. var n: i32;
  12. }
  13. impl i32 as Core.As(X) {
  14. fn Convert[self: i32]() -> X { return {.n = self}; }
  15. }
  16. impl X as Core.As(i32) {
  17. fn Convert[self: X]() -> i32 { return self.n; }
  18. }
  19. let n: i32 = ((4 as i32) as X) as i32;
  20. // CHECK:STDOUT: --- overloaded.carbon
  21. // CHECK:STDOUT:
  22. // CHECK:STDOUT: constants {
  23. // CHECK:STDOUT: %X: type = class_type @X [concrete]
  24. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  25. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  26. // CHECK:STDOUT: %X.elem: type = unbound_element_type %X, %i32 [concrete]
  27. // CHECK:STDOUT: %struct_type.n: type = struct_type {.n: %i32} [concrete]
  28. // CHECK:STDOUT: %complete_type.54b: <witness> = complete_type_witness %struct_type.n [concrete]
  29. // CHECK:STDOUT: %As.type.90f: type = generic_interface_type @As [concrete]
  30. // CHECK:STDOUT: %As.generic: %As.type.90f = struct_value () [concrete]
  31. // CHECK:STDOUT: %As.type.602: type = facet_type <@As, @As(%X)> [concrete]
  32. // CHECK:STDOUT: %Convert.type.35b: type = fn_type @Convert.1, @As(%X) [concrete]
  33. // CHECK:STDOUT: %impl_witness.491: <witness> = impl_witness (@impl.d8c.%Convert.decl) [concrete]
  34. // CHECK:STDOUT: %Convert.type.0e3: type = fn_type @Convert.2 [concrete]
  35. // CHECK:STDOUT: %Convert.311: %Convert.type.0e3 = struct_value () [concrete]
  36. // CHECK:STDOUT: %As.facet.206: %As.type.602 = facet_value %i32, (%impl_witness.491) [concrete]
  37. // CHECK:STDOUT: %As.type.fd4: type = facet_type <@As, @As(%i32)> [concrete]
  38. // CHECK:STDOUT: %Convert.type.99b: type = fn_type @Convert.1, @As(%i32) [concrete]
  39. // CHECK:STDOUT: %impl_witness.662: <witness> = impl_witness (@impl.18a.%Convert.decl) [concrete]
  40. // CHECK:STDOUT: %Convert.type.c23: type = fn_type @Convert.3 [concrete]
  41. // CHECK:STDOUT: %Convert.8bb: %Convert.type.c23 = struct_value () [concrete]
  42. // CHECK:STDOUT: %As.facet.79a: %As.type.fd4 = facet_value %X, (%impl_witness.662) [concrete]
  43. // CHECK:STDOUT: %int_4.0c1: Core.IntLiteral = int_value 4 [concrete]
  44. // CHECK:STDOUT: %impl_witness.882: <witness> = impl_witness (imports.%Core.import_ref.78a), @impl.686(%int_32) [concrete]
  45. // CHECK:STDOUT: %Convert.type.4fd: type = fn_type @Convert.7, @impl.686(%int_32) [concrete]
  46. // CHECK:STDOUT: %Convert.197: %Convert.type.4fd = struct_value () [concrete]
  47. // CHECK:STDOUT: %As.facet.d7e: %As.type.fd4 = facet_value Core.IntLiteral, (%impl_witness.882) [concrete]
  48. // CHECK:STDOUT: %.249: type = fn_type_with_self_type %Convert.type.99b, %As.facet.d7e [concrete]
  49. // CHECK:STDOUT: %Convert.bound.e80: <bound method> = bound_method %int_4.0c1, %Convert.197 [concrete]
  50. // CHECK:STDOUT: %Convert.specific_fn: <specific function> = specific_function %Convert.197, @Convert.7(%int_32) [concrete]
  51. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_4.0c1, %Convert.specific_fn [concrete]
  52. // CHECK:STDOUT: %int_4.940: %i32 = int_value 4 [concrete]
  53. // CHECK:STDOUT: %.e84: type = fn_type_with_self_type %Convert.type.35b, %As.facet.206 [concrete]
  54. // CHECK:STDOUT: %Convert.bound.483: <bound method> = bound_method %int_4.940, %Convert.311 [concrete]
  55. // CHECK:STDOUT: %.c95: type = fn_type_with_self_type %Convert.type.99b, %As.facet.79a [concrete]
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: imports {
  59. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  60. // CHECK:STDOUT: .Int = %Core.Int
  61. // CHECK:STDOUT: .As = %Core.As
  62. // CHECK:STDOUT: import Core//prelude
  63. // CHECK:STDOUT: import Core//prelude/...
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT: %Core.As: %As.type.90f = import_ref Core//prelude/operators/as, As, loaded [concrete = constants.%As.generic]
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: file {
  69. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  70. // CHECK:STDOUT: .Core = imports.%Core
  71. // CHECK:STDOUT: .X = %X.decl
  72. // CHECK:STDOUT: .n = %n
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT: %Core.import = import Core
  75. // CHECK:STDOUT: %X.decl: type = class_decl @X [concrete = constants.%X] {} {}
  76. // CHECK:STDOUT: impl_decl @impl.d8c [concrete] {} {
  77. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  78. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  79. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [concrete = imports.%Core]
  80. // CHECK:STDOUT: %As.ref: %As.type.90f = name_ref As, imports.%Core.As [concrete = constants.%As.generic]
  81. // CHECK:STDOUT: %X.ref: type = name_ref X, file.%X.decl [concrete = constants.%X]
  82. // CHECK:STDOUT: %As.type: type = facet_type <@As, @As(constants.%X)> [concrete = constants.%As.type.602]
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT: %impl_witness.loc15: <witness> = impl_witness (@impl.d8c.%Convert.decl) [concrete = constants.%impl_witness.491]
  85. // CHECK:STDOUT: impl_decl @impl.18a [concrete] {} {
  86. // CHECK:STDOUT: %X.ref: type = name_ref X, file.%X.decl [concrete = constants.%X]
  87. // CHECK:STDOUT: %Core.ref: <namespace> = name_ref Core, imports.%Core [concrete = imports.%Core]
  88. // CHECK:STDOUT: %As.ref: %As.type.90f = name_ref As, imports.%Core.As [concrete = constants.%As.generic]
  89. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  90. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  91. // CHECK:STDOUT: %As.type: type = facet_type <@As, @As(constants.%i32)> [concrete = constants.%As.type.fd4]
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT: %impl_witness.loc19: <witness> = impl_witness (@impl.18a.%Convert.decl) [concrete = constants.%impl_witness.662]
  94. // CHECK:STDOUT: name_binding_decl {
  95. // CHECK:STDOUT: %n.patt: %i32 = binding_pattern n
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT: %.loc23: type = splice_block %i32 [concrete = constants.%i32] {
  98. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  99. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT: %n: %i32 = bind_name n, @__global_init.%.loc23_32.2
  102. // CHECK:STDOUT: }
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: impl @impl.d8c: %i32 as %As.type {
  105. // CHECK:STDOUT: %Convert.decl: %Convert.type.0e3 = fn_decl @Convert.2 [concrete = constants.%Convert.311] {
  106. // CHECK:STDOUT: %self.patt: %i32 = binding_pattern self
  107. // CHECK:STDOUT: %self.param_patt: %i32 = value_param_pattern %self.patt, call_param0
  108. // CHECK:STDOUT: %return.patt: %X = return_slot_pattern
  109. // CHECK:STDOUT: %return.param_patt: %X = out_param_pattern %return.patt, call_param1
  110. // CHECK:STDOUT: } {
  111. // CHECK:STDOUT: %X.ref: type = name_ref X, file.%X.decl [concrete = constants.%X]
  112. // CHECK:STDOUT: %self.param: %i32 = value_param call_param0
  113. // CHECK:STDOUT: %.loc16_20: type = splice_block %i32 [concrete = constants.%i32] {
  114. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  115. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT: %self: %i32 = bind_name self, %self.param
  118. // CHECK:STDOUT: %return.param: ref %X = out_param call_param1
  119. // CHECK:STDOUT: %return: ref %X = return_slot %return.param
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT:
  122. // CHECK:STDOUT: !members:
  123. // CHECK:STDOUT: .X = <poisoned>
  124. // CHECK:STDOUT: .Convert = %Convert.decl
  125. // CHECK:STDOUT: witness = file.%impl_witness.loc15
  126. // CHECK:STDOUT: }
  127. // CHECK:STDOUT:
  128. // CHECK:STDOUT: impl @impl.18a: %X.ref as %As.type {
  129. // CHECK:STDOUT: %Convert.decl: %Convert.type.c23 = fn_decl @Convert.3 [concrete = constants.%Convert.8bb] {
  130. // CHECK:STDOUT: %self.patt: %X = binding_pattern self
  131. // CHECK:STDOUT: %self.param_patt: %X = value_param_pattern %self.patt, call_param0
  132. // CHECK:STDOUT: %return.patt: %i32 = return_slot_pattern
  133. // CHECK:STDOUT: %return.param_patt: %i32 = out_param_pattern %return.patt, call_param1
  134. // CHECK:STDOUT: } {
  135. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  136. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  137. // CHECK:STDOUT: %self.param: %X = value_param call_param0
  138. // CHECK:STDOUT: %X.ref: type = name_ref X, file.%X.decl [concrete = constants.%X]
  139. // CHECK:STDOUT: %self: %X = bind_name self, %self.param
  140. // CHECK:STDOUT: %return.param: ref %i32 = out_param call_param1
  141. // CHECK:STDOUT: %return: ref %i32 = return_slot %return.param
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: !members:
  145. // CHECK:STDOUT: .X = <poisoned>
  146. // CHECK:STDOUT: .Convert = %Convert.decl
  147. // CHECK:STDOUT: witness = file.%impl_witness.loc19
  148. // CHECK:STDOUT: }
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: class @X {
  151. // CHECK:STDOUT: %.loc12_8: %X.elem = field_decl n, element0 [concrete]
  152. // CHECK:STDOUT: name_binding_decl {
  153. // CHECK:STDOUT: %.loc12_3: %X.elem = var_pattern %.loc12_8
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT: %.var: ref %X.elem = var <none>
  156. // CHECK:STDOUT: %struct_type.n: type = struct_type {.n: %i32} [concrete = constants.%struct_type.n]
  157. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %struct_type.n [concrete = constants.%complete_type.54b]
  158. // CHECK:STDOUT: complete_type_witness = %complete_type
  159. // CHECK:STDOUT:
  160. // CHECK:STDOUT: !members:
  161. // CHECK:STDOUT: .Self = constants.%X
  162. // CHECK:STDOUT: .n = %.loc12_8
  163. // CHECK:STDOUT: }
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: fn @Convert.2[%self.param_patt: %i32]() -> %return.param_patt: %X {
  166. // CHECK:STDOUT: !entry:
  167. // CHECK:STDOUT: %self.ref: %i32 = name_ref self, %self
  168. // CHECK:STDOUT: %.loc16_51.1: %struct_type.n = struct_literal (%self.ref)
  169. // CHECK:STDOUT: %.loc16_51.2: ref %i32 = class_element_access %return, element0
  170. // CHECK:STDOUT: %.loc16_51.3: init %i32 = initialize_from %self.ref to %.loc16_51.2
  171. // CHECK:STDOUT: %.loc16_51.4: init %X = class_init (%.loc16_51.3), %return
  172. // CHECK:STDOUT: %.loc16_52: init %X = converted %.loc16_51.1, %.loc16_51.4
  173. // CHECK:STDOUT: return %.loc16_52 to %return
  174. // CHECK:STDOUT: }
  175. // CHECK:STDOUT:
  176. // CHECK:STDOUT: fn @Convert.3[%self.param_patt: %X]() -> %i32 {
  177. // CHECK:STDOUT: !entry:
  178. // CHECK:STDOUT: %self.ref: %X = name_ref self, %self
  179. // CHECK:STDOUT: %n.ref: %X.elem = name_ref n, @X.%.loc12_8 [concrete = @X.%.loc12_8]
  180. // CHECK:STDOUT: %.loc20_45.1: ref %i32 = class_element_access %self.ref, element0
  181. // CHECK:STDOUT: %.loc20_45.2: %i32 = bind_value %.loc20_45.1
  182. // CHECK:STDOUT: return %.loc20_45.2
  183. // CHECK:STDOUT: }
  184. // CHECK:STDOUT:
  185. // CHECK:STDOUT: fn @__global_init() {
  186. // CHECK:STDOUT: !entry:
  187. // CHECK:STDOUT: %int_4: Core.IntLiteral = int_value 4 [concrete = constants.%int_4.0c1]
  188. // CHECK:STDOUT: %int_32.loc23_21: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  189. // CHECK:STDOUT: %i32.loc23_21: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  190. // CHECK:STDOUT: %impl.elem0.loc23_18: %.249 = impl_witness_access constants.%impl_witness.882, element0 [concrete = constants.%Convert.197]
  191. // CHECK:STDOUT: %bound_method.loc23_18.1: <bound method> = bound_method %int_4, %impl.elem0.loc23_18 [concrete = constants.%Convert.bound.e80]
  192. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0.loc23_18, @Convert.7(constants.%int_32) [concrete = constants.%Convert.specific_fn]
  193. // CHECK:STDOUT: %bound_method.loc23_18.2: <bound method> = bound_method %int_4, %specific_fn [concrete = constants.%bound_method]
  194. // CHECK:STDOUT: %int.convert_checked: init %i32 = call %bound_method.loc23_18.2(%int_4) [concrete = constants.%int_4.940]
  195. // CHECK:STDOUT: %.loc23_18.1: %i32 = value_of_initializer %int.convert_checked [concrete = constants.%int_4.940]
  196. // CHECK:STDOUT: %.loc23_18.2: %i32 = converted %int_4, %.loc23_18.1 [concrete = constants.%int_4.940]
  197. // CHECK:STDOUT: %X.ref: type = name_ref X, file.%X.decl [concrete = constants.%X]
  198. // CHECK:STDOUT: %impl.elem0.loc23_26: %.e84 = impl_witness_access constants.%impl_witness.491, element0 [concrete = constants.%Convert.311]
  199. // CHECK:STDOUT: %bound_method.loc23_26: <bound method> = bound_method %.loc23_18.2, %impl.elem0.loc23_26 [concrete = constants.%Convert.bound.483]
  200. // CHECK:STDOUT: %.loc23_26.1: ref %X = temporary_storage
  201. // CHECK:STDOUT: %Convert.call.loc23_26: init %X = call %bound_method.loc23_26(%.loc23_18.2) to %.loc23_26.1
  202. // CHECK:STDOUT: %.loc23_26.2: init %X = converted %.loc23_18.2, %Convert.call.loc23_26
  203. // CHECK:STDOUT: %int_32.loc23_35: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  204. // CHECK:STDOUT: %i32.loc23_35: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  205. // CHECK:STDOUT: %impl.elem0.loc23_32: %.c95 = impl_witness_access constants.%impl_witness.662, element0 [concrete = constants.%Convert.8bb]
  206. // CHECK:STDOUT: %bound_method.loc23_32: <bound method> = bound_method %.loc23_26.2, %impl.elem0.loc23_32
  207. // CHECK:STDOUT: %.loc23_26.3: ref %X = temporary %.loc23_26.1, %.loc23_26.2
  208. // CHECK:STDOUT: %.loc23_26.4: %X = bind_value %.loc23_26.3
  209. // CHECK:STDOUT: %Convert.call.loc23_32: init %i32 = call %bound_method.loc23_32(%.loc23_26.4)
  210. // CHECK:STDOUT: %.loc23_32.1: %i32 = value_of_initializer %Convert.call.loc23_32
  211. // CHECK:STDOUT: %.loc23_32.2: %i32 = converted %.loc23_26.2, %.loc23_32.1
  212. // CHECK:STDOUT: return
  213. // CHECK:STDOUT: }
  214. // CHECK:STDOUT: