self_in_signature.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. interface UseSelf {
  7. fn F[self: Self](x: Self) -> Self;
  8. }
  9. class C {}
  10. class D {}
  11. impl C as UseSelf {
  12. fn F[self: C](x: C) -> C { return {}; }
  13. }
  14. impl D as UseSelf {
  15. fn F[self: Self](x: Self) -> Self { return {}; }
  16. }
  17. interface SelfNested {
  18. fn F(x: (Self*, {.x: Self, .y: i32}));
  19. }
  20. impl C as SelfNested {
  21. fn F(x: (C*, {.x: C, .y: i32}));
  22. }
  23. impl D as SelfNested {
  24. fn F(x: (Self*, {.x: Self, .y: i32}));
  25. }
  26. // CHECK:STDOUT: --- self_in_signature.carbon
  27. // CHECK:STDOUT:
  28. // CHECK:STDOUT: constants {
  29. // CHECK:STDOUT: %.1: type = interface_type @UseSelf [template]
  30. // CHECK:STDOUT: %.2: type = assoc_entity_type @UseSelf, <function> [template]
  31. // CHECK:STDOUT: %.3: <associated <function> in UseSelf> = assoc_entity element0, @UseSelf.%F [template]
  32. // CHECK:STDOUT: %C: type = class_type @C [template]
  33. // CHECK:STDOUT: %.4: type = struct_type {} [template]
  34. // CHECK:STDOUT: %D: type = class_type @D [template]
  35. // CHECK:STDOUT: %.5: type = tuple_type () [template]
  36. // CHECK:STDOUT: %.6: type = ptr_type {} [template]
  37. // CHECK:STDOUT: %.7: C = struct_value () [template]
  38. // CHECK:STDOUT: %.8: <witness> = interface_witness (@impl.1.%F) [template]
  39. // CHECK:STDOUT: %.9: D = struct_value () [template]
  40. // CHECK:STDOUT: %.10: <witness> = interface_witness (@impl.2.%F) [template]
  41. // CHECK:STDOUT: %.11: type = interface_type @SelfNested [template]
  42. // CHECK:STDOUT: %.12: type = ptr_type Self [symbolic]
  43. // CHECK:STDOUT: %.13: type = struct_type {.x: Self, .y: i32} [symbolic]
  44. // CHECK:STDOUT: %.14: type = tuple_type (type, type) [template]
  45. // CHECK:STDOUT: %.15: type = tuple_type (Self*, {.x: Self, .y: i32}) [symbolic]
  46. // CHECK:STDOUT: %.16: type = assoc_entity_type @SelfNested, <function> [template]
  47. // CHECK:STDOUT: %.17: <associated <function> in SelfNested> = assoc_entity element0, @SelfNested.%F [template]
  48. // CHECK:STDOUT: %.18: type = ptr_type C [template]
  49. // CHECK:STDOUT: %.19: type = struct_type {.x: C, .y: i32} [template]
  50. // CHECK:STDOUT: %.20: type = tuple_type (C*, {.x: C, .y: i32}) [template]
  51. // CHECK:STDOUT: %.21: <witness> = interface_witness (@impl.3.%F) [template]
  52. // CHECK:STDOUT: %.22: type = ptr_type D [template]
  53. // CHECK:STDOUT: %.23: type = struct_type {.x: D, .y: i32} [template]
  54. // CHECK:STDOUT: %.24: type = tuple_type (D*, {.x: D, .y: i32}) [template]
  55. // CHECK:STDOUT: %.25: <witness> = interface_witness (@impl.4.%F) [template]
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: file {
  59. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  60. // CHECK:STDOUT: .UseSelf = %UseSelf.decl
  61. // CHECK:STDOUT: .C = %C.decl
  62. // CHECK:STDOUT: .D = %D.decl
  63. // CHECK:STDOUT: .SelfNested = %SelfNested.decl
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT: %UseSelf.decl: type = interface_decl @UseSelf [template = constants.%.1] {}
  66. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {}
  67. // CHECK:STDOUT: %D.decl: type = class_decl @D [template = constants.%D] {}
  68. // CHECK:STDOUT: impl_decl @impl.1 {
  69. // CHECK:STDOUT: %C.ref.loc15: type = name_ref C, %C.decl [template = constants.%C]
  70. // CHECK:STDOUT: %UseSelf.ref.loc15: type = name_ref UseSelf, %UseSelf.decl [template = constants.%.1]
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: impl_decl @impl.2 {
  73. // CHECK:STDOUT: %D.ref.loc19: type = name_ref D, %D.decl [template = constants.%D]
  74. // CHECK:STDOUT: %UseSelf.ref.loc19: type = name_ref UseSelf, %UseSelf.decl [template = constants.%.1]
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT: %SelfNested.decl: type = interface_decl @SelfNested [template = constants.%.11] {}
  77. // CHECK:STDOUT: impl_decl @impl.3 {
  78. // CHECK:STDOUT: %C.ref.loc27: type = name_ref C, %C.decl [template = constants.%C]
  79. // CHECK:STDOUT: %SelfNested.ref.loc27: type = name_ref SelfNested, %SelfNested.decl [template = constants.%.11]
  80. // CHECK:STDOUT: }
  81. // CHECK:STDOUT: impl_decl @impl.4 {
  82. // CHECK:STDOUT: %D.ref.loc31: type = name_ref D, %D.decl [template = constants.%D]
  83. // CHECK:STDOUT: %SelfNested.ref.loc31: type = name_ref SelfNested, %SelfNested.decl [template = constants.%.11]
  84. // CHECK:STDOUT: }
  85. // CHECK:STDOUT: }
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: interface @UseSelf {
  88. // CHECK:STDOUT: %Self: UseSelf = bind_symbolic_name Self [symbolic]
  89. // CHECK:STDOUT: %F: <function> = fn_decl @F.1 [template] {
  90. // CHECK:STDOUT: %Self.ref.loc8_14: UseSelf = name_ref Self, %Self [symbolic = %Self]
  91. // CHECK:STDOUT: %.loc8_14.1: type = facet_type_access %Self.ref.loc8_14 [symbolic = %Self]
  92. // CHECK:STDOUT: %.loc8_14.2: type = converted %Self.ref.loc8_14, %.loc8_14.1 [symbolic = %Self]
  93. // CHECK:STDOUT: %self.loc8_8.1: Self = param self
  94. // CHECK:STDOUT: %self.loc8_8.2: Self = bind_name self, %self.loc8_8.1
  95. // CHECK:STDOUT: %Self.ref.loc8_23: UseSelf = name_ref Self, %Self [symbolic = %Self]
  96. // CHECK:STDOUT: %.loc8_23.1: type = facet_type_access %Self.ref.loc8_23 [symbolic = %Self]
  97. // CHECK:STDOUT: %.loc8_23.2: type = converted %Self.ref.loc8_23, %.loc8_23.1 [symbolic = %Self]
  98. // CHECK:STDOUT: %x.loc8_20.1: Self = param x
  99. // CHECK:STDOUT: %x.loc8_20.2: Self = bind_name x, %x.loc8_20.1
  100. // CHECK:STDOUT: %Self.ref.loc8_32: UseSelf = name_ref Self, %Self [symbolic = %Self]
  101. // CHECK:STDOUT: %.loc8_32.1: type = facet_type_access %Self.ref.loc8_32 [symbolic = %Self]
  102. // CHECK:STDOUT: %.loc8_32.2: type = converted %Self.ref.loc8_32, %.loc8_32.1 [symbolic = %Self]
  103. // CHECK:STDOUT: %return.var: ref Self = var <return slot>
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT: %.loc8_36: <associated <function> in UseSelf> = assoc_entity element0, %F [template = constants.%.3]
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: !members:
  108. // CHECK:STDOUT: .Self = %Self
  109. // CHECK:STDOUT: .F = %.loc8_36
  110. // CHECK:STDOUT: witness = (%F)
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: interface @SelfNested {
  114. // CHECK:STDOUT: %Self: SelfNested = bind_symbolic_name Self [symbolic]
  115. // CHECK:STDOUT: %F: <function> = fn_decl @F.4 [template] {
  116. // CHECK:STDOUT: %Self.ref.loc24_12: SelfNested = name_ref Self, %Self [symbolic = %Self]
  117. // CHECK:STDOUT: %.loc24_16.1: type = facet_type_access %Self.ref.loc24_12 [symbolic = %Self]
  118. // CHECK:STDOUT: %.loc24_12: type = converted %Self.ref.loc24_12, %.loc24_16.1 [symbolic = %Self]
  119. // CHECK:STDOUT: %.loc24_16.2: type = ptr_type Self [symbolic = constants.%.12]
  120. // CHECK:STDOUT: %Self.ref.loc24_24: SelfNested = name_ref Self, %Self [symbolic = %Self]
  121. // CHECK:STDOUT: %.loc24_24.1: type = facet_type_access %Self.ref.loc24_24 [symbolic = %Self]
  122. // CHECK:STDOUT: %.loc24_24.2: type = converted %Self.ref.loc24_24, %.loc24_24.1 [symbolic = %Self]
  123. // CHECK:STDOUT: %.loc24_37: type = struct_type {.x: Self, .y: i32} [symbolic = constants.%.13]
  124. // CHECK:STDOUT: %.loc24_38.1: (type, type) = tuple_literal (%.loc24_16.2, %.loc24_37)
  125. // CHECK:STDOUT: %.loc24_38.2: type = converted %.loc24_38.1, constants.%.15 [symbolic = constants.%.15]
  126. // CHECK:STDOUT: %x.loc24_8.1: (Self*, {.x: Self, .y: i32}) = param x
  127. // CHECK:STDOUT: %x.loc24_8.2: (Self*, {.x: Self, .y: i32}) = bind_name x, %x.loc24_8.1
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT: %.loc24_40: <associated <function> in SelfNested> = assoc_entity element0, %F [template = constants.%.17]
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: !members:
  132. // CHECK:STDOUT: .Self = %Self
  133. // CHECK:STDOUT: .F = %.loc24_40
  134. // CHECK:STDOUT: witness = (%F)
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: impl @impl.1: C as UseSelf {
  138. // CHECK:STDOUT: %F: <function> = fn_decl @F.2 [template] {
  139. // CHECK:STDOUT: %C.ref.loc16_14: type = name_ref C, file.%C.decl [template = constants.%C]
  140. // CHECK:STDOUT: %self.loc16_8.1: C = param self
  141. // CHECK:STDOUT: %self.loc16_8.2: C = bind_name self, %self.loc16_8.1
  142. // CHECK:STDOUT: %C.ref.loc16_20: type = name_ref C, file.%C.decl [template = constants.%C]
  143. // CHECK:STDOUT: %x.loc16_17.1: C = param x
  144. // CHECK:STDOUT: %x.loc16_17.2: C = bind_name x, %x.loc16_17.1
  145. // CHECK:STDOUT: %C.ref.loc16_26: type = name_ref C, file.%C.decl [template = constants.%C]
  146. // CHECK:STDOUT: %return.var: ref C = var <return slot>
  147. // CHECK:STDOUT: }
  148. // CHECK:STDOUT: %.1: <witness> = interface_witness (%F) [template = constants.%.8]
  149. // CHECK:STDOUT:
  150. // CHECK:STDOUT: !members:
  151. // CHECK:STDOUT: .F = %F
  152. // CHECK:STDOUT: witness = %.1
  153. // CHECK:STDOUT: }
  154. // CHECK:STDOUT:
  155. // CHECK:STDOUT: impl @impl.2: D as UseSelf {
  156. // CHECK:STDOUT: %F: <function> = fn_decl @F.3 [template] {
  157. // CHECK:STDOUT: %Self.ref.loc20_14: type = name_ref Self, constants.%D [template = constants.%D]
  158. // CHECK:STDOUT: %self.loc20_8.1: D = param self
  159. // CHECK:STDOUT: %self.loc20_8.2: D = bind_name self, %self.loc20_8.1
  160. // CHECK:STDOUT: %Self.ref.loc20_23: type = name_ref Self, constants.%D [template = constants.%D]
  161. // CHECK:STDOUT: %x.loc20_20.1: D = param x
  162. // CHECK:STDOUT: %x.loc20_20.2: D = bind_name x, %x.loc20_20.1
  163. // CHECK:STDOUT: %Self.ref.loc20_32: type = name_ref Self, constants.%D [template = constants.%D]
  164. // CHECK:STDOUT: %return.var: ref D = var <return slot>
  165. // CHECK:STDOUT: }
  166. // CHECK:STDOUT: %.1: <witness> = interface_witness (%F) [template = constants.%.10]
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: !members:
  169. // CHECK:STDOUT: .F = %F
  170. // CHECK:STDOUT: witness = %.1
  171. // CHECK:STDOUT: }
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: impl @impl.3: C as SelfNested {
  174. // CHECK:STDOUT: %F: <function> = fn_decl @F.5 [template] {
  175. // CHECK:STDOUT: %C.ref.loc28_12: type = name_ref C, file.%C.decl [template = constants.%C]
  176. // CHECK:STDOUT: %.loc28_13: type = ptr_type C [template = constants.%.18]
  177. // CHECK:STDOUT: %C.ref.loc28_21: type = name_ref C, file.%C.decl [template = constants.%C]
  178. // CHECK:STDOUT: %.loc28_31: type = struct_type {.x: C, .y: i32} [template = constants.%.19]
  179. // CHECK:STDOUT: %.loc28_32.1: (type, type) = tuple_literal (%.loc28_13, %.loc28_31)
  180. // CHECK:STDOUT: %.loc28_32.2: type = converted %.loc28_32.1, constants.%.20 [template = constants.%.20]
  181. // CHECK:STDOUT: %x.loc28_8.1: (C*, {.x: C, .y: i32}) = param x
  182. // CHECK:STDOUT: %x.loc28_8.2: (C*, {.x: C, .y: i32}) = bind_name x, %x.loc28_8.1
  183. // CHECK:STDOUT: }
  184. // CHECK:STDOUT: %.1: <witness> = interface_witness (%F) [template = constants.%.21]
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: !members:
  187. // CHECK:STDOUT: .F = %F
  188. // CHECK:STDOUT: witness = %.1
  189. // CHECK:STDOUT: }
  190. // CHECK:STDOUT:
  191. // CHECK:STDOUT: impl @impl.4: D as SelfNested {
  192. // CHECK:STDOUT: %F: <function> = fn_decl @F.6 [template] {
  193. // CHECK:STDOUT: %Self.ref.loc32_12: type = name_ref Self, constants.%D [template = constants.%D]
  194. // CHECK:STDOUT: %.loc32_16: type = ptr_type D [template = constants.%.22]
  195. // CHECK:STDOUT: %Self.ref.loc32_24: type = name_ref Self, constants.%D [template = constants.%D]
  196. // CHECK:STDOUT: %.loc32_37: type = struct_type {.x: D, .y: i32} [template = constants.%.23]
  197. // CHECK:STDOUT: %.loc32_38.1: (type, type) = tuple_literal (%.loc32_16, %.loc32_37)
  198. // CHECK:STDOUT: %.loc32_38.2: type = converted %.loc32_38.1, constants.%.24 [template = constants.%.24]
  199. // CHECK:STDOUT: %x.loc32_8.1: (D*, {.x: D, .y: i32}) = param x
  200. // CHECK:STDOUT: %x.loc32_8.2: (D*, {.x: D, .y: i32}) = bind_name x, %x.loc32_8.1
  201. // CHECK:STDOUT: }
  202. // CHECK:STDOUT: %.1: <witness> = interface_witness (%F) [template = constants.%.25]
  203. // CHECK:STDOUT:
  204. // CHECK:STDOUT: !members:
  205. // CHECK:STDOUT: .F = %F
  206. // CHECK:STDOUT: witness = %.1
  207. // CHECK:STDOUT: }
  208. // CHECK:STDOUT:
  209. // CHECK:STDOUT: class @C {
  210. // CHECK:STDOUT: !members:
  211. // CHECK:STDOUT: .Self = constants.%C
  212. // CHECK:STDOUT: }
  213. // CHECK:STDOUT:
  214. // CHECK:STDOUT: class @D {
  215. // CHECK:STDOUT: !members:
  216. // CHECK:STDOUT: .Self = constants.%D
  217. // CHECK:STDOUT: }
  218. // CHECK:STDOUT:
  219. // CHECK:STDOUT: fn @F.1[@UseSelf.%self.loc8_8.2: Self](@UseSelf.%x.loc8_20.2: Self) -> Self;
  220. // CHECK:STDOUT:
  221. // CHECK:STDOUT: fn @F.2[@impl.1.%self.loc16_8.2: C](@impl.1.%x.loc16_17.2: C) -> @impl.1.%return.var: C {
  222. // CHECK:STDOUT: !entry:
  223. // CHECK:STDOUT: %.loc16_38.1: {} = struct_literal ()
  224. // CHECK:STDOUT: %.loc16_38.2: init C = class_init (), @impl.1.%return.var [template = constants.%.7]
  225. // CHECK:STDOUT: %.loc16_38.3: init C = converted %.loc16_38.1, %.loc16_38.2 [template = constants.%.7]
  226. // CHECK:STDOUT: return %.loc16_38.3
  227. // CHECK:STDOUT: }
  228. // CHECK:STDOUT:
  229. // CHECK:STDOUT: fn @F.3[@impl.2.%self.loc20_8.2: D](@impl.2.%x.loc20_20.2: D) -> @impl.2.%return.var: D {
  230. // CHECK:STDOUT: !entry:
  231. // CHECK:STDOUT: %.loc20_47.1: {} = struct_literal ()
  232. // CHECK:STDOUT: %.loc20_47.2: init D = class_init (), @impl.2.%return.var [template = constants.%.9]
  233. // CHECK:STDOUT: %.loc20_47.3: init D = converted %.loc20_47.1, %.loc20_47.2 [template = constants.%.9]
  234. // CHECK:STDOUT: return %.loc20_47.3
  235. // CHECK:STDOUT: }
  236. // CHECK:STDOUT:
  237. // CHECK:STDOUT: fn @F.4(@SelfNested.%x.loc24_8.2: (Self*, {.x: Self, .y: i32}));
  238. // CHECK:STDOUT:
  239. // CHECK:STDOUT: fn @F.5(@impl.3.%x.loc28_8.2: (C*, {.x: C, .y: i32}));
  240. // CHECK:STDOUT:
  241. // CHECK:STDOUT: fn @F.6(@impl.4.%x.loc32_8.2: (D*, {.x: D, .y: i32}));
  242. // CHECK:STDOUT: