designator.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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/designator.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/where_expr/designator.carbon
  12. // --- success.carbon
  13. library "[[@TEST_NAME]]";
  14. interface I {
  15. let Member:! type;
  16. }
  17. //@dump-sem-ir-begin
  18. fn PeriodSelf(T:! I where .Self == ());
  19. fn PeriodMember(U:! I where .Member == ());
  20. fn TypeSelfImpls(V:! type where .Self impls I);
  21. //@dump-sem-ir-end
  22. // --- fail_wrong_member.carbon
  23. library "[[@TEST_NAME]]";
  24. interface J {
  25. let Member:! type;
  26. }
  27. // CHECK:STDERR: fail_wrong_member.carbon:[[@LINE+4]]:31: error: member name `Mismatch` not found in `J` [MemberNameNotFoundInInstScope]
  28. // CHECK:STDERR: fn PeriodMismatch(W:! J where .Mismatch = {});
  29. // CHECK:STDERR: ^~~~~~~~~
  30. // CHECK:STDERR:
  31. fn PeriodMismatch(W:! J where .Mismatch = {});
  32. // --- fail_designator_matches_var.carbon
  33. library "[[@TEST_NAME]]";
  34. fn Foo() -> () {
  35. var x: ();
  36. // CHECK:STDERR: fail_designator_matches_var.carbon:[[@LINE+5]]:10: error: name `.Self` not found [NameNotFound]
  37. // CHECK:STDERR: return .x;
  38. // CHECK:STDERR: ^~
  39. // CHECK:STDERR: fail_designator_matches_var.carbon: note: designator may only be used when `.Self` is in scope [NoPeriodSelfForDesignator]
  40. // CHECK:STDERR:
  41. return .x;
  42. }
  43. // --- fail_unknown_designator.carbon
  44. library "[[@TEST_NAME]]";
  45. fn Bar() -> () {
  46. // CHECK:STDERR: fail_unknown_designator.carbon:[[@LINE+5]]:10: error: name `.Self` not found [NameNotFound]
  47. // CHECK:STDERR: return .undef;
  48. // CHECK:STDERR: ^~~~~~
  49. // CHECK:STDERR: fail_unknown_designator.carbon: note: designator may only be used when `.Self` is in scope [NoPeriodSelfForDesignator]
  50. // CHECK:STDERR:
  51. return .undef;
  52. }
  53. // --- fail_dot_self_method_return_value.carbon
  54. library "[[@TEST_NAME]]";
  55. class C {
  56. // CHECK:STDERR: fail_dot_self_method_return_value.carbon:[[@LINE+4]]:27: error: name `.Self` not found [NameNotFound]
  57. // CHECK:STDERR: fn F() -> Self { return .Self; }
  58. // CHECK:STDERR: ^~~~~
  59. // CHECK:STDERR:
  60. fn F() -> Self { return .Self; }
  61. }
  62. // --- fail_dot_self_method_return_type.carbon
  63. library "[[@TEST_NAME]]";
  64. class D {
  65. // CHECK:STDERR: fail_dot_self_method_return_type.carbon:[[@LINE+4]]:13: error: name `.Self` not found [NameNotFound]
  66. // CHECK:STDERR: fn G() -> .Self { return Self; }
  67. // CHECK:STDERR: ^~~~~
  68. // CHECK:STDERR:
  69. fn G() -> .Self { return Self; }
  70. }
  71. // --- todo_fail_impls_constraint_does_not_constrain_self.carbon
  72. library "[[@TEST_NAME]]";
  73. interface I {}
  74. // TODO: Diagnose that the `where` on `T` does not refer to `.Self` at all.
  75. // See https://docs.carbon-lang.dev/docs/design/generics/details.html#constraints-must-use-a-designator
  76. fn F(U:! type, T:! type where U impls I) {}
  77. // --- todo_fail_equality_constraint_does_not_constrain_self.carbon
  78. library "[[@TEST_NAME]]";
  79. interface I {
  80. let X:! type;
  81. }
  82. // TODO: Diagnose that the `where` on `T` does not refer to `.Self` at all.
  83. // See https://docs.carbon-lang.dev/docs/design/generics/details.html#constraints-must-use-a-designator
  84. fn F(U:! I, T:! type where U.X == ()) {}
  85. // --- todo_fail_combined_constraint_does_not_constrain_self.carbon
  86. library "[[@TEST_NAME]]";
  87. interface I {
  88. let X:! type;
  89. }
  90. // TODO: Diagnose that the `where` on `T` does not refer to `.Self` at all.
  91. // See https://docs.carbon-lang.dev/docs/design/generics/details.html#constraints-must-use-a-designator
  92. fn F(U:! I, T:! type where U impls I and U.X == ()) {}
  93. // --- impls_constraint_does_constrain_self.carbon
  94. library "[[@TEST_NAME]]";
  95. interface I(T:! type) {}
  96. class C(T:! type);
  97. fn F(T:! type where C(.Self) impls I(())) {}
  98. fn G(T:! type where C(()) impls I(.Self)) {}
  99. // CHECK:STDOUT: --- success.carbon
  100. // CHECK:STDOUT:
  101. // CHECK:STDOUT: constants {
  102. // CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete]
  103. // CHECK:STDOUT: %I.assoc_type: type = assoc_entity_type @I [concrete]
  104. // CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, @I.%Member [concrete]
  105. // CHECK:STDOUT: %.Self.420: %I.type = symbolic_binding .Self [symbolic_self]
  106. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  107. // CHECK:STDOUT: %empty_tuple: %empty_tuple.type = tuple_value () [concrete]
  108. // CHECK:STDOUT: %I_where.type: type = facet_type <@I where TODO> [concrete]
  109. // CHECK:STDOUT: %T: %I_where.type = symbolic_binding T, 0 [symbolic]
  110. // CHECK:STDOUT: %pattern_type.917: type = pattern_type %I_where.type [concrete]
  111. // CHECK:STDOUT: %PeriodSelf.type: type = fn_type @PeriodSelf [concrete]
  112. // CHECK:STDOUT: %PeriodSelf: %PeriodSelf.type = struct_value () [concrete]
  113. // CHECK:STDOUT: %.Self.binding.as_type: type = symbolic_binding_type .Self, %.Self.420 [symbolic_self]
  114. // CHECK:STDOUT: %I.lookup_impl_witness: <witness> = lookup_impl_witness %.Self.420, @I [symbolic_self]
  115. // CHECK:STDOUT: %impl.elem0: type = impl_witness_access %I.lookup_impl_witness, element0 [symbolic_self]
  116. // CHECK:STDOUT: %U: %I_where.type = symbolic_binding U, 0 [symbolic]
  117. // CHECK:STDOUT: %PeriodMember.type: type = fn_type @PeriodMember [concrete]
  118. // CHECK:STDOUT: %PeriodMember: %PeriodMember.type = struct_value () [concrete]
  119. // CHECK:STDOUT: %.Self.bcc: type = symbolic_binding .Self [symbolic_self]
  120. // CHECK:STDOUT: %type_where: type = facet_type <type where .Self impls @I> [concrete]
  121. // CHECK:STDOUT: %V: %type_where = symbolic_binding V, 0 [symbolic]
  122. // CHECK:STDOUT: %pattern_type.09a: type = pattern_type %type_where [concrete]
  123. // CHECK:STDOUT: %TypeSelfImpls.type: type = fn_type @TypeSelfImpls [concrete]
  124. // CHECK:STDOUT: %TypeSelfImpls: %TypeSelfImpls.type = struct_value () [concrete]
  125. // CHECK:STDOUT: }
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: imports {
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: file {
  131. // CHECK:STDOUT: %PeriodSelf.decl: %PeriodSelf.type = fn_decl @PeriodSelf [concrete = constants.%PeriodSelf] {
  132. // CHECK:STDOUT: %T.patt: %pattern_type.917 = symbolic_binding_pattern T, 0 [concrete]
  133. // CHECK:STDOUT: } {
  134. // CHECK:STDOUT: %.loc9_21.1: type = splice_block %.loc9_21.2 [concrete = constants.%I_where.type] {
  135. // CHECK:STDOUT: <elided>
  136. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
  137. // CHECK:STDOUT: <elided>
  138. // CHECK:STDOUT: %.Self.ref: %I.type = name_ref .Self, %.Self.2 [symbolic_self = constants.%.Self.420]
  139. // CHECK:STDOUT: %.loc9_37: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
  140. // CHECK:STDOUT: %.loc9_21.2: type = where_expr %.Self.2 [concrete = constants.%I_where.type] {
  141. // CHECK:STDOUT: requirement_base_facet_type constants.%I.type
  142. // CHECK:STDOUT: requirement_equivalent %.Self.ref, %.loc9_37
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT: }
  145. // CHECK:STDOUT: %T.loc9_15.2: %I_where.type = symbolic_binding T, 0 [symbolic = %T.loc9_15.1 (constants.%T)]
  146. // CHECK:STDOUT: }
  147. // CHECK:STDOUT: %PeriodMember.decl: %PeriodMember.type = fn_decl @PeriodMember [concrete = constants.%PeriodMember] {
  148. // CHECK:STDOUT: %U.patt: %pattern_type.917 = symbolic_binding_pattern U, 0 [concrete]
  149. // CHECK:STDOUT: } {
  150. // CHECK:STDOUT: %.loc11_23.1: type = splice_block %.loc11_23.2 [concrete = constants.%I_where.type] {
  151. // CHECK:STDOUT: <elided>
  152. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
  153. // CHECK:STDOUT: <elided>
  154. // CHECK:STDOUT: %.Self.ref: %I.type = name_ref .Self, %.Self.2 [symbolic_self = constants.%.Self.420]
  155. // CHECK:STDOUT: %Member.ref: %I.assoc_type = name_ref Member, @Member.%assoc0 [concrete = constants.%assoc0]
  156. // CHECK:STDOUT: %.Self.as_type: type = facet_access_type %.Self.ref [symbolic_self = constants.%.Self.binding.as_type]
  157. // CHECK:STDOUT: %.loc11_29: type = converted %.Self.ref, %.Self.as_type [symbolic_self = constants.%.Self.binding.as_type]
  158. // CHECK:STDOUT: %impl.elem0: type = impl_witness_access constants.%I.lookup_impl_witness, element0 [symbolic_self = constants.%impl.elem0]
  159. // CHECK:STDOUT: %.loc11_41: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
  160. // CHECK:STDOUT: %.loc11_23.2: type = where_expr %.Self.2 [concrete = constants.%I_where.type] {
  161. // CHECK:STDOUT: requirement_base_facet_type constants.%I.type
  162. // CHECK:STDOUT: requirement_equivalent %impl.elem0, %.loc11_41
  163. // CHECK:STDOUT: }
  164. // CHECK:STDOUT: }
  165. // CHECK:STDOUT: %U.loc11_17.2: %I_where.type = symbolic_binding U, 0 [symbolic = %U.loc11_17.1 (constants.%U)]
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT: %TypeSelfImpls.decl: %TypeSelfImpls.type = fn_decl @TypeSelfImpls [concrete = constants.%TypeSelfImpls] {
  168. // CHECK:STDOUT: %V.patt: %pattern_type.09a = symbolic_binding_pattern V, 0 [concrete]
  169. // CHECK:STDOUT: } {
  170. // CHECK:STDOUT: %.loc13_27.1: type = splice_block %.loc13_27.2 [concrete = constants.%type_where] {
  171. // CHECK:STDOUT: <elided>
  172. // CHECK:STDOUT: %.Self.ref: type = name_ref .Self, %.Self.2 [symbolic_self = constants.%.Self.bcc]
  173. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
  174. // CHECK:STDOUT: %.loc13_27.2: type = where_expr %.Self.2 [concrete = constants.%type_where] {
  175. // CHECK:STDOUT: requirement_base_facet_type type
  176. // CHECK:STDOUT: requirement_impls %.Self.ref, %I.ref
  177. // CHECK:STDOUT: }
  178. // CHECK:STDOUT: }
  179. // CHECK:STDOUT: %V.loc13_18.2: %type_where = symbolic_binding V, 0 [symbolic = %V.loc13_18.1 (constants.%V)]
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT: }
  182. // CHECK:STDOUT:
  183. // CHECK:STDOUT: generic fn @PeriodSelf(%T.loc9_15.2: %I_where.type) {
  184. // CHECK:STDOUT: %T.loc9_15.1: %I_where.type = symbolic_binding T, 0 [symbolic = %T.loc9_15.1 (constants.%T)]
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: fn();
  187. // CHECK:STDOUT: }
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: generic fn @PeriodMember(%U.loc11_17.2: %I_where.type) {
  190. // CHECK:STDOUT: %U.loc11_17.1: %I_where.type = symbolic_binding U, 0 [symbolic = %U.loc11_17.1 (constants.%U)]
  191. // CHECK:STDOUT:
  192. // CHECK:STDOUT: fn();
  193. // CHECK:STDOUT: }
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: generic fn @TypeSelfImpls(%V.loc13_18.2: %type_where) {
  196. // CHECK:STDOUT: %V.loc13_18.1: %type_where = symbolic_binding V, 0 [symbolic = %V.loc13_18.1 (constants.%V)]
  197. // CHECK:STDOUT:
  198. // CHECK:STDOUT: fn();
  199. // CHECK:STDOUT: }
  200. // CHECK:STDOUT:
  201. // CHECK:STDOUT: specific @PeriodSelf(constants.%T) {
  202. // CHECK:STDOUT: %T.loc9_15.1 => constants.%T
  203. // CHECK:STDOUT: }
  204. // CHECK:STDOUT:
  205. // CHECK:STDOUT: specific @PeriodMember(constants.%U) {
  206. // CHECK:STDOUT: %U.loc11_17.1 => constants.%U
  207. // CHECK:STDOUT: }
  208. // CHECK:STDOUT:
  209. // CHECK:STDOUT: specific @TypeSelfImpls(constants.%V) {
  210. // CHECK:STDOUT: %V.loc13_18.1 => constants.%V
  211. // CHECK:STDOUT: }
  212. // CHECK:STDOUT: