impl_cycle.carbon 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. // EXTRA-ARGS: --no-dump-sem-ir
  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/no_prelude/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/no_prelude/impl_cycle.carbon
  12. // --- core.carbon
  13. package Core;
  14. interface As(Dest:! type) {
  15. fn Convert[self: Self]() -> Dest;
  16. }
  17. interface ImplicitAs(Dest:! type) {
  18. fn Convert[self: Self]() -> Dest;
  19. }
  20. // --- fail_impl_simple_cycle.carbon
  21. library "[[@TEST_NAME]]";
  22. import Core;
  23. interface Z {}
  24. // This creates a dependency cycle with itself.
  25. impl forall [T:! Z] T as Z {}
  26. class Point {
  27. impl as Z {}
  28. }
  29. fn F() {
  30. // CHECK:STDERR: fail_impl_simple_cycle.carbon:[[@LINE+7]]:21: error: cycle found in search for impl of `Z` for type `Point` [ImplLookupCycle]
  31. // CHECK:STDERR: ({} as Point) as (Point as Z);
  32. // CHECK:STDERR: ^~~~~~~~~~
  33. // CHECK:STDERR: fail_impl_simple_cycle.carbon:[[@LINE-10]]:1: note: determining if this impl clause matches [ImplLookupCycleNote]
  34. // CHECK:STDERR: impl forall [T:! Z] T as Z {}
  35. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  36. // CHECK:STDERR:
  37. ({} as Point) as (Point as Z);
  38. }
  39. // --- fail_impl_simple_where_cycle.carbon
  40. library "[[@TEST_NAME]]";
  41. import Core;
  42. interface Z {}
  43. // This creates a dependency cycle with itself.
  44. impl forall [T:! type where .Self impls Z] T as Z {}
  45. class Point {
  46. impl as Z {}
  47. }
  48. fn F() {
  49. // TODO: A cycle should be found in the `impl forall T as Z` above.
  50. // CHECK:STDERR: fail_impl_simple_where_cycle.carbon:[[@LINE+4]]:21: error: semantics TODO: `impl lookup for a FacetType with no interface (using `where .Self impls ...` instead?)` [SemanticsTodo]
  51. // CHECK:STDERR: ({} as Point) as (Point as Z);
  52. // CHECK:STDERR: ^~~~~~~~~~
  53. // CHECK:STDERR:
  54. ({} as Point) as (Point as Z);
  55. }
  56. // --- fail_impl_simple_two_interfaces.carbon
  57. library "[[@TEST_NAME]]";
  58. import Core;
  59. interface Z {}
  60. interface Y {}
  61. // This creates a dependency cycle with itself.
  62. // CHECK:STDERR: fail_impl_simple_two_interfaces.carbon:[[@LINE+4]]:18: error: name `Core.BitAnd` implicitly referenced here, but not found [CoreNameNotFound]
  63. // CHECK:STDERR: impl forall [T:! Z & Y] T as Z {}
  64. // CHECK:STDERR: ^~~~~
  65. // CHECK:STDERR:
  66. impl forall [T:! Z & Y] T as Z {}
  67. class Point {
  68. impl as Z {}
  69. }
  70. fn F() {
  71. // TODO: A cycle should be found in the `impl forall T as Z` above.
  72. ({} as Point) as (Point as Z);
  73. }
  74. // --- fail_impl_long_cycle.carbon
  75. library "[[@TEST_NAME]]";
  76. import Core;
  77. interface X {}
  78. interface Y {}
  79. interface Z {}
  80. // This creates a dependency cycle with itself.
  81. impl forall [T:! X] T as Y {}
  82. impl forall [T:! Y] T as Z {}
  83. impl forall [T:! Z] T as X {}
  84. class C {}
  85. impl C as Z {}
  86. fn F() {
  87. // CHECK:STDERR: fail_impl_long_cycle.carbon:[[@LINE+13]]:17: error: cycle found in search for impl of `Z` for type `C` [ImplLookupCycle]
  88. // CHECK:STDERR: ({} as C) as (C as Z);
  89. // CHECK:STDERR: ^~~~~~
  90. // CHECK:STDERR: fail_impl_long_cycle.carbon:[[@LINE-10]]:1: note: determining if this impl clause matches [ImplLookupCycleNote]
  91. // CHECK:STDERR: impl forall [T:! Y] T as Z {}
  92. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  93. // CHECK:STDERR: fail_impl_long_cycle.carbon:[[@LINE-14]]:1: note: determining if this impl clause matches [ImplLookupCycleNote]
  94. // CHECK:STDERR: impl forall [T:! X] T as Y {}
  95. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  96. // CHECK:STDERR: fail_impl_long_cycle.carbon:[[@LINE-15]]:1: note: determining if this impl clause matches [ImplLookupCycleNote]
  97. // CHECK:STDERR: impl forall [T:! Z] T as X {}
  98. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  99. // CHECK:STDERR:
  100. ({} as C) as (C as Z);
  101. }
  102. // --- fail_impl_cycle_one_generic_param.carbon
  103. library "[[@TEST_NAME]]";
  104. import Core;
  105. interface ComparableWith(T:! type) {}
  106. // This creates a dependency cycle with itself.
  107. impl forall [U:! type, T:! ComparableWith(U)]
  108. U as ComparableWith(T) {}
  109. class C {}
  110. class D {}
  111. impl C as ComparableWith(D) {}
  112. fn Compare[T:! type, U:! ComparableWith(T)](t: T, u: U) {}
  113. fn F() {
  114. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE+20]]:3: error: cycle found in search for impl of `ComparableWith(C)` for type `C` [ImplLookupCycle]
  115. // CHECK:STDERR: Compare({} as C, {} as C);
  116. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  117. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-13]]:1: note: determining if this impl clause matches [ImplLookupCycleNote]
  118. // CHECK:STDERR: impl forall [U:! type, T:! ComparableWith(U)]
  119. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  120. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-9]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  121. // CHECK:STDERR: fn Compare[T:! type, U:! ComparableWith(T)](t: T, u: U) {}
  122. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  123. // CHECK:STDERR:
  124. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE+10]]:3: error: cannot implicitly convert value of type `type` to `ComparableWith(C)` [ImplicitAsConversionFailure]
  125. // CHECK:STDERR: Compare({} as C, {} as C);
  126. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  127. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE+7]]:3: note: type `type` does not implement interface `Core.ImplicitAs(ComparableWith(C))` [MissingImplInMemberAccessNote]
  128. // CHECK:STDERR: Compare({} as C, {} as C);
  129. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  130. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-19]]: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+13]]: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-35]]: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-38]]: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-34]]: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. Compare({} as C, {} as D);
  149. }
  150. // --- impl_recurse_with_simpler_type_no_cycle.carbon
  151. library "[[@TEST_NAME]]";
  152. import Core;
  153. class Wraps(T:! type) {}
  154. interface Printable {}
  155. // If T is printable, so is Wraps(T).
  156. impl forall [T:! Printable] Wraps(T) as Printable {}
  157. class C {}
  158. impl C as Printable {}
  159. fn F() {
  160. ({} as C) as (C as Printable);
  161. ({} as Wraps(C)) as (Wraps(C) as Printable);
  162. }
  163. // --- impl_recurse_with_simpler_type_in_generic_param_no_cycle.carbon
  164. library "[[@TEST_NAME]]";
  165. import Core;
  166. class Wraps(T:! type) {}
  167. interface ComparableTo(T:! type) {}
  168. // If U is comparable to T, then U is comparable to Wraps(T).
  169. impl forall [T:! type, U:! ComparableTo(T)] U as ComparableTo(Wraps(T)) {}
  170. // If U is comparable to T then Wraps(U) is comparable to T.
  171. impl forall [T:! type, U:! ComparableTo(T)] Wraps(U) as ComparableTo(T) {}
  172. class C {}
  173. class D {}
  174. impl C as ComparableTo(D) {}
  175. fn F() {
  176. ({} as C) as (C as ComparableTo(D));
  177. ({} as C) as (C as ComparableTo(Wraps(D)));
  178. ({} as Wraps(C)) as (Wraps(C) as ComparableTo(D));
  179. ({} as Wraps(C)) as (Wraps(C) as ComparableTo(Wraps(D)));
  180. }
  181. // --- impl_recurse_with_simpler_type_in_generic_param_bidirectional_no_cycle.carbon
  182. library "[[@TEST_NAME]]";
  183. import Core;
  184. // Implement this for a type in one direction.
  185. interface ComparableTo(T:! type) {}
  186. // Use this as a bound with two types in any direction.
  187. interface ComparableWith(T:! type) {}
  188. class C {}
  189. class D {}
  190. // C is comparable to D. This should imply:
  191. // - C is ComparableWith(C).
  192. // - C is ComparableWith(D).
  193. // - D is ComparableWith(C).
  194. impl C as ComparableTo(D) {}
  195. // T is always ComparableWith(T).
  196. impl forall [T:! type] T as ComparableWith(T) {}
  197. // If U is ComparableTo(T), U is ComparableWith(T).
  198. impl forall [T:! type, U:! ComparableTo(T)] U as ComparableWith(T) {}
  199. // If U is ComparableTo(T), T is ComparableWith(U).
  200. impl forall [T:! type, U:! ComparableTo(T)] T as ComparableWith(U) {}
  201. fn Compare[T:! type, U:! ComparableWith(T)](t: T, u: U) {}
  202. fn F() {
  203. Compare({} as C, {} as C);
  204. Compare({} as C, {} as D);
  205. Compare({} as D, {} as C);
  206. }
  207. // --- deduce_cycle_without_symbolic.carbon
  208. library "[[@TEST_NAME]]";
  209. interface First {}
  210. interface Second {}
  211. // When answering a query "does T impl Second", we must avoid trying to deduce
  212. // parameters for this impl. Not only is doing so unnecessary, it would start a new
  213. // "does T impl Second" query, leading to a "cycle in impl lookup" error.
  214. impl forall [T:! Second] T as First {}
  215. impl forall [T:! type] T as Second {}
  216. class C {}
  217. fn F() {
  218. ({} as C) as (C as Second);
  219. ({} as C) as (C as First);
  220. }
  221. // --- deduce_cycle_with_symbolic.carbon
  222. library "[[@TEST_NAME]]";
  223. interface First(T:! type) {}
  224. interface Second {}
  225. // When answering a query "does T impl Second", we must avoid trying to deduce
  226. // parameters for this impl. Not only is doing so unnecessary, it would start a new
  227. // "does T impl Second" query, leading to a "cycle in impl lookup" error.
  228. impl forall [T:! Second, U:! type] T as First(U) {}
  229. impl forall [T:! type] T as Second {}
  230. class C {}
  231. fn F() {
  232. ({} as C) as (C as Second);
  233. ({} as C) as (C as First(C));
  234. }