partially_identified.carbon 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/partially_identified.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/facet/partially_identified.carbon
  12. // --- fail_self_doesnt_meet_constraint.carbon
  13. library "[[@TEST_NAME]]";
  14. interface Y {}
  15. interface NeedsY(T:! Y) {}
  16. constraint W {
  17. // CHECK:STDERR: fail_self_doesnt_meet_constraint.carbon:[[@LINE+7]]:24: error: cannot convert type `Self` that implements `W` into type implementing `Y` [ConversionFailureFacetToFacet]
  18. // CHECK:STDERR: extend require impls NeedsY(Self);
  19. // CHECK:STDERR: ^~~~~~~~~~~~
  20. // CHECK:STDERR: fail_self_doesnt_meet_constraint.carbon:[[@LINE-5]]:18: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  21. // CHECK:STDERR: interface NeedsY(T:! Y) {}
  22. // CHECK:STDERR: ^~~~~
  23. // CHECK:STDERR:
  24. extend require impls NeedsY(Self);
  25. }
  26. // --- self_meets_constraint.carbon
  27. library "[[@TEST_NAME]]";
  28. interface Y {}
  29. interface NeedsY(T:! Y) {}
  30. constraint W {
  31. require impls Y;
  32. extend require impls NeedsY(Self);
  33. }
  34. fn F(w:! W) {
  35. w as Y;
  36. w as NeedsY(w);
  37. }
  38. // --- self_meets_constraint_through_indirection.carbon
  39. library "[[@TEST_NAME]]";
  40. interface Y {}
  41. interface NeedsY(T:! Y) {}
  42. constraint GivesY {
  43. require impls Y;
  44. }
  45. constraint W {
  46. require impls GivesY;
  47. extend require impls NeedsY(Self);
  48. }
  49. fn F(w:! W) {
  50. w as Y;
  51. w as NeedsY(w);
  52. }
  53. // --- self_in_constraint_specific_interface.carbon
  54. library "[[@TEST_NAME]]";
  55. interface Y {}
  56. interface NeedsY(T:! Y) {}
  57. // All types impl Y. This requires an impl lookup to find a witness, and it will
  58. // be a symbolic witness for a symbolic self type.
  59. impl forall [T:! type] T as Y {}
  60. constraint W {
  61. // Constructs a FacetValue of `Self as Y`, containing the impl lookup
  62. // instruction as its witness, which will be part of the generic definition of
  63. // this require.
  64. require impls NeedsY(Self);
  65. }
  66. class C {}
  67. impl C as NeedsY(C) {}
  68. fn G(w:! W) {
  69. // The witness of NeedsY(Self) monomorphized by `w` must be the same as the
  70. // witness of `NeedsY(w)` constructed directly here in order for this to find
  71. // `NeedsY(w)` in the facet type `W`.
  72. w as NeedsY(w);
  73. }
  74. fn F() {
  75. // Further monomorphize `NeedsY(w)` to `NeedsY(C)`.
  76. G(C);
  77. }
  78. // --- fail_todo_facet_access_type_of_self_meets_constraint.carbon
  79. library "[[@TEST_NAME]]";
  80. class C(T:! type) {}
  81. interface Y {}
  82. interface NeedsY(T:! Y) {}
  83. constraint W {
  84. require C(Self) impls Y;
  85. // CHECK:STDERR: fail_todo_facet_access_type_of_self_meets_constraint.carbon:[[@LINE+7]]:17: error: cannot convert type `C(Self)` into type implementing `Y` [ConversionFailureTypeToFacet]
  86. // CHECK:STDERR: require impls NeedsY(C(Self));
  87. // CHECK:STDERR: ^~~~~~~~~~~~~~~
  88. // CHECK:STDERR: fail_todo_facet_access_type_of_self_meets_constraint.carbon:[[@LINE-6]]:18: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  89. // CHECK:STDERR: interface NeedsY(T:! Y) {}
  90. // CHECK:STDERR: ^~~~~
  91. // CHECK:STDERR:
  92. require impls NeedsY(C(Self));
  93. }
  94. fn F(w:! W) {
  95. // TODO: Lookups for `C(w) as <something>` need to look into the facet types
  96. // of any facet value in the type `C(w)`, so that it can find a witness from
  97. // the constraint `W`.
  98. // CHECK:STDERR: fail_todo_facet_access_type_of_self_meets_constraint.carbon:[[@LINE+4]]:3: error: cannot convert type `C(w)` into type implementing `Y` [ConversionFailureTypeToFacet]
  99. // CHECK:STDERR: C(w) as Y;
  100. // CHECK:STDERR: ^~~~~~~~~
  101. // CHECK:STDERR:
  102. C(w) as Y;
  103. // CHECK:STDERR: fail_todo_facet_access_type_of_self_meets_constraint.carbon:[[@LINE+7]]:11: error: cannot convert type `C(w)` into type implementing `Y` [ConversionFailureTypeToFacet]
  104. // CHECK:STDERR: C(w) as NeedsY(C(w));
  105. // CHECK:STDERR: ^~~~~~~~~~~~
  106. // CHECK:STDERR: fail_todo_facet_access_type_of_self_meets_constraint.carbon:[[@LINE-25]]:18: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  107. // CHECK:STDERR: interface NeedsY(T:! Y) {}
  108. // CHECK:STDERR: ^~~~~
  109. // CHECK:STDERR:
  110. C(w) as NeedsY(C(w));
  111. }
  112. // --- multiple_use_of_self.carbon
  113. library "[[@TEST_NAME]]";
  114. interface Y {}
  115. interface NeedsY(T:! Y) {}
  116. interface NeedsNeedsY(T:! Y, U:! NeedsY(T)) {}
  117. constraint W {
  118. require impls Y;
  119. // The identified facet type of Self is {Y}.
  120. extend require impls NeedsY(Self);
  121. // The identified facet type of Self is {Y, NeedsY}. Ensure we don't keep
  122. // seeing the previous identified facet type of Self.
  123. extend require impls NeedsNeedsY(Self, Self);
  124. }
  125. fn F(w:! W) {
  126. w as Y;
  127. w as NeedsY(w);
  128. }
  129. // --- access_through_previous_require.carbon
  130. library "[[@TEST_NAME]]";
  131. interface Z { let Z0:! type; }
  132. interface Y {}
  133. interface X {
  134. let X0:! type;
  135. fn XF() -> X0;
  136. }
  137. class P(T:! type) { adapt (); }
  138. constraint M {
  139. require impls Z where .Z0 = {};
  140. require impls Y;
  141. }
  142. constraint N {
  143. require impls M;
  144. // The facts from M travel back to here.
  145. require impls X where .X0 = P(Self.(Z.Z0));
  146. }
  147. fn F(T:! N) -> T.(X.X0) { return T.(X.XF)(); }
  148. fn G() -> P({}) {
  149. class C {}
  150. impl C as Z where .Z0 = {} {}
  151. impl C as Y {}
  152. impl C as X where .X0 = P({}) {
  153. fn XF() -> P({}) { return () as P({}); }
  154. }
  155. return F(C);
  156. }
  157. // --- fail_impl_lookup_target_partially_identified.carbon
  158. library "[[@TEST_NAME]]";
  159. interface Z {}
  160. interface X { let XZ:! Z; }
  161. constraint W {
  162. require impls Z;
  163. // Target of impl lookup can not be partially identified.
  164. // CHECK:STDERR: fail_impl_lookup_target_partially_identified.carbon:[[@LINE+7]]:32: error: facet type `Z & W` can not be identified [ImplLookupInUnidentifiedFacetType]
  165. // CHECK:STDERR: require impls X where .XZ = (Self as (W & Z));
  166. // CHECK:STDERR: ^~~~~~~~~~~~~~~
  167. // CHECK:STDERR: fail_impl_lookup_target_partially_identified.carbon:[[@LINE-6]]:1: note: constraint is currently being defined [NamedConstraintIncompleteWithinDefinition]
  168. // CHECK:STDERR: constraint W {
  169. // CHECK:STDERR: ^~~~~~~~~~~~~~
  170. // CHECK:STDERR:
  171. require impls X where .XZ = (Self as (W & Z));
  172. }
  173. // --- convert_from_forward_declaration.carbon
  174. library "[[@TEST_NAME]]";
  175. interface Z {}
  176. // The facet type `W` is partially identified as soon as it is declared.
  177. // Converting from a facet requires its facet type to be at least partially
  178. // identified, not fully identified.
  179. constraint W;
  180. fn F(T:! W & Z) {
  181. T as Z;
  182. }