require_satisified.carbon 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. //
  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/facet/require_satisified.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/facet/require_satisified.carbon
  12. // --- fail_missing_require_for_concrete_type.carbon
  13. library "[[@TEST_NAME]]";
  14. interface Z {}
  15. interface Y {
  16. extend require impls Z;
  17. }
  18. // () does not need to impl Z yet.
  19. impl () as Y;
  20. // () needs to impl Z at the start of the definition.
  21. // CHECK:STDERR: fail_missing_require_for_concrete_type.carbon:[[@LINE+4]]:1: error: interface `Y` being implemented requires that `()` implements `Z` [RequireImplsNotImplemented]
  22. // CHECK:STDERR: impl () as Y {}
  23. // CHECK:STDERR: ^~~~~~~~~~~~~~
  24. // CHECK:STDERR:
  25. impl () as Y {}
  26. // --- fail_missing_require_for_symbolic_type.carbon
  27. library "[[@TEST_NAME]]";
  28. interface Z {}
  29. interface Y {
  30. extend require impls Z;
  31. }
  32. // T does not need to impl Z yet.
  33. impl forall [T:! type] T as Y;
  34. // T needs to impl Z at the start of the definition.
  35. // CHECK:STDERR: fail_missing_require_for_symbolic_type.carbon:[[@LINE+4]]:1: error: interface `Y` being implemented requires that `T` implements `Z` [RequireImplsNotImplemented]
  36. // CHECK:STDERR: impl forall [T:! type] T as Y {}
  37. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. // CHECK:STDERR:
  39. impl forall [T:! type] T as Y {}
  40. // --- fail_missing_require_double.carbon
  41. library "[[@TEST_NAME]]";
  42. interface Z1(T:! type) {}
  43. interface Z2(T:! type) {}
  44. class A;
  45. class B;
  46. interface Y(U:! type) {
  47. extend require impls Z1(U) & Z2(B);
  48. }
  49. // CHECK:STDERR: fail_missing_require_double.carbon:[[@LINE+4]]:1: error: interface `Y(A)` being implemented requires that `()` implements `Z1(A) & Z2(B)` [RequireImplsNotImplemented]
  50. // CHECK:STDERR: impl () as Y(A) {}
  51. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~
  52. // CHECK:STDERR:
  53. impl () as Y(A) {}
  54. // --- require_for_concrete_type.carbon
  55. library "[[@TEST_NAME]]";
  56. interface Z {}
  57. interface Y {
  58. extend require impls Z;
  59. }
  60. // () does not need to impl Z yet.
  61. impl () as Y;
  62. // Now we know () impls Z.
  63. impl () as Z;
  64. // So no error here.
  65. impl () as Y {}
  66. impl () as Z {}
  67. // --- require_for_symbolic_type.carbon
  68. library "[[@TEST_NAME]]";
  69. interface Z {}
  70. interface Y {
  71. extend require impls Z;
  72. }
  73. // T does not need to impl Z yet.
  74. impl forall [T:! type] T as Y;
  75. // Now we know T impls Z.
  76. impl forall [T:! type] T as Z;
  77. // So no error here.
  78. impl forall [T:! type] T as Y {}
  79. impl forall [T:! type] T as Z {}
  80. // --- require_for_symbolic_type_impl_matches_same_interface.carbon
  81. library "[[@TEST_NAME]]";
  82. interface Z {}
  83. interface Y {
  84. extend require impls Z;
  85. }
  86. // This only matches T that impl Z so no error here.
  87. impl forall [T:! Z] T as Y {}
  88. // --- require_for_generic_interfaces.carbon
  89. library "[[@TEST_NAME]]";
  90. interface Z1(T:! type) {}
  91. interface Z2(T:! type) {}
  92. class A;
  93. class B;
  94. interface Y(U:! type) {
  95. extend require impls Z1(U) & Z2(B);
  96. }
  97. // () does not need to impl Z1(A) and Z2(B) yet.
  98. impl () as Y(A);
  99. // Now we know () impls Z1(A) and Z2(B).
  100. impl () as Z1(A);
  101. impl () as Z2(B);
  102. // So no error here.
  103. impl () as Y(A) {}
  104. impl () as Z1(A) {}
  105. impl () as Z2(B) {}
  106. // --- fail_require_non_self_missing.carbon
  107. library "[[@TEST_NAME]]";
  108. interface Z(T:! type) {}
  109. class C;
  110. interface Y(U:! type) {
  111. // `Self` must appear somewhere in the require decl, so it's in the interface
  112. // specific because we want to test a different self-type.
  113. require C impls Z(Self);
  114. }
  115. // Y requires that C impls Z(Self), but it does not do so.
  116. // CHECK:STDERR: fail_require_non_self_missing.carbon:[[@LINE+4]]:1: error: interface `Y(())` being implemented requires that `C` implements `Z(())` [RequireImplsNotImplemented]
  117. // CHECK:STDERR: impl () as Y(()) {}
  118. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
  119. // CHECK:STDERR:
  120. impl () as Y(()) {}
  121. // --- require_non_self.carbon
  122. library "[[@TEST_NAME]]";
  123. interface Z(T:! type) {}
  124. class C;
  125. interface Y(U:! type) {
  126. // `Self` must appear somewhere in the require decl, so it's in the interface
  127. // specific because we want to test a different self-type.
  128. require C impls Z(Self);
  129. }
  130. impl C as Z(());
  131. // Y requires that C impls Z(Self), which is done above.
  132. impl () as Y(Self) {}
  133. impl C as Z(()) {}