impl_cycle.carbon 13 KB

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