self_in_class.carbon 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 @DefaultConstructible, %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: %A: type = class_type @A [template]
  34. // CHECK:STDOUT: %Make.type.2: type = fn_type @Make.2 [template]
  35. // CHECK:STDOUT: %Make.2: %Make.type.2 = struct_value () [template]
  36. // CHECK:STDOUT: %.6: <witness> = interface_witness (%Make.2) [template]
  37. // CHECK:STDOUT: %.7: type = ptr_type %.5 [template]
  38. // CHECK:STDOUT: %struct: %C = struct_value () [template]
  39. // CHECK:STDOUT: }
  40. // CHECK:STDOUT:
  41. // CHECK:STDOUT: file {
  42. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  43. // CHECK:STDOUT: .DefaultConstructible = %DefaultConstructible.decl
  44. // CHECK:STDOUT: .C = %C.decl
  45. // CHECK:STDOUT: .A = %A.decl
  46. // CHECK:STDOUT: }
  47. // CHECK:STDOUT: %DefaultConstructible.decl: type = interface_decl @DefaultConstructible [template = constants.%.1] {}
  48. // CHECK:STDOUT: %C.decl: type = class_decl @C [template = constants.%C] {}
  49. // CHECK:STDOUT: %A.decl: type = class_decl @A [template = constants.%A] {}
  50. // CHECK:STDOUT: }
  51. // CHECK:STDOUT:
  52. // CHECK:STDOUT: interface @DefaultConstructible {
  53. // CHECK:STDOUT: %Self: %.1 = bind_symbolic_name Self 0 [symbolic = constants.%Self]
  54. // CHECK:STDOUT: %Make.decl: %Make.type.1 = fn_decl @Make.1 [template = constants.%Make.1] {
  55. // CHECK:STDOUT: %Self.ref: %.1 = name_ref Self, %Self [symbolic = constants.%Self]
  56. // CHECK:STDOUT: %.loc12_16.1: type = facet_type_access %Self.ref [symbolic = constants.%Self]
  57. // CHECK:STDOUT: %.loc12_16.2: type = converted %Self.ref, %.loc12_16.1 [symbolic = constants.%Self]
  58. // CHECK:STDOUT: %return.var: ref %Self = var <return slot>
  59. // CHECK:STDOUT: }
  60. // CHECK:STDOUT: %.loc12_20: %.3 = assoc_entity element0, %Make.decl [template = constants.%.4]
  61. // CHECK:STDOUT:
  62. // CHECK:STDOUT: !members:
  63. // CHECK:STDOUT: .Self = %Self
  64. // CHECK:STDOUT: .Make = %.loc12_20
  65. // CHECK:STDOUT: witness = (%Make.decl)
  66. // CHECK:STDOUT: }
  67. // CHECK:STDOUT:
  68. // CHECK:STDOUT: impl @impl: %C as %.1 {
  69. // CHECK:STDOUT: %Make.decl: %Make.type.2 = fn_decl @Make.2 [template = constants.%Make.2] {
  70. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [template = constants.%C]
  71. // CHECK:STDOUT: %return.var: ref %C = var <return slot>
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT: %.1: <witness> = interface_witness (%Make.decl) [template = constants.%.6]
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: !members:
  76. // CHECK:STDOUT: .Make = %Make.decl
  77. // CHECK:STDOUT: witness = %.1
  78. // CHECK:STDOUT: }
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: class @C {
  81. // CHECK:STDOUT: !members:
  82. // CHECK:STDOUT: .Self = constants.%C
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT:
  85. // CHECK:STDOUT: class @A {
  86. // CHECK:STDOUT: impl_decl @impl {
  87. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [template = constants.%C]
  88. // CHECK:STDOUT: %DefaultConstructible.ref: type = name_ref DefaultConstructible, file.%DefaultConstructible.decl [template = constants.%.1]
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT:
  91. // CHECK:STDOUT: !members:
  92. // CHECK:STDOUT: .Self = constants.%A
  93. // CHECK:STDOUT: }
  94. // CHECK:STDOUT:
  95. // CHECK:STDOUT: fn @Make.1() -> %Self
  96. // CHECK:STDOUT: generic [@DefaultConstructible.%Self: %.1];
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: fn @Make.2() -> @impl.%return.var: %C {
  99. // CHECK:STDOUT: !entry:
  100. // CHECK:STDOUT: %.loc21_33.1: %.5 = struct_literal ()
  101. // CHECK:STDOUT: %.loc21_33.2: init %C = class_init (), @impl.%return.var [template = constants.%struct]
  102. // CHECK:STDOUT: %.loc21_34: init %C = converted %.loc21_33.1, %.loc21_33.2 [template = constants.%struct]
  103. // CHECK:STDOUT: return %.loc21_34 to @impl.%return.var
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT: