dot_self_impls.carbon 7.9 KB

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