facet_assoc_const.carbon 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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/facet_assoc_const.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/facet/facet_assoc_const.carbon
  12. // --- success.carbon
  13. library "[[@TEST_NAME]]";
  14. interface I { let T:! type; }
  15. fn F(T:! I where .T = {}) {}
  16. // --- success_associated.carbon
  17. library "[[@TEST_NAME]]";
  18. interface I { let T:! type; let U:! type; }
  19. fn F(T:! I where .T = .U) {}
  20. // --- fail_two_different.carbon
  21. library "[[@TEST_NAME]]";
  22. interface L { let W:! type; }
  23. // CHECK:STDERR: fail_two_different.carbon:[[@LINE+4]]:10: error: associated constant `.(L.W)` given two different values `{}` and `()` [AssociatedConstantWithDifferentValues]
  24. // CHECK:STDERR: fn F(T:! L where .W = {} and .W = ()) {}
  25. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  26. // CHECK:STDERR:
  27. fn F(T:! L where .W = {} and .W = ()) {}
  28. // --- fail_two_different_first_associated.carbon
  29. library "[[@TEST_NAME]]";
  30. interface L { let W:! type; let X:! type; }
  31. // CHECK:STDERR: fail_two_different_first_associated.carbon:[[@LINE+4]]:10: error: associated constant `.(L.W)` given two different values `.(L.X)` and `()` [AssociatedConstantWithDifferentValues]
  32. // CHECK:STDERR: fn F(T:! L where .W = .X and .W = ()) {}
  33. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  34. // CHECK:STDERR:
  35. fn F(T:! L where .W = .X and .W = ()) {}
  36. // --- fail_two_different_second_associated.carbon
  37. library "[[@TEST_NAME]]";
  38. interface L { let W:! type; let X:! type; }
  39. // CHECK:STDERR: fail_two_different_second_associated.carbon:[[@LINE+4]]:10: error: associated constant `.(L.W)` given two different values `()` and `.(L.X)` [AssociatedConstantWithDifferentValues]
  40. // CHECK:STDERR: fn F(T:! L where .W = () and .W = .X) {}
  41. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  42. // CHECK:STDERR:
  43. fn F(T:! L where .W = () and .W = .X) {}
  44. // --- fail_two_different_first_bad.carbon
  45. library "[[@TEST_NAME]]";
  46. interface L { let W:! type; }
  47. // CHECK:STDERR: fail_two_different_first_bad.carbon:[[@LINE+4]]:23: error: name `BAD5` not found [NameNotFound]
  48. // CHECK:STDERR: fn F(T:! L where .W = BAD5 and .W = ()) {}
  49. // CHECK:STDERR: ^~~~
  50. // CHECK:STDERR:
  51. fn F(T:! L where .W = BAD5 and .W = ()) {}
  52. // --- fail_two_different_second_bad.carbon
  53. library "[[@TEST_NAME]]";
  54. interface L { let W:! type; }
  55. // CHECK:STDERR: fail_two_different_second_bad.carbon:[[@LINE+4]]:35: error: name `BAD6` not found [NameNotFound]
  56. // CHECK:STDERR: fn F(T:! L where .W = {} and .W = BAD6) {}
  57. // CHECK:STDERR: ^~~~
  58. // CHECK:STDERR:
  59. fn F(T:! L where .W = {} and .W = BAD6) {}
  60. // --- fail_two_different_both_bad.carbon
  61. library "[[@TEST_NAME]]";
  62. interface L { let W:! type; }
  63. // CHECK:STDERR: fail_two_different_both_bad.carbon:[[@LINE+8]]:23: error: name `BAD7` not found [NameNotFound]
  64. // CHECK:STDERR: fn F(T:! L where .W = BAD7 and .W = BAD8) {}
  65. // CHECK:STDERR: ^~~~
  66. // CHECK:STDERR:
  67. // CHECK:STDERR: fail_two_different_both_bad.carbon:[[@LINE+4]]:37: error: name `BAD8` not found [NameNotFound]
  68. // CHECK:STDERR: fn F(T:! L where .W = BAD7 and .W = BAD8) {}
  69. // CHECK:STDERR: ^~~~
  70. // CHECK:STDERR:
  71. fn F(T:! L where .W = BAD7 and .W = BAD8) {}
  72. // --- fail_two_different_combined_from_bitand.carbon
  73. library "[[@TEST_NAME]]";
  74. interface L { let W:! type; }
  75. // CHECK:STDERR: fail_two_different_combined_from_bitand.carbon:[[@LINE+4]]:10: error: associated constant `.(L.W)` given two different values `{}` and `()` [AssociatedConstantWithDifferentValues]
  76. // CHECK:STDERR: fn F(T:! (L where .W = {}) & (L where .W = ())) {}
  77. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  78. // CHECK:STDERR:
  79. fn F(T:! (L where .W = {}) & (L where .W = ())) {}
  80. // --- two_different_combined_from_impl_and_facet.carbon
  81. library "[[@TEST_NAME]]";
  82. interface L { let W:! type; }
  83. interface M {}
  84. impl forall [T:! M] T as L where .W = () {}
  85. fn F(T:! M & (L where .W = {})) {}
  86. class C;
  87. impl C as L where .W = {} {}
  88. impl C as M {}
  89. fn G() {
  90. F(C);
  91. }
  92. // --- fail_two_different_combined_from_final_impl_and_facet.carbon
  93. library "[[@TEST_NAME]]";
  94. interface L { let W:! type; }
  95. interface M {}
  96. final impl forall [T:! M] T as L where .W = () {}
  97. fn G(T:! M & L, a: T.W) -> () { return a; }
  98. fn H(T:! L where .W = {}, a: T.W) -> {} { return a; }
  99. fn F(T:! M & (L where .W = {}), a: T.W) {
  100. // One of `b` or `c` must fail, because `T.W` is either found to be `()` from
  101. // the impl or `{}` from the facet type of T.
  102. let b: () = G(T, a);
  103. // TODO: This diagnostic sucks. Can we make the facet type's value take
  104. // precidence over final, since that's what is written in the code and more
  105. // likely to show up in diagnostics? Or should we diagnose `T` as being
  106. // invalid directly, where we can see both `.W` values and print them?
  107. //
  108. // CHECK:STDERR: fail_two_different_combined_from_final_impl_and_facet.carbon:[[@LINE+7]]:15: error: cannot convert type `T` that implements `L & M where .(L.W) = {}` into type implementing `L where .(L.W) = {}` [ConversionFailureFacetToFacet]
  109. // CHECK:STDERR: let c: {} = H(T, a);
  110. // CHECK:STDERR: ^~~~~~~
  111. // CHECK:STDERR: fail_two_different_combined_from_final_impl_and_facet.carbon:[[@LINE-15]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  112. // CHECK:STDERR: fn H(T:! L where .W = {}, a: T.W) -> {} { return a; }
  113. // CHECK:STDERR: ^
  114. // CHECK:STDERR:
  115. let c: {} = H(T, a);
  116. }
  117. // --- fail_many_different.carbon
  118. library "[[@TEST_NAME]]";
  119. interface L { let W:! type; }
  120. // CHECK:STDERR: fail_many_different.carbon:[[@LINE+4]]:10: error: associated constant `.(L.W)` given two different values `((), (), ())` and `({}, (), ())` [AssociatedConstantWithDifferentValues]
  121. // CHECK:STDERR: fn G(T:! L where .W = ((), (), ()) and .W = ({}, (), ()) and .W = ({}, {}, ()) and .W = ({}, (), {})) {}
  122. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  123. // CHECK:STDERR:
  124. fn G(T:! L where .W = ((), (), ()) and .W = ({}, (), ()) and .W = ({}, {}, ()) and .W = ({}, (), {})) {}
  125. // --- rewrite_uses_second_facet.carbon
  126. library "[[@TEST_NAME]]";
  127. interface M { let X:! type; let Y:! type; }
  128. fn F(T:! M where .X = (), U:! M where .Y = T.X) -> U.Y {
  129. return ();
  130. }
  131. // --- fail_rewrite_conflicts_with_second_facet.carbon
  132. library "[[@TEST_NAME]]";
  133. interface M { let X:! type; let Y:! type; }
  134. // CHECK:STDERR: fail_rewrite_conflicts_with_second_facet.carbon:[[@LINE+4]]:31: error: associated constant `.(M.Y)` given two different values `T.(M.X)` and `.(M.X)` [AssociatedConstantWithDifferentValues]
  135. // CHECK:STDERR: fn F(T:! M where .X = (), U:! M where .Y = T.X and .Y = .X) {}
  136. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  137. // CHECK:STDERR:
  138. fn F(T:! M where .X = (), U:! M where .Y = T.X and .Y = .X) {}
  139. // --- repeated.carbon
  140. library "[[@TEST_NAME]]";
  141. interface M { let X:! type; }
  142. fn F(T:! M where .X = {} and .X = {}) {}
  143. fn G(T:! M where .X = {}) {
  144. F(T);
  145. }
  146. // --- repeated_associated.carbon
  147. library "[[@TEST_NAME]]";
  148. interface M { let X:! type; let Y:! type; }
  149. fn F(T:! M where .X = .Y and .X = .Y) {}
  150. fn G(T:! M where .X = () and .Y = ()) {
  151. F(T);
  152. }
  153. // --- repeated_concrete_value_and_associated.carbon
  154. library "[[@TEST_NAME]]";
  155. interface M { let X:! type; let Y:! type; }
  156. fn F1(T:! M where .X = () and .Y = .X and .X = .Y) {}
  157. fn F2(T:! M where .X = () and .X = .X) {}
  158. fn G(T:! M where .X = () and .Y = ()) {
  159. F1(T);
  160. F2(T);
  161. }
  162. // --- repeated_with_bitand.carbon
  163. library "[[@TEST_NAME]]";
  164. interface M { let X:! type; let Y:! type; }
  165. fn F1(T:! (M where .X = .Y) & (M where .X = .Y and .Y = ())) -> T.X {
  166. return ();
  167. }
  168. fn F2(T:! (M where .X = .Y and .Y = ()) & (M where .X = .Y)) -> T.X {
  169. return ();
  170. }
  171. // --- fail_repeated_and_different.carbon
  172. library "[[@TEST_NAME]]";
  173. interface M { let X:! type; }
  174. // CHECK:STDERR: fail_repeated_and_different.carbon:[[@LINE+4]]:10: error: associated constant `.(M.X)` given two different values `{}` and `()` [AssociatedConstantWithDifferentValues]
  175. // CHECK:STDERR: fn F(T:! M where .X = {} and .X = () and .X = {}) {}
  176. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  177. // CHECK:STDERR:
  178. fn F(T:! M where .X = {} and .X = () and .X = {}) {}
  179. // --- fail_cycle_single.carbon
  180. library "[[@TEST_NAME]]";
  181. interface M { let X:! type; }
  182. // This fails because it resolves to `.X = .X` which is cyclical.
  183. //
  184. // CHECK:STDERR: fail_cycle_single.carbon:[[@LINE+4]]:10: error: found cycle in facet type constraint for `.(M.X)` [FacetTypeConstraintCycle]
  185. // CHECK:STDERR: fn F(T:! M where .X = .X) {}
  186. // CHECK:STDERR: ^~~~~~~~~~~~~~~
  187. // CHECK:STDERR:
  188. fn F(T:! M where .X = .X) {}
  189. // Even though `.X = ()` is specified, the rewrites are resolved left to right
  190. // and a cycle `.X = .X` is found first.
  191. //
  192. // CHECK:STDERR: fail_cycle_single.carbon:[[@LINE+4]]:10: error: found cycle in facet type constraint for `.(M.X)` [FacetTypeConstraintCycle]
  193. // CHECK:STDERR: fn G(T:! M where .X = .X and .X = ()) {}
  194. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  195. // CHECK:STDERR:
  196. fn G(T:! M where .X = .X and .X = ()) {}
  197. // --- fail_cycle.carbon
  198. library "[[@TEST_NAME]]";
  199. interface M { let X:! type; let Y:! type; let Z:! type; }
  200. // This fails because it resolves to `.X = .X` which is cyclical.
  201. // The value of .X and .Y becomes <error> but .Z is still valid.
  202. //
  203. //@dump-sem-ir-begin
  204. // CHECK:STDERR: fail_cycle.carbon:[[@LINE+4]]:10: error: found cycle in facet type constraint for `.(M.Y)` [FacetTypeConstraintCycle]
  205. // CHECK:STDERR: fn F(T:! M where .X = .Y and .Y = .X and .Z = ()) {}
  206. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  207. // CHECK:STDERR:
  208. fn F(T:! M where .X = .Y and .Y = .X and .Z = ()) {}
  209. //@dump-sem-ir-end
  210. // --- fail_cycle_between_interfaces.carbon
  211. library "[[@TEST_NAME]]";
  212. interface I {
  213. let X1:! type;
  214. let X2:! type;
  215. }
  216. interface J {
  217. let X3:! type;
  218. }
  219. // This fails because it resolves to `.X1 = .X1` which is cyclical.
  220. //
  221. // CHECK:STDERR: fail_cycle_between_interfaces.carbon:[[@LINE+4]]:10: error: found cycle in facet type constraint for `.(J.X3)` [FacetTypeConstraintCycle]
  222. // CHECK:STDERR: fn G(T:! I & J where .X1 = .X3 and .X2 = .X1 and .X3 = .X2) {}
  223. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  224. // CHECK:STDERR:
  225. fn G(T:! I & J where .X1 = .X3 and .X2 = .X1 and .X3 = .X2) {}
  226. // --- fail_indirect_cycle.carbon
  227. library "[[@TEST_NAME]]";
  228. interface I {
  229. let X1:! type;
  230. let X2:! type;
  231. }
  232. // This fails because it resolves to `.X1 = .X1**` which is cyclical.
  233. //
  234. // CHECK:STDERR: fail_indirect_cycle.carbon:[[@LINE+4]]:10: error: found cycle in facet type constraint for `.(I.X2)` [FacetTypeConstraintCycle]
  235. // CHECK:STDERR: fn F(T:! I where .X1 = .X2* and .X2 = .X1*);
  236. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  237. // CHECK:STDERR:
  238. fn F(T:! I where .X1 = .X2* and .X2 = .X1*);
  239. class C(T:! type);
  240. // This fails because it resolves to `.X1 = C(C(.X1))` which is cyclical.
  241. //
  242. // CHECK:STDERR: fail_indirect_cycle.carbon:[[@LINE+4]]:10: error: found cycle in facet type constraint for `.(I.X2)` [FacetTypeConstraintCycle]
  243. // CHECK:STDERR: fn G(T:! I where .X1 = C(.X2) and .X2 = C(.X1));
  244. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  245. // CHECK:STDERR:
  246. fn G(T:! I where .X1 = C(.X2) and .X2 = C(.X1));
  247. // --- fail_complex_indirect_cycle.carbon
  248. library "[[@TEST_NAME]]";
  249. interface I {
  250. let X1:! type;
  251. let X2:! type;
  252. let X3:! type;
  253. }
  254. class C(T:! type, U:! type);
  255. // This fails because it resolves to `.X1 = C(C(.X3, .X1), .X3)` which is
  256. // cyclical.
  257. //
  258. // CHECK:STDERR: fail_complex_indirect_cycle.carbon:[[@LINE+4]]:10: error: found cycle in facet type constraint for `.(I.X2)` [FacetTypeConstraintCycle]
  259. // CHECK:STDERR: fn F(T:! I where .X1 = C(.X2, .X3) and .X2 = C(.X3, .X1));
  260. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  261. // CHECK:STDERR:
  262. fn F(T:! I where .X1 = C(.X2, .X3) and .X2 = C(.X3, .X1));
  263. // --- exponential_large.carbon
  264. library "[[@TEST_NAME]]";
  265. interface Z {
  266. let T0:! type;
  267. let T1:! type;
  268. let T2:! type;
  269. let T3:! type;
  270. let T4:! type;
  271. let T5:! type;
  272. let T6:! type;
  273. let T7:! type;
  274. let T8:! type;
  275. let T9:! type;
  276. }
  277. // A naive attempt to resolve the rewrite rules will run take minutes to
  278. // complete, since the resulting RHS values are exponential in size, and a naive
  279. // approach can recursively rebuild the RHS values from the ground up
  280. // repeatedly.
  281. fn F(
  282. T:! Z where
  283. .T0 = (.T1, .T1, .T1, .T1, .T1, .T1, .T1, .T1, .T1, .T1, .T1, .T1) and
  284. .T1 = (.T2, .T2, .T2, .T2, .T2, .T2, .T2, .T2, .T2, .T2, .T2, .T2) and
  285. .T2 = (.T3, .T3, .T3, .T3, .T3, .T3, .T3, .T3, .T3, .T3, .T3, .T3) and
  286. .T3 = (.T4, .T4, .T4, .T4, .T4, .T4, .T4, .T4, .T4, .T4, .T4, .T4) and
  287. .T4 = (.T5, .T5, .T5, .T5, .T5, .T5, .T5, .T5, .T5, .T5, .T5, .T5) and
  288. .T5 = (.T6, .T6, .T6, .T6, .T6, .T6, .T6, .T6, .T6, .T6, .T6, .T6) and
  289. .T6 = (.T7, .T7, .T7, .T7, .T7, .T7, .T7, .T7, .T7, .T7, .T7, .T7) and
  290. .T7 = (.T8, .T8, .T8, .T8, .T8, .T8, .T8, .T8, .T8, .T8, .T8, .T8) and
  291. .T8 = (.T9, .T9, .T9, .T9, .T9, .T9, .T9, .T9, .T9, .T9, .T9, .T9) and
  292. .T9 = ()
  293. );
  294. // --- fail_exponential_large_cycle.carbon
  295. library "[[@TEST_NAME]]";
  296. interface Z {
  297. let T0:! type;
  298. let T1:! type;
  299. let T2:! type;
  300. let T3:! type;
  301. let T4:! type;
  302. let T5:! type;
  303. let T6:! type;
  304. let T7:! type;
  305. let T8:! type;
  306. let T9:! type;
  307. }
  308. // A naive attempt to resolve the rewrite rules will run take minutes to
  309. // complete, since the resulting RHS values are exponential in size, and a naive
  310. // approach can recursively rebuild the RHS values from the ground up
  311. // repeatedly.
  312. fn F(
  313. // CHECK:STDERR: fail_exponential_large_cycle.carbon:[[@LINE+4]]:9: error: found cycle in facet type constraint for `.(Z.T0)` [FacetTypeConstraintCycle]
  314. // CHECK:STDERR: T:! Z where
  315. // CHECK:STDERR: ^~~~~~~
  316. // CHECK:STDERR:
  317. T:! Z where
  318. .T9 = .T0 and
  319. .T8 = (.T9, .T9, .T9, .T9, .T9, .T9, .T9, .T9, .T9, .T9, .T9, .T9) and
  320. .T7 = (.T8, .T8, .T8, .T8, .T8, .T8, .T8, .T8, .T8, .T8, .T8, .T8) and
  321. .T6 = (.T7, .T7, .T7, .T7, .T7, .T7, .T7, .T7, .T7, .T7, .T7, .T7) and
  322. .T5 = (.T6, .T6, .T6, .T6, .T6, .T6, .T6, .T6, .T6, .T6, .T6, .T6) and
  323. .T4 = (.T5, .T5, .T5, .T5, .T5, .T5, .T5, .T5, .T5, .T5, .T5, .T5) and
  324. .T3 = (.T4, .T4, .T4, .T4, .T4, .T4, .T4, .T4, .T4, .T4, .T4, .T4) and
  325. .T2 = (.T3, .T3, .T3, .T3, .T3, .T3, .T3, .T3, .T3, .T3, .T3, .T3) and
  326. .T1 = (.T2, .T2, .T2, .T2, .T2, .T2, .T2, .T2, .T2, .T2, .T2, .T2) and
  327. .T0 = (.T1, .T1, .T1, .T1, .T1, .T1, .T1, .T1, .T1, .T1, .T1, .T1)
  328. );
  329. // --- non-type.carbon
  330. library "[[@TEST_NAME]]";
  331. interface N {
  332. let Y:! {.a: {}};
  333. }
  334. fn F(T:! N where .Y = {.a = {}}) { }
  335. // --- non-type_repeated.carbon
  336. library "[[@TEST_NAME]]";
  337. interface N {
  338. let Y:! {.a: {}};
  339. }
  340. fn F(T:! N where .Y = {.a = {}} and .Y = {.a = {}}) { }
  341. // --- fail_non-type_different.carbon
  342. library "[[@TEST_NAME]]";
  343. interface N {
  344. let Y:! {.a: type};
  345. }
  346. // CHECK:STDERR: fail_non-type_different.carbon:[[@LINE+4]]:10: error: associated constant `.(N.Y)` given two different values `{.a = {}}` and `{.a = ()}` [AssociatedConstantWithDifferentValues]
  347. // CHECK:STDERR: fn F(T:! N where .Y = {.a = {}} and .Y = {.a = ()}) {}
  348. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  349. // CHECK:STDERR:
  350. fn F(T:! N where .Y = {.a = {}} and .Y = {.a = ()}) {}
  351. // --- self_repeated_explicitly.carbon
  352. library "[[@TEST_NAME]]";
  353. interface N {
  354. let Y1:! type;
  355. let Y2:! type;
  356. }
  357. fn F(T:! N where .Y2 = .Y1 and .Y2 = .Self.Y1) { }
  358. // --- self_repeated_explicitly_with_value.carbon
  359. library "[[@TEST_NAME]]";
  360. interface N {
  361. let Y1:! type;
  362. let Y2:! type;
  363. }
  364. fn F(T:! N where .Y1 = () and .Y2 = .Y1 and .Y2 = .Self.Y1) { }
  365. // --- fail_todo_cycle_through_self_reference.carbon
  366. library "[[@TEST_NAME]]";
  367. interface Z {
  368. let T:! type;
  369. // TODO: This should not be an error.
  370. //
  371. // CHECK:STDERR: fail_todo_cycle_through_self_reference.carbon:[[@LINE+7]]:11: error: associated constant has incomplete type `Z` [IncompleteTypeInAssociatedConstantDecl]
  372. // CHECK:STDERR: let U:! Z;
  373. // CHECK:STDERR: ^
  374. // CHECK:STDERR: fail_todo_cycle_through_self_reference.carbon:[[@LINE-7]]:1: note: interface is currently being defined [InterfaceIncompleteWithinDefinition]
  375. // CHECK:STDERR: interface Z {
  376. // CHECK:STDERR: ^~~~~~~~~~~~~
  377. // CHECK:STDERR:
  378. let U:! Z;
  379. }
  380. // TODO: Should be diagnosed as a cycle.
  381. fn F(A:! Z where .T = .U.T and .U = .Self) {}
  382. // --- fail_todo_reference_same_constant_in_different_self.carbon
  383. library "[[@TEST_NAME]]";
  384. interface Z {
  385. let T:! type;
  386. // TODO: This should not be an error.
  387. //
  388. // CHECK:STDERR: fail_todo_reference_same_constant_in_different_self.carbon:[[@LINE+7]]:11: error: associated constant has incomplete type `Z` [IncompleteTypeInAssociatedConstantDecl]
  389. // CHECK:STDERR: let U:! Z;
  390. // CHECK:STDERR: ^
  391. // CHECK:STDERR: fail_todo_reference_same_constant_in_different_self.carbon:[[@LINE-7]]:1: note: interface is currently being defined [InterfaceIncompleteWithinDefinition]
  392. // CHECK:STDERR: interface Z {
  393. // CHECK:STDERR: ^~~~~~~~~~~~~
  394. // CHECK:STDERR:
  395. let U:! Z;
  396. }
  397. // TODO: Should not be diagnosed as a cycle, once the incorrect failure above is
  398. // fixed.
  399. fn F(A:! Z where .T = (), B:! Z where .T = .U.T and .U = A) {}
  400. // --- fail_todo_non_cycle_with_self_reference.carbon
  401. library "[[@TEST_NAME]]";
  402. interface Z {
  403. // TODO: This should not be an error.
  404. //
  405. // CHECK:STDERR: fail_todo_non_cycle_with_self_reference.carbon:[[@LINE+7]]:11: error: associated constant has incomplete type `Z` [IncompleteTypeInAssociatedConstantDecl]
  406. // CHECK:STDERR: let T:! Z;
  407. // CHECK:STDERR: ^
  408. // CHECK:STDERR: fail_todo_non_cycle_with_self_reference.carbon:[[@LINE-6]]:1: note: interface is currently being defined [InterfaceIncompleteWithinDefinition]
  409. // CHECK:STDERR: interface Z {
  410. // CHECK:STDERR: ^~~~~~~~~~~~~
  411. // CHECK:STDERR:
  412. let T:! Z;
  413. let U:! type;
  414. // TODO: This should not be an error.
  415. //
  416. // CHECK:STDERR: fail_todo_non_cycle_with_self_reference.carbon:[[@LINE+7]]:11: error: associated constant has incomplete type `Z` [IncompleteTypeInAssociatedConstantDecl]
  417. // CHECK:STDERR: let V:! Z;
  418. // CHECK:STDERR: ^
  419. // CHECK:STDERR: fail_todo_non_cycle_with_self_reference.carbon:[[@LINE-17]]:1: note: interface is currently being defined [InterfaceIncompleteWithinDefinition]
  420. // CHECK:STDERR: interface Z {
  421. // CHECK:STDERR: ^~~~~~~~~~~~~
  422. // CHECK:STDERR:
  423. let V:! Z;
  424. }
  425. // TODO: Should not be diagnosed as a cycle, once the incorrect failure above is
  426. // fixed.
  427. fn F(A:! Z where .T = .V.U and .V = .Self and .U = ()) -> A.T {
  428. return ();
  429. }
  430. // --- fail_cycle_with_unrelated_associated_constant.carbon
  431. library "[[@TEST_NAME]]";
  432. interface Z {
  433. let T0:! type;
  434. let T1:! type;
  435. let T2:! type;
  436. let T3:! type;
  437. }
  438. // CHECK:STDERR: fail_cycle_with_unrelated_associated_constant.carbon:[[@LINE+4]]:10: error: found cycle in facet type constraint for `.(Z.T1)` [FacetTypeConstraintCycle]
  439. // CHECK:STDERR: fn F(T:! Z where .T0 = .T1 and .T1 = .T0 and .T2 = .T3) {}
  440. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  441. // CHECK:STDERR:
  442. fn F(T:! Z where .T0 = .T1 and .T1 = .T0 and .T2 = .T3) {}
  443. // --- fail_cycle_with_branching_in_rhs.carbon
  444. library "[[@TEST_NAME]]";
  445. interface Z {
  446. let T0:! type;
  447. let T1:! type;
  448. let T2:! type;
  449. let T3:! type;
  450. let T4:! type;
  451. }
  452. // TODO: There should only be one diagnostic here.
  453. //
  454. // CHECK:STDERR: fail_cycle_with_branching_in_rhs.carbon:[[@LINE+4]]:10: error: found cycle in facet type constraint for `.(Z.T3)` [FacetTypeConstraintCycle]
  455. // CHECK:STDERR: fn F(T:! Z where .T0 = .T1 and .T1 = (.T2, .T3) and .T2 = .T4 and .T3 = .T1) {}
  456. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  457. // CHECK:STDERR:
  458. fn F(T:! Z where .T0 = .T1 and .T1 = (.T2, .T3) and .T2 = .T4 and .T3 = .T1) {}
  459. // CHECK:STDERR: fail_cycle_with_branching_in_rhs.carbon:[[@LINE+4]]:10: error: found cycle in facet type constraint for `.(Z.T1)` [FacetTypeConstraintCycle]
  460. // CHECK:STDERR: fn G(T:! Z where .T0 = .T1 and .T1 = (.T2, .T3) and .T2 = .T4 and .T3 = .T0) {}
  461. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  462. // CHECK:STDERR:
  463. fn G(T:! Z where .T0 = .T1 and .T1 = (.T2, .T3) and .T2 = .T4 and .T3 = .T0) {}
  464. // --- no_cycle_with_branching_in_rhs.carbon
  465. library "[[@TEST_NAME]]";
  466. interface Z {
  467. let T0:! type;
  468. let T1:! type;
  469. let T2:! type;
  470. let T3:! type;
  471. let T4:! type;
  472. let T5:! type;
  473. }
  474. // These create misdiagnostics if the resolving algorithms messes up tracking
  475. // its stack during replacements by leaving either of .T2 or .T3 on the stack
  476. // (from the RHS of .T1) while resolving the other. Or it can fail to apply the
  477. // () up the chain correctly.
  478. fn F(T:! Z where .T0 = .T1 and .T1 = (.T2, .T3) and .T2 = .T4 and .T4 = () and .T3 = .T2) -> T.T0 {
  479. return ((), ());
  480. }
  481. fn G(T:! Z where .T0 = .T1 and .T1 = (.T2, .T3) and .T2 = .T4 and .T4 = .T5 and .T5 = () and .T3 = .T2) -> T.T0 {
  482. return ((), ());
  483. }
  484. fn H(T:! Z where .T0 = .T1 and .T1 = (.T2, .T3) and .T2 = (.T4, ()) and .T3 = .T2 and .T4 = {}) -> T.T0 {
  485. return (({}, ()), ({}, ()));
  486. }
  487. fn I(T:! Z where .T0 = .T1 and .T1 = (.T2, .T3) and .T2 = .T4 and .T3 = .T2 and .T4 = ()) -> T.T0 {
  488. return ((), ());
  489. }
  490. fn J(T:! Z where .T0 = .T1 and .T1 = (.T2, .T3) and .T2 = .T4 and .T4 = .T5 and .T3 = .T2 and .T5 = ()) -> T.T0 {
  491. return ((), ());
  492. }
  493. // --- indirection_through_self_rhs.carbon
  494. library "[[@TEST_NAME]]";
  495. interface I {
  496. let I1:! type;
  497. let I2:! type;
  498. }
  499. interface J {
  500. let J1:! I;
  501. }
  502. // The value of .I1 is (), but to know that requires resolving .J1 first then
  503. // .J1.I2.
  504. fn F(T:! I & J where .J1 = .Self and .I1 = .J1.I2 and .I2 = ()) -> T.I1 {
  505. return ();
  506. }
  507. // --- indirection_through_not_self_rhs.carbon
  508. library "[[@TEST_NAME]]";
  509. interface I {
  510. let I1:! type;
  511. let I2:! type;
  512. }
  513. interface J {
  514. let J1:! I;
  515. }
  516. // The value of .I1 is (), but to know that requires resolving .J1 first then
  517. // .J1.I2.
  518. fn F(U:! I where .I2 = (), T:! I & J where .J1 = U and .I1 = .J1.I2) -> T.I1 {
  519. return ();
  520. }
  521. // --- indirection_through_unresolved_access_rhs.carbon
  522. library "[[@TEST_NAME]]";
  523. interface I {
  524. let I1:! type;
  525. let I2:! type;
  526. }
  527. interface J {
  528. let J1:! I;
  529. }
  530. // If we assume the nested `.J1` access will resolve to a facet value, we may
  531. // loop forever trying to resolve the `.I2` access. We should gracefully accept
  532. // that it does not resolve further.
  533. fn F(T:! I & J where .I1 = .J1.I2) {}
  534. // CHECK:STDOUT: --- fail_cycle.carbon
  535. // CHECK:STDOUT:
  536. // CHECK:STDOUT: constants {
  537. // CHECK:STDOUT: %M.type: type = facet_type <@M> [concrete]
  538. // CHECK:STDOUT: %M.assoc_type: type = assoc_entity_type @M [concrete]
  539. // CHECK:STDOUT: %assoc0: %M.assoc_type = assoc_entity element0, @M.%X [concrete]
  540. // CHECK:STDOUT: %assoc1: %M.assoc_type = assoc_entity element1, @M.%Y [concrete]
  541. // CHECK:STDOUT: %assoc2: %M.assoc_type = assoc_entity element2, @M.%Z [concrete]
  542. // CHECK:STDOUT: %.Self.1bb: %M.type = bind_symbolic_name .Self [symbolic_self]
  543. // CHECK:STDOUT: %.Self.as_type: type = facet_access_type %.Self.1bb [symbolic_self]
  544. // CHECK:STDOUT: %M.lookup_impl_witness: <witness> = lookup_impl_witness %.Self.1bb, @M [symbolic_self]
  545. // CHECK:STDOUT: %impl.elem0: type = impl_witness_access %M.lookup_impl_witness, element0 [symbolic_self]
  546. // CHECK:STDOUT: %impl.elem1: type = impl_witness_access %M.lookup_impl_witness, element1 [symbolic_self]
  547. // CHECK:STDOUT: %impl.elem2: type = impl_witness_access %M.lookup_impl_witness, element2 [symbolic_self]
  548. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  549. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  550. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  551. // CHECK:STDOUT: }
  552. // CHECK:STDOUT:
  553. // CHECK:STDOUT: imports {
  554. // CHECK:STDOUT: }
  555. // CHECK:STDOUT:
  556. // CHECK:STDOUT: file {
  557. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {
  558. // CHECK:STDOUT: %T.patt: <error> = symbolic_binding_pattern T, 0 [concrete]
  559. // CHECK:STDOUT: } {
  560. // CHECK:STDOUT: %.loc13_12.1: type = splice_block %.loc13_12.2 [concrete = <error>] {
  561. // CHECK:STDOUT: <elided>
  562. // CHECK:STDOUT: %M.ref: type = name_ref M, file.%M.decl [concrete = constants.%M.type]
  563. // CHECK:STDOUT: <elided>
  564. // CHECK:STDOUT: %.Self.ref.loc13_18: %M.type = name_ref .Self, %.Self.2 [symbolic_self = constants.%.Self.1bb]
  565. // CHECK:STDOUT: %X.ref.loc13_18: %M.assoc_type = name_ref X, @X.%assoc0 [concrete = constants.%assoc0]
  566. // CHECK:STDOUT: %.Self.as_type.loc13_18: type = facet_access_type %.Self.ref.loc13_18 [symbolic_self = constants.%.Self.as_type]
  567. // CHECK:STDOUT: %.loc13_18: type = converted %.Self.ref.loc13_18, %.Self.as_type.loc13_18 [symbolic_self = constants.%.Self.as_type]
  568. // CHECK:STDOUT: %impl.elem0.loc13_18: type = impl_witness_access constants.%M.lookup_impl_witness, element0 [symbolic_self = constants.%impl.elem0]
  569. // CHECK:STDOUT: %.Self.ref.loc13_23: %M.type = name_ref .Self, %.Self.2 [symbolic_self = constants.%.Self.1bb]
  570. // CHECK:STDOUT: %Y.ref.loc13_23: %M.assoc_type = name_ref Y, @Y.%assoc1 [concrete = constants.%assoc1]
  571. // CHECK:STDOUT: %.Self.as_type.loc13_23: type = facet_access_type %.Self.ref.loc13_23 [symbolic_self = constants.%.Self.as_type]
  572. // CHECK:STDOUT: %.loc13_23: type = converted %.Self.ref.loc13_23, %.Self.as_type.loc13_23 [symbolic_self = constants.%.Self.as_type]
  573. // CHECK:STDOUT: %impl.elem1.loc13_23: type = impl_witness_access constants.%M.lookup_impl_witness, element1 [symbolic_self = constants.%impl.elem1]
  574. // CHECK:STDOUT: %.Self.ref.loc13_30: %M.type = name_ref .Self, %.Self.2 [symbolic_self = constants.%.Self.1bb]
  575. // CHECK:STDOUT: %Y.ref.loc13_30: %M.assoc_type = name_ref Y, @Y.%assoc1 [concrete = constants.%assoc1]
  576. // CHECK:STDOUT: %.Self.as_type.loc13_30: type = facet_access_type %.Self.ref.loc13_30 [symbolic_self = constants.%.Self.as_type]
  577. // CHECK:STDOUT: %.loc13_30: type = converted %.Self.ref.loc13_30, %.Self.as_type.loc13_30 [symbolic_self = constants.%.Self.as_type]
  578. // CHECK:STDOUT: %impl.elem1.loc13_30: type = impl_witness_access constants.%M.lookup_impl_witness, element1 [symbolic_self = constants.%impl.elem1]
  579. // CHECK:STDOUT: %.Self.ref.loc13_35: %M.type = name_ref .Self, %.Self.2 [symbolic_self = constants.%.Self.1bb]
  580. // CHECK:STDOUT: %X.ref.loc13_35: %M.assoc_type = name_ref X, @X.%assoc0 [concrete = constants.%assoc0]
  581. // CHECK:STDOUT: %.Self.as_type.loc13_35: type = facet_access_type %.Self.ref.loc13_35 [symbolic_self = constants.%.Self.as_type]
  582. // CHECK:STDOUT: %.loc13_35: type = converted %.Self.ref.loc13_35, %.Self.as_type.loc13_35 [symbolic_self = constants.%.Self.as_type]
  583. // CHECK:STDOUT: %impl.elem0.loc13_35: type = impl_witness_access constants.%M.lookup_impl_witness, element0 [symbolic_self = constants.%impl.elem0]
  584. // CHECK:STDOUT: %impl.elem0.subst: type = impl_witness_access_substituted %impl.elem0.loc13_35, %impl.elem1.loc13_23 [symbolic_self = constants.%impl.elem1]
  585. // CHECK:STDOUT: %.Self.ref.loc13_42: %M.type = name_ref .Self, %.Self.2 [symbolic_self = constants.%.Self.1bb]
  586. // CHECK:STDOUT: %Z.ref: %M.assoc_type = name_ref Z, @Z.%assoc2 [concrete = constants.%assoc2]
  587. // CHECK:STDOUT: %.Self.as_type.loc13_42: type = facet_access_type %.Self.ref.loc13_42 [symbolic_self = constants.%.Self.as_type]
  588. // CHECK:STDOUT: %.loc13_42: type = converted %.Self.ref.loc13_42, %.Self.as_type.loc13_42 [symbolic_self = constants.%.Self.as_type]
  589. // CHECK:STDOUT: %impl.elem2: type = impl_witness_access constants.%M.lookup_impl_witness, element2 [symbolic_self = constants.%impl.elem2]
  590. // CHECK:STDOUT: %.loc13_48.1: %empty_tuple.type = tuple_literal ()
  591. // CHECK:STDOUT: %.loc13_48.2: type = converted %.loc13_48.1, constants.%empty_tuple.type [concrete = constants.%empty_tuple.type]
  592. // CHECK:STDOUT: %.loc13_12.2: type = where_expr %.Self.2 [concrete = <error>] {
  593. // CHECK:STDOUT: requirement_base_facet_type constants.%M.type
  594. // CHECK:STDOUT: requirement_rewrite %impl.elem0.loc13_18, %impl.elem1.loc13_23
  595. // CHECK:STDOUT: requirement_rewrite %impl.elem1.loc13_30, %impl.elem0.subst
  596. // CHECK:STDOUT: requirement_rewrite %impl.elem2, %.loc13_48.2
  597. // CHECK:STDOUT: }
  598. // CHECK:STDOUT: }
  599. // CHECK:STDOUT: %T: <error> = bind_symbolic_name T, 0 [concrete = <error>]
  600. // CHECK:STDOUT: }
  601. // CHECK:STDOUT: }
  602. // CHECK:STDOUT:
  603. // CHECK:STDOUT: generic fn @F(%T: <error>) {
  604. // CHECK:STDOUT: !definition:
  605. // CHECK:STDOUT:
  606. // CHECK:STDOUT: fn() {
  607. // CHECK:STDOUT: !entry:
  608. // CHECK:STDOUT: return
  609. // CHECK:STDOUT: }
  610. // CHECK:STDOUT: }
  611. // CHECK:STDOUT:
  612. // CHECK:STDOUT: specific @F(<error>) {}
  613. // CHECK:STDOUT: