self_in_class.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/none.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/impl/self_in_class.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/self_in_class.carbon
  14. interface DefaultConstructible {
  15. fn Make() -> Self;
  16. }
  17. class C {}
  18. class A {
  19. impl C as DefaultConstructible {
  20. // `Self` here refers to `C`, not `A`.
  21. // TODO: Revisit this once #3714 is resolved.
  22. fn Make() -> Self { return {}; }
  23. }
  24. }
  25. // CHECK:STDOUT: --- self_in_class.carbon
  26. // CHECK:STDOUT:
  27. // CHECK:STDOUT: constants {
  28. // CHECK:STDOUT: %DefaultConstructible.type: type = facet_type <@DefaultConstructible> [concrete]
  29. // CHECK:STDOUT: %Self: %DefaultConstructible.type = symbolic_binding Self, 0 [symbolic]
  30. // CHECK:STDOUT: %Self.as_type: type = facet_access_type %Self [symbolic]
  31. // CHECK:STDOUT: %.136: Core.Form = init_form %Self.as_type [symbolic]
  32. // CHECK:STDOUT: %pattern_type.493: type = pattern_type %Self.as_type [symbolic]
  33. // CHECK:STDOUT: %DefaultConstructible.WithSelf.Make.type.17e: type = fn_type @DefaultConstructible.WithSelf.Make, @DefaultConstructible.WithSelf(%Self) [symbolic]
  34. // CHECK:STDOUT: %DefaultConstructible.WithSelf.Make.cfb: %DefaultConstructible.WithSelf.Make.type.17e = struct_value () [symbolic]
  35. // CHECK:STDOUT: %DefaultConstructible.assoc_type: type = assoc_entity_type @DefaultConstructible [concrete]
  36. // CHECK:STDOUT: %assoc0: %DefaultConstructible.assoc_type = assoc_entity element0, @DefaultConstructible.WithSelf.%DefaultConstructible.WithSelf.Make.decl [concrete]
  37. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  38. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  39. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  40. // CHECK:STDOUT: %A: type = class_type @A [concrete]
  41. // CHECK:STDOUT: %DefaultConstructible.impl_witness: <witness> = impl_witness @C.as.DefaultConstructible.impl.%DefaultConstructible.impl_witness_table [concrete]
  42. // CHECK:STDOUT: %.a69: Core.Form = init_form %C [concrete]
  43. // CHECK:STDOUT: %pattern_type.7c7: type = pattern_type %C [concrete]
  44. // CHECK:STDOUT: %C.as.DefaultConstructible.impl.Make.type: type = fn_type @C.as.DefaultConstructible.impl.Make [concrete]
  45. // CHECK:STDOUT: %C.as.DefaultConstructible.impl.Make: %C.as.DefaultConstructible.impl.Make.type = struct_value () [concrete]
  46. // CHECK:STDOUT: %DefaultConstructible.facet: %DefaultConstructible.type = facet_value %C, (%DefaultConstructible.impl_witness) [concrete]
  47. // CHECK:STDOUT: %DefaultConstructible.WithSelf.Make.type.0cb: type = fn_type @DefaultConstructible.WithSelf.Make, @DefaultConstructible.WithSelf(%DefaultConstructible.facet) [concrete]
  48. // CHECK:STDOUT: %DefaultConstructible.WithSelf.Make.49d: %DefaultConstructible.WithSelf.Make.type.0cb = struct_value () [concrete]
  49. // CHECK:STDOUT: %empty_struct: %empty_struct_type = struct_value () [concrete]
  50. // CHECK:STDOUT: %C.val: %C = struct_value () [concrete]
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT:
  53. // CHECK:STDOUT: file {
  54. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  55. // CHECK:STDOUT: .DefaultConstructible = %DefaultConstructible.decl
  56. // CHECK:STDOUT: .C = %C.decl
  57. // CHECK:STDOUT: .A = %A.decl
  58. // CHECK:STDOUT: }
  59. // CHECK:STDOUT: %DefaultConstructible.decl: type = interface_decl @DefaultConstructible [concrete = constants.%DefaultConstructible.type] {} {}
  60. // CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
  61. // CHECK:STDOUT: %A.decl: type = class_decl @A [concrete = constants.%A] {} {}
  62. // CHECK:STDOUT: }
  63. // CHECK:STDOUT:
  64. // CHECK:STDOUT: interface @DefaultConstructible {
  65. // CHECK:STDOUT: %Self: %DefaultConstructible.type = symbolic_binding Self, 0 [symbolic = constants.%Self]
  66. // CHECK:STDOUT: %DefaultConstructible.WithSelf.decl = interface_with_self_decl @DefaultConstructible [concrete]
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: !with Self:
  69. // CHECK:STDOUT: %DefaultConstructible.WithSelf.Make.decl: @DefaultConstructible.WithSelf.%DefaultConstructible.WithSelf.Make.type (%DefaultConstructible.WithSelf.Make.type.17e) = fn_decl @DefaultConstructible.WithSelf.Make [symbolic = @DefaultConstructible.WithSelf.%DefaultConstructible.WithSelf.Make (constants.%DefaultConstructible.WithSelf.Make.cfb)] {
  70. // CHECK:STDOUT: %return.param_patt: @DefaultConstructible.WithSelf.Make.%pattern_type (%pattern_type.493) = out_param_pattern [concrete]
  71. // CHECK:STDOUT: %return.patt: @DefaultConstructible.WithSelf.Make.%pattern_type (%pattern_type.493) = return_slot_pattern %return.param_patt, %.loc16_16.2 [concrete]
  72. // CHECK:STDOUT: } {
  73. // CHECK:STDOUT: %Self.ref: %DefaultConstructible.type = name_ref Self, @DefaultConstructible.%Self [symbolic = %Self (constants.%Self)]
  74. // CHECK:STDOUT: %Self.as_type.loc16_16.2: type = facet_access_type %Self.ref [symbolic = %Self.as_type.loc16_16.1 (constants.%Self.as_type)]
  75. // CHECK:STDOUT: %.loc16_16.2: type = converted %Self.ref, %Self.as_type.loc16_16.2 [symbolic = %Self.as_type.loc16_16.1 (constants.%Self.as_type)]
  76. // CHECK:STDOUT: %.loc16_16.3: Core.Form = init_form %.loc16_16.2 [symbolic = %.loc16_16.1 (constants.%.136)]
  77. // CHECK:STDOUT: %return.param: ref @DefaultConstructible.WithSelf.Make.%Self.as_type.loc16_16.1 (%Self.as_type) = out_param call_param0
  78. // CHECK:STDOUT: %return: ref @DefaultConstructible.WithSelf.Make.%Self.as_type.loc16_16.1 (%Self.as_type) = return_slot %return.param
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT: %assoc0: %DefaultConstructible.assoc_type = assoc_entity element0, %DefaultConstructible.WithSelf.Make.decl [concrete = constants.%assoc0]
  81. // CHECK:STDOUT:
  82. // CHECK:STDOUT: !members:
  83. // CHECK:STDOUT: .Self = %Self
  84. // CHECK:STDOUT: .Make = @DefaultConstructible.WithSelf.%assoc0
  85. // CHECK:STDOUT: witness = (@DefaultConstructible.WithSelf.%DefaultConstructible.WithSelf.Make.decl)
  86. // CHECK:STDOUT:
  87. // CHECK:STDOUT: !requires:
  88. // CHECK:STDOUT: }
  89. // CHECK:STDOUT:
  90. // CHECK:STDOUT: impl @C.as.DefaultConstructible.impl: %C.ref as %DefaultConstructible.ref {
  91. // CHECK:STDOUT: %C.as.DefaultConstructible.impl.Make.decl: %C.as.DefaultConstructible.impl.Make.type = fn_decl @C.as.DefaultConstructible.impl.Make [concrete = constants.%C.as.DefaultConstructible.impl.Make] {
  92. // CHECK:STDOUT: %return.param_patt: %pattern_type.7c7 = out_param_pattern [concrete]
  93. // CHECK:STDOUT: %return.patt: %pattern_type.7c7 = return_slot_pattern %return.param_patt, %Self.ref [concrete]
  94. // CHECK:STDOUT: } {
  95. // CHECK:STDOUT: %Self.ref: type = name_ref Self, @C.as.DefaultConstructible.impl.%C.ref [concrete = constants.%C]
  96. // CHECK:STDOUT: %.loc25_18: Core.Form = init_form %Self.ref [concrete = constants.%.a69]
  97. // CHECK:STDOUT: %return.param: ref %C = out_param call_param0
  98. // CHECK:STDOUT: %return: ref %C = return_slot %return.param
  99. // CHECK:STDOUT: }
  100. // CHECK:STDOUT: %DefaultConstructible.impl_witness_table = impl_witness_table (%C.as.DefaultConstructible.impl.Make.decl), @C.as.DefaultConstructible.impl [concrete]
  101. // CHECK:STDOUT: %DefaultConstructible.impl_witness: <witness> = impl_witness %DefaultConstructible.impl_witness_table [concrete = constants.%DefaultConstructible.impl_witness]
  102. // CHECK:STDOUT:
  103. // CHECK:STDOUT: !members:
  104. // CHECK:STDOUT: .Make = %C.as.DefaultConstructible.impl.Make.decl
  105. // CHECK:STDOUT: witness = %DefaultConstructible.impl_witness
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: class @C {
  109. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type]
  110. // CHECK:STDOUT: complete_type_witness = %complete_type
  111. // CHECK:STDOUT:
  112. // CHECK:STDOUT: !members:
  113. // CHECK:STDOUT: .Self = constants.%C
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT:
  116. // CHECK:STDOUT: class @A {
  117. // CHECK:STDOUT: impl_decl @C.as.DefaultConstructible.impl [concrete] {} {
  118. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  119. // CHECK:STDOUT: %DefaultConstructible.ref: type = name_ref DefaultConstructible, file.%DefaultConstructible.decl [concrete = constants.%DefaultConstructible.type]
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type]
  122. // CHECK:STDOUT: complete_type_witness = %complete_type
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: !members:
  125. // CHECK:STDOUT: .Self = constants.%A
  126. // CHECK:STDOUT: .C = <poisoned>
  127. // CHECK:STDOUT: .DefaultConstructible = <poisoned>
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: generic fn @DefaultConstructible.WithSelf.Make(@DefaultConstructible.%Self: %DefaultConstructible.type) {
  131. // CHECK:STDOUT: %Self: %DefaultConstructible.type = symbolic_binding Self, 0 [symbolic = %Self (constants.%Self)]
  132. // CHECK:STDOUT: %Self.as_type.loc16_16.1: type = facet_access_type %Self [symbolic = %Self.as_type.loc16_16.1 (constants.%Self.as_type)]
  133. // CHECK:STDOUT: %.loc16_16.1: Core.Form = init_form %Self.as_type.loc16_16.1 [symbolic = %.loc16_16.1 (constants.%.136)]
  134. // CHECK:STDOUT: %pattern_type: type = pattern_type %Self.as_type.loc16_16.1 [symbolic = %pattern_type (constants.%pattern_type.493)]
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: fn() -> out %return.param: @DefaultConstructible.WithSelf.Make.%Self.as_type.loc16_16.1 (%Self.as_type);
  137. // CHECK:STDOUT: }
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: fn @C.as.DefaultConstructible.impl.Make() -> out %return.param: %C {
  140. // CHECK:STDOUT: !entry:
  141. // CHECK:STDOUT: %.loc25_33.1: %empty_struct_type = struct_literal () [concrete = constants.%empty_struct]
  142. // CHECK:STDOUT: %.loc25_33.2: init %C to %return.param = class_init () [concrete = constants.%C.val]
  143. // CHECK:STDOUT: %.loc25_34: init %C = converted %.loc25_33.1, %.loc25_33.2 [concrete = constants.%C.val]
  144. // CHECK:STDOUT: return %.loc25_34 to %return.param
  145. // CHECK:STDOUT: }
  146. // CHECK:STDOUT:
  147. // CHECK:STDOUT: specific @DefaultConstructible.WithSelf(constants.%Self) {
  148. // CHECK:STDOUT: !definition:
  149. // CHECK:STDOUT: %Self => constants.%Self
  150. // CHECK:STDOUT: %DefaultConstructible.WithSelf.Make.type => constants.%DefaultConstructible.WithSelf.Make.type.17e
  151. // CHECK:STDOUT: %DefaultConstructible.WithSelf.Make => constants.%DefaultConstructible.WithSelf.Make.cfb
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: specific @DefaultConstructible.WithSelf.Make(constants.%Self) {
  155. // CHECK:STDOUT: %Self => constants.%Self
  156. // CHECK:STDOUT: %Self.as_type.loc16_16.1 => constants.%Self.as_type
  157. // CHECK:STDOUT: %.loc16_16.1 => constants.%.136
  158. // CHECK:STDOUT: %pattern_type => constants.%pattern_type.493
  159. // CHECK:STDOUT: }
  160. // CHECK:STDOUT:
  161. // CHECK:STDOUT: specific @DefaultConstructible.WithSelf(constants.%DefaultConstructible.facet) {
  162. // CHECK:STDOUT: !definition:
  163. // CHECK:STDOUT: %Self => constants.%DefaultConstructible.facet
  164. // CHECK:STDOUT: %DefaultConstructible.WithSelf.Make.type => constants.%DefaultConstructible.WithSelf.Make.type.0cb
  165. // CHECK:STDOUT: %DefaultConstructible.WithSelf.Make => constants.%DefaultConstructible.WithSelf.Make.49d
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT:
  168. // CHECK:STDOUT: specific @DefaultConstructible.WithSelf.Make(constants.%DefaultConstructible.facet) {
  169. // CHECK:STDOUT: %Self => constants.%DefaultConstructible.facet
  170. // CHECK:STDOUT: %Self.as_type.loc16_16.1 => constants.%C
  171. // CHECK:STDOUT: %.loc16_16.1 => constants.%.a69
  172. // CHECK:STDOUT: %pattern_type => constants.%pattern_type.7c7
  173. // CHECK:STDOUT: }
  174. // CHECK:STDOUT: