validate_rewrite_constraints.carbon 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  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. let Constant:! type = I where .X = .Y;
  718. fn G(T:! I where .X = (I where .Y = (Constant, ))) {
  719. // CHECK:STDERR: fail_nested_facet_type_from_constant.carbon:[[@LINE+7]]:5: error: cannot convert type `T` that implements `I where .(I.X) = I where .(I.Y) = (Constant,)` into type implementing `I where .(I.X) = I where .(I.Y) = (I where .(I.X) = .(I.Y),)` [ConversionFailureFacetToFacet]
  720. // CHECK:STDERR: F(T);
  721. // CHECK:STDERR: ^~~~
  722. // CHECK:STDERR: fail_nested_facet_type_from_constant.carbon:[[@LINE-16]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  723. // CHECK:STDERR: fn F(T:! I where .X = (I where .Y = (I where .X = .Y, ))) {}
  724. // CHECK:STDERR: ^
  725. // CHECK:STDERR:
  726. F(T);
  727. }
  728. }
  729. fn G2() {
  730. let Constant:! type = (I where .X = .Y, );
  731. fn G(T:! I where .X = (I where .Y = Constant)) {
  732. // CHECK:STDERR: fail_nested_facet_type_from_constant.carbon:[[@LINE+7]]:5: error: cannot convert type `T` that implements `I where .(I.X) = I where .(I.Y) = Constant` into type implementing `I where .(I.X) = I where .(I.Y) = (I where .(I.X) = .(I.Y),)` [ConversionFailureFacetToFacet]
  733. // CHECK:STDERR: F(T);
  734. // CHECK:STDERR: ^~~~
  735. // CHECK:STDERR: fail_nested_facet_type_from_constant.carbon:[[@LINE-31]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  736. // CHECK:STDERR: fn F(T:! I where .X = (I where .Y = (I where .X = .Y, ))) {}
  737. // CHECK:STDERR: ^
  738. // CHECK:STDERR:
  739. F(T);
  740. }
  741. }
  742. fn G3() {
  743. let Constant:! type = I where .Y = (I where .X = .Y, );
  744. fn G(T:! I where .X = Constant) {
  745. // CHECK:STDERR: fail_nested_facet_type_from_constant.carbon:[[@LINE+7]]:5: error: cannot convert type `T` that implements `I where .(I.X) = Constant` into type implementing `I where .(I.X) = I where .(I.Y) = (I where .(I.X) = .(I.Y),)` [ConversionFailureFacetToFacet]
  746. // CHECK:STDERR: F(T);
  747. // CHECK:STDERR: ^~~~
  748. // CHECK:STDERR: fail_nested_facet_type_from_constant.carbon:[[@LINE-46]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  749. // CHECK:STDERR: fn F(T:! I where .X = (I where .Y = (I where .X = .Y, ))) {}
  750. // CHECK:STDERR: ^
  751. // CHECK:STDERR:
  752. F(T);
  753. }
  754. }
  755. fn G4() {
  756. let Constant2:! type = (I where .X = .Y, );
  757. let Constant:! type = Constant2;
  758. fn G(T:! I where .X = (I where .Y = Constant)) {
  759. // CHECK:STDERR: fail_nested_facet_type_from_constant.carbon:[[@LINE+7]]:5: error: cannot convert type `T` that implements `I where .(I.X) = I where .(I.Y) = Constant` into type implementing `I where .(I.X) = I where .(I.Y) = (I where .(I.X) = .(I.Y),)` [ConversionFailureFacetToFacet]
  760. // CHECK:STDERR: F(T);
  761. // CHECK:STDERR: ^~~~
  762. // CHECK:STDERR: fail_nested_facet_type_from_constant.carbon:[[@LINE-62]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  763. // CHECK:STDERR: fn F(T:! I where .X = (I where .Y = (I where .X = .Y, ))) {}
  764. // CHECK:STDERR: ^
  765. // CHECK:STDERR:
  766. F(T);
  767. }
  768. }
  769. fn G5() {
  770. let Constant2:! type = I where .X = .Y;
  771. let Constant:! type = (Constant2, );
  772. fn G(T:! I where .X = (I where .Y = Constant)) {
  773. // CHECK:STDERR: fail_nested_facet_type_from_constant.carbon:[[@LINE+7]]:5: error: cannot convert type `T` that implements `I where .(I.X) = I where .(I.Y) = Constant` into type implementing `I where .(I.X) = I where .(I.Y) = (I where .(I.X) = .(I.Y),)` [ConversionFailureFacetToFacet]
  774. // CHECK:STDERR: F(T);
  775. // CHECK:STDERR: ^~~~
  776. // CHECK:STDERR: fail_nested_facet_type_from_constant.carbon:[[@LINE-78]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  777. // CHECK:STDERR: fn F(T:! I where .X = (I where .Y = (I where .X = .Y, ))) {}
  778. // CHECK:STDERR: ^
  779. // CHECK:STDERR:
  780. F(T);
  781. }
  782. }
  783. fn G6() {
  784. let Constant2:! type = (I where .X = .Y, );
  785. let Constant:! type = I where .Y = Constant2;
  786. fn G(T:! I where .X = Constant) {
  787. // CHECK:STDERR: fail_nested_facet_type_from_constant.carbon:[[@LINE+7]]:5: error: cannot convert type `T` that implements `I where .(I.X) = Constant` into type implementing `I where .(I.X) = I where .(I.Y) = (I where .(I.X) = .(I.Y),)` [ConversionFailureFacetToFacet]
  788. // CHECK:STDERR: F(T);
  789. // CHECK:STDERR: ^~~~
  790. // CHECK:STDERR: fail_nested_facet_type_from_constant.carbon:[[@LINE-94]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  791. // CHECK:STDERR: fn F(T:! I where .X = (I where .Y = (I where .X = .Y, ))) {}
  792. // CHECK:STDERR: ^
  793. // CHECK:STDERR:
  794. F(T);
  795. }
  796. }
  797. // ---fail_nested_facet_type_from_constant_differs.carbon
  798. library "[[@TEST_NAME]]";
  799. interface I {
  800. let X:! type;
  801. let Y:! type;
  802. }
  803. fn F(T:! I where .X = (I where .Y = (I where .X = .Y, ))) {}
  804. fn G1() {
  805. let Constant:! type = I where .X = () and .Y = ();
  806. fn G(T:! I where .X = (I where .Y = (Constant, ))) {
  807. // CHECK:STDERR: fail_nested_facet_type_from_constant_differs.carbon:[[@LINE+7]]:5: error: cannot convert type `T` that implements `I where .(I.X) = I where .(I.Y) = (Constant,)` into type implementing `I where .(I.X) = I where .(I.Y) = (I where .(I.X) = .(I.Y),)` [ConversionFailureFacetToFacet]
  808. // CHECK:STDERR: F(T);
  809. // CHECK:STDERR: ^~~~
  810. // CHECK:STDERR: fail_nested_facet_type_from_constant_differs.carbon:[[@LINE-9]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  811. // CHECK:STDERR: fn F(T:! I where .X = (I where .Y = (I where .X = .Y, ))) {}
  812. // CHECK:STDERR: ^
  813. // CHECK:STDERR:
  814. F(T);
  815. }
  816. }
  817. // --- rewrite_requires_subst_in_nested_access_of_self.carbon
  818. library "[[@TEST_NAME]]";
  819. interface I {
  820. let I1:! type;
  821. let I2:! type;
  822. }
  823. interface J {
  824. let J1:! I;
  825. }
  826. interface K {
  827. let K1:! J;
  828. }
  829. fn F(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I1 = (.K1.J1).I2) {}
  830. fn G(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I1 = .I2) {
  831. F(T);
  832. }
  833. // --- fail_rewrite_requires_subst_in_nested_access_of_self_wrong_type.carbon
  834. library "[[@TEST_NAME]]";
  835. interface I {
  836. let I1:! type;
  837. let I2:! type;
  838. }
  839. interface J {
  840. let J1:! I;
  841. }
  842. interface K {
  843. let K1:! J;
  844. }
  845. fn F(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I1 = (.K1.J1).I2) {}
  846. // F's requirement I1 = I2 is not met, as I1 = () and I2 = {}
  847. fn G(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I1 = () and .I2 = {}) {
  848. // 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]
  849. // CHECK:STDERR: F(T);
  850. // CHECK:STDERR: ^~~~
  851. // 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]
  852. // CHECK:STDERR: fn F(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I1 = (.K1.J1).I2) {}
  853. // CHECK:STDERR: ^
  854. // CHECK:STDERR:
  855. F(T);
  856. }
  857. // F's requirement I1 = I2 is not met, as I1 = {} and I2 is unspecified
  858. fn G2(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I1 = {}) {
  859. // 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]
  860. // CHECK:STDERR: F(T);
  861. // CHECK:STDERR: ^~~~
  862. // 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]
  863. // CHECK:STDERR: fn F(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I1 = (.K1.J1).I2) {}
  864. // CHECK:STDERR: ^
  865. // CHECK:STDERR:
  866. F(T);
  867. }
  868. // F's requirement I1 = I2 is not met, as I2 = {} and I1 is unspecified
  869. fn G3(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I2 = {}) {
  870. // 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]
  871. // CHECK:STDERR: F(T);
  872. // CHECK:STDERR: ^~~~
  873. // 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]
  874. // CHECK:STDERR: fn F(T:! I & J & K where .K1 = .Self and .J1 = .Self and .I1 = (.K1.J1).I2) {}
  875. // CHECK:STDERR: ^
  876. // CHECK:STDERR:
  877. F(T);
  878. }
  879. // --- fail_todo_rewrite_requires_subst_in_nested_access_of_other.carbon
  880. library "[[@TEST_NAME]]";
  881. interface I {
  882. let I1:! type;
  883. }
  884. interface J {
  885. let J1:! I;
  886. let J2:! type;
  887. }
  888. interface K {
  889. let K1:! J;
  890. }
  891. // .K1.J1 is an ImplWitnessAccess into Self. The Self witness will be subst'd in
  892. // for `U` and it will eval to the ImplWitnessAccess inside of `U.I1`. Then that
  893. // will need to be subst'd to find the value of I1 in U's witness.
  894. fn F(U:! I, T:! J & K where .J2 = (.K1.J1).I1) {}
  895. fn G(U:! I where .I1 = (), T:! J & K where .K1 = .Self and .J1 = U and .J2 = ()) {
  896. // 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]
  897. // CHECK:STDERR: F(U, T);
  898. // CHECK:STDERR: ^~~~~~~
  899. // CHECK:STDERR: fail_todo_rewrite_requires_subst_in_nested_access_of_other.carbon:[[@LINE-6]]:13: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  900. // CHECK:STDERR: fn F(U:! I, T:! J & K where .J2 = (.K1.J1).I1) {}
  901. // CHECK:STDERR: ^
  902. // CHECK:STDERR:
  903. F(U, T);
  904. }
  905. // --- fail_rewrite_requires_subst_in_nested_access_of_other_wrong_type.carbon
  906. library "[[@TEST_NAME]]";
  907. interface I {
  908. let I1:! type;
  909. }
  910. interface J {
  911. let J1:! I;
  912. let J2:! type;
  913. }
  914. interface K {
  915. let K1:! J;
  916. }
  917. fn F(U:! I, T:! J & K where .J2 = (.K1.J1).I1) {}
  918. // F's requirement J2 = I1 is not met as J2 = () and I1 = {}
  919. fn G(U:! I where .I1 = {}, T:! J & K where .K1 = .Self and .J1 = U and .J2 = ()) {
  920. // 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]
  921. // CHECK:STDERR: F(U, T);
  922. // CHECK:STDERR: ^~~~~~~
  923. // 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]
  924. // CHECK:STDERR: fn F(U:! I, T:! J & K where .J2 = (.K1.J1).I1) {}
  925. // CHECK:STDERR: ^
  926. // CHECK:STDERR:
  927. F(U, T);
  928. }
  929. // F's requirement J2 = I1 is not met as J2 = () and I1 is unspecified
  930. fn G2(U:! I, T:! J & K where .K1 = .Self and .J1 = U and .J2 = ()) {
  931. // 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]
  932. // CHECK:STDERR: F(U, T);
  933. // CHECK:STDERR: ^~~~~~~
  934. // 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]
  935. // CHECK:STDERR: fn F(U:! I, T:! J & K where .J2 = (.K1.J1).I1) {}
  936. // CHECK:STDERR: ^
  937. // CHECK:STDERR:
  938. F(U, T);
  939. }
  940. // F's requirement J2 = I1 is not met as I1 = {} and J2 is unspecified
  941. fn G3(U:! I where .I1 = {}, T:! J & K where .K1 = .Self and .J1 = U) {
  942. // 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]
  943. // CHECK:STDERR: F(U, T);
  944. // CHECK:STDERR: ^~~~~~~
  945. // 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]
  946. // CHECK:STDERR: fn F(U:! I, T:! J & K where .J2 = (.K1.J1).I1) {}
  947. // CHECK:STDERR: ^
  948. // CHECK:STDERR:
  949. F(U, T);
  950. }
  951. // --- rewrite_requires_subst_in_nested_access_of_other_with_two_witnesses.carbon
  952. library "[[@TEST_NAME]]";
  953. interface I {
  954. let I1:! type;
  955. }
  956. interface J {
  957. let J1:! type;
  958. }
  959. interface K {
  960. let K1:! J & I;
  961. let K2:! type;
  962. let K3:! type;
  963. }
  964. fn F(U:! I & J, T:! K where .K2 = .K1.I1 and .K3 = .K1.J1) {}
  965. fn G(U:! I & J where .I1 = () and .J1 = {}, T:! K where .K1 = U and .K2 = () and .K3 = {}) {
  966. F(U, T);
  967. }
  968. // --- fail_rewrite_requires_subst_in_nested_access_of_other_with_two_witnesses_wrong_type.carbon
  969. library "[[@TEST_NAME]]";
  970. interface I {
  971. let I1:! type;
  972. }
  973. interface J {
  974. let J1:! type;
  975. }
  976. interface K {
  977. let K1:! J & I;
  978. let K2:! type;
  979. let K3:! type;
  980. }
  981. fn F(U:! I & J, T:! K where .K2 = .K1.I1 and .K3 = .K1.J1) {}
  982. // K2 = I1 fails since K2 = {} and I1 = ()
  983. fn G(U:! I & J where .I1 = () and .J1 = {}, T:! K where .K1 = U and .K2 = {} and .K3 = {}) {
  984. // 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]
  985. // CHECK:STDERR: F(U, T);
  986. // CHECK:STDERR: ^~~~~~~
  987. // 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]
  988. // CHECK:STDERR: fn F(U:! I & J, T:! K where .K2 = .K1.I1 and .K3 = .K1.J1) {}
  989. // CHECK:STDERR: ^
  990. // CHECK:STDERR:
  991. F(U, T);
  992. }
  993. // K2 = I1 fails since I1 = () and K2 is unspecified.
  994. fn G2(U:! I & J where .I1 = () and .J1 = {}, T:! K where .K1 = U and .K3 = {}) {
  995. // 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]
  996. // CHECK:STDERR: F(U, T);
  997. // CHECK:STDERR: ^~~~~~~
  998. // 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]
  999. // CHECK:STDERR: fn F(U:! I & J, T:! K where .K2 = .K1.I1 and .K3 = .K1.J1) {}
  1000. // CHECK:STDERR: ^
  1001. // CHECK:STDERR:
  1002. F(U, T);
  1003. }
  1004. // K2 = I1 fails since K2 = () and I1 is unspecified.
  1005. fn G3(U:! I & J where .J1 = {}, T:! K where .K1 = U and .K2 = () and .K3 = {}) {
  1006. // 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]
  1007. // CHECK:STDERR: F(U, T);
  1008. // CHECK:STDERR: ^~~~~~~
  1009. // 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]
  1010. // CHECK:STDERR: fn F(U:! I & J, T:! K where .K2 = .K1.I1 and .K3 = .K1.J1) {}
  1011. // CHECK:STDERR: ^
  1012. // CHECK:STDERR:
  1013. F(U, T);
  1014. }
  1015. // K3 = J1 fails since J1 = {} and K3 is unspecified.
  1016. fn G4(U:! I & J where .I1 = () and .J1 = {}, T:! K where .K1 = U and .K2 = ()) {
  1017. // 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]
  1018. // CHECK:STDERR: F(U, T);
  1019. // CHECK:STDERR: ^~~~~~~
  1020. // 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]
  1021. // CHECK:STDERR: fn F(U:! I & J, T:! K where .K2 = .K1.I1 and .K3 = .K1.J1) {}
  1022. // CHECK:STDERR: ^
  1023. // CHECK:STDERR:
  1024. F(U, T);
  1025. }
  1026. // K3 = J1 fails since K3 = {} and J1 is unspecified.
  1027. fn G5(U:! I & J where .I1 = (), T:! K where .K1 = U and .K2 = () and .K3 = {}) {
  1028. // 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]
  1029. // CHECK:STDERR: F(U, T);
  1030. // CHECK:STDERR: ^~~~~~~
  1031. // 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]
  1032. // CHECK:STDERR: fn F(U:! I & J, T:! K where .K2 = .K1.I1 and .K3 = .K1.J1) {}
  1033. // CHECK:STDERR: ^
  1034. // CHECK:STDERR:
  1035. F(U, T);
  1036. }
  1037. // --- fail_target_rewrites_dont_apply_to_source.carbon
  1038. library "[[@TEST_NAME]]";
  1039. interface I {
  1040. let I1:! type;
  1041. let I2:! type;
  1042. let I3:! type;
  1043. let I4:! type;
  1044. }
  1045. fn F(T:! I where .I1 = () and .I2 = ()) {}
  1046. fn G(T:! I where .I1 = .I2) {
  1047. // 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]
  1048. // CHECK:STDERR: F(T);
  1049. // CHECK:STDERR: ^~~~
  1050. // CHECK:STDERR: fail_target_rewrites_dont_apply_to_source.carbon:[[@LINE-6]]:6: note: initializing generic parameter `T` declared here [InitializingGenericParam]
  1051. // CHECK:STDERR: fn F(T:! I where .I1 = () and .I2 = ()) {}
  1052. // CHECK:STDERR: ^
  1053. // CHECK:STDERR:
  1054. F(T);
  1055. }
  1056. // --- nested_facet_type_used_as_root_facet_type.carbon
  1057. library "[[@TEST_NAME]]";
  1058. interface I {
  1059. let X:! type;
  1060. let Y:! type;
  1061. }
  1062. fn F(T:! I where .X = (I where .Y = {}), U:! T.X) {}
  1063. fn G(T:! I where .X = (I where .Y = {}), U:! I where .Y = {}) {
  1064. F(T, U);
  1065. }