require_satisified.carbon 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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/int.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(()) {}
  134. // --- impl_requires_itself_cycle.carbon
  135. library "[[@TEST_NAME]]";
  136. interface A { fn AA(); }
  137. interface B {
  138. require impls A;
  139. }
  140. interface C {
  141. require impls B;
  142. }
  143. impl forall [T:! B] T as A { fn AA() {} }
  144. // When we get to the definition we check that anything B requires is satisfied.
  145. // - The interface B requires A, so we must check T impls A.
  146. // - The impl for A requires T impls B.
  147. // - This impl is what provides T impls B.
  148. impl forall [T:! C] T as B {}
  149. fn F(T:! C) {
  150. // If things go wrong, we find the impl `T as B`, but inside its definition is
  151. // a lookup for `T as A`, which (through deducing `T`) includes a lookup for
  152. // `T as B`. This creates a cycle of evaluating `T as B` recursively.
  153. //
  154. // This was solved by doing the check for requirements of `B` outside the
  155. // definition of `T as B`.
  156. // https://discord.com/channels/655572317891461132/941071822756143115/1463189087598022861
  157. T.(A.AA)();
  158. }
  159. // --- fail_impl_requires_itself_cycle_with_monomorphization_error.carbon
  160. library "[[@TEST_NAME]]";
  161. class W(T:! type) {
  162. adapt {};
  163. }
  164. interface A(N:! Core.IntLiteral()) {
  165. fn AA() -> W(array((), N));
  166. }
  167. interface B(N:! Core.IntLiteral()) {
  168. require impls A(N);
  169. }
  170. interface C(N:! Core.IntLiteral()) {
  171. require impls B(N);
  172. }
  173. impl forall [N:! Core.IntLiteral(), T:! B(N)] T as A(N) {
  174. fn AA() -> W(array((), N)) { return {} as W(array((), N)); }
  175. }
  176. impl forall [N:! Core.IntLiteral(), T:! C(N)] T as B(N) {
  177. // The definition here does not contain the lookups verifying that C(N) impls
  178. // `A(N)`, so they do not get re-evaluated for a specific `N`. That doesn't
  179. // prevent us from producing a reasonable diagnostic when that `N` causes an
  180. // error in the specific use of this impl, which we use to get from `C(N)` to
  181. // `A(N)` (in the deduction of `T` in the impl as `A(N)`). The error just
  182. // happens where we use `N` in `A(N)` instead of inside the verification that
  183. // `T as B(N)` implies `T as A(N)`.
  184. }
  185. fn F(T:! C(-1)) {
  186. // CHECK:STDERR: fail_impl_requires_itself_cycle_with_monomorphization_error.carbon:[[@LINE+7]]:3: error: unable to monomorphize specific `AA()` [ResolvingSpecificHere]
  187. // CHECK:STDERR: T.(A(-1).AA)();
  188. // CHECK:STDERR: ^~~~~~~~~~~~~~
  189. // CHECK:STDERR: fail_impl_requires_itself_cycle_with_monomorphization_error.carbon:[[@LINE-27]]:26: note: array bound of -1 is negative [ArrayBoundNegative]
  190. // CHECK:STDERR: fn AA() -> W(array((), N));
  191. // CHECK:STDERR: ^
  192. // CHECK:STDERR:
  193. T.(A(-1).AA)();
  194. }