default_fn.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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/interface/default_fn.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interface/default_fn.carbon
  14. class C {
  15. interface I {
  16. // TODO: Use `default` here.
  17. fn F[self: Self]() {
  18. // I and F are both complete here, and the impl below is in scope.
  19. var c: C = {};
  20. c.(I.F)();
  21. }
  22. }
  23. impl C as I {
  24. fn F[self: Self]() {}
  25. }
  26. }
  27. // CHECK:STDOUT: --- default_fn.carbon
  28. // CHECK:STDOUT:
  29. // CHECK:STDOUT: constants {
  30. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  31. // CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete]
  32. // CHECK:STDOUT: %Self.6ef: %I.type = bind_symbolic_name Self, 0 [symbolic]
  33. // CHECK:STDOUT: %Self.as_type.390: type = facet_access_type %Self.6ef [symbolic]
  34. // CHECK:STDOUT: %pattern_type.b2f: type = pattern_type %Self.as_type.390 [symbolic]
  35. // CHECK:STDOUT: %I.F.type: type = fn_type @I.F [concrete]
  36. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  37. // CHECK:STDOUT: %I.F: %I.F.type = struct_value () [concrete]
  38. // CHECK:STDOUT: %I.assoc_type: type = assoc_entity_type @I [concrete]
  39. // CHECK:STDOUT: %assoc0.a33: %I.assoc_type = assoc_entity element0, @I.%I.F.decl [concrete]
  40. // CHECK:STDOUT: %I.impl_witness: <witness> = impl_witness @C.%I.impl_witness_table [concrete]
  41. // CHECK:STDOUT: %pattern_type.c48: type = pattern_type %C [concrete]
  42. // CHECK:STDOUT: %C.as.I.impl.F.type: type = fn_type @C.as.I.impl.F [concrete]
  43. // CHECK:STDOUT: %C.as.I.impl.F: %C.as.I.impl.F.type = struct_value () [concrete]
  44. // CHECK:STDOUT: %I.facet: %I.type = facet_value %C, (%I.impl_witness) [concrete]
  45. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  46. // CHECK:STDOUT: %pattern_type.f6d: type = pattern_type auto [concrete]
  47. // CHECK:STDOUT: %Destroy.impl_witness.edd: <witness> = impl_witness @C.%Destroy.impl_witness_table [concrete]
  48. // CHECK:STDOUT: %ptr.019: type = ptr_type %C [concrete]
  49. // CHECK:STDOUT: %pattern_type.44a: type = pattern_type %ptr.019 [concrete]
  50. // CHECK:STDOUT: %C.as.Destroy.impl.Op.type: type = fn_type @C.as.Destroy.impl.Op [concrete]
  51. // CHECK:STDOUT: %C.as.Destroy.impl.Op: %C.as.Destroy.impl.Op.type = struct_value () [concrete]
  52. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  53. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  54. // CHECK:STDOUT: %require_complete.8cb: <witness> = require_complete_type %Self.as_type.390 [symbolic]
  55. // CHECK:STDOUT: %C.val: %C = struct_value () [concrete]
  56. // CHECK:STDOUT: %.e5b: type = fn_type_with_self_type %I.F.type, %I.facet [concrete]
  57. // CHECK:STDOUT: }
  58. // CHECK:STDOUT:
  59. // CHECK:STDOUT: imports {
  60. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  61. // CHECK:STDOUT: .Destroy = %Core.Destroy
  62. // CHECK:STDOUT: import Core//prelude
  63. // CHECK:STDOUT: import Core//prelude/...
  64. // CHECK:STDOUT: }
  65. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  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: .C = %C.decl
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: %Core.import = import Core
  74. // CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: interface @I {
  78. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self.6ef]
  79. // CHECK:STDOUT: %I.F.decl: %I.F.type = fn_decl @I.F [concrete = constants.%I.F] {
  80. // CHECK:STDOUT: %self.patt: @I.F.%pattern_type (%pattern_type.b2f) = binding_pattern self [concrete]
  81. // CHECK:STDOUT: %self.param_patt: @I.F.%pattern_type (%pattern_type.b2f) = value_param_pattern %self.patt, call_param0 [concrete]
  82. // CHECK:STDOUT: } {
  83. // CHECK:STDOUT: %self.param: @I.F.%Self.as_type.loc18_16.1 (%Self.as_type.390) = value_param call_param0
  84. // CHECK:STDOUT: %.loc18_16.1: type = splice_block %.loc18_16.2 [symbolic = %Self.as_type.loc18_16.1 (constants.%Self.as_type.390)] {
  85. // CHECK:STDOUT: %Self.ref: %I.type = name_ref Self, @I.%Self [symbolic = %Self (constants.%Self.6ef)]
  86. // CHECK:STDOUT: %Self.as_type.loc18_16.2: type = facet_access_type %Self.ref [symbolic = %Self.as_type.loc18_16.1 (constants.%Self.as_type.390)]
  87. // CHECK:STDOUT: %.loc18_16.2: type = converted %Self.ref, %Self.as_type.loc18_16.2 [symbolic = %Self.as_type.loc18_16.1 (constants.%Self.as_type.390)]
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT: %self: @I.F.%Self.as_type.loc18_16.1 (%Self.as_type.390) = bind_name self, %self.param
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, %I.F.decl [concrete = constants.%assoc0.a33]
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: !members:
  94. // CHECK:STDOUT: .Self = %Self
  95. // CHECK:STDOUT: .F = %assoc0
  96. // CHECK:STDOUT: .C = <poisoned>
  97. // CHECK:STDOUT: .I = <poisoned>
  98. // CHECK:STDOUT: witness = (%I.F.decl)
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: impl @C.as.I.impl: %C.ref as %I.ref {
  102. // CHECK:STDOUT: %C.as.I.impl.F.decl: %C.as.I.impl.F.type = fn_decl @C.as.I.impl.F [concrete = constants.%C.as.I.impl.F] {
  103. // CHECK:STDOUT: %self.patt: %pattern_type.c48 = binding_pattern self [concrete]
  104. // CHECK:STDOUT: %self.param_patt: %pattern_type.c48 = value_param_pattern %self.patt, call_param0 [concrete]
  105. // CHECK:STDOUT: } {
  106. // CHECK:STDOUT: %self.param: %C = value_param call_param0
  107. // CHECK:STDOUT: %Self.ref: type = name_ref Self, @C.as.I.impl.%C.ref [concrete = constants.%C]
  108. // CHECK:STDOUT: %self: %C = bind_name self, %self.param
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT:
  111. // CHECK:STDOUT: !members:
  112. // CHECK:STDOUT: .F = %C.as.I.impl.F.decl
  113. // CHECK:STDOUT: witness = @C.%I.impl_witness
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: impl @C.as.Destroy.impl: @C.%Self.ref as constants.%Destroy.type {
  117. // CHECK:STDOUT: %C.as.Destroy.impl.Op.decl: %C.as.Destroy.impl.Op.type = fn_decl @C.as.Destroy.impl.Op [concrete = constants.%C.as.Destroy.impl.Op] {
  118. // CHECK:STDOUT: %self.patt: %pattern_type.44a = binding_pattern self [concrete]
  119. // CHECK:STDOUT: %self.param_patt: %pattern_type.44a = value_param_pattern %self.patt, call_param0 [concrete]
  120. // CHECK:STDOUT: %.loc15: %pattern_type.f6d = addr_pattern %self.param_patt [concrete]
  121. // CHECK:STDOUT: } {
  122. // CHECK:STDOUT: %self.param: %ptr.019 = value_param call_param0
  123. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [concrete = constants.%C]
  124. // CHECK:STDOUT: %self: %ptr.019 = bind_name self, %self.param
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: !members:
  128. // CHECK:STDOUT: .Op = %C.as.Destroy.impl.Op.decl
  129. // CHECK:STDOUT: witness = @C.%Destroy.impl_witness
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT:
  132. // CHECK:STDOUT: class @C {
  133. // CHECK:STDOUT: %I.decl: type = interface_decl @I [concrete = constants.%I.type] {} {}
  134. // CHECK:STDOUT: impl_decl @C.as.I.impl [concrete] {} {
  135. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  136. // CHECK:STDOUT: %I.ref: type = name_ref I, @C.%I.decl [concrete = constants.%I.type]
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT: %I.impl_witness_table = impl_witness_table (@C.as.I.impl.%C.as.I.impl.F.decl), @C.as.I.impl [concrete]
  139. // CHECK:STDOUT: %I.impl_witness: <witness> = impl_witness %I.impl_witness_table [concrete = constants.%I.impl_witness]
  140. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [concrete = constants.%C]
  141. // CHECK:STDOUT: impl_decl @C.as.Destroy.impl [concrete] {} {}
  142. // CHECK:STDOUT: %Destroy.impl_witness_table = impl_witness_table (@C.as.Destroy.impl.%C.as.Destroy.impl.Op.decl), @C.as.Destroy.impl [concrete]
  143. // CHECK:STDOUT: %Destroy.impl_witness: <witness> = impl_witness %Destroy.impl_witness_table [concrete = constants.%Destroy.impl_witness.edd]
  144. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type]
  145. // CHECK:STDOUT: complete_type_witness = %complete_type
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: !members:
  148. // CHECK:STDOUT: .Self = constants.%C
  149. // CHECK:STDOUT: .I = %I.decl
  150. // CHECK:STDOUT: .C = <poisoned>
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: generic fn @I.F(@I.%Self: %I.type) {
  154. // CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic = %Self (constants.%Self.6ef)]
  155. // CHECK:STDOUT: %Self.as_type.loc18_16.1: type = facet_access_type %Self [symbolic = %Self.as_type.loc18_16.1 (constants.%Self.as_type.390)]
  156. // CHECK:STDOUT: %pattern_type: type = pattern_type %Self.as_type.loc18_16.1 [symbolic = %pattern_type (constants.%pattern_type.b2f)]
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: !definition:
  159. // CHECK:STDOUT: %require_complete: <witness> = require_complete_type %Self.as_type.loc18_16.1 [symbolic = %require_complete (constants.%require_complete.8cb)]
  160. // CHECK:STDOUT:
  161. // CHECK:STDOUT: fn(%self.param: @I.F.%Self.as_type.loc18_16.1 (%Self.as_type.390)) {
  162. // CHECK:STDOUT: !entry:
  163. // CHECK:STDOUT: name_binding_decl {
  164. // CHECK:STDOUT: %c.patt: %pattern_type.c48 = binding_pattern c [concrete]
  165. // CHECK:STDOUT: %c.var_patt: %pattern_type.c48 = var_pattern %c.patt [concrete]
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT: %c.var: ref %C = var %c.var_patt
  168. // CHECK:STDOUT: %.loc20_19.1: %empty_struct_type = struct_literal ()
  169. // CHECK:STDOUT: %.loc20_19.2: init %C = class_init (), %c.var [concrete = constants.%C.val]
  170. // CHECK:STDOUT: %.loc20_7: init %C = converted %.loc20_19.1, %.loc20_19.2 [concrete = constants.%C.val]
  171. // CHECK:STDOUT: assign %c.var, %.loc20_7
  172. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  173. // CHECK:STDOUT: %c: ref %C = bind_name c, %c.var
  174. // CHECK:STDOUT: %c.ref: ref %C = name_ref c, %c
  175. // CHECK:STDOUT: %I.ref: type = name_ref I, @C.%I.decl [concrete = constants.%I.type]
  176. // CHECK:STDOUT: %F.ref: %I.assoc_type = name_ref F, @I.%assoc0 [concrete = constants.%assoc0.a33]
  177. // CHECK:STDOUT: %impl.elem0: %.e5b = impl_witness_access constants.%I.impl_witness, element0 [concrete = constants.%C.as.I.impl.F]
  178. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %c.ref, %impl.elem0
  179. // CHECK:STDOUT: %.loc21: %C = bind_value %c.ref
  180. // CHECK:STDOUT: %C.as.I.impl.F.call: init %empty_tuple.type = call %bound_method(%.loc21)
  181. // CHECK:STDOUT: %C.as.Destroy.impl.Op.bound: <bound method> = bound_method %c.var, constants.%C.as.Destroy.impl.Op
  182. // CHECK:STDOUT: %addr: %ptr.019 = addr_of %c.var
  183. // CHECK:STDOUT: %C.as.Destroy.impl.Op.call: init %empty_tuple.type = call %C.as.Destroy.impl.Op.bound(%addr)
  184. // CHECK:STDOUT: return
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT: }
  187. // CHECK:STDOUT:
  188. // CHECK:STDOUT: fn @C.as.I.impl.F(%self.param: %C) {
  189. // CHECK:STDOUT: !entry:
  190. // CHECK:STDOUT: return
  191. // CHECK:STDOUT: }
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: fn @C.as.Destroy.impl.Op(%self.param: %ptr.019) = "no_op";
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: specific @I.F(constants.%Self.6ef) {
  196. // CHECK:STDOUT: %Self => constants.%Self.6ef
  197. // CHECK:STDOUT: %Self.as_type.loc18_16.1 => constants.%Self.as_type.390
  198. // CHECK:STDOUT: %pattern_type => constants.%pattern_type.b2f
  199. // CHECK:STDOUT: }
  200. // CHECK:STDOUT:
  201. // CHECK:STDOUT: specific @I.F(constants.%I.facet) {
  202. // CHECK:STDOUT: %Self => constants.%I.facet
  203. // CHECK:STDOUT: %Self.as_type.loc18_16.1 => constants.%C
  204. // CHECK:STDOUT: %pattern_type => constants.%pattern_type.c48
  205. // CHECK:STDOUT: }
  206. // CHECK:STDOUT: