validate_rewrite_constraints.carbon 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  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_rewrite_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_rewrite_constraints.carbon
  12. // --- facet_value.carbon
  13. library "[[@TEST_NAME]]";
  14. interface I { let X:! type; }
  15. fn F(T:! I where .X = ()) {}
  16. fn G(T:! I where .X = ()) {
  17. F(T);
  18. }
  19. // --- generic_interface_facet_value.carbon
  20. library "[[@TEST_NAME]]";
  21. interface I(T:! type) { let X:! type; }
  22. class C;
  23. final impl forall [T:! type] T as I(()) where .X = () {}
  24. final impl forall [T:! type] T as I({}) where .X = {} {}
  25. fn F(U:! type, T:! I(U) where .X = ()) {}
  26. fn G() {
  27. F((), C);
  28. }
  29. // --- fail_generic_interface_facet_value_wrong_specific_impl.carbon
  30. library "[[@TEST_NAME]]";
  31. interface I(T:! type) { let X:! type; }
  32. class C;
  33. final impl forall [T:! type] T as I(()) where .X = () {}
  34. final impl forall [T:! type] T as I({}) where .X = {} {}
  35. fn F(U:! type, T:! I(U) where .X = ()) {}
  36. fn G() {
  37. // This finds the impl where `.X = {}`, but F requires that `.X = ()`.
  38. //
  39. // CHECK:STDERR: fail_generic_interface_facet_value_wrong_specific_impl.carbon:[[@LINE+7]]:3: error: cannot convert type `C` into type implementing `I({}) where .(I({}).X) = ()` [ConversionFailureTypeToFacet]
  40. // CHECK:STDERR: F({}, C);
  41. // CHECK:STDERR: ^~~~~~~~
  42. // CHECK:STDERR: fail_generic_interface_facet_value_wrong_specific_impl.carbon:[[@LINE-7]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  43. // CHECK:STDERR: fn F(U:! type, T:! I(U) where .X = ()) {}
  44. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  45. // CHECK:STDERR:
  46. F({}, C);
  47. }
  48. // --- dependent_rules.carbon
  49. library "[[@TEST_NAME]]";
  50. interface I { let X:! type; }
  51. interface J { let Y:! type; }
  52. fn F(T:! I & J where .X = .Y) {
  53. // Allowed since `T impls I`, `T impls J`, and has constraint providing
  54. // T.(I.X) = T.(J.Y)`.
  55. F(T);
  56. }
  57. // --- fail_convert.carbon
  58. library "[[@TEST_NAME]]";
  59. interface I { let X:! type; }
  60. fn F(T:! I where .X = {.a: (), .b: {}}) {}
  61. fn H() {
  62. class C;
  63. impl C as I where .X = {.b: {}, .a: ()} {}
  64. // CHECK:STDERR: fail_convert.carbon:[[@LINE+7]]:3: error: cannot convert type `C` into type implementing `I where .(I.X) = {.a: (), .b: {}}` [ConversionFailureTypeToFacet]
  65. // CHECK:STDERR: F(C);
  66. // CHECK:STDERR: ^~~~
  67. // CHECK:STDERR: fail_convert.carbon:[[@LINE-8]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  68. // CHECK:STDERR: fn F(T:! I where .X = {.a: (), .b: {}}) {}
  69. // CHECK:STDERR: ^
  70. // CHECK:STDERR:
  71. F(C);
  72. }
  73. fn G(T:! I where .X = {.b: {}, .a: ()}) {
  74. // CHECK:STDERR: fail_convert.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I where .(I.X) = {.b: {}, .a: ()}` into type implementing `I where .(I.X) = {.a: (), .b: {}}` [ConversionFailureFacetToFacet]
  75. // CHECK:STDERR: F(T);
  76. // CHECK:STDERR: ^~~~
  77. // CHECK:STDERR: fail_convert.carbon:[[@LINE-19]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  78. // CHECK:STDERR: fn F(T:! I where .X = {.a: (), .b: {}}) {}
  79. // CHECK:STDERR: ^
  80. // CHECK:STDERR:
  81. F(T);
  82. }
  83. // --- fail_todo_dependent_rules_compound.carbon
  84. library "[[@TEST_NAME]]";
  85. interface I { let X:! type; }
  86. interface J { let Y:! type; }
  87. // CHECK:STDERR: fail_todo_dependent_rules_compound.carbon:[[@LINE+8]]:23: error: expected identifier or `Self` after `.` [ExpectedIdentifierOrSelfAfterPeriod]
  88. // CHECK:STDERR: fn F(T:! I & J where .(I.X) = .(J.Y)) {
  89. // CHECK:STDERR: ^
  90. // CHECK:STDERR:
  91. // CHECK:STDERR: fail_todo_dependent_rules_compound.carbon:[[@LINE+4]]:23: error: semantics TODO: `handle invalid parse trees in `check`` [SemanticsTodo]
  92. // CHECK:STDERR: fn F(T:! I & J where .(I.X) = .(J.Y)) {
  93. // CHECK:STDERR: ^
  94. // CHECK:STDERR:
  95. fn F(T:! I & J where .(I.X) = .(J.Y)) {
  96. // Allowed since `T impls I`, `T impls J`, and has constraint providing
  97. // T.(I.X) = T.(J.Y)`.
  98. F(T);
  99. }
  100. // --- parameterized_interface.carbon
  101. library "[[@TEST_NAME]]";
  102. interface I(T:! type) { let X:! type; }
  103. final impl forall [J:! I(()) where .X = ()] J as I({}) where .X = {} {}
  104. fn F(T:! I({}) where .X = {}) {}
  105. fn G(T:! I(()) where .X = ()) {
  106. F(T);
  107. }
  108. // --- fail_todo_parameterized_interface_compound.carbon
  109. library "[[@TEST_NAME]]";
  110. interface I(T:! type) { let X:! type; }
  111. final impl forall [J:! I(())] J as I({}) where .X = {} {}
  112. // CHECK:STDERR: fail_todo_parameterized_interface_compound.carbon:[[@LINE+8]]:31: error: expected identifier or `Self` after `.` [ExpectedIdentifierOrSelfAfterPeriod]
  113. // CHECK:STDERR: fn F(T:! I(()) & I({}) where .(I(()).X) = () and .(I({}).X) = {}) {}
  114. // CHECK:STDERR: ^
  115. // CHECK:STDERR:
  116. // CHECK:STDERR: fail_todo_parameterized_interface_compound.carbon:[[@LINE+4]]:31: error: semantics TODO: `handle invalid parse trees in `check`` [SemanticsTodo]
  117. // CHECK:STDERR: fn F(T:! I(()) & I({}) where .(I(()).X) = () and .(I({}).X) = {}) {}
  118. // CHECK:STDERR: ^
  119. // CHECK:STDERR:
  120. fn F(T:! I(()) & I({}) where .(I(()).X) = () and .(I({}).X) = {}) {}
  121. fn G(T:! I(()) where .X = ()) {
  122. F(T);
  123. }
  124. // --- fail_parameterized_interface_with_wrong_where_in_impl_deduction.carbon
  125. library "[[@TEST_NAME]]";
  126. interface I(T:! type) { let X:! type; }
  127. final impl forall [J:! I(()) where .X = {}] J as I({}) where .X = {} {}
  128. fn F(T:! I({}) where .X = {}) {}
  129. fn G(T:! I(()) where .X = ()) {
  130. // CHECK:STDERR: fail_parameterized_interface_with_wrong_where_in_impl_deduction.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I(()) where .(I(()).X) = ()` into type implementing `I({}) where .(I({}).X) = {}` [ConversionFailureFacetToFacet]
  131. // CHECK:STDERR: F(T);
  132. // CHECK:STDERR: ^~~~
  133. // CHECK:STDERR: fail_parameterized_interface_with_wrong_where_in_impl_deduction.carbon:[[@LINE-6]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  134. // CHECK:STDERR: fn F(T:! I({}) where .X = {}) {}
  135. // CHECK:STDERR: ^
  136. // CHECK:STDERR:
  137. F(T);
  138. }
  139. // --- source_rhs_is_assoc_constant.carbon
  140. library "[[@TEST_NAME]]";
  141. interface I {
  142. let X1:! type;
  143. let X2:! type;
  144. }
  145. fn F(T:! I where .X1 = () and .X2 = ()) {}
  146. fn G() {
  147. class C;
  148. impl C as I where .X1 = .X2 and .X2 = () {}
  149. F(C);
  150. }
  151. fn H(T:! I where .X1 = .X2 and .X2 = ()) {
  152. F(T);
  153. }
  154. // --- target_rhs_is_assoc_constant.carbon
  155. library "[[@TEST_NAME]]";
  156. interface I {
  157. let X1:! type;
  158. let X2:! type;
  159. }
  160. fn F(T:! I where .X1 = .X2 and .X2 = ()) {}
  161. fn G() {
  162. class C;
  163. impl C as I where .X1 = () and .X2 = () {}
  164. F(C);
  165. }
  166. fn H(T:! I where .X1 = () and .X2 = ()) {
  167. F(T);
  168. }
  169. // --- both_rhs_is_assoc_constant.carbon
  170. library "[[@TEST_NAME]]";
  171. interface I {
  172. let X1:! type;
  173. let X2:! type;
  174. }
  175. fn F(T:! I where .X1 = .X2 and .X2 = ()) {}
  176. fn G() {
  177. class C1;
  178. impl C1 as I where .X1 = .X2 and .X2 = () {}
  179. F(C1);
  180. class C2;
  181. impl C2 as I where .X1 = () and .X2 = .X1 {}
  182. F(C2);
  183. }
  184. fn H1(T:! I where .X1 = .X2 and .X2 = ()) {
  185. F(T);
  186. }
  187. fn H2(T:! I where .X1 = () and .X2 = .X1) {
  188. F(T);
  189. }
  190. // --- fail_error_in_witness_table.carbon
  191. library "[[@TEST_NAME]]";
  192. interface I {
  193. let X1:! type;
  194. let X2:! type;
  195. }
  196. fn F(T:! I where .X1 = .X2) {}
  197. class C;
  198. // .X2 is not set in the impl definition, so its value is an ErrorInst
  199. // in the impl's witness table.
  200. //
  201. // CHECK:STDERR: fail_error_in_witness_table.carbon:[[@LINE+7]]:1: error: associated constant X2 not given a value in impl of interface I [ImplAssociatedConstantNeedsValue]
  202. // CHECK:STDERR: impl C as I where .X1 = () {}
  203. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  204. // CHECK:STDERR: fail_error_in_witness_table.carbon:[[@LINE-12]]:7: note: associated constant declared here [AssociatedConstantHere]
  205. // CHECK:STDERR: let X2:! type;
  206. // CHECK:STDERR: ^~~~~~~~~
  207. // CHECK:STDERR:
  208. impl C as I where .X1 = () {}
  209. fn G() {
  210. // CHECK:STDERR: fail_error_in_witness_table.carbon:[[@LINE+7]]:3: error: cannot convert type `C` into type implementing `I where .(I.X1) = .(I.X2)` [ConversionFailureTypeToFacet]
  211. // CHECK:STDERR: F(C);
  212. // CHECK:STDERR: ^~~~
  213. // CHECK:STDERR: fail_error_in_witness_table.carbon:[[@LINE-19]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  214. // CHECK:STDERR: fn F(T:! I where .X1 = .X2) {}
  215. // CHECK:STDERR: ^
  216. // CHECK:STDERR:
  217. F(C);
  218. }
  219. // --- impl_provides_rewrite_requirements.carbon
  220. library "[[@TEST_NAME]]";
  221. interface I {}
  222. interface J { let Y:! type; }
  223. class C;
  224. final impl forall [T:! I] T as J where .Y = C {}
  225. fn F(T:! I & J where .Y = C) {}
  226. fn G(T:! I) {
  227. F(T);
  228. }
  229. // --- facet_provides_rewrite_requirements.carbon
  230. library "[[@TEST_NAME]]";
  231. interface I {}
  232. interface J { let Y:! type; }
  233. class C;
  234. final impl forall [T:! J] T as I {}
  235. fn F(T:! I & J where .Y = C) {}
  236. fn G(T:! J where .Y = C) {
  237. F(T);
  238. }
  239. // --- fail_non_final_impl_deduce.carbon
  240. library "[[@TEST_NAME]]";
  241. interface I { let X:! type; }
  242. impl forall [U:! type] U as I where .X = () {}
  243. fn F(T:! I where .X = ()) {}
  244. class C(T:! type);
  245. fn G(T:! type) {
  246. // The type `C(T)` is generic so that the resulting impl witness will not be
  247. // effectively final.
  248. //
  249. // CHECK:STDERR: fail_non_final_impl_deduce.carbon:[[@LINE+7]]:3: error: cannot convert type `C(T)` into type implementing `I where .(I.X) = ()` [ConversionFailureTypeToFacet]
  250. // CHECK:STDERR: F(C(T));
  251. // CHECK:STDERR: ^~~~~~~
  252. // CHECK:STDERR: fail_non_final_impl_deduce.carbon:[[@LINE-11]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  253. // CHECK:STDERR: fn F(T:! I where .X = ()) {}
  254. // CHECK:STDERR: ^
  255. // CHECK:STDERR:
  256. F(C(T));
  257. }
  258. // --- fail_non_final_impl_explicit.carbon
  259. library "[[@TEST_NAME]]";
  260. interface I { let X:! type; }
  261. impl forall [U:! type] U as I where .X = () {}
  262. class C(T:! type);
  263. fn F(T:! type) {
  264. // The type `C(T)` is generic so that the resulting impl witness will not be
  265. // effectively final.
  266. //
  267. // CHECK:STDERR: fail_non_final_impl_explicit.carbon:[[@LINE+4]]:3: error: cannot convert type `C(T)` into type implementing `I where .(I.X) = ()` [ConversionFailureTypeToFacet]
  268. // CHECK:STDERR: C(T) as (I where .X = ());
  269. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  270. // CHECK:STDERR:
  271. C(T) as (I where .X = ());
  272. }
  273. // --- concrete_query_non_final_impl_deduce.carbon
  274. library "[[@TEST_NAME]]";
  275. interface I { let X:! type; }
  276. impl forall [U:! type] U as I where .X = () {}
  277. fn F(T:! I where .X = ()) {}
  278. fn G() {
  279. class C;
  280. F(C);
  281. }
  282. // --- concrete_query_non_final_impl_explicit_as.carbon
  283. library "[[@TEST_NAME]]";
  284. interface I { let X:! type; }
  285. impl forall [U:! type] U as I where .X = () {}
  286. fn F() {
  287. class C;
  288. C as (I where .X = ());
  289. }
  290. // --- final_impl_deduce.carbon
  291. library "[[@TEST_NAME]]";
  292. interface I { let X:! type; }
  293. final impl forall [U:! type] U as I where .X = () {}
  294. fn F(T:! I where .X = ()) {}
  295. fn G() {
  296. class C;
  297. F(C);
  298. }
  299. // --- final_impl_explicit_as.carbon
  300. library "[[@TEST_NAME]]";
  301. interface I { let X:! type; }
  302. final impl forall [U:! type] U as I where .X = () {}
  303. fn F() {
  304. class C;
  305. C as (I where .X = ());
  306. }
  307. // --- concrete_specialization_impl_deduce.carbon
  308. library "[[@TEST_NAME]]";
  309. interface I { let X:! type; }
  310. class C;
  311. impl C as I where .X = () {}
  312. fn F(T:! I where .X = ()) {}
  313. fn G() {
  314. F(C);
  315. }
  316. // --- concrete_specialization_impl_explicit_as.carbon
  317. library "[[@TEST_NAME]]";
  318. interface I { let X:! type; }
  319. class C;
  320. impl C as I where .X = () {}
  321. fn F() {
  322. C as (I where .X = ());
  323. }
  324. // --- rewrite_value_in_class_param.carbon
  325. library "[[@TEST_NAME]]";
  326. interface I {
  327. let X:! type;
  328. let Y:! type;
  329. }
  330. class C(T:! type);
  331. fn F(T:! I where .X = C(.Y)) {}
  332. fn G(T:! I where .X = C(()) and .Y = ()) {
  333. F(T);
  334. }
  335. // --- fail_wrong_rewrite_value_in_class_param.carbon
  336. library "[[@TEST_NAME]]";
  337. interface I {
  338. let X:! type;
  339. let Y:! type;
  340. }
  341. class C(T:! type);
  342. fn F(T:! I where .X = C(.Y)) {}
  343. fn G(T:! I where .X = C(()) and .Y = {}) {
  344. // CHECK:STDERR: fail_wrong_rewrite_value_in_class_param.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I where .(I.X) = C(()) and .(I.Y) = {}` into type implementing `I where .(I.X) = C(.(I.Y))` [ConversionFailureFacetToFacet]
  345. // CHECK:STDERR: F(T);
  346. // CHECK:STDERR: ^~~~
  347. // CHECK:STDERR: fail_wrong_rewrite_value_in_class_param.carbon:[[@LINE-6]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  348. // CHECK:STDERR: fn F(T:! I where .X = C(.Y)) {}
  349. // CHECK:STDERR: ^
  350. // CHECK:STDERR:
  351. F(T);
  352. }
  353. // --- function_in_interface_ignored.carbon
  354. library "[[@TEST_NAME]]";
  355. interface I {
  356. let X:! type;
  357. fn F();
  358. }
  359. fn F(T:! I where .X = ()) {}
  360. fn G(T:! I where .X = ()) {
  361. F(T);
  362. }
  363. // --- function_as_rewrite_value.carbon
  364. library "[[@TEST_NAME]]";
  365. interface I {
  366. let X:! type;
  367. let Y:! X;
  368. fn A();
  369. }
  370. fn F(T:! I where .Y = .A) {}
  371. fn G(T:! I where .Y = .A) {
  372. F(T);
  373. }
  374. // --- fail_wrong_function_as_rewrite_value.carbon
  375. library "[[@TEST_NAME]]";
  376. interface I {
  377. let X:! type;
  378. let Y:! X;
  379. fn A();
  380. fn B();
  381. }
  382. fn F(T:! I where .Y = .A) {}
  383. fn G(T:! I where .Y = .B) {
  384. // CHECK:STDERR: fail_wrong_function_as_rewrite_value.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I where .(I.Y) = .(I.B)` into type implementing `I where .(I.Y) = .(I.A)` [ConversionFailureFacetToFacet]
  385. // CHECK:STDERR: F(T);
  386. // CHECK:STDERR: ^~~~
  387. // CHECK:STDERR: fail_wrong_function_as_rewrite_value.carbon:[[@LINE-6]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  388. // CHECK:STDERR: fn F(T:! I where .Y = .A) {}
  389. // CHECK:STDERR: ^
  390. // CHECK:STDERR:
  391. F(T);
  392. }
  393. // --- fail_wrong_function_as_rewrite_value_in_other_interface.carbon
  394. library "[[@TEST_NAME]]";
  395. interface I {
  396. let X:! type;
  397. let Y:! X;
  398. fn A();
  399. }
  400. interface J {
  401. let J1:! type;
  402. let J2:! type;
  403. // B has the same index in J as A does in I, ensuring the index is not enough
  404. // for comparing them.
  405. fn B();
  406. }
  407. fn F(T:! I & J where .Y = .A) {}
  408. fn G(T:! I & J where .Y = .B) {
  409. // CHECK:STDERR: fail_wrong_function_as_rewrite_value_in_other_interface.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I & J where .(I.Y) = .(J.B)` into type implementing `I & J where .(I.Y) = .(I.A)` [ConversionFailureFacetToFacet]
  410. // CHECK:STDERR: F(T);
  411. // CHECK:STDERR: ^~~~
  412. // CHECK:STDERR: fail_wrong_function_as_rewrite_value_in_other_interface.carbon:[[@LINE-6]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  413. // CHECK:STDERR: fn F(T:! I & J where .Y = .A) {}
  414. // CHECK:STDERR: ^
  415. // CHECK:STDERR:
  416. F(T);
  417. }
  418. // --- recursive_facet.carbon
  419. library "[[@TEST_NAME]]";
  420. interface I { let X:! type; }
  421. interface K(Y:! type) { }
  422. fn F(T:! I where .X = {.k: K(I where .X = ())}) {}
  423. fn G(T:! I where .X = {.k: K(I where .X = ())}) {
  424. F(T);
  425. }
  426. // --- fail_facet_type_concrete_types_match_blanket_impl_concrete_types.carbon
  427. library "[[@TEST_NAME]]";
  428. interface A { let X:! type; }
  429. interface B { let Y:! type; }
  430. interface C(BB:! B) { let AX:! type; let BY:! type; }
  431. impl forall [AA:! A, BB:! B] AA as C(BB) where .AX = () and .BY = {} {}
  432. fn F(AA:! A where .X = (), BB:! B where .Y = {}) {
  433. // The types match but there may be a specialization that specifies different
  434. // types and would be prefered for a specific `AA` or `BB`.
  435. //
  436. // CHECK:STDERR: fail_facet_type_concrete_types_match_blanket_impl_concrete_types.carbon:[[@LINE+4]]:3: error: cannot convert type `AA` that implements `A where .(A.X) = ()` into type implementing `C(BB as B) where .(C(BB as B).AX) = () and .(C(BB as B).BY) = {}` [ConversionFailureFacetToFacet]
  437. // CHECK:STDERR: AA as (C(BB) where .AX = () and .BY = {});
  438. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  439. // CHECK:STDERR:
  440. AA as (C(BB) where .AX = () and .BY = {});
  441. }
  442. // --- fail_facet_type_concrete_types_become_blank_impl_types.carbon
  443. library "[[@TEST_NAME]]";
  444. interface A { let X:! type; }
  445. interface B { let Y:! type; }
  446. interface C(BB:! B) { let AX:! type; let BY:! type; }
  447. impl forall [AA:! A, BB:! B] AA as C(BB) where .AX = AA.X and .BY = BB.Y {}
  448. fn F(AA:! A where .X = (), BB:! B where .Y = {}) {
  449. // The types match but there may be a specialization that specifies different
  450. // types and would be prefered for a specific `AA` or `BB`.
  451. //
  452. // CHECK:STDERR: fail_facet_type_concrete_types_become_blank_impl_types.carbon:[[@LINE+4]]:3: error: cannot convert type `AA` that implements `A where .(A.X) = ()` into type implementing `C(BB as B) where .(C(BB as B).AX) = () and .(C(BB as B).BY) = {}` [ConversionFailureFacetToFacet]
  453. // CHECK:STDERR: AA as (C(BB) where .AX = () and .BY = {});
  454. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  455. // CHECK:STDERR:
  456. AA as (C(BB) where .AX = () and .BY = {});
  457. }
  458. // --- facet_type_concrete_types_match_final_blanket_impl_concrete_types.carbon
  459. library "[[@TEST_NAME]]";
  460. interface A { let X:! type; }
  461. interface B { let Y:! type; }
  462. interface C(BB:! B) { let AX:! type; let BY:! type; }
  463. final impl forall [AA:! A, BB:! B] AA as C(BB) where .AX = () and .BY = {} {}
  464. fn F(AA:! A where .X = (), BB:! B where .Y = {}) {
  465. AA as (C(BB) where .AX = () and .BY = {});
  466. }
  467. // --- facet_type_concrete_types_become_final_blanket_impl_types.carbon
  468. library "[[@TEST_NAME]]";
  469. interface A { let X:! type; }
  470. interface B { let Y:! type; }
  471. interface C(BB:! B) { let AX:! type; let BY:! type; }
  472. final impl forall [AA:! A, BB:! B] AA as C(BB) where .AX = AA.X and .BY = BB.Y {}
  473. fn F(AA:! A where .X = (), BB:! B where .Y = {}) {
  474. AA as (C(BB) where .AX = () and .BY = {});
  475. }
  476. // --- fail_facet_type_concrete_types_become_final_blanket_impl_types_wrong_types.carbon
  477. library "[[@TEST_NAME]]";
  478. interface A { let X:! type; }
  479. interface B { let Y:! type; }
  480. interface C(BB:! B) { let AX:! type; let BY:! type; }
  481. final impl forall [AA:! A, BB:! B] AA as C(BB) where .AX = AA.X and .BY = BB.Y {}
  482. fn F(AA:! A where .X = (), BB:! B where .Y = {}) {
  483. // CHECK:STDERR: fail_facet_type_concrete_types_become_final_blanket_impl_types_wrong_types.carbon:[[@LINE+4]]:3: error: cannot convert type `AA` that implements `A where .(A.X) = ()` into type implementing `C(BB as B) where .(C(BB as B).AX) = {} and .(C(BB as B).BY) = ()` [ConversionFailureFacetToFacet]
  484. // CHECK:STDERR: AA as (C(BB) where .AX = {} and .BY = ());
  485. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  486. // CHECK:STDERR:
  487. AA as (C(BB) where .AX = {} and .BY = ());
  488. }
  489. // --- chain_in_target.carbon
  490. library "[[@TEST_NAME]]";
  491. interface I {
  492. let X1:! type;
  493. let X2:! type;
  494. let X3:! type;
  495. }
  496. class C(T:! type);
  497. fn F(T:! I where .X1 = .X3 and .X2 = C(.X3) and .X3 = ()) {}
  498. fn G(T:! I where .X1 = () and .X2 = C(()) and .X3 = ()) {
  499. F(T);
  500. }
  501. // --- chain_in_source.carbon
  502. library "[[@TEST_NAME]]";
  503. interface I {
  504. let X1:! type;
  505. let X2:! type;
  506. let X3:! type;
  507. }
  508. class C(T:! type);
  509. fn F(T:! I where .X1 = () and .X2 = C(()) and .X3 = .X1) {}
  510. fn G(T:! I where .X1 = .X3 and .X2 = C(.X1) and .X3 = ()) {
  511. F(T);
  512. }
  513. // --- reference_other_facet_value.carbon
  514. library "[[@TEST_NAME]]";
  515. interface I {
  516. let X1:! type;
  517. let X2:! type;
  518. }
  519. fn F(U:! I where .X1 = {}, T:! I where .X1 = () and .X2 = U.X1) {}
  520. fn G(U:! I where .X1 = {} and .X2 = (), T:! I where .X1 = U.X2 and .X2 = {}) {
  521. F(U, T);
  522. }
  523. // --- fail_todo_value_through_self_value.carbon
  524. library "[[@TEST_NAME]]";
  525. interface I {
  526. // TODO: This should not be diagnosed.
  527. //
  528. // CHECK:STDERR: fail_todo_value_through_self_value.carbon:[[@LINE+7]]:12: error: associated constant has incomplete type `I` [IncompleteTypeInAssociatedConstantDecl]
  529. // CHECK:STDERR: let X1:! I;
  530. // CHECK:STDERR: ^
  531. // CHECK:STDERR: fail_todo_value_through_self_value.carbon:[[@LINE-6]]:1: note: interface is currently being defined [InterfaceIncompleteWithinDefinition]
  532. // CHECK:STDERR: interface I {
  533. // CHECK:STDERR: ^~~~~~~~~~~~~
  534. // CHECK:STDERR:
  535. let X1:! I;
  536. let X2:! type;
  537. let X3:! type;
  538. }
  539. fn F(T:! I where .X1 = .Self and .X2 = .X1.X3 and .X3 = ());
  540. fn G(T:! I where .X1 = .Self and .X2 = () and .X3 = ()) {
  541. F(T);
  542. }
  543. fn H(T:! I where .X1 = .Self and .X2 = .X1.X3 and .X3 = ()) {
  544. G(T);
  545. }
  546. // --- associated_constant_is_facet_type_of_same_interface.carbon
  547. library "[[@TEST_NAME]]";
  548. interface I {
  549. let A:! type;
  550. let X:! type;
  551. }
  552. class C;
  553. // This looks for a bug where `.Self.A` resolves to `C` from `.T.A` in the
  554. // incoming facet value, which is incorrect. It should be `.T.X.A` which
  555. // resolves to `{}`.
  556. fn F(U:! I where .X = (I where .A = {})) {}
  557. fn G(T:! I where .X = (I where .A = {}) and .A = C) {
  558. F(T);
  559. }
  560. // --- rewrite_requires_subst_in_rhs.carbon
  561. library "[[@TEST_NAME]]";
  562. interface I {
  563. let X:! type;
  564. let Y:! type;
  565. }
  566. class C(T:! type);
  567. fn F(T:! I where .X = C(.Y)) {}
  568. fn G(T:! I where .X = C({}) and .Y = {}) {
  569. F(T);
  570. }
  571. // --- fail_todo_rewrite_requires_subst_in_nested_facet_type.carbon
  572. library "[[@TEST_NAME]]";
  573. interface I(T:! type) {
  574. let X:! type;
  575. let Y:! type;
  576. }
  577. class C;
  578. fn F(T:! I(C) where .X = (I(.Y) where .Y = ())) {}
  579. fn G(T:! I(C) where .X = (I({}) where .Y = ()) and .Y = {}) {
  580. // TODO: The T in G should match the T in F, once the .Self reference to the
  581. // top level facet value in `I(.Y)` is correctly substituted by tracking that
  582. // it is a .Self reference to the top level Self.
  583. // CHECK:STDERR: fail_todo_rewrite_requires_subst_in_nested_facet_type.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I(C) where .(I(C).X) = I({}) where .(I({}).Y) = () and .(I(C).Y) = {}` into type implementing `I(C) where .(I(C).X) = I(.(I(C).Y)) where .(I(.(I(C).Y)).Y) = ()` [ConversionFailureFacetToFacet]
  584. // CHECK:STDERR: F(T);
  585. // CHECK:STDERR: ^~~~
  586. // CHECK:STDERR: fail_todo_rewrite_requires_subst_in_nested_facet_type.carbon:[[@LINE-9]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  587. // CHECK:STDERR: fn F(T:! I(C) where .X = (I(.Y) where .Y = ())) {}
  588. // CHECK:STDERR: ^
  589. // CHECK:STDERR:
  590. F(T);
  591. }
  592. fn G2(T:! I(C) where .X = (I(.Y) where .Y = ()) and .Y = {}) {
  593. F(T);
  594. }
  595. // --- fail_rewrite_requires_subst_in_nested_facet_type_types_differ.carbon
  596. library "[[@TEST_NAME]]";
  597. interface I(T:! type) {
  598. let X:! type;
  599. let Y:! type;
  600. }
  601. fn F(T:! I({}) where .X = (I(.Y) where .X = ())) {}
  602. // I(.Y) is I({}) which doesn't match I(()).
  603. fn G(T:! I({}) where .X = (I(()) where .X = ()) and .Y = {}) {
  604. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_facet_type_types_differ.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I({}) where .(I({}).X) = I(()) where .(I(()).X) = () and .(I({}).Y) = {}` into type implementing `I({}) where .(I({}).X) = I(.(I({}).Y)) where .(I(.(I({}).Y)).X) = ()` [ConversionFailureFacetToFacet]
  605. // CHECK:STDERR: F(T);
  606. // CHECK:STDERR: ^~~~
  607. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_facet_type_types_differ.carbon:[[@LINE-7]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  608. // CHECK:STDERR: fn F(T:! I({}) where .X = (I(.Y) where .X = ())) {}
  609. // CHECK:STDERR: ^
  610. // CHECK:STDERR:
  611. F(T);
  612. }
  613. // --- facet_type_in_assoc_constant.carbon
  614. library "[[@TEST_NAME]]";
  615. interface I {
  616. let X:! type;
  617. let Y:! type;
  618. }
  619. fn F(T:! I where .X = (I where .X = () and .Y = ())) {}
  620. fn G(T:! I where .X = (I where .X = .Y and .Y = ())) {
  621. F(T);
  622. }
  623. // --- fail_facet_type_in_assoc_constant_differs.carbon
  624. library "[[@TEST_NAME]]";
  625. interface I {
  626. let X:! type;
  627. let Y:! type;
  628. let Z:! type;
  629. }
  630. fn F(T:! I where .X = (I where .X = .Y)) {}
  631. fn G(T:! I where .X = (I where .X = () and .Y = () and .Z = {})) {
  632. // CHECK:STDERR: fail_facet_type_in_assoc_constant_differs.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I where .(I.X) = I where .(I.X) = () and .(I.Y) = () and .(I.Z) = {}` into type implementing `I where .(I.X) = I where .(I.X) = .(I.Y)` [ConversionFailureFacetToFacet]
  633. // CHECK:STDERR: F(T);
  634. // CHECK:STDERR: ^~~~
  635. // CHECK:STDERR: fail_facet_type_in_assoc_constant_differs.carbon:[[@LINE-6]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  636. // CHECK:STDERR: fn F(T:! I where .X = (I where .X = .Y)) {}
  637. // CHECK:STDERR: ^
  638. // CHECK:STDERR:
  639. F(T);
  640. }
  641. // --- nested_facet_type_in_assoc_constant.carbon
  642. library "[[@TEST_NAME]]";
  643. interface I {
  644. let X:! type;
  645. let Y:! type;
  646. let Z:! type;
  647. }
  648. fn F(T:! I where .X = (I where .Y = (I where .X = () and .Y = ()))) {}
  649. fn G(T:! I where .X = (I where .Y = (I where .X = .Y and .Y = ()))) {
  650. F(T);
  651. }
  652. // --- fail_nested_facet_type_assigns_same_assoc_constant.carbon
  653. library "[[@TEST_NAME]]";
  654. interface I {
  655. let X:! type;
  656. let Y:! type;
  657. }
  658. fn F(T:! I where .Y = ()) {}
  659. // The `.Y = ()` is on a different `.Self` than `T` (an unattached self), so
  660. // should not satisfy `F`.
  661. fn G(T:! I where .X = (I where .Y = ())) {
  662. // CHECK:STDERR: fail_nested_facet_type_assigns_same_assoc_constant.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I where .(I.X) = I where .(I.Y) = ()` into type implementing `I where .(I.Y) = ()` [ConversionFailureFacetToFacet]
  663. // CHECK:STDERR: F(T);
  664. // CHECK:STDERR: ^~~~
  665. // CHECK:STDERR: fail_nested_facet_type_assigns_same_assoc_constant.carbon:[[@LINE-8]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  666. // CHECK:STDERR: fn F(T:! I where .Y = ()) {}
  667. // CHECK:STDERR: ^
  668. // CHECK:STDERR:
  669. F(T);
  670. }
  671. // --- fail_nested_facet_type_in_assoc_constant_differs.carbon
  672. library "[[@TEST_NAME]]";
  673. interface I {
  674. let X:! type;
  675. let Y:! type;
  676. let Z:! type;
  677. }
  678. fn F(T:! I where .X = (I where .Y = (I where .X = .Y))) {}
  679. // `.X = .Y` does not match `.X = () and .Y = ()` as they are different resolved
  680. // facet types.
  681. fn G(T:! I where .X = (I where .Y = (I where .X = () and .Y = ()))) {
  682. // CHECK:STDERR: fail_nested_facet_type_in_assoc_constant_differs.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I where .(I.X) = I where .(I.Y) = I where .(I.X) = () and .(I.Y) = ()` into type implementing `I where .(I.X) = I where .(I.Y) = I where .(I.X) = .(I.Y)` [ConversionFailureFacetToFacet]
  683. // CHECK:STDERR: F(T);
  684. // CHECK:STDERR: ^~~~
  685. // CHECK:STDERR: fail_nested_facet_type_in_assoc_constant_differs.carbon:[[@LINE-8]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  686. // CHECK:STDERR: fn F(T:! I where .X = (I where .Y = (I where .X = .Y))) {}
  687. // CHECK:STDERR: ^
  688. // CHECK:STDERR:
  689. F(T);
  690. }
  691. // The extra .Z rewrite makes a different resolved facet type which does not
  692. // match.
  693. fn G2(T:! I where .X = (I where .Y = (I where .X = .Y and .Z = {}))) {
  694. // CHECK:STDERR: fail_nested_facet_type_in_assoc_constant_differs.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I where .(I.X) = I where .(I.Y) = I where .(I.X) = .(I.Y) and .(I.Z) = {}` into type implementing `I where .(I.X) = I where .(I.Y) = I where .(I.X) = .(I.Y)` [ConversionFailureFacetToFacet]
  695. // CHECK:STDERR: F(T);
  696. // CHECK:STDERR: ^~~~
  697. // CHECK:STDERR: fail_nested_facet_type_in_assoc_constant_differs.carbon:[[@LINE-21]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  698. // CHECK:STDERR: fn F(T:! I where .X = (I where .Y = (I where .X = .Y))) {}
  699. // CHECK:STDERR: ^
  700. // CHECK:STDERR:
  701. F(T);
  702. }
  703. // --- fail_nested_facet_type_from_constant.carbon
  704. library "[[@TEST_NAME]]";
  705. interface I {
  706. let X:! type;
  707. let Y:! type;
  708. }
  709. fn F(T:! I where .X = (I where .Y = (I where .X = .Y, ))) {}
  710. // References to named constants in a facet type don't work at all. If they did,
  711. // then when the `Constant` facet type is put into the RHS of a rewrite
  712. // constraint, its references to `.Self` must be modified to not refer to the
  713. // top level `.Self` which is `T`. If done correctly, they will match the
  714. // `.Self` references in the same position in the parameter of `F`. If not, the
  715. // `.X` within becomes self-referential and makes a cycle.
  716. fn G1() {
  717. // CHECK:STDERR: fail_nested_facet_type_from_constant.carbon:[[@LINE+4]]:7: error: semantics TODO: `local `let :!` bindings are currently unsupported` [SemanticsTodo]
  718. // CHECK:STDERR: let Constant:! type = I where .X = .Y;
  719. // CHECK:STDERR: ^~~~~~~~~~~~~~~
  720. // CHECK:STDERR:
  721. let Constant:! type = I where .X = .Y;
  722. fn G(T:! I where .X = (I where .Y = (Constant, ))) {
  723. F(T);
  724. }
  725. }
  726. fn G2() {
  727. let Constant:! type = (I where .X = .Y, );
  728. fn G(T:! I where .X = (I where .Y = Constant)) {
  729. F(T);
  730. }
  731. }
  732. fn G3() {
  733. let Constant:! type = I where .Y = (I where .X = .Y, );
  734. fn G(T:! I where .X = Constant) {
  735. F(T);
  736. }
  737. }
  738. fn G4() {
  739. let Constant2:! type = (I where .X = .Y, );
  740. let Constant:! type = Constant2;
  741. fn G(T:! I where .X = (I where .Y = Constant)) {
  742. F(T);
  743. }
  744. }
  745. fn G5() {
  746. let Constant2:! type = I where .X = .Y;
  747. let Constant:! type = (Constant2, );
  748. fn G(T:! I where .X = (I where .Y = Constant)) {
  749. F(T);
  750. }
  751. }
  752. fn G6() {
  753. let Constant2:! type = (I where .X = .Y, );
  754. let Constant:! type = I where .Y = Constant2;
  755. fn G(T:! I where .X = Constant) {
  756. F(T);
  757. }
  758. }
  759. // --- fail_nested_facet_type_from_constant_differs.carbon
  760. library "[[@TEST_NAME]]";
  761. interface I {
  762. let X:! type;
  763. let Y:! type;
  764. }
  765. fn F(T:! I where .X = (I where .Y = (I where .X = .Y, ))) {}
  766. fn G1() {
  767. // CHECK:STDERR: fail_nested_facet_type_from_constant_differs.carbon:[[@LINE+4]]:7: error: semantics TODO: `local `let :!` bindings are currently unsupported` [SemanticsTodo]
  768. // CHECK:STDERR: let Constant:! type = I where .X = () and .Y = ();
  769. // CHECK:STDERR: ^~~~~~~~~~~~~~~
  770. // CHECK:STDERR:
  771. let Constant:! type = I where .X = () and .Y = ();
  772. fn G(T:! I where .X = (I where .Y = (Constant, ))) {
  773. F(T);
  774. }
  775. }
  776. // --- rewrite_requires_subst_in_nested_access_of_self.carbon
  777. library "[[@TEST_NAME]]";
  778. interface I {
  779. let I1:! type;
  780. let I2:! type;
  781. }
  782. interface J {
  783. let J1:! I;
  784. }
  785. interface K {
  786. let K1:! J;
  787. }
  788. fn F(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I1 = (.K1.J1).I2) {}
  789. fn G(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I1 = .I2) {
  790. F(T);
  791. }
  792. // --- fail_rewrite_requires_subst_in_nested_access_of_self_wrong_type.carbon
  793. library "[[@TEST_NAME]]";
  794. interface I {
  795. let I1:! type;
  796. let I2:! type;
  797. }
  798. interface J {
  799. let J1:! I;
  800. }
  801. interface K {
  802. let K1:! J;
  803. }
  804. fn F(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I1 = (.K1.J1).I2) {}
  805. // F's requirement I1 = I2 is not met, as I1 = () and I2 = {}
  806. fn G(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I1 = () and .I2 = {}) {
  807. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_self_wrong_type.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I & J & K where .(K.K1) = .Self as J and .(J.J1) = .Self as I and .(I.I1) = () and .(I.I2) = {}` into type implementing `I & J & K where .(K.K1) = .Self as J and .(J.J1) = .Self as I and .(I.I1) = .(I.I2)` [ConversionFailureFacetToFacet]
  808. // CHECK:STDERR: F(T);
  809. // CHECK:STDERR: ^~~~
  810. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_self_wrong_type.carbon:[[@LINE-7]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  811. // CHECK:STDERR: fn F(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I1 = (.K1.J1).I2) {}
  812. // CHECK:STDERR: ^
  813. // CHECK:STDERR:
  814. F(T);
  815. }
  816. // F's requirement I1 = I2 is not met, as I1 = {} and I2 is unspecified
  817. fn G2(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I1 = {}) {
  818. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_self_wrong_type.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I & J & K where .(K.K1) = .Self as J and .(J.J1) = .Self as I and .(I.I1) = {}` into type implementing `I & J & K where .(K.K1) = .Self as J and .(J.J1) = .Self as I and .(I.I1) = .(I.I2)` [ConversionFailureFacetToFacet]
  819. // CHECK:STDERR: F(T);
  820. // CHECK:STDERR: ^~~~
  821. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_self_wrong_type.carbon:[[@LINE-19]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  822. // CHECK:STDERR: fn F(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I1 = (.K1.J1).I2) {}
  823. // CHECK:STDERR: ^
  824. // CHECK:STDERR:
  825. F(T);
  826. }
  827. // F's requirement I1 = I2 is not met, as I2 = {} and I1 is unspecified
  828. fn G3(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I2 = {}) {
  829. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_self_wrong_type.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I & J & K where .(K.K1) = .Self as J and .(J.J1) = .Self as I and .(I.I2) = {}` into type implementing `I & J & K where .(K.K1) = .Self as J and .(J.J1) = .Self as I and .(I.I1) = .(I.I2)` [ConversionFailureFacetToFacet]
  830. // CHECK:STDERR: F(T);
  831. // CHECK:STDERR: ^~~~
  832. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_self_wrong_type.carbon:[[@LINE-31]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  833. // CHECK:STDERR: fn F(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I1 = (.K1.J1).I2) {}
  834. // CHECK:STDERR: ^
  835. // CHECK:STDERR:
  836. F(T);
  837. }
  838. // --- fail_todo_rewrite_requires_subst_in_nested_access_of_other.carbon
  839. library "[[@TEST_NAME]]";
  840. interface I {
  841. let I1:! type;
  842. }
  843. interface J {
  844. let J1:! I;
  845. let J2:! type;
  846. }
  847. interface K {
  848. let K1:! J;
  849. }
  850. // .K1.J1 is an ImplWitnessAccess into Self. The Self witness will be subst'd in
  851. // for `U` and it will eval to the ImplWitnessAccess inside of `U.I1`. Then that
  852. // will need to be subst'd to find the value of I1 in U's witness.
  853. fn F(U:! I, T:! J & K where .J2 = (.K1.J1).I1) {}
  854. fn G(U:! I where .I1 = (), T:! J & K where .K1 = .Self and .J1 = U and .J2 = ()) {
  855. // CHECK:STDERR: fail_todo_rewrite_requires_subst_in_nested_access_of_other.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `J & K where .(J.J2) = () and .(K.K1) = .Self as J and .(J.J1) = U as I` into type implementing `J & K where .(J.J2) = .(K.K1).(J.J1).(I.I1)` [ConversionFailureFacetToFacet]
  856. // CHECK:STDERR: F(U, T);
  857. // CHECK:STDERR: ^~~~~~~
  858. // CHECK:STDERR: fail_todo_rewrite_requires_subst_in_nested_access_of_other.carbon:[[@LINE-6]]:13: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  859. // CHECK:STDERR: fn F(U:! I, T:! J & K where .J2 = (.K1.J1).I1) {}
  860. // CHECK:STDERR: ^
  861. // CHECK:STDERR:
  862. F(U, T);
  863. }
  864. // --- fail_rewrite_requires_subst_in_nested_access_of_other_wrong_type.carbon
  865. library "[[@TEST_NAME]]";
  866. interface I {
  867. let I1:! type;
  868. }
  869. interface J {
  870. let J1:! I;
  871. let J2:! type;
  872. }
  873. interface K {
  874. let K1:! J;
  875. }
  876. fn F(U:! I, T:! J & K where .J2 = (.K1.J1).I1) {}
  877. // F's requirement J2 = I1 is not met as J2 = () and I1 = {}
  878. fn G(U:! I where .I1 = {}, T:! J & K where .K1 = .Self and .J1 = U and .J2 = ()) {
  879. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_other_wrong_type.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `J & K where .(J.J2) = () and .(K.K1) = .Self as J and .(J.J1) = U as I` into type implementing `J & K where .(J.J2) = .(K.K1).(J.J1).(I.I1)` [ConversionFailureFacetToFacet]
  880. // CHECK:STDERR: F(U, T);
  881. // CHECK:STDERR: ^~~~~~~
  882. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_other_wrong_type.carbon:[[@LINE-7]]:13: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  883. // CHECK:STDERR: fn F(U:! I, T:! J & K where .J2 = (.K1.J1).I1) {}
  884. // CHECK:STDERR: ^
  885. // CHECK:STDERR:
  886. F(U, T);
  887. }
  888. // F's requirement J2 = I1 is not met as J2 = () and I1 is unspecified
  889. fn G2(U:! I, T:! J & K where .K1 = .Self and .J1 = U and .J2 = ()) {
  890. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_other_wrong_type.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `J & K where .(J.J2) = () and .(K.K1) = .Self as J and .(J.J1) = U` into type implementing `J & K where .(J.J2) = .(K.K1).(J.J1).(I.I1)` [ConversionFailureFacetToFacet]
  891. // CHECK:STDERR: F(U, T);
  892. // CHECK:STDERR: ^~~~~~~
  893. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_other_wrong_type.carbon:[[@LINE-19]]:13: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  894. // CHECK:STDERR: fn F(U:! I, T:! J & K where .J2 = (.K1.J1).I1) {}
  895. // CHECK:STDERR: ^
  896. // CHECK:STDERR:
  897. F(U, T);
  898. }
  899. // F's requirement J2 = I1 is not met as I1 = {} and J2 is unspecified
  900. fn G3(U:! I where .I1 = {}, T:! J & K where .K1 = .Self and .J1 = U) {
  901. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_other_wrong_type.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `J & K where .(K.K1) = .Self as J and .(J.J1) = U as I` into type implementing `J & K where .(J.J2) = .(K.K1).(J.J1).(I.I1)` [ConversionFailureFacetToFacet]
  902. // CHECK:STDERR: F(U, T);
  903. // CHECK:STDERR: ^~~~~~~
  904. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_other_wrong_type.carbon:[[@LINE-31]]:13: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  905. // CHECK:STDERR: fn F(U:! I, T:! J & K where .J2 = (.K1.J1).I1) {}
  906. // CHECK:STDERR: ^
  907. // CHECK:STDERR:
  908. F(U, T);
  909. }
  910. // --- rewrite_requires_subst_in_nested_access_of_other_with_two_witnesses.carbon
  911. library "[[@TEST_NAME]]";
  912. interface I {
  913. let I1:! type;
  914. }
  915. interface J {
  916. let J1:! type;
  917. }
  918. interface K {
  919. let K1:! J & I;
  920. let K2:! type;
  921. let K3:! type;
  922. }
  923. fn F(U:! I & J, T:! K where .K2 = .K1.I1 and .K3 = .K1.J1) {}
  924. fn G(U:! I & J where .I1 = () and .J1 = {}, T:! K where .K1 = U and .K2 = () and .K3 = {}) {
  925. F(U, T);
  926. }
  927. // --- fail_rewrite_requires_subst_in_nested_access_of_other_with_two_witnesses_wrong_type.carbon
  928. library "[[@TEST_NAME]]";
  929. interface I {
  930. let I1:! type;
  931. }
  932. interface J {
  933. let J1:! type;
  934. }
  935. interface K {
  936. let K1:! J & I;
  937. let K2:! type;
  938. let K3:! type;
  939. }
  940. fn F(U:! I & J, T:! K where .K2 = .K1.I1 and .K3 = .K1.J1) {}
  941. // K2 = I1 fails since K2 = {} and I1 = ()
  942. fn G(U:! I & J where .I1 = () and .J1 = {}, T:! K where .K1 = U and .K2 = {} and .K3 = {}) {
  943. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_other_with_two_witnesses_wrong_type.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `K where .(K.K2) = {} and .(K.K1) = U as I & J and .(K.K3) = {}` into type implementing `K where .(K.K2) = .(K.K1).(I.I1) and .(K.K3) = .(K.K1).(J.J1)` [ConversionFailureFacetToFacet]
  944. // CHECK:STDERR: F(U, T);
  945. // CHECK:STDERR: ^~~~~~~
  946. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_other_with_two_witnesses_wrong_type.carbon:[[@LINE-7]]:17: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  947. // CHECK:STDERR: fn F(U:! I & J, T:! K where .K2 = .K1.I1 and .K3 = .K1.J1) {}
  948. // CHECK:STDERR: ^
  949. // CHECK:STDERR:
  950. F(U, T);
  951. }
  952. // K2 = I1 fails since I1 = () and K2 is unspecified.
  953. fn G2(U:! I & J where .I1 = () and .J1 = {}, T:! K where .K1 = U and .K3 = {}) {
  954. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_other_with_two_witnesses_wrong_type.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `K where .(K.K1) = U as I & J and .(K.K3) = {}` into type implementing `K where .(K.K2) = .(K.K1).(I.I1) and .(K.K3) = .(K.K1).(J.J1)` [ConversionFailureFacetToFacet]
  955. // CHECK:STDERR: F(U, T);
  956. // CHECK:STDERR: ^~~~~~~
  957. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_other_with_two_witnesses_wrong_type.carbon:[[@LINE-19]]:17: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  958. // CHECK:STDERR: fn F(U:! I & J, T:! K where .K2 = .K1.I1 and .K3 = .K1.J1) {}
  959. // CHECK:STDERR: ^
  960. // CHECK:STDERR:
  961. F(U, T);
  962. }
  963. // K2 = I1 fails since K2 = () and I1 is unspecified.
  964. fn G3(U:! I & J where .J1 = {}, T:! K where .K1 = U and .K2 = () and .K3 = {}) {
  965. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_other_with_two_witnesses_wrong_type.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `K where .(K.K2) = () and .(K.K1) = U as I & J and .(K.K3) = {}` into type implementing `K where .(K.K2) = .(K.K1).(I.I1) and .(K.K3) = .(K.K1).(J.J1)` [ConversionFailureFacetToFacet]
  966. // CHECK:STDERR: F(U, T);
  967. // CHECK:STDERR: ^~~~~~~
  968. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_other_with_two_witnesses_wrong_type.carbon:[[@LINE-31]]:17: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  969. // CHECK:STDERR: fn F(U:! I & J, T:! K where .K2 = .K1.I1 and .K3 = .K1.J1) {}
  970. // CHECK:STDERR: ^
  971. // CHECK:STDERR:
  972. F(U, T);
  973. }
  974. // K3 = J1 fails since J1 = {} and K3 is unspecified.
  975. fn G4(U:! I & J where .I1 = () and .J1 = {}, T:! K where .K1 = U and .K2 = ()) {
  976. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_other_with_two_witnesses_wrong_type.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `K where .(K.K2) = () and .(K.K1) = U as I & J` into type implementing `K where .(K.K2) = .(K.K1).(I.I1) and .(K.K3) = .(K.K1).(J.J1)` [ConversionFailureFacetToFacet]
  977. // CHECK:STDERR: F(U, T);
  978. // CHECK:STDERR: ^~~~~~~
  979. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_other_with_two_witnesses_wrong_type.carbon:[[@LINE-43]]:17: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  980. // CHECK:STDERR: fn F(U:! I & J, T:! K where .K2 = .K1.I1 and .K3 = .K1.J1) {}
  981. // CHECK:STDERR: ^
  982. // CHECK:STDERR:
  983. F(U, T);
  984. }
  985. // K3 = J1 fails since K3 = {} and J1 is unspecified.
  986. fn G5(U:! I & J where .I1 = (), T:! K where .K1 = U and .K2 = () and .K3 = {}) {
  987. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_other_with_two_witnesses_wrong_type.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `K where .(K.K2) = () and .(K.K1) = U as I & J and .(K.K3) = {}` into type implementing `K where .(K.K2) = .(K.K1).(I.I1) and .(K.K3) = .(K.K1).(J.J1)` [ConversionFailureFacetToFacet]
  988. // CHECK:STDERR: F(U, T);
  989. // CHECK:STDERR: ^~~~~~~
  990. // CHECK:STDERR: fail_rewrite_requires_subst_in_nested_access_of_other_with_two_witnesses_wrong_type.carbon:[[@LINE-55]]:17: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  991. // CHECK:STDERR: fn F(U:! I & J, T:! K where .K2 = .K1.I1 and .K3 = .K1.J1) {}
  992. // CHECK:STDERR: ^
  993. // CHECK:STDERR:
  994. F(U, T);
  995. }
  996. // --- fail_target_rewrites_dont_apply_to_source.carbon
  997. library "[[@TEST_NAME]]";
  998. interface I {
  999. let I1:! type;
  1000. let I2:! type;
  1001. let I3:! type;
  1002. let I4:! type;
  1003. }
  1004. fn F(T:! I where .I1 = () and .I2 = ()) {}
  1005. fn G(T:! I where .I1 = .I2) {
  1006. // CHECK:STDERR: fail_target_rewrites_dont_apply_to_source.carbon:[[@LINE+7]]:3: error: cannot convert type `T` that implements `I where .(I.I1) = .(I.I2)` into type implementing `I where .(I.I1) = () and .(I.I2) = ()` [ConversionFailureFacetToFacet]
  1007. // CHECK:STDERR: F(T);
  1008. // CHECK:STDERR: ^~~~
  1009. // CHECK:STDERR: fail_target_rewrites_dont_apply_to_source.carbon:[[@LINE-6]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  1010. // CHECK:STDERR: fn F(T:! I where .I1 = () and .I2 = ()) {}
  1011. // CHECK:STDERR: ^
  1012. // CHECK:STDERR:
  1013. F(T);
  1014. }
  1015. // --- nested_facet_type_used_as_root_facet_type.carbon
  1016. library "[[@TEST_NAME]]";
  1017. interface I {
  1018. let X:! type;
  1019. let Y:! type;
  1020. }
  1021. fn F(T:! I where .X = (I where .Y = {}), U:! T.X) {}
  1022. fn G(T:! I where .X = (I where .Y = {}), U:! I where .Y = {}) {
  1023. F(T, U);
  1024. }