early_rewrites.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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/early_rewrites.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/facet/early_rewrites.carbon
  12. // Tests where `ImplWitnessAccess` on the RHS of a rewrite constraint is used in
  13. // other constraints within the same facet type in ways that require the RHS to
  14. // be evaluated to a concrete value before the facet type is resolved (such as
  15. // by passing it as a parameter to a generic class). This can be done either by
  16. // eagerly reading rewrite constraints from the same facet type, or by relying
  17. // on implied constraints which are checked later.
  18. // --- fail_todo_implied_constraint_on_self.carbon
  19. library "[[@TEST_NAME]]";
  20. interface Z {
  21. let Z1:! type;
  22. let Z2:! type;
  23. }
  24. class C(T:! Z where .Z1 = ()) {}
  25. interface J {
  26. // Implied: .Z1 == ()
  27. // CHECK:STDERR: fail_todo_implied_constraint_on_self.carbon:[[@LINE+7]]:26: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
  28. // CHECK:STDERR: let J1:! Z where .Z2 = C(.Self);
  29. // CHECK:STDERR: ^~~~~~~~
  30. // CHECK:STDERR: fail_todo_implied_constraint_on_self.carbon:[[@LINE-7]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  31. // CHECK:STDERR: class C(T:! Z where .Z1 = ()) {}
  32. // CHECK:STDERR: ^
  33. // CHECK:STDERR:
  34. let J1:! Z where .Z2 = C(.Self);
  35. }
  36. fn F(T:! J) {}
  37. class D;
  38. // Satisfies the implied constraint in J.J1.
  39. // CHECK:STDERR: fail_todo_implied_constraint_on_self.carbon:[[@LINE+7]]:38: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
  40. // CHECK:STDERR: impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
  41. // CHECK:STDERR: ^~~~~~~~
  42. // CHECK:STDERR: fail_todo_implied_constraint_on_self.carbon:[[@LINE-21]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  43. // CHECK:STDERR: class C(T:! Z where .Z1 = ()) {}
  44. // CHECK:STDERR: ^
  45. // CHECK:STDERR:
  46. impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
  47. fn G(T:! J where .J1 = D) {
  48. F(T);
  49. }
  50. // --- fail_todo_implied_constraint_on_associated_constant.carbon
  51. library "[[@TEST_NAME]]";
  52. interface Z {
  53. let Z1:! type;
  54. let Z2:! type;
  55. }
  56. interface Tuple {}
  57. impl () as Tuple {}
  58. class C(T:! Tuple) {}
  59. interface J {
  60. // Implied: .Z1 impls Tuple
  61. // CHECK:STDERR: fail_todo_implied_constraint_on_associated_constant.carbon:[[@LINE+7]]:26: error: cannot convert type `.(Z.Z1)` into type implementing `Tuple` [ConversionFailureTypeToFacet]
  62. // CHECK:STDERR: let J1:! Z where .Z2 = C(.Z1);
  63. // CHECK:STDERR: ^~~~~~
  64. // CHECK:STDERR: fail_todo_implied_constraint_on_associated_constant.carbon:[[@LINE-7]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  65. // CHECK:STDERR: class C(T:! Tuple) {}
  66. // CHECK:STDERR: ^
  67. // CHECK:STDERR:
  68. let J1:! Z where .Z2 = C(.Z1);
  69. }
  70. fn F(T:! J) {}
  71. class D;
  72. // Satisfies the implied constraint in J.J1.
  73. impl D as Z where .Z1 = () and .Z2 = C(()) {}
  74. fn G(T:! J where .J1 = D) {
  75. F(T);
  76. }
  77. // --- fail_todo_nested_constraint_visible_in_type_parameter.carbon
  78. library "[[@TEST_NAME]]";
  79. interface Z {
  80. let Z1:! type;
  81. let Z2:! type;
  82. }
  83. class C(T:! Z where .Z1 = ()) {}
  84. interface J {
  85. // Implied: .Z1 == (), but this is also explicitly required for J1.
  86. // CHECK:STDERR: fail_todo_nested_constraint_visible_in_type_parameter.carbon:[[@LINE+7]]:43: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
  87. // CHECK:STDERR: let J1:! (Z where .Z1 = ()) where .Z2 = C(.Self);
  88. // CHECK:STDERR: ^~~~~~~~
  89. // CHECK:STDERR: fail_todo_nested_constraint_visible_in_type_parameter.carbon:[[@LINE-7]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  90. // CHECK:STDERR: class C(T:! Z where .Z1 = ()) {}
  91. // CHECK:STDERR: ^
  92. // CHECK:STDERR:
  93. let J1:! (Z where .Z1 = ()) where .Z2 = C(.Self);
  94. }
  95. fn F(T:! J) {}
  96. class D;
  97. // Satisfies the implied constraint in J.J1.
  98. // CHECK:STDERR: fail_todo_nested_constraint_visible_in_type_parameter.carbon:[[@LINE+7]]:38: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
  99. // CHECK:STDERR: impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
  100. // CHECK:STDERR: ^~~~~~~~
  101. // CHECK:STDERR: fail_todo_nested_constraint_visible_in_type_parameter.carbon:[[@LINE-21]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  102. // CHECK:STDERR: class C(T:! Z where .Z1 = ()) {}
  103. // CHECK:STDERR: ^
  104. // CHECK:STDERR:
  105. impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
  106. fn G(T:! J where .J1 = D) {
  107. F(T);
  108. }
  109. // --- fail_todo_outer_nested_constraint_visible_in_type_parameter.carbon
  110. library "[[@TEST_NAME]]";
  111. interface Z {
  112. let Z1:! type;
  113. let Z2:! type;
  114. }
  115. class C(T:! Z where .Z1 = ()) {}
  116. interface J {
  117. // Implied: .Z1 == (), but this is also explicitly required for J1.
  118. // CHECK:STDERR: fail_todo_outer_nested_constraint_visible_in_type_parameter.carbon:[[@LINE+7]]:27: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
  119. // CHECK:STDERR: let J1:! (Z where .Z2 = C(.Self)) where .Z1 = ();
  120. // CHECK:STDERR: ^~~~~~~~
  121. // CHECK:STDERR: fail_todo_outer_nested_constraint_visible_in_type_parameter.carbon:[[@LINE-7]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  122. // CHECK:STDERR: class C(T:! Z where .Z1 = ()) {}
  123. // CHECK:STDERR: ^
  124. // CHECK:STDERR:
  125. let J1:! (Z where .Z2 = C(.Self)) where .Z1 = ();
  126. }
  127. fn F(T:! J) {}
  128. class D;
  129. // Satisfies the implied constraint in J.J1.
  130. // CHECK:STDERR: fail_todo_outer_nested_constraint_visible_in_type_parameter.carbon:[[@LINE+7]]:38: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
  131. // CHECK:STDERR: impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
  132. // CHECK:STDERR: ^~~~~~~~
  133. // CHECK:STDERR: fail_todo_outer_nested_constraint_visible_in_type_parameter.carbon:[[@LINE-21]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  134. // CHECK:STDERR: class C(T:! Z where .Z1 = ()) {}
  135. // CHECK:STDERR: ^
  136. // CHECK:STDERR:
  137. impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
  138. fn G(T:! J where .J1 = D) {
  139. F(T);
  140. }
  141. // --- fail_todo_earlier_constraint_visible_in_type_parameter.carbon
  142. library "[[@TEST_NAME]]";
  143. interface Z {
  144. let Z1:! type;
  145. let Z2:! type;
  146. }
  147. class C(T:! Z where .Z1 = ()) {}
  148. interface J {
  149. // Implied: .Z1 == (), but this is also explicitly required for J1.
  150. // CHECK:STDERR: fail_todo_earlier_constraint_visible_in_type_parameter.carbon:[[@LINE+7]]:39: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
  151. // CHECK:STDERR: let J1:! Z where .Z1 = () and .Z2 = C(.Self);
  152. // CHECK:STDERR: ^~~~~~~~
  153. // CHECK:STDERR: fail_todo_earlier_constraint_visible_in_type_parameter.carbon:[[@LINE-7]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  154. // CHECK:STDERR: class C(T:! Z where .Z1 = ()) {}
  155. // CHECK:STDERR: ^
  156. // CHECK:STDERR:
  157. let J1:! Z where .Z1 = () and .Z2 = C(.Self);
  158. }
  159. fn F(T:! J) {}
  160. class D;
  161. // Satisfies the implied constraint in J.J1.
  162. // CHECK:STDERR: fail_todo_earlier_constraint_visible_in_type_parameter.carbon:[[@LINE+7]]:38: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
  163. // CHECK:STDERR: impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
  164. // CHECK:STDERR: ^~~~~~~~
  165. // CHECK:STDERR: fail_todo_earlier_constraint_visible_in_type_parameter.carbon:[[@LINE-21]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  166. // CHECK:STDERR: class C(T:! Z where .Z1 = ()) {}
  167. // CHECK:STDERR: ^
  168. // CHECK:STDERR:
  169. impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
  170. fn G(T:! J where .J1 = D) {
  171. F(T);
  172. }
  173. // --- fail_todo_later_constraint_visible_in_type_parameter.carbon
  174. library "[[@TEST_NAME]]";
  175. interface Z {
  176. let Z1:! type;
  177. let Z2:! type;
  178. }
  179. class C(T:! Z where .Z1 = ()) {}
  180. interface J {
  181. // Implied: .Z1 == (), but this is also explicitly required for J1.
  182. // CHECK:STDERR: fail_todo_later_constraint_visible_in_type_parameter.carbon:[[@LINE+7]]:26: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
  183. // CHECK:STDERR: let J1:! Z where .Z2 = C(.Self) and .Z1 = ();
  184. // CHECK:STDERR: ^~~~~~~~
  185. // CHECK:STDERR: fail_todo_later_constraint_visible_in_type_parameter.carbon:[[@LINE-7]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  186. // CHECK:STDERR: class C(T:! Z where .Z1 = ()) {}
  187. // CHECK:STDERR: ^
  188. // CHECK:STDERR:
  189. let J1:! Z where .Z2 = C(.Self) and .Z1 = ();
  190. }
  191. fn F(T:! J) {}
  192. class D;
  193. // Satisfies the implied constraint in J.J1.
  194. // CHECK:STDERR: fail_todo_later_constraint_visible_in_type_parameter.carbon:[[@LINE+7]]:38: error: cannot convert type `.Self` that implements `Z` into type implementing `Z where .(Z.Z1) = ()` [ConversionFailureFacetToFacet]
  195. // CHECK:STDERR: impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
  196. // CHECK:STDERR: ^~~~~~~~
  197. // CHECK:STDERR: fail_todo_later_constraint_visible_in_type_parameter.carbon:[[@LINE-21]]:9: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  198. // CHECK:STDERR: class C(T:! Z where .Z1 = ()) {}
  199. // CHECK:STDERR: ^
  200. // CHECK:STDERR:
  201. impl D as Z where .Z1 = () and .Z2 = C(.Self) {}
  202. fn G(T:! J where .J1 = D) {
  203. F(T);
  204. }
  205. // --- early_rewrite_applied.carbon
  206. library "[[@TEST_NAME]]";
  207. interface Z {
  208. let Z1:! type;
  209. let Z2:! type;
  210. }
  211. interface Tuple {}
  212. impl () as Tuple {}
  213. class C(T:! Tuple) {}
  214. interface J {
  215. // Implied: .Z1 == (), but this is also explicitly required for J1.
  216. let J1:! Z where .Z1 = () and .Z2 = C(.Z1);
  217. }
  218. fn F(T:! J) {}
  219. class D;
  220. // Satisfies the implied constraint in J.J1.
  221. impl D as Z where .Z1 = () and .Z2 = C(()) {}
  222. fn G(T:! J where .J1 = D) {
  223. F(T);
  224. }
  225. // --- early_rewrite_applied_from_nested_self.carbon
  226. library "[[@TEST_NAME]]";
  227. interface Z {
  228. let Z1:! type;
  229. let Z2:! type;
  230. }
  231. interface Tuple {}
  232. impl () as Tuple {}
  233. class C(T:! Tuple) {}
  234. interface J {
  235. // Implied: .Z1 == (), but this is also explicitly required for J1.
  236. let J1:! (Z where .Z1 = ()) where .Z2 = C(.Z1);
  237. }
  238. fn F(T:! J) {}
  239. class D;
  240. // Satisfies the implied constraint in J.J1.
  241. impl D as Z where .Z1 = () and .Z2 = C(()) {}
  242. fn G(T:! J where .J1 = D) {
  243. F(T);
  244. }
  245. // --- fail_todo_early_rewrite_applied_extra_indirection.carbon
  246. library "[[@TEST_NAME]]";
  247. interface Z {
  248. let Z1:! type;
  249. let Z2:! type;
  250. }
  251. interface Y {
  252. let Y1:! Z;
  253. }
  254. interface Tuple {}
  255. impl () as Tuple {}
  256. class C(T:! Tuple) {}
  257. interface J {
  258. // Implied: .Z1 == (), but this is also explicitly required for J1.
  259. let J1:! Y & Z where .Y1 = .Self and .Z1 = () and .Z2 = C(.Y1.Z1);
  260. }
  261. fn F(T:! J) {}
  262. class D;
  263. // Satisfies the implied constraint in J.J1.
  264. impl D as Z where .Z1 = () and .Z2 = C(()) {}
  265. impl D as Y where .Y1 = D {}
  266. // CHECK:STDERR: fail_todo_early_rewrite_applied_extra_indirection.carbon:[[@LINE+4]]:24: error: cannot convert type `D` into type implementing `Z & Y where .(Y.Y1) = .Self as Z and .(Z.Z1) = () and .(Z.Z2) = C(() as Tuple)` [ConversionFailureTypeToFacet]
  267. // CHECK:STDERR: fn G(T:! J where .J1 = D) {
  268. // CHECK:STDERR: ^
  269. // CHECK:STDERR:
  270. fn G(T:! J where .J1 = D) {
  271. F(T);
  272. }
  273. // --- fail_todo_resolved_constraint_visible_in_type_parameter.carbon
  274. library "[[@TEST_NAME]]";
  275. interface Y {
  276. let Y1:! type;
  277. }
  278. interface Z {
  279. let Z1:! Y;
  280. let Z2:! type;
  281. let Z3:! type;
  282. }
  283. interface Tuple {}
  284. impl () as Tuple {}
  285. class C(T:! Tuple) {}
  286. interface J {
  287. // Implied: .Y1 impls Tuple, but this is also explicitly required for J1.
  288. let J1:! Z & Y where .Y1 = () and .Z1 = .Self and .Z2 = .Z1.Y1 and .Z3 = C(.Z2);
  289. }
  290. fn F(T:! J) {}
  291. class D;
  292. // Satisfies the implied constraint in J.J1.
  293. impl D as Y where .Y1 = () {}
  294. // CHECK:STDERR: fail_todo_resolved_constraint_visible_in_type_parameter.carbon:[[@LINE+4]]:25: error: cannot convert type `.Self` that implements `Z` into type implementing `Y` [ConversionFailureFacetToFacet]
  295. // CHECK:STDERR: impl D as Z where .Z1 = .Self and .Z2 = () and .Z3 = C(()) {}
  296. // CHECK:STDERR: ^~~~~
  297. // CHECK:STDERR:
  298. impl D as Z where .Z1 = .Self and .Z2 = () and .Z3 = C(()) {}
  299. // CHECK:STDERR: fail_todo_resolved_constraint_visible_in_type_parameter.carbon:[[@LINE+4]]:24: error: cannot convert type `D` into type implementing `Y & Z where .(Y.Y1) = () and .(Z.Z1) = .Self as Y and .(Z.Z2) = () and .(Z.Z3) = C(() as Tuple)` [ConversionFailureTypeToFacet]
  300. // CHECK:STDERR: fn G(T:! J where .J1 = D) {
  301. // CHECK:STDERR: ^
  302. // CHECK:STDERR:
  303. fn G(T:! J where .J1 = D) {
  304. F(T);
  305. }