combine.carbon 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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/combine.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/facet/combine.carbon
  12. // --- fail_name_collision.carbon
  13. library "[[@TEST_NAME]]";
  14. interface A {
  15. fn G();
  16. }
  17. interface B {
  18. fn G();
  19. }
  20. class C {}
  21. impl C as A {
  22. fn G();
  23. }
  24. impl C as B {
  25. fn G() {}
  26. }
  27. fn F() {
  28. // TODO: This error message is wrong here, we are not using `extend`.
  29. // CHECK:STDERR: fail_name_collision.carbon:[[@LINE+4]]:14: error: ambiguous use of name `G` found in multiple extended scopes [NameAmbiguousDueToExtend]
  30. // CHECK:STDERR: ({} as C).((A & B).G)();
  31. // CHECK:STDERR: ^~~~~~~~~
  32. // CHECK:STDERR:
  33. ({} as C).((A & B).G)();
  34. }
  35. // --- combine.carbon
  36. library "[[@TEST_NAME]]";
  37. interface A {}
  38. interface B {
  39. fn BB[self: Self]();
  40. }
  41. class C {}
  42. impl C as A {}
  43. impl C as B {
  44. fn BB[self: Self]() {}
  45. }
  46. fn G[T:! A & B](t: T) {}
  47. fn F() {
  48. ({} as C).((A & B).BB)();
  49. (({} as C) as (C as (A & B))).((A & B).BB)();
  50. (({} as C) as (C as (A & B))).(B.BB)();
  51. G({} as C);
  52. }
  53. // --- generic_interface.carbon
  54. library "[[@TEST_NAME]]";
  55. interface A(T:! type) {}
  56. interface B {}
  57. class P1 {}
  58. class P2 {}
  59. class C {}
  60. impl C as A(P1) {}
  61. impl C as B {}
  62. fn G[T:! A(P1) & B](t: T) {}
  63. fn F() {
  64. G({} as C);
  65. }
  66. // --- fail_wrong_generic_interface.carbon
  67. library "[[@TEST_NAME]]";
  68. interface A(T:! type) {}
  69. interface B {}
  70. class P1 {}
  71. class P2 {}
  72. class C {}
  73. impl C as A(P1) {}
  74. impl C as B {}
  75. fn G[T:! A(P2) & B](t: T) {}
  76. fn F() {
  77. // CHECK:STDERR: fail_wrong_generic_interface.carbon:[[@LINE+7]]:3: error: cannot convert type `C` into type implementing `A(P2) & B` [ConversionFailureTypeToFacet]
  78. // CHECK:STDERR: G({} as C);
  79. // CHECK:STDERR: ^~~~~~~~~~
  80. // CHECK:STDERR: fail_wrong_generic_interface.carbon:[[@LINE-6]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  81. // CHECK:STDERR: fn G[T:! A(P2) & B](t: T) {}
  82. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  83. // CHECK:STDERR:
  84. G({} as C);
  85. }
  86. // --- generic_forall_impl.carbon
  87. library "[[@TEST_NAME]]";
  88. interface Iface {}
  89. interface GenericIface(T:! type) {}
  90. class GenericClass(T:! type) {}
  91. class ImplIface {}
  92. impl ImplIface as Iface {}
  93. class C {}
  94. impl C as Iface {}
  95. impl forall [IfaceType:! Iface] C as GenericIface(GenericClass(IfaceType)) {}
  96. fn G[T:! Iface & GenericIface(GenericClass(ImplIface))](t: T) {}
  97. fn F() {
  98. G({} as C);
  99. }
  100. // --- compare_equal.carbon
  101. library "[[@TEST_NAME]]";
  102. class WrapType(T:! type) {}
  103. fn AssertSame[T:! type](a: WrapType(T), b: WrapType(T)) {}
  104. fn Type(T:! type) -> WrapType(T) { return {}; }
  105. interface I;
  106. interface J;
  107. interface K(T:! type);
  108. fn TestIncomplete() {
  109. AssertSame(Type(I), Type(I & I));
  110. AssertSame(Type(I), Type(I & I & I));
  111. AssertSame(Type(I & J), Type(J & I));
  112. AssertSame(Type(I & J), Type(I & I & J));
  113. AssertSame(Type(I & J), Type(I & J & I));
  114. AssertSame(Type(I & J), Type(J & I & I));
  115. AssertSame(Type(I & K({})), Type(K({}) & I));
  116. AssertSame(Type(I & K({}) & K(())), Type(K(()) & K({}) & I));
  117. }
  118. interface I {}
  119. interface J {}
  120. interface K(T:! type) {}
  121. fn TestComplete() {
  122. AssertSame(Type(I), Type(I & I));
  123. AssertSame(Type(I), Type(I & I & I));
  124. AssertSame(Type(I & J), Type(J & I));
  125. AssertSame(Type(I & J), Type(I & I & J));
  126. AssertSame(Type(I & J), Type(I & J & I));
  127. AssertSame(Type(I & J), Type(J & I & I));
  128. AssertSame(Type(I & K({})), Type(K({}) & I));
  129. AssertSame(Type(I & K({}) & K(())), Type(K(()) & K({}) & I));
  130. }
  131. // --- fail_compare_not_equal.carbon
  132. library "[[@TEST_NAME]]";
  133. class WrapType(T:! type) {}
  134. fn Same[T:! type](a: WrapType(T), b: WrapType(T)) {}
  135. fn Type(T:! type) -> WrapType(T) { return {}; }
  136. interface I {}
  137. interface J {}
  138. interface K {}
  139. fn Test() {
  140. // CHECK:STDERR: fail_compare_not_equal.carbon:[[@LINE+7]]:3: error: inconsistent deductions for value of generic parameter `T` [DeductionInconsistent]
  141. // CHECK:STDERR: Same(Type(I & J), Type(K & I & J));
  142. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  143. // CHECK:STDERR: fail_compare_not_equal.carbon:[[@LINE-11]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  144. // CHECK:STDERR: fn Same[T:! type](a: WrapType(T), b: WrapType(T)) {}
  145. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  146. // CHECK:STDERR:
  147. Same(Type(I & J), Type(K & I & J));
  148. }
  149. // --- fail_compare_not_equal_parameterized.carbon
  150. library "[[@TEST_NAME]]";
  151. class WrapType(T:! type) {}
  152. fn Same[T:! type](a: WrapType(T), b: WrapType(T)) {}
  153. fn Type(T:! type) -> WrapType(T) { return {}; }
  154. interface I {}
  155. interface J(T:! type) {}
  156. fn Test() {
  157. // CHECK:STDERR: fail_compare_not_equal_parameterized.carbon:[[@LINE+7]]:3: error: inconsistent deductions for value of generic parameter `T` [DeductionInconsistent]
  158. // CHECK:STDERR: Same(Type(I & J(())), Type(J({}) & I));
  159. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  160. // CHECK:STDERR: fail_compare_not_equal_parameterized.carbon:[[@LINE-10]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  161. // CHECK:STDERR: fn Same[T:! type](a: WrapType(T), b: WrapType(T)) {}
  162. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  163. // CHECK:STDERR:
  164. Same(Type(I & J(())), Type(J({}) & I));
  165. }
  166. // --- fail_compare_not_equal_parameterized_extra.carbon
  167. library "[[@TEST_NAME]]";
  168. class WrapType(T:! type) {}
  169. fn Same[T:! type](a: WrapType(T), b: WrapType(T)) {}
  170. fn Type(T:! type) -> WrapType(T) { return {}; }
  171. interface I {}
  172. interface J(T:! type) {}
  173. fn Test() {
  174. // CHECK:STDERR: fail_compare_not_equal_parameterized_extra.carbon:[[@LINE+7]]:3: error: inconsistent deductions for value of generic parameter `T` [DeductionInconsistent]
  175. // CHECK:STDERR: Same(Type(I & J(())), Type(J(()) & J({}) & I));
  176. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  177. // CHECK:STDERR: fail_compare_not_equal_parameterized_extra.carbon:[[@LINE-10]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  178. // CHECK:STDERR: fn Same[T:! type](a: WrapType(T), b: WrapType(T)) {}
  179. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  180. // CHECK:STDERR:
  181. Same(Type(I & J(())), Type(J(()) & J({}) & I));
  182. }