dot_self_impls.carbon 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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/where_expr/min_prelude/dot_self_impls.carbon
  11. // TIP: To dump output, run:
  12. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/where_expr/min_prelude/dot_self_impls.carbon
  13. // --- compound_member_access_through_where_self_impls.carbon
  14. library "[[@TEST_NAME]]";
  15. interface I {
  16. fn FNonInstance();
  17. fn FSelf[self: Self]();
  18. }
  19. interface J {
  20. fn GNonInstance();
  21. fn GSelf[self: Self]();
  22. }
  23. fn INotJ[T:! I where .Self impls J](x: T) {
  24. // Can access members of `I` using either kind of member access.
  25. T.FNonInstance();
  26. T.(I.FNonInstance)();
  27. x.FNonInstance();
  28. x.FSelf();
  29. x.(I.FSelf)();
  30. // Can still find members of `J` using compound member access,
  31. // even though they are not available via `T.GNonInstance` or
  32. // `x.GSelf`.
  33. T.(J.GNonInstance)();
  34. x.(J.GSelf)();
  35. }
  36. fn TypeNotJ[T:! type where .Self impls J](x: T) {
  37. T.(J.GNonInstance)();
  38. x.(J.GSelf)();
  39. }
  40. // --- no_name_conflict_with_where_self_impls.carbon
  41. library "[[@TEST_NAME]]";
  42. interface I {
  43. fn F() -> ();
  44. }
  45. interface J {
  46. fn F() -> {};
  47. }
  48. fn INotJ(T:! I where .Self impls J) {
  49. // Gets `T.(I.F)`. Doesn't consider `T.(J.F)`, so no ambiguity.
  50. let x: () = T.F();
  51. }
  52. // --- fail_name_lookup_through_where_self_impls.carbon
  53. library "[[@TEST_NAME]]";
  54. interface I {
  55. fn F();
  56. }
  57. interface J {
  58. fn G();
  59. }
  60. fn INotJ(T:! I where .Self impls J) {
  61. // CHECK:STDERR: fail_name_lookup_through_where_self_impls.carbon:[[@LINE+4]]:3: error: member name `G` not found in `I` [MemberNameNotFoundInInstScope]
  62. // CHECK:STDERR: T.G();
  63. // CHECK:STDERR: ^~~
  64. // CHECK:STDERR:
  65. T.G();
  66. }
  67. // --- fail_name_lookup_with_type.carbon
  68. library "[[@TEST_NAME]]";
  69. interface J {
  70. fn G();
  71. }
  72. fn TypeNotJ(T:! type where .Self impls J) {
  73. // CHECK:STDERR: fail_name_lookup_with_type.carbon:[[@LINE+4]]:3: error: member name `G` not found [MemberNameNotFound]
  74. // CHECK:STDERR: T.G();
  75. // CHECK:STDERR: ^~~
  76. // CHECK:STDERR:
  77. T.G();
  78. }
  79. // --- fail_facet_type_simple_member_access.carbon
  80. library "[[@TEST_NAME]]";
  81. interface A {}
  82. interface B { fn Bfn(); }
  83. fn F(T:! A & B) {
  84. // CHECK:STDERR: fail_facet_type_simple_member_access.carbon:[[@LINE+4]]:6: error: member name `Bfn` not found in `A` [MemberNameNotFoundInInstScope]
  85. // CHECK:STDERR: T.((A where .Self impls B).Bfn)();
  86. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  87. // CHECK:STDERR:
  88. T.((A where .Self impls B).Bfn)();
  89. // CHECK:STDERR: fail_facet_type_simple_member_access.carbon:[[@LINE+4]]:3: error: member name `Bfn` not found in `A` [MemberNameNotFoundInInstScope]
  90. // CHECK:STDERR: (A where .Self impls B).Bfn;
  91. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  92. // CHECK:STDERR:
  93. (A where .Self impls B).Bfn;
  94. }
  95. // --- fail_name_lookup_instance.carbon
  96. library "[[@TEST_NAME]]";
  97. interface I {
  98. fn FNonInstance();
  99. fn FSelf[self: Self]();
  100. }
  101. interface J {
  102. fn GNonInstance();
  103. fn GSelf[self: Self]();
  104. }
  105. fn INotJ[T:! I where .Self impls J](x: T) {
  106. // CHECK:STDERR: fail_name_lookup_instance.carbon:[[@LINE+4]]:3: error: member name `GNonInstance` not found in `I` [MemberNameNotFoundInInstScope]
  107. // CHECK:STDERR: x.GNonInstance();
  108. // CHECK:STDERR: ^~~~~~~~~~~~~~
  109. // CHECK:STDERR:
  110. x.GNonInstance();
  111. // CHECK:STDERR: fail_name_lookup_instance.carbon:[[@LINE+4]]:3: error: member name `GSelf` not found in `I` [MemberNameNotFoundInInstScope]
  112. // CHECK:STDERR: x.GSelf();
  113. // CHECK:STDERR: ^~~~~~~
  114. // CHECK:STDERR:
  115. x.GSelf();
  116. }
  117. // --- compare_equal.carbon
  118. library "[[@TEST_NAME]]";
  119. class WrapType(T:! type) {}
  120. fn AssertSame[T:! type](a: WrapType(T), b: WrapType(T)) {}
  121. fn Type(T:! type) -> WrapType(T) { return {}; }
  122. interface I;
  123. interface J;
  124. interface K;
  125. fn Test() {
  126. AssertSame(Type(I), Type(I where .Self impls I));
  127. AssertSame(Type(I), Type(I where .Self impls I & I));
  128. AssertSame(Type(I & J), Type(J & I where .Self impls I));
  129. AssertSame(Type(I & J), Type(J & I where .Self impls J));
  130. AssertSame(Type(I & J), Type(J & I where .Self impls I & J));
  131. AssertSame(Type(I & J where .Self impls K), Type(J & I where .Self impls I & K));
  132. AssertSame(Type(I & J where .Self impls K), Type(J & I where .Self impls J & K));
  133. AssertSame(Type(I & J where .Self impls K), Type(J & I where .Self impls K & I & J));
  134. AssertSame(Type(I where .Self impls J & K), Type(I where .Self impls K & I & J));
  135. AssertSame(Type(I where .Self impls J & K),
  136. Type(I where .Self impls (J where .Self impls K)));
  137. AssertSame(Type(I where .Self impls J & K),
  138. Type(I where .Self impls (K where .Self impls J)));
  139. AssertSame(Type(I where .Self impls J & K),
  140. Type(I where .Self impls (type where .Self impls (J where .Self impls K))));
  141. }
  142. interface I {}
  143. interface J {}
  144. interface K {}
  145. // --- compare_equal_with_associated_constant.carbon
  146. library "[[@TEST_NAME]]";
  147. class WrapType(T:! type) {}
  148. fn AssertSame[T:! type](a: WrapType(T), b: WrapType(T)) {}
  149. fn Type(T:! type) -> WrapType(T) { return {}; }
  150. interface I { let A:! type; }
  151. interface J { let A:! type; }
  152. fn Test() {
  153. AssertSame(Type((I where .A = ()) & J),
  154. Type(I & J where .Self impls (I where .A = ())));
  155. AssertSame(Type(I & (J where .A = {})),
  156. Type(I & J where .Self impls (J where .A = {})));
  157. AssertSame(Type((I where .A = ()) & (J where .A = {})),
  158. Type(I & J where .Self impls (I where .A = ())
  159. and .Self impls (J where .A = {})));
  160. AssertSame(Type((I where .A = ()) & (J where .A = {})),
  161. Type(I & J where .Self impls (I where .A = ()) & (J where .A = {})));
  162. }
  163. // --- fail_compare_not_equal.carbon
  164. library "[[@TEST_NAME]]";
  165. class WrapType(T:! type) {}
  166. fn Same[T:! type](a: WrapType(T), b: WrapType(T)) {}
  167. fn Type(T:! type) -> WrapType(T) { return {}; }
  168. interface I {}
  169. interface J {}
  170. fn Test() {
  171. // CHECK:STDERR: fail_compare_not_equal.carbon:[[@LINE+7]]:3: error: inconsistent deductions for value of generic parameter `T` [DeductionInconsistent]
  172. // CHECK:STDERR: Same(Type(I where .Self impls J), Type(J where .Self impls I));
  173. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  174. // CHECK:STDERR: fail_compare_not_equal.carbon:[[@LINE-10]]:1: note: while deducing parameters of generic declared here [DeductionGenericHere]
  175. // CHECK:STDERR: fn Same[T:! type](a: WrapType(T), b: WrapType(T)) {}
  176. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  177. // CHECK:STDERR:
  178. Same(Type(I where .Self impls J), Type(J where .Self impls I));
  179. }
  180. // --- impl_as.carbon
  181. library "[[@TEST_NAME]]";
  182. class C {}
  183. interface I {}
  184. interface J { let T:! type; }
  185. // Interfaces to the right of the `where` don't interfere with the
  186. // "can only impl a facet type with a single interface" rule. Only
  187. // the interface being implemented needs to have all of its
  188. // associated constants set
  189. impl {.a: C} as I where .Self impls J {}
  190. impl {.b: C} as J where .Self impls I and .T = () {}
  191. // Rewrite constraints can appear inside the `where .Self impls`.
  192. impl {.c: C} as J where .Self impls (I & J where .T = ()) {}
  193. // --- impl_with_rewrite_of_interface_not_being_implemented.carbon
  194. library "[[@TEST_NAME]]";
  195. interface I { let A:! type; }
  196. interface J { let A:! type; }
  197. class C {}
  198. // Implementation of `C as J`.
  199. impl C as J where .A = {} {}
  200. // This is an implementation of `I` with `I.A = ()`. The requirement
  201. // that `C` also impls `J where .A = {}` is an additional constraint
  202. // that must be satisfied (and is satisfied by the impl above, though
  203. // that currently isn't checked), but doesn't affect `C as I`.
  204. impl C as I where .Self impls
  205. (J where .A = {} and .Self impls (I where .A = ())) {}
  206. let x: C.(I.A) = ();
  207. let y: C.(J.A) = {};