validate_impl_constraints.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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/validate_impl_constraints.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/facet/validate_impl_constraints.carbon
  12. // --- self_impls_modifies_assoc_constant.carbon
  13. library "[[@TEST_NAME]]";
  14. interface I { let X:! type; }
  15. fn F(unused T:! I where .X = ()) {}
  16. fn G(T:! I where .Self impls (I where .X = ())) {
  17. F(T);
  18. }
  19. // --- fail_self_impls_modifies_assoc_constant_type_differs.carbon
  20. library "[[@TEST_NAME]]";
  21. interface I { let X:! type; }
  22. fn F(unused T:! I where .X = ()) {}
  23. fn G(T:! I where .Self impls (I where .X = {})) {
  24. // CHECK:STDERR: fail_self_impls_modifies_assoc_constant_type_differs.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I where .(I.X) = {}` into type implementing `I where .(I.X) = ()` [ConversionFailureFacetToFacet]
  25. // CHECK:STDERR: F(T);
  26. // CHECK:STDERR: ^~~~
  27. // CHECK:STDERR: fail_self_impls_modifies_assoc_constant_type_differs.carbon:[[@LINE-6]]:13: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  28. // CHECK:STDERR: fn F(unused T:! I where .X = ()) {}
  29. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
  30. // CHECK:STDERR:
  31. F(T);
  32. }
  33. // --- fail_where_impls_tests_associated_constant_of_generic_type_non_final_impl.carbon
  34. library "[[@TEST_NAME]]";
  35. class C(U:! type) {}
  36. // C(U) impls M if U impls L.
  37. interface L {}
  38. interface M { let M0:! type; }
  39. impl forall [U:! L] C(U) as M where .M0 = {} {}
  40. // U requires that C(.Self) impls M.
  41. // - C(.Self) impls M can be rewritten as C(U) impls M.
  42. // - C(U) impls M if U impls L => Requires U impls L.
  43. fn F(unused U:! type where C(.Self) impls (M where .M0 = {})) {}
  44. fn G(T:! L) {
  45. // We have no final impl for `C(T) as M`, so we don't know the value of
  46. // `(C(T) as M).M0` concretely, so we don't know that it is `{}` and we can't
  47. // convert assuming it is.
  48. //
  49. // CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_type_non_final_impl.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `L` into type implementing `type where C(.Self) impls M and C(.Self).(M.M0) = {}` [ConversionFailureFacetToFacet]
  50. // CHECK:STDERR: F(T);
  51. // CHECK:STDERR: ^~~~
  52. // CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_type_non_final_impl.carbon:[[@LINE-10]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  53. // CHECK:STDERR: fn F(unused U:! type where C(.Self) impls (M where .M0 = {})) {}
  54. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  55. // CHECK:STDERR:
  56. F(T);
  57. }
  58. // --- where_impls_tests_associated_constant_of_generic_type.carbon
  59. library "[[@TEST_NAME]]";
  60. class C(U:! type) {}
  61. // C(U) impls M if U impls L.
  62. interface L {}
  63. interface M { let M0:! type; }
  64. final impl forall [U:! L] C(U) as M where .M0 = {} {}
  65. // U requires that C(.Self) impls M.
  66. // - C(.Self) impls M can be rewritten as C(U) impls M.
  67. // - C(U) impls M if U impls L => Requires U impls L.
  68. fn F(unused U:! type where C(.Self) impls (M where .M0 = {})) {}
  69. fn G(T:! L) {
  70. F(T);
  71. }
  72. // --- fail_where_impls_tests_associated_constant_of_generic_type_type_differs.carbon
  73. library "[[@TEST_NAME]]";
  74. class C(U:! type) {}
  75. // C(U) impls M if U impls L.
  76. interface L {}
  77. interface M { let M0:! type; }
  78. final impl forall [U:! L] C(U) as M where .M0 = () {}
  79. // U requires that C(.Self) impls M.
  80. // - C(.Self) impls M can be rewritten as C(U) impls M.
  81. // - C(U) impls M if U impls L => Requires U impls L.
  82. fn F(unused U:! type where C(.Self) impls (M where .M0 = {})) {}
  83. fn G(T:! L) {
  84. // CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_type_type_differs.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `L` into type implementing `type where C(.Self) impls M and C(.Self).(M.M0) = {}` [ConversionFailureFacetToFacet]
  85. // CHECK:STDERR: F(T);
  86. // CHECK:STDERR: ^~~~
  87. // CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_type_type_differs.carbon:[[@LINE-6]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  88. // CHECK:STDERR: fn F(unused U:! type where C(.Self) impls (M where .M0 = {})) {}
  89. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  90. // CHECK:STDERR:
  91. F(T);
  92. }
  93. // --- fail_where_impls_tests_associated_constant_of_generic_interface_non_final_impl.carbon
  94. library "[[@TEST_NAME]]";
  95. class C {}
  96. // C impls M(U) if U impls L.
  97. interface L {}
  98. interface M(U:! type) { let M0:! type; }
  99. impl forall [U:! L] C as M(U) where .M0 = {} {}
  100. // U requires that C impls M(.Self).
  101. // - C impls M(.Self) can be rewritten as C impls M(U).
  102. // - C impls M(U) if U impls L => Requires U impls L.
  103. fn F(unused U:! type where C impls (M(.Self) where .M0 = {})) {}
  104. fn G(T:! L) {
  105. // We have no final impl for `C as M(T)`, so we don't know the value of
  106. // `(C as M(T)).M0` concretely, so we don't know that it is `{}` and we can't
  107. // convert assuming it is.
  108. //
  109. // CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_interface_non_final_impl.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `L` into type implementing `type where C impls M(.Self) and C.(M(.Self).M0) = {}` [ConversionFailureFacetToFacet]
  110. // CHECK:STDERR: F(T);
  111. // CHECK:STDERR: ^~~~
  112. // CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_interface_non_final_impl.carbon:[[@LINE-10]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  113. // CHECK:STDERR: fn F(unused U:! type where C impls (M(.Self) where .M0 = {})) {}
  114. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  115. // CHECK:STDERR:
  116. F(T);
  117. }
  118. // --- where_impls_tests_associated_constant_of_generic_interface.carbon
  119. library "[[@TEST_NAME]]";
  120. class C {}
  121. // C impls M(U) if U impls L.
  122. interface L {}
  123. interface M(U:! type) { let M0:! type; }
  124. final impl forall [U:! L] C as M(U) where .M0 = {} {}
  125. // U requires that C impls M(.Self).
  126. // - C impls M(.Self) can be rewritten as C impls M(U).
  127. // - C impls M(U) if U impls L => Requires U impls L.
  128. fn F(unused U:! type where C impls (M(.Self) where .M0 = {})) {}
  129. fn G(T:! L) {
  130. F(T);
  131. }
  132. // --- fail_where_impls_tests_associated_constant_of_generic_interface_type_differs.carbon
  133. library "[[@TEST_NAME]]";
  134. class C {}
  135. // C impls M(U) if U impls L.
  136. interface L {}
  137. interface M(U:! type) { let M0:! type; }
  138. final impl forall [U:! L] C as M(U) where .M0 = () {}
  139. // U requires that C impls M(.Self).
  140. // - C impls M(.Self) can be rewritten as C impls M(U).
  141. // - C impls M(U) if U impls L => Requires U impls L.
  142. fn F(unused U:! type where C impls (M(.Self) where .M0 = {})) {}
  143. fn G(T:! L) {
  144. // CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_interface_type_differs.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `L` into type implementing `type where C impls M(.Self) and C.(M(.Self).M0) = {}` [ConversionFailureFacetToFacet]
  145. // CHECK:STDERR: F(T);
  146. // CHECK:STDERR: ^~~~
  147. // CHECK:STDERR: fail_where_impls_tests_associated_constant_of_generic_interface_type_differs.carbon:[[@LINE-6]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  148. // CHECK:STDERR: fn F(unused U:! type where C impls (M(.Self) where .M0 = {})) {}
  149. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  150. // CHECK:STDERR:
  151. F(T);
  152. }
  153. // --- self_in_interface_generic_param_unconstrained.carbon
  154. library "[[@TEST_NAME]]";
  155. interface Z {}
  156. interface I(T:! type) {}
  157. fn F(unused T:! I(.Self) where .Self impls Z) {}
  158. fn G(T:! Z & I(.Self)) {
  159. F(T);
  160. }
  161. // --- fail_todo_self_in_interface_generic_param_constrained.carbon
  162. library "[[@TEST_NAME]]";
  163. interface Z {}
  164. interface I(T:! Z) {}
  165. // TODO: I(.Self) introduces an implied constraint `.Self impls Z`, which is
  166. // satisfied and checked at the end of the fn signature.
  167. //
  168. // CHECK:STDERR: fail_todo_self_in_interface_generic_param_constrained.carbon:[[@LINE+7]]:17: error: cannot convert type `.Self` that implements `type` into type implementing `Z` [ConversionFailureFacetToFacet]
  169. // CHECK:STDERR: fn F(unused T:! I(.Self) where .Self impls Z) {}
  170. // CHECK:STDERR: ^~~~~~~~
  171. // CHECK:STDERR: fail_todo_self_in_interface_generic_param_constrained.carbon:[[@LINE-8]]:13: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  172. // CHECK:STDERR: interface I(T:! Z) {}
  173. // CHECK:STDERR: ^~~~~
  174. // CHECK:STDERR:
  175. fn F(unused T:! I(.Self) where .Self impls Z) {}
  176. // CHECK:STDERR: fail_todo_self_in_interface_generic_param_constrained.carbon:[[@LINE+7]]:14: error: cannot convert type `.Self` that implements `type` into type implementing `Z` [ConversionFailureFacetToFacet]
  177. // CHECK:STDERR: fn G(T:! Z & I(.Self)) {
  178. // CHECK:STDERR: ^~~~~~~~
  179. // CHECK:STDERR: fail_todo_self_in_interface_generic_param_constrained.carbon:[[@LINE-17]]:13: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  180. // CHECK:STDERR: interface I(T:! Z) {}
  181. // CHECK:STDERR: ^~~~~
  182. // CHECK:STDERR:
  183. fn G(T:! Z & I(.Self)) {
  184. F(T);
  185. }
  186. // --- where_period_self_rhs_sees_lhs.carbon
  187. library "[[@TEST_NAME]]";
  188. interface Z {}
  189. interface Y {}
  190. interface X(T:! Z & Y) {}
  191. constraint N {
  192. require impls Y;
  193. }
  194. // The RHS of `where` can see the extend constraints on the LHS of `where`.
  195. fn F(_:! Z & N where .Self impls X(.Self)) {}
  196. fn G() {
  197. class C;
  198. impl C as Z {}
  199. impl C as Y {}
  200. impl C as X(C) {}
  201. F(C);
  202. }
  203. // --- where_type_rhs_sees_lhs.carbon
  204. library "[[@TEST_NAME]]";
  205. interface Z {}
  206. interface Y {}
  207. interface X(T:! Z & Y) {}
  208. constraint N {
  209. require impls Y;
  210. }
  211. class R(T:! Z & Y);
  212. // The RHS of `where` can see the extend constraints on the LHS of `where`.
  213. fn F(_:! Z & N where R(.Self) impls X(.Self)) {}
  214. fn G() {
  215. class C;
  216. impl C as Z {}
  217. impl C as Y {}
  218. impl R(C) as X(C) {}
  219. F(C);
  220. }
  221. // --- fail_todo_where_period_self_rhs_impls_nested_period_self.carbon
  222. library "[[@TEST_NAME]]";
  223. interface Z {}
  224. interface Y {}
  225. interface X(T:! Z & Y) {}
  226. constraint N {
  227. require impls Y;
  228. }
  229. // TODO: The inner nested facet type (`N where...`) does not know about
  230. // constraints on the outer facet type (`Z where...`). But it should produce an
  231. // implied constraint that `.Self impls Z & Y` that is checked once the full
  232. // facet type is known.
  233. //
  234. // CHECK:STDERR: fail_todo_where_period_self_rhs_impls_nested_period_self.carbon:[[@LINE+7]]:51: error: cannot convert type `.Self` that implements `N` into type implementing `Z & Y` [ConversionFailureFacetToFacet]
  235. // CHECK:STDERR: fn F(_:! Z where .Self impls (N where .Self impls X(.Self))) {}
  236. // CHECK:STDERR: ^~~~~~~~
  237. // CHECK:STDERR: fail_todo_where_period_self_rhs_impls_nested_period_self.carbon:[[@LINE-14]]:13: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  238. // CHECK:STDERR: interface X(T:! Z & Y) {}
  239. // CHECK:STDERR: ^~~~~~~~~
  240. // CHECK:STDERR:
  241. fn F(_:! Z where .Self impls (N where .Self impls X(.Self))) {}
  242. fn G() {
  243. class C;
  244. impl C as Z {}
  245. impl C as Y {}
  246. impl C as X(C) {}
  247. F(C);
  248. }
  249. // --- associated_const_impls_interface_with_period_self.carbon
  250. library "[[@TEST_NAME]]";
  251. interface I {
  252. let I1:! type;
  253. }
  254. interface J(T:! I) {}
  255. // There is a .Self reference on the LHS and RHS of the `impls` constraint,
  256. // which must be replaced, each with `C as I`
  257. fn F(_:! I where .I1 impls J(.Self)) {}
  258. fn G() {
  259. class C;
  260. class D;
  261. impl C as I where .I1 = D {}
  262. impl D as J(C) {}
  263. F(C);
  264. }
  265. // --- fail_concrete_witness_without_impl.carbon
  266. library "[[@TEST_NAME]]";
  267. interface I {
  268. let I1:! type;
  269. }
  270. interface J {}
  271. fn F(_:! I where .I1 impls J) {}
  272. fn G() {
  273. class C;
  274. // This identifies `C as (I where .I1 impls J)`, which replaces `.Self.I1`
  275. // with `C.(I.I1)`. This is a concrete lookup since C and I are both concrete,
  276. // but it doesn't find anything as there is no impl.
  277. //
  278. // This tests that we correctly handle the case where the witness in an
  279. // ImplWitnessAccess would become concrete without being able to provide any
  280. // value, since it's still a `LookupImplWitness` type of witness.
  281. // CHECK:STDERR: fail_concrete_witness_without_impl.carbon:[[@LINE+7]]:3: error: cannot convert type `C` into type implementing `I where .(I.I1) impls J` [ConversionFailureTypeToFacet]
  282. // CHECK:STDERR: F(C);
  283. // CHECK:STDERR: ^~~~
  284. // CHECK:STDERR: fail_concrete_witness_without_impl.carbon:[[@LINE-16]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  285. // CHECK:STDERR: fn F(_:! I where .I1 impls J) {}
  286. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  287. // CHECK:STDERR:
  288. F(C);
  289. }
  290. // --- fail_todo_convert_to_period_self_preserves_self_facet_impls.carbon
  291. library "[[@TEST_NAME]]";
  292. interface K {}
  293. interface J {}
  294. interface I {
  295. let I1:! type;
  296. }
  297. fn F(unused U:! I & J where .I1 impls K) {}
  298. impl forall [T:! type] T as J {}
  299. fn G(T:! I where .I1 impls K) {
  300. // Replaces `.Self` with `T`, which converts `T` to `I & J`. Doing so has to
  301. // invent a witness for `J`, and this tests we do so without losing the
  302. // information that `.I1 impls K`.
  303. //
  304. // TODO: When converting T to the type of U, the identified facet type
  305. // includes `.Self.I1 impls K`, which is replaced with `T.I1 impls K`. The
  306. // identified facet type of `T` also contains `T.I1 impls K` so we should find
  307. // a witness for that.
  308. //
  309. // CHECK:STDERR: fail_todo_convert_to_period_self_preserves_self_facet_impls.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I where .(I.I1) impls K` into type implementing `J & I where .(I.I1) impls K` [ConversionFailureFacetToFacet]
  310. // CHECK:STDERR: F(T);
  311. // CHECK:STDERR: ^~~~
  312. // CHECK:STDERR: fail_todo_convert_to_period_self_preserves_self_facet_impls.carbon:[[@LINE-17]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  313. // CHECK:STDERR: fn F(unused U:! I & J where .I1 impls K) {}
  314. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  315. // CHECK:STDERR:
  316. F(T);
  317. }