impl_cycle.carbon 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. interface Z {}
  23. // This creates a dependency cycle with itself.
  24. impl forall [T:! Z] T as Z {}
  25. class Point {
  26. impl as Z {}
  27. }
  28. fn F() {
  29. // CHECK:STDERR: fail_impl_simple_cycle.carbon:[[@LINE+7]]:21: error: cycle found in lookup of interface <TODO: interface name> for type `Point` [ImplLookupCycle]
  30. // CHECK:STDERR: ({} as Point) as (Point as Z);
  31. // CHECK:STDERR: ^~~~~~~~~~
  32. // CHECK:STDERR: fail_impl_simple_cycle.carbon:[[@LINE-10]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  33. // CHECK:STDERR: impl forall [T:! Z] T as Z {}
  34. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. // CHECK:STDERR:
  36. ({} as Point) as (Point as Z);
  37. }
  38. // --- fail_impl_cycle_one_generic_param.carbon
  39. library "[[@TEST_NAME]]";
  40. import Core;
  41. interface ComparableWith(T:! type) {}
  42. // This creates a dependency cycle with itself.
  43. impl forall [U:! type, T:! ComparableWith(U)]
  44. U as ComparableWith(T) {}
  45. class C {}
  46. class D {}
  47. impl C as ComparableWith(D) {}
  48. fn Compare[T:! type, U:! ComparableWith(T)](t: T, u: U) {}
  49. fn F() {
  50. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE+20]]:3: error: cycle found in lookup of interface <TODO: interface name> for type `C` [ImplLookupCycle]
  51. // CHECK:STDERR: Compare({} as C, {} as C);
  52. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  53. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-13]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  54. // CHECK:STDERR: impl forall [U:! type, T:! ComparableWith(U)]
  55. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  56. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-9]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  57. // CHECK:STDERR: fn Compare[T:! type, U:! ComparableWith(T)](t: T, u: U) {}
  58. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  59. // CHECK:STDERR:
  60. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE+10]]:3: error: cannot implicitly convert from `type` to `ComparableWith(C)` [ImplicitAsConversionFailure]
  61. // CHECK:STDERR: Compare({} as C, {} as C);
  62. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  63. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE+7]]:3: note: type `type` does not implement interface `Core.ImplicitAs(ComparableWith(C))` [MissingImplInMemberAccessNote]
  64. // CHECK:STDERR: Compare({} as C, {} as C);
  65. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  66. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-19]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  67. // CHECK:STDERR: fn Compare[T:! type, U:! ComparableWith(T)](t: T, u: U) {}
  68. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  69. // CHECK:STDERR:
  70. Compare({} as C, {} as C);
  71. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE+13]]:3: error: cycle found in lookup of interface <TODO: interface name> for type `D` [ImplLookupCycle]
  72. // CHECK:STDERR: Compare({} as C, {} as D);
  73. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~
  74. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-35]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  75. // CHECK:STDERR: impl forall [U:! type, T:! ComparableWith(U)]
  76. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  77. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-38]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  78. // CHECK:STDERR: impl forall [U:! type, T:! ComparableWith(U)]
  79. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  80. // CHECK:STDERR: fail_impl_cycle_one_generic_param.carbon:[[@LINE-34]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  81. // CHECK:STDERR: fn Compare[T:! type, U:! ComparableWith(T)](t: T, u: U) {}
  82. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  83. // CHECK:STDERR:
  84. Compare({} as C, {} as D);
  85. }
  86. // --- impl_recurse_with_simpler_type_no_cycle.carbon
  87. library "[[@TEST_NAME]]";
  88. import Core;
  89. class Wraps(T:! type) {}
  90. interface Printable {}
  91. // If T is printable, so is Wraps(T).
  92. impl forall [T:! Printable] Wraps(T) as Printable {}
  93. class C {}
  94. impl C as Printable {}
  95. fn F() {
  96. ({} as C) as (C as Printable);
  97. ({} as Wraps(C)) as (Wraps(C) as Printable);
  98. }
  99. // --- impl_recurse_with_simpler_type_in_generic_param_no_cycle.carbon
  100. library "[[@TEST_NAME]]";
  101. import Core;
  102. class Wraps(T:! type) {}
  103. interface ComparableTo(T:! type) {}
  104. // If U is comparable to T, then U is comparable to Wraps(T).
  105. impl forall [T:! type, U:! ComparableTo(T)] U as ComparableTo(Wraps(T)) {}
  106. // If U is comparable to T then Wraps(U) is comparable to T.
  107. impl forall [T:! type, U:! ComparableTo(T)] Wraps(U) as ComparableTo(T) {}
  108. class C {}
  109. class D {}
  110. impl C as ComparableTo(D) {}
  111. fn F() {
  112. ({} as C) as (C as ComparableTo(D));
  113. ({} as C) as (C as ComparableTo(Wraps(D)));
  114. ({} as Wraps(C)) as (Wraps(C) as ComparableTo(D));
  115. ({} as Wraps(C)) as (Wraps(C) as ComparableTo(Wraps(D)));
  116. }
  117. // --- impl_recurse_with_simpler_type_in_generic_param_bidirectional_no_cycle.carbon
  118. library "[[@TEST_NAME]]";
  119. import Core;
  120. // Implement this for a type in one direction.
  121. interface ComparableTo(T:! type) {}
  122. // Use this as a bound with two types in any direction.
  123. interface ComparableWith(T:! type) {}
  124. class C {}
  125. class D {}
  126. // C is comparable to D. This should imply:
  127. // - C is ComparableWith(C).
  128. // - C is ComparableWith(D).
  129. // - D is ComparableWith(C).
  130. impl C as ComparableTo(D) {}
  131. // T is always ComparableWith(T).
  132. impl forall [T:! type] T as ComparableWith(T) {}
  133. // If U is ComparableTo(T), U is ComparableWith(T).
  134. impl forall [T:! type, U:! ComparableTo(T)] U as ComparableWith(T) {}
  135. // If U is ComparableTo(T), T is ComparableWith(U).
  136. impl forall [T:! type, U:! ComparableTo(T)] T as ComparableWith(U) {}
  137. fn Compare[T:! type, U:! ComparableWith(T)](t: T, u: U) {}
  138. fn F() {
  139. Compare({} as C, {} as C);
  140. Compare({} as C, {} as D);
  141. Compare({} as D, {} as C);
  142. }