impl_cycle.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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/impl/lookup/impl_cycle.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/lookup/impl_cycle.carbon
  12. // --- fail_impl_simple_cycle.carbon
  13. library "[[@TEST_NAME]]";
  14. interface Z {}
  15. // This creates a dependency cycle with itself.
  16. impl forall [T:! Z] T as Z {}
  17. class Point {}
  18. fn F() {
  19. // TODO: We should just get one diagnostic here, either the conversion fails
  20. // with a note about the cycle as the reason for impl lookup failing, or the
  21. // cycle causes an error so the conversion is not diagnosed.
  22. // CHECK:STDERR: fail_impl_simple_cycle.carbon:[[@LINE+11]]:3: error: cycle found in search for impl of `Z` for type `Point` [ImplLookupCycle]
  23. // CHECK:STDERR: Point as Z;
  24. // CHECK:STDERR: ^~~~~~~~~~
  25. // CHECK:STDERR: fail_impl_simple_cycle.carbon:[[@LINE-12]]:1: note: determining if this impl clause matches [ImplLookupCycleNote]
  26. // CHECK:STDERR: impl forall [T:! Z] T as Z {}
  27. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28. // CHECK:STDERR:
  29. // CHECK:STDERR: fail_impl_simple_cycle.carbon:[[@LINE+4]]:3: error: cannot convert type `Point` into type implementing `Z` [ConversionFailureTypeToFacet]
  30. // CHECK:STDERR: Point as Z;
  31. // CHECK:STDERR: ^~~~~~~~~~
  32. // CHECK:STDERR:
  33. Point as Z;
  34. }
  35. // --- fail_impl_simple_where_cycle.carbon
  36. library "[[@TEST_NAME]]";
  37. interface Y {}
  38. interface Z {}
  39. // This creates a dependency cycle with itself.
  40. impl forall [T:! type where .Self impls Z] T as Z {}
  41. class Point {}
  42. fn F() {
  43. // CHECK:STDERR: fail_impl_simple_where_cycle.carbon:[[@LINE+11]]:3: error: cycle found in search for impl of `type where .Self impls Z` for type `Point` [ImplLookupCycle]
  44. // CHECK:STDERR: Point as Z;
  45. // CHECK:STDERR: ^~~~~~~~~~
  46. // CHECK:STDERR: fail_impl_simple_where_cycle.carbon:[[@LINE-8]]:1: note: determining if this impl clause matches [ImplLookupCycleNote]
  47. // CHECK:STDERR: impl forall [T:! type where .Self impls Z] T as Z {}
  48. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  49. // CHECK:STDERR:
  50. // CHECK:STDERR: fail_impl_simple_where_cycle.carbon:[[@LINE+4]]:3: error: cannot convert type `Point` into type implementing `Z` [ConversionFailureTypeToFacet]
  51. // CHECK:STDERR: Point as Z;
  52. // CHECK:STDERR: ^~~~~~~~~~
  53. // CHECK:STDERR:
  54. Point as Z;
  55. }
  56. // --- fail_impl_simple_two_interfaces.carbon
  57. library "[[@TEST_NAME]]";
  58. interface Z {}
  59. interface Y {}
  60. // This creates a dependency cycle with itself.
  61. impl forall [T:! Z & Y] T as Z {}
  62. class Point {}
  63. fn F() {
  64. // CHECK:STDERR: fail_impl_simple_two_interfaces.carbon:[[@LINE+11]]:3: error: cycle found in search for impl of `Z & Y` for type `Point` [ImplLookupCycle]
  65. // CHECK:STDERR: Point as Z;
  66. // CHECK:STDERR: ^~~~~~~~~~
  67. // CHECK:STDERR: fail_impl_simple_two_interfaces.carbon:[[@LINE-8]]:1: note: determining if this impl clause matches [ImplLookupCycleNote]
  68. // CHECK:STDERR: impl forall [T:! Z & Y] T as Z {}
  69. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  70. // CHECK:STDERR:
  71. // CHECK:STDERR: fail_impl_simple_two_interfaces.carbon:[[@LINE+4]]:3: error: cannot convert type `Point` into type implementing `Z` [ConversionFailureTypeToFacet]
  72. // CHECK:STDERR: Point as Z;
  73. // CHECK:STDERR: ^~~~~~~~~~
  74. // CHECK:STDERR:
  75. Point as Z;
  76. }
  77. // --- fail_impl_long_cycle.carbon
  78. library "[[@TEST_NAME]]";
  79. interface X {}
  80. interface Y {}
  81. interface Z {}
  82. // This creates a dependency cycle with itself.
  83. impl forall [T:! X] T as Y {}
  84. impl forall [T:! Y] T as Z {}
  85. impl forall [T:! Z] T as X {}
  86. class C {}
  87. fn F() {
  88. // CHECK:STDERR: fail_impl_long_cycle.carbon:[[@LINE+17]]:3: error: cycle found in search for impl of `Z` for type `C` [ImplLookupCycle]
  89. // CHECK:STDERR: C as Z;
  90. // CHECK:STDERR: ^~~~~~
  91. // CHECK:STDERR: fail_impl_long_cycle.carbon:[[@LINE-9]]:1: note: determining if this impl clause matches [ImplLookupCycleNote]
  92. // CHECK:STDERR: impl forall [T:! Y] T as Z {}
  93. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  94. // CHECK:STDERR: fail_impl_long_cycle.carbon:[[@LINE-13]]:1: note: determining if this impl clause matches [ImplLookupCycleNote]
  95. // CHECK:STDERR: impl forall [T:! X] T as Y {}
  96. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  97. // CHECK:STDERR: fail_impl_long_cycle.carbon:[[@LINE-14]]:1: note: determining if this impl clause matches [ImplLookupCycleNote]
  98. // CHECK:STDERR: impl forall [T:! Z] T as X {}
  99. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  100. // CHECK:STDERR:
  101. // CHECK:STDERR: fail_impl_long_cycle.carbon:[[@LINE+4]]:3: error: cannot convert type `C` into type implementing `Z` [ConversionFailureTypeToFacet]
  102. // CHECK:STDERR: C as Z;
  103. // CHECK:STDERR: ^~~~~~
  104. // CHECK:STDERR:
  105. C as Z;
  106. }
  107. // --- fail_impl_cycle_one_generic_param.carbon
  108. library "[[@TEST_NAME]]";
  109. interface ComparableWith(T:! type) {}
  110. // This creates a dependency cycle with itself.
  111. impl forall [U:! type, T:! ComparableWith(U)]
  112. U as ComparableWith(T) {}
  113. class C {}
  114. class D {}
  115. fn Compare[T:! type, U:! ComparableWith(T)](t: T, u: U) {}
  116. fn F() {
  117. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE+17]]:3: error: cycle found in search for impl of `ComparableWith(C)` for type `C` [ImplLookupCycle]
  118. // CHECK:STDERR: Compare({} as C, {} as C);
  119. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  120. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-12]]:1: note: determining if this impl clause matches [ImplLookupCycleNote]
  121. // CHECK:STDERR: impl forall [U:! type, T:! ComparableWith(U)]
  122. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  123. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-9]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  124. // CHECK:STDERR: fn Compare[T:! type, U:! ComparableWith(T)](t: T, u: U) {}
  125. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  126. // CHECK:STDERR:
  127. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE+7]]:3: error: cannot convert type `C` into type implementing `ComparableWith(C)` [ConversionFailureTypeToFacet]
  128. // CHECK:STDERR: Compare({} as C, {} as C);
  129. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  130. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-16]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  131. // CHECK:STDERR: fn Compare[T:! type, U:! ComparableWith(T)](t: T, u: U) {}
  132. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  133. // CHECK:STDERR:
  134. Compare({} as C, {} as C);
  135. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE+20]]:3: error: cycle found in search for impl of `ComparableWith(C)` for type `D` [ImplLookupCycle]
  136. // CHECK:STDERR: Compare({} as C, {} as D);
  137. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  138. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-31]]:1: note: determining if this impl clause matches [ImplLookupCycleNote]
  139. // CHECK:STDERR: impl forall [U:! type, T:! ComparableWith(U)]
  140. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  141. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-34]]:1: note: determining if this impl clause matches [ImplLookupCycleNote]
  142. // CHECK:STDERR: impl forall [U:! type, T:! ComparableWith(U)]
  143. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  144. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-31]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  145. // CHECK:STDERR: fn Compare[T:! type, U:! ComparableWith(T)](t: T, u: U) {}
  146. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  147. // CHECK:STDERR:
  148. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE+7]]:3: error: cannot convert type `D` into type implementing `ComparableWith(C)` [ConversionFailureTypeToFacet]
  149. // CHECK:STDERR: Compare({} as C, {} as D);
  150. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  151. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-38]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  152. // CHECK:STDERR: fn Compare[T:! type, U:! ComparableWith(T)](t: T, u: U) {}
  153. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  154. // CHECK:STDERR:
  155. Compare({} as C, {} as D);
  156. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE+20]]:3: error: cycle found in search for impl of `ComparableWith(D)` for type `C` [ImplLookupCycle]
  157. // CHECK:STDERR: Compare({} as D, {} as C);
  158. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  159. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-53]]:1: note: determining if this impl clause matches [ImplLookupCycleNote]
  160. // CHECK:STDERR: impl forall [U:! type, T:! ComparableWith(U)]
  161. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  162. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-56]]:1: note: determining if this impl clause matches [ImplLookupCycleNote]
  163. // CHECK:STDERR: impl forall [U:! type, T:! ComparableWith(U)]
  164. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  165. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-53]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  166. // CHECK:STDERR: fn Compare[T:! type, U:! ComparableWith(T)](t: T, u: U) {}
  167. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  168. // CHECK:STDERR:
  169. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE+7]]:3: error: cannot convert type `C` into type implementing `ComparableWith(D)` [ConversionFailureTypeToFacet]
  170. // CHECK:STDERR: Compare({} as D, {} as C);
  171. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  172. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-60]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  173. // CHECK:STDERR: fn Compare[T:! type, U:! ComparableWith(T)](t: T, u: U) {}
  174. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  175. // CHECK:STDERR:
  176. Compare({} as D, {} as C);
  177. }
  178. // --- impl_recurse_with_simpler_type_no_cycle.carbon
  179. library "[[@TEST_NAME]]";
  180. class Wraps(T:! type) {}
  181. interface Printable {}
  182. // If T is printable, so is Wraps(T).
  183. impl forall [T:! Printable] Wraps(T) as Printable {}
  184. class C {}
  185. impl C as Printable {}
  186. fn F() {
  187. ({} as C) as (C as Printable);
  188. ({} as Wraps(C)) as (Wraps(C) as Printable);
  189. }
  190. // --- impl_recurse_with_simpler_type_in_generic_param_no_cycle.carbon
  191. library "[[@TEST_NAME]]";
  192. class Wraps(T:! type) {}
  193. interface ComparableTo(T:! type) {}
  194. // If U is comparable to T, then U is comparable to Wraps(T).
  195. impl forall [T:! type, U:! ComparableTo(T)] U as ComparableTo(Wraps(T)) {}
  196. // If U is comparable to T then Wraps(U) is comparable to T.
  197. impl forall [T:! type, U:! ComparableTo(T)] Wraps(U) as ComparableTo(T) {}
  198. class C {}
  199. class D {}
  200. impl C as ComparableTo(D) {}
  201. fn F() {
  202. ({} as C) as (C as ComparableTo(D));
  203. ({} as C) as (C as ComparableTo(Wraps(D)));
  204. ({} as Wraps(C)) as (Wraps(C) as ComparableTo(D));
  205. ({} as Wraps(C)) as (Wraps(C) as ComparableTo(Wraps(D)));
  206. }
  207. // --- fail_todo_impl_recurse_with_simpler_type_in_generic_param_bidirectional_no_cycle.carbon
  208. library "[[@TEST_NAME]]";
  209. // Implement this for a type in one direction.
  210. interface ComparableTo(T:! type) {}
  211. // Use this as a bound with two types in any direction.
  212. interface ComparableWith(T:! type) {}
  213. class C {}
  214. class D {}
  215. // C is comparable to D. This should imply:
  216. // - C is ComparableWith(C).
  217. // - C is ComparableWith(D).
  218. // - D is ComparableWith(C).
  219. impl C as ComparableTo(D) {}
  220. // TODO: These three `impl`s need to be placed in a match_first as they
  221. // overlap.
  222. // T is always ComparableWith(T).
  223. impl forall [T:! type] T as ComparableWith(T) {}
  224. // If U is ComparableTo(T), U is ComparableWith(T).
  225. // CHECK:STDERR: fail_todo_impl_recurse_with_simpler_type_in_generic_param_bidirectional_no_cycle.carbon:[[@LINE+7]]:1: error: found non-final `impl` with the same type structure as another non-final `impl` [ImplNonFinalSameTypeStructure]
  226. // CHECK:STDERR: impl forall [T:! type, U:! ComparableTo(T)] U as ComparableWith(T) {}
  227. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  228. // CHECK:STDERR: fail_todo_impl_recurse_with_simpler_type_in_generic_param_bidirectional_no_cycle.carbon:[[@LINE-5]]:1: note: other `impl` here [ImplNonFinalSameTypeStructureNote]
  229. // CHECK:STDERR: impl forall [T:! type] T as ComparableWith(T) {}
  230. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  231. // CHECK:STDERR:
  232. impl forall [T:! type, U:! ComparableTo(T)] U as ComparableWith(T) {}
  233. // If U is ComparableTo(T), T is ComparableWith(U).
  234. // CHECK:STDERR: fail_todo_impl_recurse_with_simpler_type_in_generic_param_bidirectional_no_cycle.carbon:[[@LINE+7]]:1: error: found non-final `impl` with the same type structure as another non-final `impl` [ImplNonFinalSameTypeStructure]
  235. // CHECK:STDERR: impl forall [T:! type, U:! ComparableTo(T)] T as ComparableWith(U) {}
  236. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  237. // CHECK:STDERR: fail_todo_impl_recurse_with_simpler_type_in_generic_param_bidirectional_no_cycle.carbon:[[@LINE-14]]:1: note: other `impl` here [ImplNonFinalSameTypeStructureNote]
  238. // CHECK:STDERR: impl forall [T:! type] T as ComparableWith(T) {}
  239. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  240. // CHECK:STDERR:
  241. impl forall [T:! type, U:! ComparableTo(T)] T as ComparableWith(U) {}
  242. fn Compare[T:! type, U:! ComparableWith(T)](t: T, u: U) {}
  243. fn F() {
  244. Compare({} as C, {} as C);
  245. Compare({} as C, {} as D);
  246. Compare({} as D, {} as C);
  247. }
  248. // --- deduce_cycle_without_symbolic.carbon
  249. library "[[@TEST_NAME]]";
  250. interface First {}
  251. interface Second {}
  252. // When answering a query "does T impl Second", we must avoid trying to deduce
  253. // parameters for this impl. Not only is doing so unnecessary, it would start a new
  254. // "does T impl Second" query, leading to a "cycle in impl lookup" error.
  255. impl forall [T:! Second] T as First {}
  256. impl forall [T:! type] T as Second {}
  257. class C {}
  258. fn F() {
  259. ({} as C) as (C as Second);
  260. ({} as C) as (C as First);
  261. }
  262. // --- deduce_cycle_with_symbolic.carbon
  263. library "[[@TEST_NAME]]";
  264. interface First(T:! type) {}
  265. interface Second {}
  266. // When answering a query "does T impl Second", we must avoid trying to deduce
  267. // parameters for this impl. Not only is doing so unnecessary, it would start a new
  268. // "does T impl Second" query, leading to a "cycle in impl lookup" error.
  269. impl forall [T:! Second, U:! type] T as First(U) {}
  270. impl forall [T:! type] T as Second {}
  271. class C {}
  272. fn F() {
  273. ({} as C) as (C as Second);
  274. ({} as C) as (C as First(C));
  275. }