self_in_class.carbon 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/impl/no_prelude/self_in_class.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/no_prelude/self_in_class.carbon
  10. interface DefaultConstructible {
  11. fn Make() -> Self;
  12. }
  13. class C {}
  14. class A {
  15. impl C as DefaultConstructible {
  16. // `Self` here refers to `i32`, not `A`.
  17. // TODO: Revisit this once #3714 is resolved.
  18. fn Make() -> Self { return {}; }
  19. }
  20. }
  21. // CHECK:STDOUT: --- self_in_class.carbon
  22. // CHECK:STDOUT:
  23. // CHECK:STDOUT: constants {
  24. // CHECK:STDOUT: %.1: type = interface_type @DefaultConstructible [template]
  25. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic]
  26. // CHECK:STDOUT: %Make.type.1: type = fn_type @Make.1 [template]
  27. // CHECK:STDOUT: %.2: type = tuple_type () [template]
  28. // CHECK:STDOUT: %Make.1: %Make.type.1 = struct_value () [template]
  29. // CHECK:STDOUT: %.3: type = assoc_entity_type %.1, %Make.type.1 [template]
  30. // CHECK:STDOUT: %.4: %.3 = assoc_entity element0, @DefaultConstructible.%Make.decl [template]
  31. // CHECK:STDOUT: %C: type = class_type @C [template]
  32. // CHECK:STDOUT: %.5: type = struct_type {} [template]
  33. // CHECK:STDOUT: %.6: <witness> = complete_type_witness %.5 [template]
  34. // CHECK:STDOUT: %A: type = class_type @A [template]
  35. // CHECK:STDOUT: %Make.type.2: type = fn_type @Make.2 [template]
  36. // CHECK:STDOUT: %Make.2: %Make.type.2 = struct_value () [template]
  37. // CHECK:STDOUT: %.7: <witness> = interface_witness (%Make.2) [template]
  38. // CHECK:STDOUT: %.8: type = ptr_type %.5 [template]
  39. // CHECK:STDOUT: %struct: %C = struct_value () [template]
  40. // CHECK:STDOUT: }
  41. // CHECK:STDOUT:
  42. // CHECK:STDOUT: file {
  43. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  44. // CHECK:STDOUT: .DefaultConstructible = %DefaultConstructible.decl
  45. // CHECK:STDOUT: .C = %C.decl
  46. // CHECK:STDOUT: .A = %A.decl
  47. // CHECK:STDOUT: }
  48. // CHECK:STDOUT: %DefaultConstructible.decl: type = interface_decl @DefaultConstructible [template = constants.%.1] {} {}
  49. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {} {}
  50. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {} {}
  51. // CHECK:STDOUT: }
  52. // CHECK:STDOUT:
  53. // CHECK:STDOUT: interface @DefaultConstructible {
  54. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic = constants.%Self]
  55. // CHECK:STDOUT: %Make.decl: %Make.type.1 = fn_decl @Make.1 [template = constants.%Make.1] {} {
  56. // CHECK:STDOUT: %Self.ref: %.1 = name_ref Self, @DefaultConstructible.%Self [symbolic = %Self (constants.%Self)]
  57. // CHECK:STDOUT: %.loc12_16.1: type = facet_type_access %Self.ref [symbolic = %Self (constants.%Self)]
  58. // CHECK:STDOUT: %.loc12_16.2: type = converted %Self.ref, %.loc12_16.1 [symbolic = %Self (constants.%Self)]
  59. // CHECK:STDOUT: %return: ref @Make.1.%Self (%Self) = var <return slot>
  60. // CHECK:STDOUT: }
  61. // CHECK:STDOUT: %.loc12: %.3 = assoc_entity element0, %Make.decl [template = constants.%.4]
  62. // CHECK:STDOUT:
  63. // CHECK:STDOUT: !members:
  64. // CHECK:STDOUT: .Self = %Self
  65. // CHECK:STDOUT: .Make = %.loc12
  66. // CHECK:STDOUT: witness = (%Make.decl)
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: impl @impl: %C as %.1 {
  70. // CHECK:STDOUT: %Make.decl: %Make.type.2 = fn_decl @Make.2 [template = constants.%Make.2] {} {
  71. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [template = constants.%C]
  72. // CHECK:STDOUT: %return: ref %C = var <return slot>
  73. // CHECK:STDOUT: }
  74. // CHECK:STDOUT: %.loc18: <witness> = interface_witness (%Make.decl) [template = constants.%.7]
  75. // CHECK:STDOUT:
  76. // CHECK:STDOUT: !members:
  77. // CHECK:STDOUT: .Make = %Make.decl
  78. // CHECK:STDOUT: witness = %.loc18
  79. // CHECK:STDOUT: }
  80. // CHECK:STDOUT:
  81. // CHECK:STDOUT: class @C {
  82. // CHECK:STDOUT: %.loc15: <witness> = complete_type_witness %.5 [template = constants.%.6]
  83. // CHECK:STDOUT:
  84. // CHECK:STDOUT: !members:
  85. // CHECK:STDOUT: .Self = constants.%C
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: class @A {
  89. // CHECK:STDOUT: impl_decl @impl {} {
  90. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  91. // CHECK:STDOUT: %DefaultConstructible.ref: type = name_ref DefaultConstructible, file.%DefaultConstructible.decl [template = constants.%.1]
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT: %.loc23: <witness> = complete_type_witness %.5 [template = constants.%.6]
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: !members:
  96. // CHECK:STDOUT: .Self = constants.%A
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: generic fn @Make.1(@DefaultConstructible.%Self: %.1) {
  100. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic = %Self (constants.%Self)]
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: fn() -> @Make.1.%Self (%Self);
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: fn @Make.2() -> %return: %C {
  106. // CHECK:STDOUT: !entry:
  107. // CHECK:STDOUT: %.loc21_33.1: %.5 = struct_literal ()
  108. // CHECK:STDOUT: %.loc21_33.2: init %C = class_init (), %return [template = constants.%struct]
  109. // CHECK:STDOUT: %.loc21_34: init %C = converted %.loc21_33.1, %.loc21_33.2 [template = constants.%struct]
  110. // CHECK:STDOUT: return %.loc21_34 to %return
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: specific @Make.1(constants.%Self) {
  114. // CHECK:STDOUT: %Self => constants.%Self
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: specific @Make.1(constants.%C) {
  118. // CHECK:STDOUT: %Self => constants.%C
  119. // CHECK:STDOUT: }
  120. // CHECK:STDOUT: