default_fn.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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[unused 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[unused 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.849: %I.type = symbolic_binding Self, 0 [symbolic]
  33. // CHECK:STDOUT: %Self.binding.as_type.5c6: type = symbolic_binding_type Self, 0, %Self.849 [symbolic]
  34. // CHECK:STDOUT: %pattern_type.b59: type = pattern_type %Self.binding.as_type.5c6 [symbolic]
  35. // CHECK:STDOUT: %I.WithSelf.F.type.b26: type = fn_type @I.WithSelf.F, @I.WithSelf(%Self.849) [symbolic]
  36. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  37. // CHECK:STDOUT: %I.WithSelf.F.983: %I.WithSelf.F.type.b26 = struct_value () [symbolic]
  38. // CHECK:STDOUT: %I.assoc_type: type = assoc_entity_type @I [concrete]
  39. // CHECK:STDOUT: %assoc0.1f8: %I.assoc_type = assoc_entity element0, @I.WithSelf.%I.WithSelf.F.decl [concrete]
  40. // CHECK:STDOUT: %I.impl_witness: <witness> = impl_witness @C.as.I.impl.%I.impl_witness_table [concrete]
  41. // CHECK:STDOUT: %pattern_type.7c7: 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: %I.WithSelf.F.type.1f2: type = fn_type @I.WithSelf.F, @I.WithSelf(%I.facet) [concrete]
  46. // CHECK:STDOUT: %I.WithSelf.F.860: %I.WithSelf.F.type.1f2 = struct_value () [concrete]
  47. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  48. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  49. // CHECK:STDOUT: %require_complete: <witness> = require_complete_type %Self.binding.as_type.5c6 [symbolic]
  50. // CHECK:STDOUT: %empty_struct: %empty_struct_type = struct_value () [concrete]
  51. // CHECK:STDOUT: %C.val: %C = struct_value () [concrete]
  52. // CHECK:STDOUT: %.fae: type = fn_type_with_self_type %I.WithSelf.F.type.1f2, %I.facet [concrete]
  53. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  54. // CHECK:STDOUT: %Destroy.Op.type: type = fn_type @Destroy.Op [concrete]
  55. // CHECK:STDOUT: %Destroy.Op: %Destroy.Op.type = struct_value () [concrete]
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT:
  58. // CHECK:STDOUT: imports {
  59. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  60. // CHECK:STDOUT: .Destroy = %Core.Destroy
  61. // CHECK:STDOUT: import Core//prelude
  62. // CHECK:STDOUT: import Core//prelude/...
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %Core.Destroy: type = import_ref Core//prelude/parts/destroy, Destroy, loaded [concrete = constants.%Destroy.type]
  65. // CHECK:STDOUT: }
  66. // CHECK:STDOUT:
  67. // CHECK:STDOUT: file {
  68. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  69. // CHECK:STDOUT: .Core = imports.%Core
  70. // CHECK:STDOUT: .C = %C.decl
  71. // CHECK:STDOUT: }
  72. // CHECK:STDOUT: %Core.import = import Core
  73. // CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
  74. // CHECK:STDOUT: }
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: interface @I {
  77. // CHECK:STDOUT: %Self: %I.type = symbolic_binding Self, 0 [symbolic = constants.%Self.849]
  78. // CHECK:STDOUT: %I.WithSelf.decl = interface_with_self_decl @I [concrete]
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: !with Self:
  81. // CHECK:STDOUT: %I.WithSelf.F.decl: @I.WithSelf.%I.WithSelf.F.type (%I.WithSelf.F.type.b26) = fn_decl @I.WithSelf.F [symbolic = @I.WithSelf.%I.WithSelf.F (constants.%I.WithSelf.F.983)] {
  82. // CHECK:STDOUT: %self.param_patt: @I.WithSelf.F.%pattern_type (%pattern_type.b59) = value_param_pattern [concrete]
  83. // CHECK:STDOUT: %self.patt: @I.WithSelf.F.%pattern_type (%pattern_type.b59) = at_binding_pattern self, %self.param_patt [concrete]
  84. // CHECK:STDOUT: } {
  85. // CHECK:STDOUT: %self.param: @I.WithSelf.F.%Self.binding.as_type (%Self.binding.as_type.5c6) = value_param call_param0
  86. // CHECK:STDOUT: %.loc18_23.1: type = splice_block %.loc18_23.2 [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type.5c6)] {
  87. // CHECK:STDOUT: %Self.ref: %I.type = name_ref Self, @I.%Self [symbolic = %Self (constants.%Self.849)]
  88. // CHECK:STDOUT: %Self.as_type: type = facet_access_type %Self.ref [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type.5c6)]
  89. // CHECK:STDOUT: %.loc18_23.2: type = converted %Self.ref, %Self.as_type [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type.5c6)]
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT: %self: @I.WithSelf.F.%Self.binding.as_type (%Self.binding.as_type.5c6) = value_binding self, %self.param
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, %I.WithSelf.F.decl [concrete = constants.%assoc0.1f8]
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: !members:
  96. // CHECK:STDOUT: .Self = %Self
  97. // CHECK:STDOUT: .C = <poisoned>
  98. // CHECK:STDOUT: .I = <poisoned>
  99. // CHECK:STDOUT: .F = @I.WithSelf.%assoc0
  100. // CHECK:STDOUT: .C = <poisoned>
  101. // CHECK:STDOUT: .I = <poisoned>
  102. // CHECK:STDOUT: witness = (@I.WithSelf.%I.WithSelf.F.decl)
  103. // CHECK:STDOUT:
  104. // CHECK:STDOUT: !requires:
  105. // CHECK:STDOUT: }
  106. // CHECK:STDOUT:
  107. // CHECK:STDOUT: impl @C.as.I.impl: %C.ref as %I.ref {
  108. // 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] {
  109. // CHECK:STDOUT: %self.param_patt: %pattern_type.7c7 = value_param_pattern [concrete]
  110. // CHECK:STDOUT: %self.patt: %pattern_type.7c7 = at_binding_pattern self, %self.param_patt [concrete]
  111. // CHECK:STDOUT: } {
  112. // CHECK:STDOUT: %self.param: %C = value_param call_param0
  113. // CHECK:STDOUT: %Self.ref: type = name_ref Self, @C.as.I.impl.%C.ref [concrete = constants.%C]
  114. // CHECK:STDOUT: %self: %C = value_binding self, %self.param
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT: %I.impl_witness_table = impl_witness_table (%C.as.I.impl.F.decl), @C.as.I.impl [concrete]
  117. // CHECK:STDOUT: %I.impl_witness: <witness> = impl_witness %I.impl_witness_table [concrete = constants.%I.impl_witness]
  118. // CHECK:STDOUT:
  119. // CHECK:STDOUT: !members:
  120. // CHECK:STDOUT: .F = %C.as.I.impl.F.decl
  121. // CHECK:STDOUT: witness = %I.impl_witness
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: class @C {
  125. // CHECK:STDOUT: %I.decl: type = interface_decl @I [concrete = constants.%I.type] {} {}
  126. // CHECK:STDOUT: impl_decl @C.as.I.impl [concrete] {} {
  127. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  128. // CHECK:STDOUT: %I.ref: type = name_ref I, @C.%I.decl [concrete = constants.%I.type]
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type]
  131. // CHECK:STDOUT: complete_type_witness = %complete_type
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: !members:
  134. // CHECK:STDOUT: .Self = constants.%C
  135. // CHECK:STDOUT: .I = %I.decl
  136. // CHECK:STDOUT: .C = <poisoned>
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: generic fn @I.WithSelf.F(@I.%Self: %I.type) {
  140. // CHECK:STDOUT: %Self: %I.type = symbolic_binding Self, 0 [symbolic = %Self (constants.%Self.849)]
  141. // CHECK:STDOUT: %Self.binding.as_type: type = symbolic_binding_type Self, 0, %Self [symbolic = %Self.binding.as_type (constants.%Self.binding.as_type.5c6)]
  142. // CHECK:STDOUT: %pattern_type: type = pattern_type %Self.binding.as_type [symbolic = %pattern_type (constants.%pattern_type.b59)]
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: !definition:
  145. // CHECK:STDOUT: %require_complete: <witness> = require_complete_type %Self.binding.as_type [symbolic = %require_complete (constants.%require_complete)]
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: fn(%self.param: @I.WithSelf.F.%Self.binding.as_type (%Self.binding.as_type.5c6)) {
  148. // CHECK:STDOUT: !entry:
  149. // CHECK:STDOUT: name_binding_decl {
  150. // CHECK:STDOUT: %c.patt: %pattern_type.7c7 = ref_binding_pattern c [concrete]
  151. // CHECK:STDOUT: %c.var_patt: %pattern_type.7c7 = var_pattern %c.patt [concrete]
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT: %c.var: ref %C = var %c.var_patt
  154. // CHECK:STDOUT: %.loc20_19.1: %empty_struct_type = struct_literal () [concrete = constants.%empty_struct]
  155. // CHECK:STDOUT: %.loc20_19.2: init %C to %c.var = class_init () [concrete = constants.%C.val]
  156. // CHECK:STDOUT: %.loc20_7: init %C = converted %.loc20_19.1, %.loc20_19.2 [concrete = constants.%C.val]
  157. // CHECK:STDOUT: assign %c.var, %.loc20_7
  158. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  159. // CHECK:STDOUT: %c: ref %C = ref_binding c, %c.var
  160. // CHECK:STDOUT: %c.ref: ref %C = name_ref c, %c
  161. // CHECK:STDOUT: %I.ref: type = name_ref I, @C.%I.decl [concrete = constants.%I.type]
  162. // CHECK:STDOUT: %F.ref: %I.assoc_type = name_ref F, @I.WithSelf.%assoc0 [concrete = constants.%assoc0.1f8]
  163. // CHECK:STDOUT: %impl.elem0: %.fae = impl_witness_access constants.%I.impl_witness, element0 [concrete = constants.%C.as.I.impl.F]
  164. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %c.ref, %impl.elem0
  165. // CHECK:STDOUT: %.loc21: %C = acquire_value %c.ref
  166. // CHECK:STDOUT: %C.as.I.impl.F.call: init %empty_tuple.type = call %bound_method(%.loc21)
  167. // CHECK:STDOUT: %Destroy.Op.bound: <bound method> = bound_method %c.var, constants.%Destroy.Op
  168. // CHECK:STDOUT: %Destroy.Op.call: init %empty_tuple.type = call %Destroy.Op.bound(%c.var)
  169. // CHECK:STDOUT: return
  170. // CHECK:STDOUT: }
  171. // CHECK:STDOUT: }
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: fn @C.as.I.impl.F(%self.param: %C) {
  174. // CHECK:STDOUT: !entry:
  175. // CHECK:STDOUT: return
  176. // CHECK:STDOUT: }
  177. // CHECK:STDOUT:
  178. // CHECK:STDOUT: fn @Destroy.Op(%self.param: ref %C) = "no_op";
  179. // CHECK:STDOUT:
  180. // CHECK:STDOUT: specific @I.WithSelf(constants.%Self.849) {
  181. // CHECK:STDOUT: !definition:
  182. // CHECK:STDOUT: %Self => constants.%Self.849
  183. // CHECK:STDOUT: %I.WithSelf.F.type => constants.%I.WithSelf.F.type.b26
  184. // CHECK:STDOUT: %I.WithSelf.F => constants.%I.WithSelf.F.983
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: specific @I.WithSelf.F(constants.%Self.849) {
  188. // CHECK:STDOUT: %Self => constants.%Self.849
  189. // CHECK:STDOUT: %Self.binding.as_type => constants.%Self.binding.as_type.5c6
  190. // CHECK:STDOUT: %pattern_type => constants.%pattern_type.b59
  191. // CHECK:STDOUT: }
  192. // CHECK:STDOUT:
  193. // CHECK:STDOUT: specific @I.WithSelf(constants.%I.facet) {
  194. // CHECK:STDOUT: !definition:
  195. // CHECK:STDOUT: %Self => constants.%I.facet
  196. // CHECK:STDOUT: %I.WithSelf.F.type => constants.%I.WithSelf.F.type.1f2
  197. // CHECK:STDOUT: %I.WithSelf.F => constants.%I.WithSelf.F.860
  198. // CHECK:STDOUT: }
  199. // CHECK:STDOUT:
  200. // CHECK:STDOUT: specific @I.WithSelf.F(constants.%I.facet) {
  201. // CHECK:STDOUT: %Self => constants.%I.facet
  202. // CHECK:STDOUT: %Self.binding.as_type => constants.%C
  203. // CHECK:STDOUT: %pattern_type => constants.%pattern_type.7c7
  204. // CHECK:STDOUT: }
  205. // CHECK:STDOUT: