extend_impl.carbon 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/impl/extend_impl.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/extend_impl.carbon
  12. // --- extend_impl.carbon
  13. library "[[@TEST_NAME]]";
  14. interface HasF {
  15. fn F();
  16. }
  17. //@dump-sem-ir-begin
  18. class C {
  19. extend impl as HasF {
  20. fn F() {}
  21. }
  22. }
  23. //@dump-sem-ir-end
  24. fn G(c: C) {
  25. C.F();
  26. c.F();
  27. }
  28. // --- fail_extend_impl_nonexistent.carbon
  29. library "[[@TEST_NAME]]";
  30. interface I {}
  31. class C {
  32. // CHECK:STDERR: fail_extend_impl_nonexistent.carbon:[[@LINE+4]]:15: error: name `nonexistent` not found [NameNotFound]
  33. // CHECK:STDERR: extend impl nonexistent as I {}
  34. // CHECK:STDERR: ^~~~~~~~~~~
  35. // CHECK:STDERR:
  36. extend impl nonexistent as I {}
  37. fn F() {
  38. // The erroneous self-type for an `extend` causes the error to be propagated
  39. // into the class scope, which prevents errors if we fail to find a name
  40. // in that scope.
  41. Self.A;
  42. }
  43. }
  44. // --- fail_impl_nonexistent.carbon
  45. library "[[@TEST_NAME]]";
  46. interface I {}
  47. class C {
  48. // CHECK:STDERR: fail_impl_nonexistent.carbon:[[@LINE+4]]:8: error: name `nonexistent` not found [NameNotFound]
  49. // CHECK:STDERR: impl nonexistent as I {}
  50. // CHECK:STDERR: ^~~~~~~~~~~
  51. // CHECK:STDERR:
  52. impl nonexistent as I {}
  53. fn F() {
  54. // The name lookup error still happens, since the `require` is not `extend`.
  55. // CHECK:STDERR: fail_impl_nonexistent.carbon:[[@LINE+4]]:5: error: member name `A` not found in `C` [MemberNameNotFoundInInstScope]
  56. // CHECK:STDERR: Self.A;
  57. // CHECK:STDERR: ^~~~~~
  58. // CHECK:STDERR:
  59. Self.A;
  60. }
  61. }
  62. // --- fail_extend_impl_nonexistent_pointer.carbon
  63. library "[[@TEST_NAME]]";
  64. interface I {}
  65. class C {
  66. // CHECK:STDERR: fail_extend_impl_nonexistent_pointer.carbon:[[@LINE+4]]:15: error: name `nonexistent` not found [NameNotFound]
  67. // CHECK:STDERR: extend impl nonexistent* as I {}
  68. // CHECK:STDERR: ^~~~~~~~~~~
  69. // CHECK:STDERR:
  70. extend impl nonexistent* as I {}
  71. fn F() {
  72. // The erroneous self-type for an `extend` causes the error to be propagated
  73. // into the class scope, which prevents errors if we fail to find a name
  74. // in that scope.
  75. Self.A;
  76. }
  77. }
  78. // --- fail_extend_impl_nonexistent_outside_class.carbon
  79. library "[[@TEST_NAME]]";
  80. interface I {}
  81. // CHECK:STDERR: fail_extend_impl_nonexistent_outside_class.carbon:[[@LINE+4]]:13: error: name `nonexistent` not found [NameNotFound]
  82. // CHECK:STDERR: extend impl nonexistent* as I {}
  83. // CHECK:STDERR: ^~~~~~~~~~~
  84. // CHECK:STDERR:
  85. extend impl nonexistent* as I {}
  86. // CHECK:STDOUT: --- extend_impl.carbon
  87. // CHECK:STDOUT:
  88. // CHECK:STDOUT: constants {
  89. // CHECK:STDOUT: %HasF.type: type = facet_type <@HasF> [concrete]
  90. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  91. // CHECK:STDOUT: %HasF.impl_witness: <witness> = impl_witness @C.%HasF.impl_witness_table [concrete]
  92. // CHECK:STDOUT: %C.as.HasF.impl.F.type: type = fn_type @C.as.HasF.impl.F [concrete]
  93. // CHECK:STDOUT: %C.as.HasF.impl.F: %C.as.HasF.impl.F.type = struct_value () [concrete]
  94. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  95. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT:
  98. // CHECK:STDOUT: file {
  99. // CHECK:STDOUT: %C.decl: type = class_decl @C [concrete = constants.%C] {} {}
  100. // CHECK:STDOUT: }
  101. // CHECK:STDOUT:
  102. // CHECK:STDOUT: impl @C.as.HasF.impl: %Self.ref as %HasF.ref {
  103. // CHECK:STDOUT: %C.as.HasF.impl.F.decl: %C.as.HasF.impl.F.type = fn_decl @C.as.HasF.impl.F [concrete = constants.%C.as.HasF.impl.F] {} {}
  104. // CHECK:STDOUT:
  105. // CHECK:STDOUT: !members:
  106. // CHECK:STDOUT: .F = %C.as.HasF.impl.F.decl
  107. // CHECK:STDOUT: witness = @C.%HasF.impl_witness
  108. // CHECK:STDOUT: }
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: class @C {
  111. // CHECK:STDOUT: impl_decl @C.as.HasF.impl [concrete] {} {
  112. // CHECK:STDOUT: %Self.ref: type = name_ref Self, constants.%C [concrete = constants.%C]
  113. // CHECK:STDOUT: %HasF.ref: type = name_ref HasF, file.%HasF.decl [concrete = constants.%HasF.type]
  114. // CHECK:STDOUT: }
  115. // CHECK:STDOUT: %HasF.impl_witness_table = impl_witness_table (@C.as.HasF.impl.%C.as.HasF.impl.F.decl), @C.as.HasF.impl [concrete]
  116. // CHECK:STDOUT: %HasF.impl_witness: <witness> = impl_witness %HasF.impl_witness_table [concrete = constants.%HasF.impl_witness]
  117. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness constants.%empty_struct_type [concrete = constants.%complete_type]
  118. // CHECK:STDOUT: complete_type_witness = %complete_type
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: !members:
  121. // CHECK:STDOUT: .Self = constants.%C
  122. // CHECK:STDOUT: .HasF = <poisoned>
  123. // CHECK:STDOUT: .F = <poisoned>
  124. // CHECK:STDOUT: extend @C.as.HasF.impl.%HasF.ref
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: fn @C.as.HasF.impl.F() {
  128. // CHECK:STDOUT: !entry:
  129. // CHECK:STDOUT: return
  130. // CHECK:STDOUT: }
  131. // CHECK:STDOUT: